<?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>Modulo Errors &#187; Algorithms</title>
	<atom:link href="http://maths.straylight.co.uk/archives/category/algorithms/feed" rel="self" type="application/rss+xml" />
	<link>http://maths.straylight.co.uk</link>
	<description>for when the margin is too small</description>
	<lastBuildDate>Thu, 12 Jan 2012 16:36:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Genetic Algorithms with Processing</title>
		<link>http://maths.straylight.co.uk/archives/125</link>
		<comments>http://maths.straylight.co.uk/archives/125#comments</comments>
		<pubDate>Sat, 13 Dec 2008 19:28:44 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Pop.Maths]]></category>

		<guid isPermaLink="false">http://maths.straylight.co.uk/?p=125</guid>
		<description><![CDATA[A processing applet to tackle a genetic algorithm challenge.]]></description>
			<content:encoded><![CDATA[<p>Genetic Algorithms are something I&#8217;ve found intriguing for a long time, but I&#8217;d never got around to actually implementing one. A recent flurry of blog posts and forum chatter about them &#8211; such as <a href="http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/">this program</a> for evolving the Mona Lisa as a vector image, or <a href="http://www.wreck.devisland.net/ga/">this one</a> which breeds vehicles, rekindled my interest. I quickly found <a href="http://www.ai-junkie.com/ga/intro/gat1.html">a tutorial</a> outlining the basics, and posing a sample problem: given a series of disks in a given area, find the largest disk which fits within the area without overlapping any of the others. I&#8217;d been planning to work, as usual, with SAGE, but the graphical nature of this problem gave me an opportunity to dive once again in to <a href="http://processing.org">processing</a>, which as it happens has recently left beta.</p>
<p><center><a href="http://maths.straylight.co.uk/processing/CAcrossover.html"><img src="http://maths.straylight.co.uk/processing/CAcrossover.png"/></a></center></p>
<p>You can play with the results <a href="http://maths.straylight.co.uk/processing/CAcrossover.html">here</a>; for an afternoon&#8217;s coding, I&#8217;m quite pleased with it as a proof of concept even though it doesn&#8217;t always do a fantastic job of solving the problem! As implemented, each run generates a fixed obstacle set of a dozen disks (in black) and an initial population of 25 candidate solutions. The &#8216;genome&#8217; is 28 bit, split 10/9/9 for x co-ordinate, y co-ordinate and radius. Disks that overlap an obstacle or go off screen are awarded a fitness of zero and painted red; otherwise the fitness function is simply the radius. In line with the tutorial, I pick pairs by roulette selection based on this fitness (so red disks shouldn&#8217;t survive into the next generation), breed by crossover at bit 20, and mutate about 1 in 200 bits. </p>
<p>However, for this particular set up the crossover approach doesn&#8217;t seem ideal- offspring will inherit almost exactly the co-ordinates of one parent and the radius of the other. This often means large areas of the screen are missed unless a chance mutation moves a disk, which then has to be lucky enough to produce a few offspring in the next generation. It might work better if I generated offspring bit by bit (say, taking bits from each parent a certain percentage of the time), rather than the simple cut currently in use. Fortunately, it shouldn&#8217;t be hard to experiment in this way; ideally, I&#8217;d build several algorithms into the applet and offer a choice at runtime, as well as control over the assorted variables. If nothing else, it&#8217;s teaching me a lot about processing, which I continue to be impressed with.</p>
]]></content:encoded>
			<wfw:commentRss>http://maths.straylight.co.uk/archives/125/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geometry Club Talk: Computational aspects of ECDLP</title>
		<link>http://maths.straylight.co.uk/archives/118</link>
		<comments>http://maths.straylight.co.uk/archives/118#comments</comments>
		<pubDate>Wed, 23 Apr 2008 09:05:35 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
				<category><![CDATA[Algebraic Geometry]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Complexity]]></category>
		<category><![CDATA[Cryptology]]></category>
		<category><![CDATA[Group Theory]]></category>
		<category><![CDATA[PhD]]></category>
		<category><![CDATA[Talks]]></category>

		<guid isPermaLink="false">http://maths.straylight.co.uk/?p=118</guid>
		<description><![CDATA[Notes from my geometry club talk given April 18th, 2008.]]></description>
			<content:encoded><![CDATA[<p>On Friday I gave a <a href="http://www.maths.ed.ac.uk/~s0198576/gc/">geometry club</a> seminar, speaking about some of the computational aspects of discrete-logarithm cryptography in general and as implemented for elliptic curves.  <a href="http://www.maths.ed.ac.uk/~s0677951/gc_apr_08.pdf">My notes</a> supplement rather than completely describe the talk, being heavier on the formalities and lighter on the narrative. </p>
<p>The topics covered are: Diffie-Hellman and one-way functions for key exchange; the generic Discrete Logarithm Problem and BSGS algorithm; scalar multiplication- addition chains, fast exponentiation, m-ary methods and windowing; group law implementations, Side-channel attacks and the Edwards form. </p>
<p>I&#8217;ve discussed several of these ideas elsewhere on this blog, as well as the cryptanalysis ideas I mentioned on the day but which are not in the notes. I also refered to a recent real-world example of a side-channel attack; see <a href="http://www.theregister.co.uk/2008/04/03/keeloq_master_key_found/">this</a> story from The Register for details.</p>
]]></content:encoded>
			<wfw:commentRss>http://maths.straylight.co.uk/archives/118/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Extended Euclidean Algorithm</title>
		<link>http://maths.straylight.co.uk/archives/112</link>
		<comments>http://maths.straylight.co.uk/archives/112#comments</comments>
		<pubDate>Thu, 17 Jan 2008 22:45:40 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[MAT-1-GTh]]></category>
		<category><![CDATA[Number Theory]]></category>

		<guid isPermaLink="false">http://maths.straylight.co.uk/?p=112</guid>
		<description><![CDATA[Some notes on Euclid's algorithm and its extension for solving linear Diophantine equations in two variables.]]></description>
			<content:encoded><![CDATA[<p><em>I promised some of my tutorial students a demonstration of how the &#8216;highschool&#8217; approach to Euclid&#8217;s algorithm can be reversed to give rise to the extended Euclidean algorithm &#8211; as opposed to the version in their lecture notes, which finds both gcd(a,b) and x,y such that ax+by=gcd(a,b)  in one pass, at the price of some notational complexity. To do so, it seemed worth recapping some of the properties of divisibility that make Euclid&#8217;s algorithm tick, and give an application for its extended form. That ended up taking four pages, so I figured I&#8217;d post it here as well&#8230; you can read it behind the cut, or download <a href="http://aleph.straylight.co.uk/eea.pdf">the LaTeX-formatted pdf version</a>.</em></p>
<p><span id="more-112"></span></p>
<h2>The Euclidean Algorithm</h2>
<p>Euclidâ€™s algorithm recovers the greatest common divisor of two numbers a and b- that is, the largest x with the property that x&#124;a and x&#124;b (we write<br />
x = gcd(a, b)). Thus, anything that is true of divisors of a and b in general is true of their g.c.d in particular.</p>
<p> To this end, recall that if x&#124;a and x&#124;b then x&#124;a &plusmn; b. Further, if x&#124;b then x&#124;&lambda;b for any integer &lambda;. Combining the two, we have that</p>
<p align=center>x&#124;a and x&#124;b &rArr; x&#124;(a &plusmn; &lambda;b) for all &lambda;   &nbsp;&nbsp;&nbsp;&nbsp;(1)</p>
<p>How does this help us? Since gcd(a, b) = gcd(b, a), we can assume w.l.o.g that a &ge; b. Then by the quotient-remainder theorem, we can write a as a = qb + r for some integers q, r with 0 &le; r &lt; b. Rearranging gives us r = a -qb, so by (1) we have</p>
<p align=center>x&#124;a and x&#124;b &rArr; x&#124;(a &#8211; qb) = r &nbsp;&nbsp;&nbsp;&nbsp;(2)</p>
<p>So in particular</p>
<p align=center>gcd(a, b)&#124;a and gcd(a, b)&#124;b &rArr; gcd(a, b)&#124;r &nbsp;&nbsp;&nbsp;&nbsp;(3)</p>
<p>So, to solve the question of finding gcd(a, b), we can instead solve the question of finding gcd(b, r). Notice that b &le; a and r &lt; b, so this second question is easier.</p>
<p>Repeating this process, the pairs of terms keep decreasing until eventually we have the question &#8220;find the g.c.d. of r and 0&#8243; for some r. But as anything divides 0, gcd(r, 0) = r. So there is always a final question; that question is easy to answer; and its answer is the same as the original g.c.d. we were looking for!</p>
<h3>Worked Example</h3>
<p>Suppose then that we want to find the g.c.d. of 51 and 36. We first appeal to the quotient-remainder theorem:</p>
<p align=center>51 = 36 + 15</p>
<p>Thus any divisor of 51 and 36 (including the greatest) is also a divisor of 15. So we turn our attention to the easier problem of the g.c.d. of 36 and 15. Again by quotient-remainder</p>
<p align=center>36 = 2 * 15 + 6</p>
<p>so it suffices to find gcd(15,6). Noting that</p>
<p align=center>15 = 2 * 6 + 3</p>
<p>we discover that this is the same as the g.c.d. of 6 and 3. But</p>
<p align=center>6 = 2 * 3 + 0</p>
<p>so thatâ€™s simply gcd(3,0)=3. Thus we have</p>
<p align=center>gcd(51, 36) = gcd(36, 15) = gcd(15, 6) = gcd(6, 3) = gcd(3, 0) = 3</p>
<h2>The Extended Euclidean Algorithm</h2>
<p>Typically, one tabulates their progress through the algorithm more compactly; using the previous example we have</p>
<blockquote><p>
51 = 36 + 15 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(4)<br />
<br />36 = 2 * 15 + 6 &nbsp;&nbsp;(5)<br />
<br />15 = 2 * 6 + 3 &nbsp;&nbsp;&nbsp;&nbsp;(6)<br />
<br />6&nbsp;&nbsp;&nbsp;= 2 * 3 + 0 &nbsp;&nbsp;&nbsp;&nbsp;(7)
</p></blockquote>
<p>With such a representation, we can now tackle a related problem &#8211; given a, b, finding x, y such that</p>
<p align=center>ax + by = gcd(a, b)</p>
<p>For our example, this means finding x, y with the property that 51x+36y = 3.</p>
<p>Notice that we do not have an identity in terms of 3, 36 and 51 anywhere in the table. But from (6), we do have an identity for 3 in terms of 15 and 6:</p>
<p align=center>3 = 15 &#8211; 2 * 6</p>
<p>Then (5) tells us that</p>
<p align=center>6 = 36 &#8211; 2 * 15</p>
<p>so we can rewrite our result for 3 in terms of 36 and 15 instead of 15 and 6, and so on.</p>
<p>In this way, we can chain upwards through the table to an identity for 3 ultimately in terms of 51 and 36- this process is known as the extended euclidean algorithm. Whereas the Euclidean algorithm works down from a and b, through simpler and simpler terms to the g.c.d., so the extended version works up from that g.c.d. through increasingly complicated terms to an expression in terms of a and b.</p>
<p>For our example, this works as follows:</p>
<blockquote><p>
3 = 15 &#8211; 2 * 6 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by (6)<br />
<br />&nbsp;&nbsp;&nbsp;= 15 &#8211; 2 * (36 &#8211; 2 * 15) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by (5)<br />
<br />&nbsp;&nbsp;&nbsp;= 5 * 15 &#8211; 2 * 36 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(simplifying)<br />
<br />&nbsp;&nbsp;&nbsp;= 5 * (51 &#8211; 36) &#8211; 2 * 36 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by (4)<br />
<br />&nbsp;&nbsp;&nbsp;= 5 * 51 &#8211; 7 * 36 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(simplifying)
</p></blockquote>
<h3>Linear Diophantine Equations</h3>
<p>Why is all this useful? In number theory, the study of Diophantine equations concerns finding integer solutions to equations, where possible. Typically we seek to classify such equations based on whether we can find infinitely many such solutions, only finitely many, or none at all. The general question as to whether solutions exist is actually undecidable, but in various special cases we can say something.</p>
<p>Armed with the extended euclidean algorithm, we can tackle the case of linear diophantine equations in two variables:</p>
<p align=center>For fixed integers a, b, c, do there exist integer solutions x, y of ax + by = c ?</p>
<h4>Existence</h4>
<p>Suppose c = &lambda;gcd(a, b) for some integer &lambda; . Then, by the extended Euclidean algorithm we have x&#8217;, y&#8217; with the property axâ€™+byâ€™=gcd(a, b). So x = &lambda;x&#8217;, y = &lambda;y&#8217; solve the linear diophantine equation, since</p>
<p align=center>ax + by = a&lambda;x&#8217; + b&lambda;y&#8217; = &lambda;(ax&#8217; + by&#8217;) = &lambda;gcd(a, b) = c</p>
<p>Conversely, suppose c is not a multiple of gcd(a, b). Then, if the equation had solutions x, y then weâ€™d have that gcd(a, b)&#124;a&#124;ax and gcd(a, b)&#124;b&#124;by so gcd(a, b)&#124;ax + by = c, which is a contradiction.</p>
<p>Hence, the linear diophantine equation in two variables ax + by = c has integer solutions if and only if c is a multiple of gcd(a, b).</p>
<h4>Number of Solutions</h4>
<p>Assuming solutions exist, how many are there? Although the g.c.d. is unique, suitable pairs (x, y) with ax+by = gcd(a, b) (and hence solutions to the linear diophantine equation) are not. To see this, consider one of the standard tricks of analysis, adding and subtracting the same thing:</p>
<p align=center>ax+by = &lambda;gcd(a, b) ) ax+by+ab-ab = &lambda;gcd(a, b) ) a(x+b)+b(y-a) = &lambda;gcd(a, b)</p>
<p>That is, if (x, y) satisfy the equation, so do (x + b, y &#8211; a). Which means we can apply the result again, giving (x+2b, y-2a). These will always be pairs of integer solutions, and so the existence of just one pair (x, y) gives us an infinite family (x + ib, y &#8211; ia) of solutions.</p>
<p>Hence we conclude that the linear diophantine equation in two variables ax+by = c has infinitely many integer solutions if c is a multiple of gcd(a, b), and none otherwise.</p>
]]></content:encoded>
			<wfw:commentRss>http://maths.straylight.co.uk/archives/112/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Addition Chains</title>
		<link>http://maths.straylight.co.uk/archives/106</link>
		<comments>http://maths.straylight.co.uk/archives/106#comments</comments>
		<pubDate>Thu, 29 Nov 2007 12:04:12 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Complexity]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[PhD]]></category>

		<guid isPermaLink="false">http://maths.straylight.co.uk/?p=106</guid>
		<description><![CDATA[Fast addition (or exponentiation) via addition chains.]]></description>
			<content:encoded><![CDATA[<p>Suppose you have a rule for addition of some objects (most generally, elements of a <em>semigroup</em>), and you wish to compute the sum of <em>n</em> copies of the same object. How could you achieve this?</p>
<p>The primary school answer is to do just that: given <em>g</em>, compute <em>2g=g+g</em>; then compute <em>3g=2g+g</em> and so on, for <em>n</em> steps. But this rapidly becomes tedious, and isn&#8217;t (I hope) how we&#8217;d perform such a calculation in our heads: instead, we&#8217;d try to take bigger jumps. For instance, to compute the number <em>16g</em>, we can find <em>2g</em>, add that to itself to reach <em>4g</em>, and again for <em>8g</em>, hitting <em>16g</em> after just 4 steps. Had we wanted 17, we&#8217;d then just add <em>g</em> again. </p>
<p>The sequence of intermediate values correspond to an <em>addition chain</em> for <em>n</em>: this is a sequence starting at <em>a<sub>0</sub>=1</em> and ending at <em>a<sub>l</sub>=n</em> with the property that for any term <em>a<sub>k</sub></em> there exist terms <em>a<sub>i</sub></em>,<em>a<sub>j</sub></em> with <em>a<sub>k</sub>=a<sub>i</sub>+a<sub>j</sub></em>. That is, every term is the sum of two earlier terms (not necessarily distinct), so we can reach <em>ng</em> by computing each <em>a<sub>k</sub>g</em> in turn without needing anything fancier than our addition law.</p>
<p>As a side effect, an addition chain also tells us how to exponentiate if we know how to multiply, since <em>x<sup>a+b</sup>=x<sup>a</sup>x<sup>b</sup></em> so we can build up to <em>x<sup>n</sup></em> by finding the powers <em>x<sup>a<sub>k</sub></sup></em> in turn.</p>
<p>Thus our tedious sequence for 16 is 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, with the more sophisticated one being 1,2,4,8,16. If I asked you to compute 16x for a number x, you&#8217;d probably do something different: compute 10x (easy in base 10!) and 6x, then add those. With access to a multiplication procedure, this is definitely more efficient: but without that luxury it can still be used to give an addition chain for 16, by finding chains for 10 and 6. These are 1,2,4,5,10 and 1,2,3,6 respectively, so we can merge them into the chain 1,2,3,4,5,6,10,16.</p>
<p>We describe the length of the chain as <em>l</em>: that is, there are <em>l</em> terms after the 1. Notice that at worst, therefore, we need <em>n-1</em> terms. If we move as quickly as possible by taking <em>a<sub>k+1</sub>=a<sub>k</sub>+a<sub>k</sub>=2a<sub>k</sub></em> then the greatest value we can reach after <em>m</em> terms is 2<sup>m</sup>, as demonstrated with the chain for 2<sup>4</sup>=16. However, if we can compute <em>n</em> and <em>m</em> in l<sub>n</sub>,l<sub>m</sub> terms, it needn&#8217;t take as many as <em>l<sub>n</sub>+l<sub>m</sub>+1</em> terms to compute <em>n+m</em>, since there may be common terms in the chains. Finally, as defined an addition chain needn&#8217;t make use of all it&#8217;s terms: for example, 1,2,4,6,8,16 is still a chain for 16, although the 6 isn&#8217;t needed. We will obviously be interested in chains without any such wasted terms.</p>
<p>The observation for powers of two motivates a first attempt at an efficient algorithm for building addition chains. if <em>n</em> is 1, then we have nothing to do: the chain 1 suffices. Otherwise, we can work recursively: for an even number <em>n=2k</em>, we ask for the chain for <em>k</em> and then add <em>2k</em> to the end; whereas for an odd number <em>n=2k+1</em>, we ask for the chain for <em>k</em> and add <em>2k</em><em> then </em><em>2k+1</em> to the end. This will require at most log<sub>2</sub>n calls, and each step adds 1 or 2 entries to the chain. </p>
<p>This can also be captured by considering the binary representation of <em>n</em>, which will also indicate whether we hit an even or odd number at each stage. Suppose <em>n</em> is of the form
<p align=center><img src="http://straylight.co.uk/cgi-bin/mimetex.cgi?n=\sum_{i=0}^k d_i 2^i=(d_kd_{k-1} d_{k-2}\ldots d_1d_0)_2"/></p>
<p> where each <em>d<sub>i</sub></em> is either 0 or 1, and <em>d<sub>k</sub>=1</em> so there are no leading zeros. Then we start the chain with 1, and read from left to right, ignoring the leading 1: if we see a 0, then we double the last term of the chain; if we see a 1, add 1 to this new term as well. More formally:</p>
<p>Input: the binary expansion <img src="http://straylight.co.uk/cgi-bin/mimetex.cgi?(d_kd_{k-1} d_{k-2}\ldots d_1d_0)_2"/> of n.<br />
<br />Output: an addition chain for n.</p>
<p>Set a<sub>0</sub>=1<br />
Set t=1<br />
for i from k-1 down to 0:<br />
&nbsp;&nbsp;&nbsp;&nbsp;Set a<sub>t</sub>=2a<sub>t-1</sub> and t=t+1.<br />
&nbsp;&nbsp;&nbsp;&nbsp;If d_i=1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set a<sub>t</sub>=a<sub>t-1</sub>+1 and t=t+1<br />
return a<sub>0</sub>&#8230;a<sub>t</sub></p>
<p>For instance, with <em>n</em> a power of 2 we will see only a run of zeros, and thus perform the appropriate number of doublings, for a chain of length <em>log<sub>2</sub>n</em>. For <em>n</em> one less than a power of two, the expansion consists entirely of 1s, costing <em>2log<sub>2</sub>n</em>. For an &#8216;average&#8217; number where each digit has around a 50% chance of being a 1 or a 0, the chain will therefore be of approximate length <em>(3/2)log<sub>2</sub>n</em>. More formally, we can define the <em>Hamming Weight v(n)</em> to be the number of 1s in the binary expansion of <em>n</em>; then the chain will have length <em>floor(log<sub>2</sub>n) + v(n) -1</em>. </p>
<p>Is this optimal? A counterexample suffices to show it is not, the first being 15, which is 1111 in binary. Using the binary expansion we get the sequence 1,2,3,6,7,14,15 of length 6 (=3+4-1 as claimed). But the sequence would be 1,2,3,5,10,15 of length 5 and hence 1 shorter.</p>
<p>Nonetheless, our binary chain gives a much better upper bound than the naive chain of <em>(n-1)</em> steps, and is very simple to implement both in logic and memory. The idea also generalises to arbitrary bases, as follows.</p>
<p>Suppose <em>n</em> is of the form
<p align=center><img src="http://straylight.co.uk/cgi-bin/mimetex.cgi?n=\sum_{i=0}^k d_i m^i=(d_kd_{k-1} d_{k-2}\ldots d_1d_0)_m"/></p>
<p> where each d<sub>i</sub> is in the range 0&#8230;m-1, d<sub>k</sub> non-zero. Then we can compute an <em>m-ary addition chain</em> for n:</p>
<p>Input: the m-ary expansion <img src="http://straylight.co.uk/cgi-bin/mimetex.cgi?(d_kd_{k-1} d_{k-2}\ldots d_1d_0)_m"/> of n.<br />
<br />Output: an addition chain for n.</p>
<p>Set a=0<br />
Start the chain with 1,2,3,&#8230;,m-1.<br />
for i from k down to 0:<br />
&nbsp;&nbsp;&nbsp;&nbsp;Extend the chain to m*a (if not already present) and set a=m*a.<br />
&nbsp;&nbsp;&nbsp;&nbsp;Add a+d<sub>i</sub> to the chain (if not already present), and set a=a+d<sub>i</sub>.<br />
return chain.</p>
<p>Notice that <em>a</em> tracks the last term computed, and that extending the chain to <em>m*a</em> is itself an addition chain problem: this makes some 2<sup>k</sup> a good choice for <em>m</em>, since this extension can then be accomplished in <em>k</em> additions. </p>
<p>The shorter chain for 15 arises from its ternary expansion: considered as (120)<sub>3</sub> 1*9+2*3+0*1, we have an initial chain of 1,2 then apply the loop. With a=0,d<sub>2</sub>=1 we set a=3a=0 then a=a+1=1; neither of these need to be added to the chain; for a=1, d<sub>1</sub>=2 we set a=3a=3 which requires the addition of 3 to the chain; then a=a+2=5 which extends the chain to 1,2,3,5; finally for a=5,d<sub>2</sub>=0 we set a=3a=15, adding first 10 then 15 to the chain; then a=a+0=15 so we terminate with chain 1,2,3,5,10,15.</p>
<p>The optimal choice of <em>m</em> depends on <em>n</em>, since although there will be less steps (<em>log<sub>m</sub>n</em>) for greater <em>m</em>, each step will require more additions in the computation of <em>m*a</em>. Nor are m-ary expansions the end of the story- there are various other tricks available to finesse addition chains. However, there is no known algorithm for finding shortest addition chains- and a generalisation of the problem to finding shortest addition sequences (chains that contain a desired list of values n<sub>1</sub>,n<sub>2</sub>,&#8230;,n<sub>k</sub>) is NP-complete. Searching for short chains is therefore an interesting challenge, and is constrained by the fact that unless you are precomputing them for future use, any shorter chain must offer a speedup in computation greater than the time taken to find it.</p>
<p>There are, however, bounds for the length of the chain: </p>
<p align=center><img src="http://straylight.co.uk/cgi-bin/mimetex.cgi?log_2 n +log_2 v(n) \leq l(n) \leq log_2 n + v(n) -1"/></p>
<p>Where <em>v(n)</em> is the Hamming weight as before.</p>
<p><strong>References/Further Reading</strong><br />
<br />Knuth- <em>The Art of Computer Programming Vol. 2: Seminumerical Algorithms</em>.<br />
<br />Bos and Coster- <em>Addition Chain Heuristics</em> in Advances in Cryptology- Crypto&#8217;89 (Lecture Notes in Computer Science) (Available via <a href="http://www.springerlink.com/content/hc5a5p31deeypu94/">SpringerLink</a>.)<br />
<br />Gordon- <em>A Survey of Fast Exponentiation Methods.</em> (Available via <a href="http://citeseer.ist.psu.edu/gordon97survey.html">citeseer</a>.)</p>
]]></content:encoded>
			<wfw:commentRss>http://maths.straylight.co.uk/archives/106/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Baby Steps, Giant Steps, and element orders</title>
		<link>http://maths.straylight.co.uk/archives/73</link>
		<comments>http://maths.straylight.co.uk/archives/73#comments</comments>
		<pubDate>Mon, 02 Apr 2007 16:53:30 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
				<category><![CDATA[Algebraic Geometry]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Cryptology]]></category>
		<category><![CDATA[Group Theory]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[PhD]]></category>

		<guid isPermaLink="false">http://maths.straylight.co.uk/?p=73</guid>
		<description><![CDATA[Establishing the order of an element by BSGS.]]></description>
			<content:encoded><![CDATA[<p>The discrete logarithm problem is the vital part of elliptic curve cryptography, but can be defined (to varying cryptographic strength) for any cyclic group:  </p>
<blockquote><p><strong>Discrete Logarithm Problem (DLP)</strong><br />
Let G be a cyclic group, with operation &oplus;.  Let [n] represent (n-1)-fold application of &oplus; i.e., [2]g=g&oplus;g, [3]g=g&oplus;g&oplus;g etc.<br />
<br />
Given g,h from G, the discrete logarithm problem is to find t such that [t]g=h.</p>
</blockquote>
<p>The baby-step, giant step (BSGS) algorithm is a generic algorithm for solving the DLP- that is to say, it makes no appeal to properties of the group involved, merely calculating abstractly with &oplus;.  There is a theoretical upper bound to the effectiveness of generic algorithms, and BSGS approaches are of that order of magnitude. </p>
<p>The simplest demonstration of BSGS (the original by Shanks) assumes that g generates G, with both of known order n: recall that the group order is simply the number of elements it contains, whereas the order of an element g is the least n such that [n]g=id<sub>G</sub>, should such a value exist. The order n of an element always divides the order of the group N, with equality when g generates G.  Between the two there is also the exponent, the least value e such that [e]g=id<sub>G</sub> for all g in G; for cyclic groups this is N, but for products of cyclic groups (the structure of groups of rational points) it is the lowest common multiple of their respective orders, and thus, although a divisor of N, may be significantly smaller than it.</p>
<p>So, if we can find an order m rational point on a curve, we know that the cardinality of the group of rational points is zero modulo m. By testing random points and taking the lowest common multiple of their order we can usually find the exponent e of the group. With luck, but not always, this will be large enough that when combined with bounds on the cardinality the latter is established exactly (as with modular information in the SEA algorithm). </p>
<p>But it is no good attempting to do so using an algorithm that requires the order of the point! There are BSGS algorithms for the DLP which can handle unknown order, for a given g we can apply these to solve for h=id<sub>G</sub> in the cyclic group generated by g. Provided the algorithm gives the minimal t such that [t]g=h=id<sub>G</sub> in G=&lt;g&gt;, then t is the order of g. </p>
<p>One such algorithm, by Terr, is given in Cohen and Frey&#8217;s <i>Handbook of Elliptic and Hyperelliptic curve cryptography</i> (my current bible!) It relies on the following observation:</p>
<blockquote><p>
<p>Let T<sub>n</sub> be the <em>n</em>th triangular number: that is, defined by the recursion T<sub>1</sub>=0, T<sub>n+1</sub>=T<sub>n</sub>+n.<br />
<br />Then any non-negative integer t, there are unique integers j and k with t=T<sub>j+1</sub>-k with 0&le;k&lt;j.</p>
<p>
<em>To see how, notice that there is a unique j such that t lies in the interval (T<sub>j</sub> , &#8230; , T<sub>j+1</sub>]=(T<sub>j+1</sub>-j , &#8230; , T<sub>j+1</sub>-0] and that this interval has width j so there is an appropriate choice of k. </em>
</p></blockquote>
<p>We then suppose that t satisfies [t]g=h. Then we have [T<sub>j+1</sub>]g=h&oplus;[k]g. So, instead of testing each [t] in turn until we hit equality, for a given j we need only test the &#8216;giant step&#8217; [T<sub>j+1</sub>]g against the set of &#8216;baby steps&#8217; &beta;={&beta;<sub>i</sub>}={h&oplus;[0]g,&#8230;,h&oplus;[i]g,&#8230;,h&oplus;[j-1]g. If that j yields no matches, we move to j&#8217;=j+1: by keeping track of [j]g at each iteration, the new T<sub>j&#8217;+1</sub> and the extra h&oplus;[j'-1]g are found by a single group operation each. Hence this is attractive from both storage and time complexity perspectives: we need only record the baby steps &beta;, [j]g and a single giant step at any given iteration j, whilst the time complexity is of order t<sup>1/2</sup>.</p>
<blockquote><p><strong>Terr&#8217;s BSGS variant for the DLP</strong><br /><em>Finds t such that [t]g=h for g generating G of unknown order, h from G.</em><br />
<br />Initialise &beta;={&beta;<sub>0</sub>=h}, &gamma;=&delta;=id<sub>G</sub>, j=0. (<em>(For each iteration, we have &gamma;=[T<sub>j+1</sub>]g and &delta;=[j]g</em>)<br />
Then loop over j as follows:</p>
<ul>
<li>Increment j by 1, then update:
</li>
<li>Set &delta;=&delta;&oplus;g=[j-1]g&oplus;g=[j]g.</li>
<li>Set &gamma;=&gamma;&oplus;&delta;=[T<sub>j</sub>]g+[j]g=[T<sub>j+1</sub>]g.</li>
<li>If j&ge;2 then
<ul>
<li>For s from 0 to j, if &gamma;=&beta;<sub>s</sub> then return T<sub>j+1</sub>-s</li>
</ul>
</li>
<li>Add &beta;<sub>j</sub>=&beta;<sub>j-1</sub>&oplus;g to &beta;</li>
</ul>
</blockquote>
<p>I&#8217;ve implemented this generic DLP algorithm and an order-finding version (which requires you to catch g=id<sub>G</sub>) as Maple procedures for arbitrary groups- details on that will be in the next post! BSGS-based approaches become impractical at finite field sizes well within the grasp of SEA for counting on elliptic curves; but are of interest in the higher genus case which lacks an Elkies procedure.</p>
]]></content:encoded>
			<wfw:commentRss>http://maths.straylight.co.uk/archives/73/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The SEA algorithm</title>
		<link>http://maths.straylight.co.uk/archives/72</link>
		<comments>http://maths.straylight.co.uk/archives/72#comments</comments>
		<pubDate>Wed, 14 Mar 2007 12:32:38 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
				<category><![CDATA[Algebraic Geometry]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Maple]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[PhD]]></category>

		<guid isPermaLink="false">http://maths.straylight.co.uk/archives/72</guid>
		<description><![CDATA[Overview of the SEA algorithm for computing cardinality of elliptic curves over finite fields.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the last few weeks getting my head around the SEA algorithm- a series of techniques for computing cardinality of elliptic curves over finite fields. The basic Schoof algorithm (the S of SEA) is based on the Weil conjectures, from which to find the cardinality it suffices to determine the trace of Frobenius, t. This can be shown to fall within certain bounds and thus (by the chinese remainder theorem) the problem can be solved by establishing t modulo l for a series of smaller primes l. The contributions by Elkies and Atkin make these subcalculations feasible, but trade computational complexity for mathematical difficulty, and it&#8217;s these I&#8217;ve been grappling with recently- particularly the Elkies approach.</p>
<p>Fortunately there is an excellent book &#8211; Blake, Seroussi and Smart&#8217;s <i><a href="http://www.amazon.co.uk/Elliptic-Cryptography-Mathematical-Society-Lecture/dp/0521653746/ref=sr_1_2/026-7458293-4697236?ie=UTF8&#038;s=books&#038;qid=1173874831&#038;sr=8-2">Elliptic Curves in Cryptography</a></i> which covers this topic thoroughly. As usual, attempting to implement the procedures myself has uncovered (and repaired) many gaps in my comprehension of the algorithms involved. I&#8217;m finally getting correct answers, albeit for small examples, so I&#8217;ve written up some summary notes on the book and a worked example in Maple. It still requires a high degree of interaction to get to the final result; my understanding is that SAGE (on linux) implements the SEA algorithm, so it doesn&#8217;t seem worth trying to tie it all together into a standalone Maple procedure- this would be a programming challenge, but wouldn&#8217;t add anything to my mathematical understanding!</p>
<p>Notes: <a href="http://www.maths.ed.ac.uk/~s0677951/SEA.dvi">[SEA.dvi]</a><br />
Appendix (Maple procedures): [<a href="http://www.maths.ed.ac.uk/~s0677951/SEA_tools.mpl">SEA_tools.mpl]</a><br />
Example (Maple worksheet): <a href="http://www.maths.ed.ac.uk/~s0677951/SEA.mw">[SEA.mw]</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maths.straylight.co.uk/archives/72/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

