<?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>Alice and Bob in Cryptoland &#187; Math</title>
	<atom:link href="http://alicebob.cryptoland.net/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://alicebob.cryptoland.net</link>
	<description></description>
	<lastBuildDate>Mon, 14 Feb 2011 13:29:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Understanding the Montgomery reduction algorithm</title>
		<link>http://alicebob.cryptoland.net/understanding-the-montgomery-reduction-algorithm/</link>
		<comments>http://alicebob.cryptoland.net/understanding-the-montgomery-reduction-algorithm/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 20:18:09 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[montgomery]]></category>
		<category><![CDATA[understanding]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=220</guid>
		<description><![CDATA[The Montgomery reduction algorithm finds the remainder of a division. Many cryptographic schemes work with numbers modulo a prime. When you have to multiply two numbers with e.g. 128 bits each, first you multiply them the usual way (there are many techniques for this) to obtain a 256-bit (&#8220;double precision&#8221;) number. Then you need to [...]]]></description>
			<content:encoded><![CDATA[<p>The Montgomery reduction algorithm finds the remainder of a division. Many cryptographic schemes work with numbers modulo a prime. When you have to multiply two numbers with e.g. 128 bits each, first you multiply them the usual way (there are many techniques for this) to obtain a 256-bit (&#8220;double precision&#8221;) number. Then you need to reduce this result modulo the prime you&#8217;re working with, that is, compute the remainder of the division of this number over the prime.</p>
<p>You can compute the remainder of a division with the &#8220;schoolbook&#8221; technique everyone learns in school, but that is expensive and requires divisions, with are costly in many platforms (some microcontrollers don&#8217;t even have a division instruction). Montgomery reduction only needs a division by a powers of the integer size, which are cheap for computers.</p>
<p>Here I&#8217;ll try to explain how it works, in an informal approach. For detailed proofs of its correctness, check e.g. the chapter 14 of the <a href="http://www.cacr.math.uwaterloo.ca/hac/">Handbook of Applied Cryptography</a> or the <a href="http://www.jstor.org/pss/2007970">original paper</a>.</p>
<h3>First approach</h3>
<p>When working modulo a prime (or modulo anything), you can add or subtract the prime (call it p) from the number you&#8217;re working with (call it x) and you&#8217;ll always get the same number. For example, 3 = 3 + 7 = 10 = 3 (mod 7), or 3 = 3 &#8211; 7 = -4 = 3 (mod 7). This gives the basic approach for finding remainders: just subtract the p from x until you reach a number smaller than p.</p>
<p>For example, you want to compute 6 * 5 modulo 7. You have 6 * 5 = 30, now you just need the remainder of the division of 30 over 7, that is, 30 modulo 7. Now it&#8217;s just subtractions:</p>
<pre>
30 - 7 = 23 (mod 7)
23 - 7 = 16 (mod 7)
16 - 7 = 9 (mod 7)
9 - 7 = 2 (mod 7)
</pre>
<p>And you&#8217;re done: 6 * 5 = 2 (mod 7). But if the number you&#8217;re working with is big, the number of subtractions you&#8217;ll need will be too large. You can speed up this process by dividing the number by the modulus and computing r = x &#8211; (x/p * p). But as I said, division is expensive.</p>
<h3>Let&#8217;s add, not subtract</h3>
<p>The first insight of the Montgomery reduction is this: what if instead of subtracting, you add the prime modulus many times? And more, what if you add the modulus in a way that the number being reduced is filled with zeros to the right? For example, let&#8217;s work modulo 97. You want to compute 43 * 56 (mod 97). You multiply the operands obtaining 2408, and you&#8217;ll need to reduce it modulo 97. You can add the 97 to this number any times you want, that is, you can add any multiple of 97. Which multiple of 97 that, when added to 2408, leads to a number with last digit 0?</p>
<pre>
2408 + 6 * 97 =
2408 + 582 =
2990
</pre>
<p>It&#8217;s 6 times 97. It&#8217;s easy to find this &#8220;magic number&#8221;, but I won&#8217;t go into this right now. OK, you have a rightmost zero. Let&#8217;s try to add again in order to have two zeros:</p>
<pre>
2990 + 30 * 97 =
2990 + 2910 =
5900
</pre>
<p>Great! But&#8230; so what? Well, suppose these numbers live in a world where they must be divided by 100 after you multiply them and use the above procedure. Then you divide 5900 by 100, which is cheap, and you have 59.</p>
<p>Sadly, these numbers don&#8217;t live in such a world, and 59 is not the right answer. If the division is exact, you can divide by any multiple of 97, but you can&#8217;t simply divide by 100.</p>
<h3>The Montgomery World</h3>
<p>The second insight of the Montgomery reduction is: if you need such a world were you must divide by 100, then just create it!</p>
<p>The Montgomery reduction requires transforming the numbers into the &#8220;Montgomery domain&#8221;, which is exactly the world we need. First let&#8217;s look at our modulus: 97. It has two digits in base 10. Then let R be smaller power of the base that is greater than the modulus. For 97, we have R = 10^2 = 100.</p>
<p>To transform a number x into the Montgomery domain, compute x * R (mod p). In our example, suppose x = 35. Then 35 * 100 (mod 97) = 8 (mod 97).</p>
<p>(You may notice that this transformation simply begs the question &#8211; you need to reduce modulo p in order to convert the numbers to a form where it&#8217;s easier to reduce modulo p! I&#8217;ll talk about this later on.)</p>
<p>Now, the beauty of the Montgomery domain: suppose that you want to compute x * y (mod p). Converting the operands, you&#8217;ll actually compute (x * R) * (y * R) (mod p). But you want the result to still live in the Montgomery domain. That&#8217;s because you may need to do many other multiplications. Since converting the number back to the real world is somewhat expensive, it&#8217;s best to keep the computations in the Montgomery domain. Therefore, you want to compute x * y * R (mod p), i.e., you want x * y, but in the Montgomery domain.</p>
<p>Let&#8217;s see&#8230; if you just multiply the operands the usual way, you get (x * R) * (y * R) (mod p) = x * y * R * R (mod p). But you want x * y * R (mod p). Therefore, every time you multiply two numbers, you&#8217;ll need to reduce the result modulo p, but you&#8217;ll also need to divide by R. Recall our example, where R = 100, and we wanted to be required to divide by 100. That&#8217;s exactly what we got: the Montgomery domain requires the division by 100!</p>
<h3>An example</h3>
<p>Let x = 43, y = 56, p = 97, R = 100. You want to compute x * y (mod p). First you convert x and y to the Montgomery domain. For x, compute x&#8217; = x * R (mod p) = 43 * 100 (mod 97) = 32, and for y, compute y&#8217; = y * R (mod p) = 56 * 100 (mod 97) = 71.</p>
<p>Compute a := x&#8217; * y&#8217; = 32 * 71 = 2272.</p>
<p>In order to zero the first digit, compute a := a + (4p) = 2272 + 388 = 2660.</p>
<p>In order to zero the second digit, compute a := a + (20p) = 2660 + 1940 = 4600.</p>
<p>Compute a := a / R = 4600 / 100 = 46.</p>
<p>We have that 46 is the Montgomery representation of x * y (mod p), that is, x * y * R (mod p). In order to convert it back, compute a * (1/R) (mod p) = 46 * 65 (mod 97) = 80. You can check that 43 * 56 (mod 97) is indeed 80.</p>
<h3>Converting the numbers</h3>
<p>Let Montgomery(x&#8217;,y&#8217;) = (x&#8217;y')/R (mod p) be the Montgomery reduction of x&#8217; and y&#8217;.</p>
<p>Like I mentioned, in order to compute the Montgomery representation of a number or in order to convert it back, you need to multiply by R modulo p or divide by R modulo p. You can compute this remainders using a classic, expensive algorithm, since for practical applications you&#8217;ll rarely need to convert the numbers (for example, in a cryptographic application: since no one will need to actually see the real form of the numbers, there is no need for conversions and it&#8217;s possible to work entirely in the Montgomery domain). But there is also another approach. You can precompute R&#8217; = R^2 (mod p) and then convert a number x to xR (mod p) by simple computing the Montgomery reduction of x and R&#8217;, since Montgomery(x, R&#8217;) = Montgomery(x, R^2) = (xRR)/R (mod p) = xR (mod p). To convert back a number x&#8217; = xR (mod p), simply compute the Montgomery reduction of x&#8217; and 1, since Montgomery(x&#8217;, 1) = Montgomery(xR, 1) = (xR)/R (mod p) = x (mod p).</p>
<h3>The magic numbers</h3>
<p>Let B be the base you&#8217;re working with. The main step of the Montgomery reduction is &#8220;add a multiple k of p that, when added to a, makes its i-th digit zero&#8221;. It&#8217;s not hard to find k. First, notice that the only relevant digits are the first digit of p (call it p[0]) and the i-th digit of a (call it a[i]). Then:</p>
<pre>
a[i] + B^i * k * p[0] = 0 (mod B)
k = B^i * a[i] * -(1/p[0]) (mod B)
</pre>
<p>You can precompute -(1/p[0]) (mod B) and then multiply it by a[i] and B^i (which is just an offset) in order to find k. In our example, with p = 97 and base 10, this value is -(1/7) (mod 10) = -3 (mod 10) = 7.</p>
<h3>In the real world</h3>
<p>For example, in elliptic curve cryptography in the 128-bit level of security, the operands have 256 bits. Suppose the target platform is a desktop PC with 32 bits. Then the operands are represented by eight 32-bit digits (i.e. B=2^32 as opposed to B=10 in the examples); R is 2^256; and the Montgomery reduction requires eight steps (since the numbers have eight integers).</p>
<p>If you look carefully, you can notice that the Montgomery reduction is nothing more than a multiplication of p with an operand which is computed on the fly (the &#8220;magic numbers&#8221;). This allows the use of many optimizations from multiplication algorithms.</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/understanding-the-montgomery-reduction-algorithm/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Understanding the extended Euclidean algorithm</title>
		<link>http://alicebob.cryptoland.net/understanding-the-extended-euclidian-algorithm/</link>
		<comments>http://alicebob.cryptoland.net/understanding-the-extended-euclidian-algorithm/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 22:22:53 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=213</guid>
		<description><![CDATA[The Euclidean algorithm finds the greatest common divisor of two numbers a and b. There is an extended version of it that also finds two numbers x and y such that ax + by = gcd(x,y). This is useful when searching for modular multiplicative inverses. The algorithm is simple, but I&#8217;ve never bothered to study [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean algorithm</a> finds the greatest common divisor of two numbers <em>a</em> and <em>b</em>. There is an <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">extended version of it</a> that also finds two numbers <em>x</em> and <em>y</em> such that <em>ax + by = gcd(x,y)</em>. This is useful when searching for modular multiplicative inverses.</p>
<p>The algorithm is simple, but I&#8217;ve never bothered to study why and how it works (a shame, really, but sometimes you have to postpone the understanding of some basic things in order to go on&#8230;). Finally I&#8217;ve decide to put some thought on it and came up with this (this is not a proof; it&#8217;s just some intuitive thinking to grasp the inner workings of the algorithm).</p>
<p>Suppose that you want to compute the extended Euclidean algorithm for a = 14 and b = 101. You can write</p>
<pre>
14 *   1 + 101 *   0 =  14
14 *   0 + 101 *   1 = 101
</pre>
<p>Which is obviously true. Now subtract the first equation from the second:</p>
<pre>
14 *   1 + 101 *   0 =  14
14 *   0 + 101 *   1 - (14 *   1 + 101 *   0) = 101 - 14 -&gt;
14 *  -1 + 101 *   1 = 87
</pre>
<p>You can repeat this many times before reducing the right-hand 101 in the second equation to the smallest positive value possible. Notice that it&#8217;s faster to take the <code>floor(101/14)</code> and subtract the first equation times the result from the second. In this case, we have <code>floor(101/14) = 7</code>. Therefore, replace the last step with:</p>
<pre>
14 *   1 + 101 *   0 =  14
14 *   0 + 101 *   1 - [7 * (14 *   1 + 101 *   0)] = 101 - [7 * 14] -&gt;
14 *  -7 + 101 *   1 =   3
</pre>
<p>Now, switch equations, in order to make the first one the one with the smallest right-hand size (you&#8217;ll always need to switch):</p>
<pre>
14 *  -7 + 101 *   1 =   3
14 *   1 + 101 *   0 =  14
</pre>
<p>And here you are. Just repeat the same step over and over:</p>
<pre>
14 *  29 + 101 *  -4 =   2
14 *  -7 + 101 *   1 =   3

14 * -36 + 101 *   5 =   1
14 *  29 + 101 *  -4 =   2

14 * 101 + 101 * -14 =   0
14 * -36 + 101 *   5 =   1
</pre>
<p>When your first equation has right-hand side equal to 0 (this will always happen), the second equation will be <em>ax + by = gcd(x,y)</em>!</p>
<p>As I mentioned, this is useful in order to find modular inverses: to find which number that when multiplied by 14 gives 1 (modulo 101) you run the algorithm as described and finds that it&#8217;s -36 (which is 65 modulo 101). That is, 14 * 65 = 910, which when divided by 101, gives remainder 1. Modular inverses are used in many cryptographic applications, such as <a href="http://en.wikipedia.org/wiki/RSA">RSA</a>.</p>
<p>I think I&#8217;ll study next the seemingly magical <a href="http://en.wikipedia.org/wiki/Montgomery_reduction">Montgomery reduction</a> (I understand how it works, I have even implemented it, but I still don&#8217;t know why the heck it works)&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/understanding-the-extended-euclidian-algorithm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Frobenius endomorphism with finite fields</title>
		<link>http://alicebob.cryptoland.net/the-frobenius-endomorphism-with-finite-fields/</link>
		<comments>http://alicebob.cryptoland.net/the-frobenius-endomorphism-with-finite-fields/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 03:17:29 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[frobenius]]></category>
		<category><![CDATA[sage]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=153</guid>
		<description><![CDATA[The Frobenius endomorphism is defined as: where p is the characteristic of the ring you&#8217;re working with. Simple, right? If you&#8217;re working with a field with prime order, then Frobenius is actually the identity map. Since the order of the multiplicative subgroup is p, when you raise to the power of p you get back [...]]]></description>
			<content:encoded><![CDATA[<p>The Frobenius endomorphism is defined as:</p>
<img src='http://s.wordpress.com/latex.php?latex=%5CPhi%28x%29%3Dx%5Ep&#038;bg=T&#038;fg=000000&#038;s=0' alt='\Phi(x)=x^p' title='\Phi(x)=x^p' class='latex' />
<p>where p is the characteristic of the ring you&#8217;re working with. Simple, right?</p>
<p>If you&#8217;re working with a field with prime order, then Frobenius is actually the identity map. Since the order of the multiplicative subgroup is p, when you raise to the power of p you get back to x due to <a href="http://en.wikipedia.org/wiki/Fermat%27s_little_theorem">Fermat&#8217;s little theorem</a>. Things get more interesting when you&#8217;re working with a extension field (i.e. a field which order is a prime power).</p>
<p>I&#8217;m studying <a href="http://en.wikipedia.org/wiki/Pairing#Pairings_in_Cryptography">pairings</a> for my master&#8217;s degree and the Frobenius endomorphism appears all the time in their computation. For example, you need to do a &#8220;final exponentation&#8221; which can be split in multiple exponentiations, and some of them are to the power of p. This is good because powering to p is &#8220;easy&#8221; due to Frobenius, or at least all the papers I read said so. But for a while I couldn&#8217;t see why, and that&#8217;s the reason I&#8217;m posting this. It&#8217;s really easy; it&#8217;s just not that obvious to see why.</p>
<h3>Why Frobenius is easy?</h3>
<p>Say you&#8217;re working with a quadratic extension, that is, with a field <img src='http://s.wordpress.com/latex.php?latex=GF%28p%5E2%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(p^2)' title='GF(p^2)' class='latex' /> where p is a prime. You can represent this group with polynomials of degree 1 which can be added the usual way and multiplied taking the result modulo a irreducible polynomial of degree 2. To understand why this makes sense, I recommend <a href="http://everything2.com/index.pl?node_id=1674246">this excellent write-up at Everything2</a>. Assume that you pick a irreducible polynomial in the form <img src='http://s.wordpress.com/latex.php?latex=X%5E2-%5Cbeta&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^2-\beta' title='X^2-\beta' class='latex' />. Working modulo this polynomial is the same thing as working in a &#8220;world&#8221; where <img src='http://s.wordpress.com/latex.php?latex=X%5E2%20%3D%20%5Cbeta&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^2 = \beta' title='X^2 = \beta' class='latex' />. (You could of course work with a polynomial in the form <img src='http://s.wordpress.com/latex.php?latex=X%5E2%20%2B%20aX%20%2B%20b&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^2 + aX + b' title='X^2 + aX + b' class='latex' /> but that would complicate things.)</p>
<p>So every element of <img src='http://s.wordpress.com/latex.php?latex=GF%28p%5E2%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(p^2)' title='GF(p^2)' class='latex' /> can be written as <img src='http://s.wordpress.com/latex.php?latex=a%20%2B%20bX&#038;bg=T&#038;fg=000000&#038;s=0' alt='a + bX' title='a + bX' class='latex' />. What happens when you apply the Frobenius endomorphism? Let&#8217;s see:</p>
<img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep%20%3D%20%28a%5Ep%20%2B%20b%5EpX%5Ep%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p = (a^p + b^pX^p)' title='(a + bX)^p = (a^p + b^pX^p)' class='latex' />
<p>Why is that so? That&#8217;s a known fact about the Frobenius, check the <a href="http://en.wikipedia.org/wiki/Frobenius_automorphism">explanation at Wikipedia</a> for more details. But basically, the expansion of <img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p' title='(a + bX)^p' class='latex' /> has many terms, but only the first <img src='http://s.wordpress.com/latex.php?latex=%28a%5Ep%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a^p)' title='(a^p)' class='latex' /> and the last <img src='http://s.wordpress.com/latex.php?latex=%28b%5EpX%5Ep%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(b^pX^p)' title='(b^pX^p)' class='latex' /> survive because all others are multiples of p. Since we&#8217;re working with coefficients modulo p, they are all zero.</p>
<p>Let&#8217;s continue. Since <img src='http://s.wordpress.com/latex.php?latex=a%2C%20b%20%5Cin%20GF%28p%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='a, b \in GF(p)' title='a, b \in GF(p)' class='latex' />, then raising to the power of p won&#8217;t change them (yep, that&#8217;s Frobenius again). So we have:</p>
<img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep%20%3D%20%28a%5Ep%20%2B%20b%5EpX%5Ep%29%20%3D%20%28a%20%2B%20bX%5Ep%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p = (a^p + b^pX^p) = (a + bX^p)' title='(a + bX)^p = (a^p + b^pX^p) = (a + bX^p)' class='latex' />
<p>There&#8217;s only <img src='http://s.wordpress.com/latex.php?latex=X%5Ep&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^p' title='X^p' class='latex' /> left to bother us. If p is odd (not 2), then you can rearrange this as:</p>
<img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep%20%3D%20%28a%20%2B%20bX%5Ep%29%20%3D%20%28a%20%2B%20b%28X%5E2%29%5E%7B%28p-1%29%2F2%7DX%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p = (a + bX^p) = (a + b(X^2)^{(p-1)/2}X)' title='(a + bX)^p = (a + bX^p) = (a + b(X^2)^{(p-1)/2}X)' class='latex' />
<p>Now remember that we&#8217;re working in a &#8220;world&#8221; where <img src='http://s.wordpress.com/latex.php?latex=X%5E2%20%3D%20%5Cbeta&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^2 = \beta' title='X^2 = \beta' class='latex' />. Then we get:</p>
<img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep%20%3D%20%28a%20%2B%20b%28X%5E2%29%5E%7B%28p-1%29%2F2%7DX%5Ep%29%20%3D%20%28a%20%2B%20b%5Cbeta%5E%7B%28p-1%29%2F2%7DX%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p = (a + b(X^2)^{(p-1)/2}X^p) = (a + b\beta^{(p-1)/2}X)' title='(a + bX)^p = (a + b(X^2)^{(p-1)/2}X^p) = (a + b\beta^{(p-1)/2}X)' class='latex' />
<p>That&#8217;s why Frobenius is easy: &#8220;a&#8221; stays the same, all you need to do is multiply &#8220;b&#8221; with <img src='http://s.wordpress.com/latex.php?latex=%5Cbeta%5E%7B%28p-1%29%2F2%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='\beta^{(p-1)/2}' title='\beta^{(p-1)/2}' class='latex' />. But according to <a href="http://en.wikipedia.org/wiki/Euler%27s_criterion">Euler&#8217;s criterion</a>, since <img src='http://s.wordpress.com/latex.php?latex=%5Cbeta&#038;bg=T&#038;fg=000000&#038;s=0' alt='\beta' title='\beta' class='latex' /> is not a square (if it were, <img src='http://s.wordpress.com/latex.php?latex=X%5E2-%5Cbeta&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^2-\beta' title='X^2-\beta' class='latex' /> wouldn&#8217;t be irreducible), we have <img src='http://s.wordpress.com/latex.php?latex=%5Cbeta%5E%7B%28p-1%29%2F2%7D%20%5Cequiv%20-1%20%5Cpmod%7Bp%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='\beta^{(p-1)/2} \equiv -1 \pmod{p}' title='\beta^{(p-1)/2} \equiv -1 \pmod{p}' class='latex' />. Then the formula gets much simpler:</p>
<img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep%20%3D%20%28a-bX%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p = (a-bX)' title='(a + bX)^p = (a-bX)' class='latex' />
<h3>A concrete example</h3>
<p>Just for the sake of concreteness, let&#8217;s work out an example. I&#8217;ll use <a href="http://www.sagemath.org/">Sage</a> for that, but I&#8217;ll explain what each command does.</p>
<pre>
sage: K = GF(7)
</pre>
<p>We create a field with 7 elements (that&#8217;s actually integers modulo 7).</p>
<pre>
sage: K(5)^7
5
</pre>
<p>We take the element 5 and power to 7. We get 5 again. If you don&#8217;t believe it works for all elements:</p>
<pre>
sage: [(x,x^7) for x in K]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]
</pre>
<p>Let&#8217;s create <img src='http://s.wordpress.com/latex.php?latex=GF%287%5E2%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(7^2)' title='GF(7^2)' class='latex' />.</p>
<pre>
sage: KR. = GF(7)[]
</pre>
<p>This creates a polynomial ring with the X variable and coefficients in <img src='http://s.wordpress.com/latex.php?latex=GF%287%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(7)' title='GF(7)' class='latex' />, just to allow us to specify the modulus in the next step:</p>
<pre>
sage: K2. = GF(7^2, modulus=X^2+1)
</pre>
<p>We create the <img src='http://s.wordpress.com/latex.php?latex=GF%287%5E2%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(7^2)' title='GF(7^2)' class='latex' /> field using the variable X (I&#8217;m overwriting the X used in the ring, you could use other name) and modulus <img src='http://s.wordpress.com/latex.php?latex=X%5E2%2B1&#038;bg=T&#038;fg=000000&#038;s=0' alt='X^2+1' title='X^2+1' class='latex' /> (so <img src='http://s.wordpress.com/latex.php?latex=%5Cbeta%20%3D%20-1&#038;bg=T&#038;fg=000000&#038;s=0' alt='\beta = -1' title='\beta = -1' class='latex' />). Let&#8217;s take an arbitrary element to the power of 7:</p>
<pre>
sage: (3*X + 2)^7
4*X + 2
</pre>
<p>Remeber when <img src='http://s.wordpress.com/latex.php?latex=%5Cbeta%20%3D%20-1&#038;bg=T&#038;fg=000000&#038;s=0' alt='\beta = -1' title='\beta = -1' class='latex' /> then <img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%5Ep%20%3D%20%28a-bX%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX)^p = (a-bX)' title='(a + bX)^p = (a-bX)' class='latex' />. And modulo 7, -3 is actually 4, so the result is correct (of course!). Again, if you don&#8217;t believe it works for all elements:</p>
<pre>
sage: all(x^7 == (x.vector()[0] - x.vector()[1]*X) for x in K2)
True
</pre>
<p>This checks, for all elements in the field K2, if the element raised to the power of 7 is equal to the element built with our special formula. The method <code>vector()</code> returns the coefficients of the polynomial as a list. The <code>all</code> function is a relatively unknown function of Python that returns True if all the elements of the iterable passed to it evaluate to True (there&#8217;s <code>any(iter)</code> too).</p>
<h3>Extensions of higher degree</h3>
<p>The trick to calculate the Frobenius endomorphism also works for extensions of higher degree. When implementing them, usually using a tower of extensions is more efficient then using a direct extension. For example, when working with <img src='http://s.wordpress.com/latex.php?latex=GF%28p%5E%7B12%7D%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(p^{12})' title='GF(p^{12})' class='latex' />, you can represent it as a quadratic extension of <img src='http://s.wordpress.com/latex.php?latex=GF%28p%5E6%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(p^6)' title='GF(p^6)' class='latex' />, which can be represented as a cubic extension of <img src='http://s.wordpress.com/latex.php?latex=GF%28p%5E2%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(p^2)' title='GF(p^2)' class='latex' />, which can be represented as a quadratic extension of <img src='http://s.wordpress.com/latex.php?latex=GF%28p%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='GF(p)' title='GF(p)' class='latex' />.</p>
<p>For example, for <img src='http://s.wordpress.com/latex.php?latex=%28a%20%2B%20bX%29%20%5Cin%20GF%28p%5E%7B12%7D%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='(a + bX) \in GF(p^{12})' title='(a + bX) \in GF(p^{12})' class='latex' /> built this way, with <img src='http://s.wordpress.com/latex.php?latex=a%2C%20b%20%5Cin%20GF%28p%5E6%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='a, b \in GF(p^6)' title='a, b \in GF(p^6)' class='latex' />, you just apply the same trick explained above. The only difference is that a and b to the power of p aren&#8217;t a and b themselves, but that&#8217;s not a problem, you just apply the same trick recursively.</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/the-frobenius-endomorphism-with-finite-fields/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Visualizing group structure with colored addition/multiplication tables</title>
		<link>http://alicebob.cryptoland.net/visualizing-group-structure-with-colored-additionmultiplication-tables/</link>
		<comments>http://alicebob.cryptoland.net/visualizing-group-structure-with-colored-additionmultiplication-tables/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 04:32:51 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[elliptic curve]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[RSA]]></category>
		<category><![CDATA[visualization]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=70</guid>
		<description><![CDATA[When working with finite fields, if the number of elements is a prime power with m &#62; 1, you can represent the elements as polynomials with degree m-1 and do the field addition and multiplication modulo a irreducible polynomial with degree m. The field GF(5) is composed by the numbers 0 to 4. We don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>When working with finite fields, if the number of elements is a prime power <img src='http://s.wordpress.com/latex.php?latex=p%5Em&#038;bg=T&#038;fg=000000&#038;s=0' alt='p^m' title='p^m' class='latex' /> with m &gt; 1, you can represent the elements as polynomials with degree m-1 and do the field addition and multiplication modulo a irreducible polynomial with degree m.</p>
<p>The field GF(5) is composed by the numbers 0 to 4. We don&#8217;t need to represent its elements as polynomials since m=1. Addition is done modulo 5 and multiplication also modulo 5. So 2 + 3 = 0; 4 * 2 = 3; and so on. This is the addition table for GF(5):</p>
<p class="aligncenter">
<a href="http://alicebob.cryptoland.net/files/z5m.png"><img src="http://alicebob.cryptoland.net/files/z5m.png" alt="Multiplicative table of integers modulo 5" width="100" height="100" class="size-full wp-image-74" /></a></p>
<p>The rows, top down, represent 0 to 4. The columns, right to left, represent 0 to 4. Each square is the result of the addition of the respective numbers in the row / column it belongs to. Black is 0, purple is 1, red is 2, orange is 3, yellow is 4.</p>
<p>In the field GF(25) = GF(5²), as I said, you represent each element as a polynomial. So we have 25 elements: 0 to 4; x, x+ 1, &#8230;, x + 4; 2x, 2x + 1, &#8230;; 3x, 3x + 1, &#8230;; 4x, 4x + 1, &#8230;, 4x + 4.</p>
<p>In order to add two elements, add them as you would add two polynomials, but remember that the coefficients are in GF(5); for example, in GF(5²), we have (3x + 2) + (4x + 4) = (2x + 1). In order to multiply two elements, multiply them as usual but then take the result modulo an irreducible polynomial. So, with GF(5²) modulo <img src='http://s.wordpress.com/latex.php?latex=x%5E2%20%2B%204x%20%2B%202&#038;bg=T&#038;fg=000000&#038;s=0' alt='x^2 + 4x + 2' title='x^2 + 4x + 2' class='latex' />, you have (2x + 5) * (3x + 4) = (4x + 3).</p>
<p>I always wondered what would happen when you changed the modulus. Obviously the group &#8220;changes&#8221;, but in order to actually see it, I&#8217;ve built the multiplication table for GF(5²) modulus <img src='http://s.wordpress.com/latex.php?latex=x%5E2%20%2B%204x%20%2B%202&#038;bg=T&#038;fg=000000&#038;s=0' alt='x^2 + 4x + 2' title='x^2 + 4x + 2' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=x%5E2%20%2B%203x%20%2B%203&#038;bg=T&#038;fg=000000&#038;s=0' alt='x^2 + 3x + 3' title='x^2 + 3x + 3' class='latex' />:</p>
<p class="aligncenter">
<img src="http://alicebob.cryptoland.net/files/gf25m_x2_4x_2.png" alt="Multiplicative table for GF(5^2)/(x^2+4x+2)" width="200" height="200" class="size-medium wp-image-76" /><img src="http://alicebob.cryptoland.net/files/gf25m_x2_3x_3.png" alt="Multiplicative table for GF(5^2)/(x^2+3x+3)" width="200" height="200" class="size-medium wp-image-85" />
</p>
<p>Yep, they&#8217;re different <img src='http://alicebob.cryptoland.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Of course, both are isomorphic, so you&#8217;re free to pick your favorite modulus.</p>
<h3>Multiplicative group of integers modulo n vs group of points in a elliptic curve</h3>
<p>Then I got curious: how would the multiplication table of integers modulo n look like? This group is the group used in many cryptographic schemes, like RSA. This is the multiplication table for integers modulo 509:</p>
<p class="aligncenter">
<img src="http://alicebob.cryptoland.net/files/z509m.png" alt="Multiplicative table for integers modulo 509" width="508" height="508" class="size-full wp-image-78" />
</p>
<p>Pretty (and trippy)!</p>
<p>What about the group of points on a elliptic curve over a finite field, which is also a group used in cryptographic schemes? This is the additive table for the points on <img src='http://s.wordpress.com/latex.php?latex=y%5E2%20%3D%20x%5E3%20%2B%204x%20%2B%201&#038;bg=T&#038;fg=000000&#038;s=0' alt='y^2 = x^3 + 4x + 1' title='y^2 = x^3 + 4x + 1' class='latex' /> over GF(503):</p>
<p class="aligncenter">
<img src="http://alicebob.cryptoland.net/files/e_4_1_503.png" alt="Additive table for points in y^2 = x^3 +4x + 1 over GF(503)" width="516" height="516" class="size-full wp-image-79" />
</p>
<p>The difference between them is striking; the elliptic group seems almost random. This is (intuitively speaking! I’m not being formal here) the reason why this group is used in cryptography in the first place: since the group structure is more “messed up”, you can get away with using groups of much smaller size (no smaller than <img src='http://s.wordpress.com/latex.php?latex=2%5E%7B160%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='2^{160}' title='2^{160}' class='latex' /> elements) than with multiplicative groups of integers modulo n (no smaller than <img src='http://s.wordpress.com/latex.php?latex=2%5E%7B1024%7D&#038;bg=T&#038;fg=000000&#038;s=0' alt='2^{1024}' title='2^{1024}' class='latex' /> elements). This is not set in stone though; maybe someday someone will come up with a better method to crack this seemingly random structure (for now the best method to solve the discrete log problem for elliptic groups is exponential, while the best method for integers modulo n is sub-exponential).</p>
<p>It’s worth mentioning that even this elliptic group is not that extraordinary: it is isomorphic to the very simple additive group of integers modulo 506:</p>
<p class="aligncenter"><img src="http://alicebob.cryptoland.net/files/z506a.png" alt="Additive table of integers modulo 516" width="506" height="506" class="size-full wp-image-89" />
</p>
<p>The big problem is to find the isomorphism! You can see this better with a small example. This is the elliptic group of <img src='http://s.wordpress.com/latex.php?latex=y%5E2%20%3D%20x%5E3%20%2B%203x%20%2B%202&#038;bg=T&#038;fg=000000&#038;s=0' alt='y^2 = x^3 + 3x + 2' title='y^2 = x^3 + 3x + 2' class='latex' /> over GF(5) (left) which is isomorphic to the additive group modulo 5 (right):</p>
<p class="aligncenter">
<img src="http://alicebob.cryptoland.net/files/e_3_2_5.png" alt="Additive table of points on y^2 = x^3 + 3x + 2 over GF(5)" width="100" height="100" class="size-full wp-image-81" /><img src="http://alicebob.cryptoland.net/files/z5a.png" alt="Additive table for integers modulo 5" width="100" height="100" class="size-full wp-image-83" />
</p>
<p>(OK, it&#8217;s not that easy to see)</p>
<h3>Software</h3>
<p>To generate those images, I&#8217;ve used Python with <a href="http://www.pythonware.com/products/pil/">PIL</a> and <a href="http://www.sagemath.org/">Sage</a>. Sage aims to be a open source replacement for (expensive) software like Magma, Maple, Mathematica and Matlab. Since I&#8217;ve never used those, I can&#8217;t really say how it is going in its mission, but it&#8217;s really awesome. If you&#8217;re a Windows user you&#8217;ll probably be scared by the fact that the Windows version of Sage is actually an entire Linux virtual machine! They&#8217;re working to port it natively, but even until then, it&#8217;s worth it (and you&#8217;ll have an excuse to try Linux <img src='http://alicebob.cryptoland.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )</p>
<h3>Update</h3>
<p>Someone asked for the source code used to generate those. It&#8217;s ugly (I&#8217;ve added comments at least) but you can download it here: the <a href="http://alicebob.cryptoland.net/files/gentable.sage">sage script</a> and the <a href="http://alicebob.cryptoland.net/files/plottable.py">python script</a>. In the sage script, uncomment the lines representing what you want to plot, then run <code>./sage gentable.sage</code> (or whichever path to were sage is). It will generate a data.txt in the same folder. Now run <code>python plottable.py img.png</code> to plot it on the img.png file (or omit it to show on the screen). You&#8217;ll need to have PIL installed.</p>
<p>If you don&#8217;t want to plot fancy stuff as elliptic groups, you can easily transform the gentable.sage into a normal Python script and write the addition/multiplication yourself (like a + b % n). Have fun, and feel free to ask anything.</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/visualizing-group-structure-with-colored-additionmultiplication-tables/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

