<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saulo Silva&#039;s Blog &#187; computer science</title>
	<atom:link href="http://saulosilva.com/tag/computer-science/feed/" rel="self" type="application/rss+xml" />
	<link>http://saulosilva.com</link>
	<description>On technology, programming, et cetera.</description>
	<lastBuildDate>Thu, 22 Jul 2010 14:14:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Applying the Chinese Remainder Theorem</title>
		<link>http://saulosilva.com/2007/10/applying-the-chinese-remainder-theorem/</link>
		<comments>http://saulosilva.com/2007/10/applying-the-chinese-remainder-theorem/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 13:17:59 +0000</pubDate>
		<dc:creator>Saulo</dc:creator>
				<category><![CDATA[How-Tos]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[mathematics]]></category>

		<guid isPermaLink="false">http://pensador.org/wordpress/?p=1</guid>
		<description><![CDATA[In public-key Cryptography, especially with the RSA algorithm, the Chinese Remainder Theorem is often used. Say you have a system of simultaneous congruences as follows x a1 mod m1 x a3 mod m2 x ak mod mk , where m1, m2, &#8230;, mk are coprime, i.e. gcd(m1, m2, &#8230;, mk) = 1. How can we [...]]]></description>
			<content:encoded><![CDATA[<p>In public-key Cryptography, especially with the <a href="http://en.wikipedia.org/wiki/RSA">RSA algorithm</a>, the Chinese Remainder Theorem is often used. Say you have a system of simultaneous congruences as follows</p>
<p>x <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" /> a<sub>1</sub> mod m<sub>1</sub><br />
x <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" /> a<sub>3</sub> mod m<sub>2</sub><br />
<img class="size-full wp-image-106 noborder alignnone" title="Vertical Ellipsis" src="http://saulosilva.com/wp-content/uploads/2008/08/vertical_ellipsis.png" alt="" width="5" height="20" /><br />
x <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" /> a<sub>k</sub> mod m<sub>k</sub> ,</p>
<p>where m<sub>1</sub>, m<sub>2</sub>, &#8230;, m<sub>k</sub> are coprime, i.e. <acronym title="Greatest common divisor">gcd</acronym>(m<sub>1</sub>, m<sub>2</sub>, &#8230;, m<sub>k</sub>) = 1.   How can we solve for x? The solution is quite straight forward, but could involve a   fair amount of calculations. I find that breaking down the method into smaller steps   makes it easier to find and fix mistakes. By the Chinese   Remainder Theorem, the solution to that system of equations is</p>
<p style="text-align: center;">x =   (a<sub>1</sub>M<sub>1</sub>y<sub>1</sub> + a<sub>2</sub>M<sub>2</sub>y<sub>2</sub> + &#8230; + a<sub>k</sub>M<sub>k</sub>y<sub>k</sub>) mod M ,</p>
<p>where M<sub>i</sub> is the product of all   m&#8217;s except for m<sub>i</sub></p>
<p style="text-align: center;"><img class="size-full wp-image-107 noborder alignnone" title="How to compute M sub i" src="http://saulosilva.com/wp-content/uploads/2008/08/mi.png" alt="" width="77" height="54" />,</p>
<p>y<sub>i</sub> is the multiplicative inverse of M<sub>i</sub> modulo m<sub>i</sub></p>
<p style="text-align: center;">y<sub>i</sub> <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> M<sub>i</sub><sup>-1</sup> mod m<sub>i</sub> ,</p>
<p style="text-align: center;">and M = m<sub>1</sub> * m<sub>2</sub> * &#8230; * m<sub>k</sub> .</p>
<p>Let us try a numeric example. Here is a system of simultaneous congruences:</p>
<p style="text-align: center;">x <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 12 mod 25<br />
x <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 9 mod 26<br />
x <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 23 mod 27</p>
<p>We start by calculating M<sub>1</sub> and y<sub>1</sub>:</p>
<p style="text-align: center;">M<sub>1</sub> = m<sub>2</sub> * m<sub>3</sub> = 26 * 27 = 702</p>
<p style="text-align: center;">y<sub>1</sub> = M<sub>1</sub><sup>-1</sup> mod m<sub>1</sub> = 702<sup>-1</sup> mod 25 .</p>
<p>We apply the <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">Extended Euclidean Algorithm</a> to find the multiplicative inverse of 702 relative to 25:</p>
<p>702 = 28 * 25 + 2  → 2 = 702 – 28 * 25<br />
25   = 12 * 2 + 1      → 1 = 25 – 12 * <span style="text-decoration: underline;">2</span><br />
= 25 – 12 * (702 – 28 * 25)<br />
= 337 * 25 – 12 * 702</p>
<p>Then y<sub>1</sub> = 702<sup>-1</sup> mod 25 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> -12 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 13.</p>
<p>The same calculations can be carried on to find M<sub>2</sub> = 675, y<sub>2</sub> = 25, M<sub>3</sub> = 650 and y<sub>3</sub> = 14. Now it is just a matter of plugging in the values into the equation:</p>
<p>x = (a<sub>1</sub>M<sub>1</sub>y<sub>1</sub> + a<sub>2</sub>M<sub>2</sub>y<sub>2</sub> + a<sub>3</sub>M<sub>3</sub>y<sub>3</sub>) mod  M<br />
= (12*702*13 + 9*675*25 + 23*650*14) mod 17550 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 470687 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 14387</p>
<p>To verify our answer we can plug that number back into the system:</p>
<p style="text-align: center;">470687 mod 25 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 12<br />
470687 mod 26 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 9<br />
470687 mod 27 <img class="alignnone size-full wp-image-103 noborder" title="Congruence" src="http://saulosilva.com/wp-content/uploads/2008/08/congruence.png" alt="" width="11" height="13" /> 23</p>
]]></content:encoded>
			<wfw:commentRss>http://saulosilva.com/2007/10/applying-the-chinese-remainder-theorem/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
