<?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; sage</title>
	<atom:link href="http://alicebob.cryptoland.net/tag/sage/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>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>
	</channel>
</rss>

