AKS primality test

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The AKS primality test (also known as Agrawal–Kayal–Saxena primality test and cyclotomic AKS test) is a deterministic primality-proving algorithm created and published by Manindra Agrawal, Neeraj Kayal, and Nitin Saxena, computer scientists at the Indian Institute of Technology Kanpur, on August 6, 2002, in a paper titled "PRIMES is in P".[1] The algorithm determines whether a number is prime or composite within polynomial time. The authors received the 2006 Gödel Prize and the 2006 Fulkerson Prize for this work.


AKS is the first primality-proving algorithm to be simultaneously general, polynomial, deterministic, and unconditional. Previous algorithms had been developed for centuries and achieved three of these properties at most, but not all four.

  • The AKS algorithm can be used to verify the primality of any general number given. Many fast primality tests are known that work only for numbers with certain properties. For example, the Lucas–Lehmer test works only for Mersenne numbers, while Pépin's test can be applied to Fermat numbers only.
  • The maximum running time of the algorithm can be expressed as a polynomial over the number of digits in the target number. ECPP and APR conclusively prove or disprove that a given number is prime, but are not known to have polynomial time bounds for all inputs.
  • The algorithm is guaranteed to distinguish deterministically whether the target number is prime or composite. Randomized tests, such as Miller–Rabin and Baillie–PSW, can test any given number for primality in polynomial time, but are known to produce only a probabilistic result.
  • The correctness of AKS is not conditional on any subsidiary unproven hypothesis. In contrast, Miller's version of the Miller-Rabin test is fully deterministic and runs in polynomial time over all inputs, but its correctness depends on the truth of the yet-unproven generalized Riemann hypothesis.

While the algorithm is of immense theoretical importance, it is not used in practice. For 64-bit inputs, the Baillie–PSW is deterministic and runs many orders of magnitude faster. For larger inputs, the performance of the (also unconditionally correct) ECPP and APR tests is far superior to AKS. Additionally, ECPP can output a primality certificate that allows independent and rapid verification of the results, which is not possible with the AKS algorithm.


The AKS primality test is based upon the following theorem: An integer n (≥ 2) is prime if and only if the polynomial congruence relation






holds for some a coprime to n.[1] Note that x is a free variable.

This theorem is a generalization to polynomials of Fermat's little theorem, and can easily be proven using the binomial theorem together with the following property of the binomial coefficient:

for all if and only if n is prime.

While the relation (1) constitutes a primality test in itself, verifying it takes exponential time: the brute force approach would require the expansion of the (x - a)n polynomial and a reduction (mod n) of the resulting n + 1 coefficients.

The congruence is an equality in the polynomial ringn[x]. Evaluating in a quotient ring of ℤn[x] creates an upper bound for the degree of the polynomials involved. The AKS evaluates the equality in ℤn[x]/(xr - 1), making the computational complexity dependent on the size of r. For clarity,[1] this is expressed as the congruence






which is the same as:






for some polynomials f and g.

Note that all primes satisfy this relation (choosing g = 0 in (3) gives (1), which holds for n prime). This congruence can be checked in polynomial time when r is polynomial to the digits of n. The AKS algorithm evaluates this congruence for a large set of a values, whose size is polynomial to the digits of n. The proof of validity of the AKS algorithm shows that one can find an r and a set of a values with the above properties such that if the congruences hold then n is a power of a prime.[1]

History and running time[edit]

In the first version of the above-cited paper, the authors proved the asymptotic time complexity of the algorithm to be (using Õ from big O notation). In other words, the algorithm takes less time than the twelfth power of the number of digits in n times a polylogarithmic (in the number of digits) factor. However, the upper bound proved in the paper was rather loose; indeed, a widely held conjecture about the distribution of the Sophie Germain primes would, if true, immediately cut the worst case down to .

In the months following the discovery, new variants appeared (Lenstra 2002, Pomerance 2002, Berrizbeitia 2003, Cheng 2003, Bernstein 2003a/b, Lenstra and Pomerance 2003), which improved the speed of computation by orders of magnitude. Due to the existence of the many variants, Crandall and Papadopoulos refer to the "AKS-class" of algorithms in their scientific paper "On the implementation of AKS-class primality tests", published in March 2003.

In response to some of these variants, and to other feedback, the paper "PRIMES is in P" was updated with a new formulation of the AKS algorithm and of its proof of correctness. (This version was eventually published in Annals of Mathematics.) While the basic idea remained the same, r was chosen in a new manner, and the proof of correctness was more coherently organized. While the previous proof had relied on many different methods, the new version relied almost exclusively on the behavior of cyclotomic polynomials over finite fields. The new version also allowed for an improved bound on the time complexity, which can now be shown by simple methods to be . Using additional results from sieve theory, this can be further reduced to .

In 2005, Carl Pomerance and H. W. Lenstra, Jr. demonstrated a variant of AKS that runs in operations, where n is the number to be tested – a marked improvement over the initial bound in the original algorithm.[2] An updated version of the paper is also available.[3]

Agrawal, Kayal and Saxena suggest a variant of their algorithm which would run in if Agrawal's conjecture is true; however, a heuristic argument by Hendrik Lenstra and Carl Pomerance suggests that it is probably false.[1]

The algorithm[edit]

The algorithm is as follows:[1]

Input: integer n > 1.
  1. Check if n is a perfect power: if n = ab for integers a > 1 and b > 1, output composite.
  2. Find the smallest r such that ordr(n) > (log2 n)2. (if r and n are not coprime, then skip this r)
  3. For all 2 ≤ a ≤ min(r, n−1), check that a does not divide n: If a|n for some 2 ≤ a ≤ min(r, n−1), output composite.
  4. If nr, output prime.
  5. For a = 2 to do
    if (X+a)nXn+a (mod Xr − 1,n), output composite;
  6. Output prime.

Here ordr(n) is the multiplicative order of n modulo r, log2 is the binary logarithm, and is Euler's totient function of r.

Step 3 is shown in the paper as checking 1 < (a,n) < n for all ar. It can be seen this is equivalent to trial division up to r, which can be done very efficiently without using gcd. Similarly the comparison in step 4 can be replaced by having the trial division return prime once it has checked all values up to and including .

Once beyond very small inputs, step 5 dominates the time taken. Most improvements made to the algorithm have concentrated on reducing the size of r which makes the core operation in step 5 faster, and in reducing the size of s, the number of loops performed in step 5.[4] Typically these changes do not change the computational complexity, but can lead to many orders of magnitude less time taken, e.g. Bernstein's final version has a theoretical speedup by a factor of over 2 million.

Proof of validity outline[edit]

For the algorithm to be correct, all steps that identify n must be correct. Steps 1, 3 and 4 are trivially correct, since they are based on direct tests of the divisibility of n. Step 5 is also correct: since (2) is true for any choice of a coprime to n and r if n is prime, an inequality means that n must be composite.

The difficult case of the algorithm is step 6. Its proof of correctness is based on the upper and lower bounds of a multiplicative group in ℤn[x] constructed from the (X + a) binomials that are tested in step 5. Step 4 guarantees that these binomials are distinct elements of ℤn[x]. For the particular choice of r, the bounds produce a contradiction unless n is prime or a power of a prime. Together with the test of step 1, this implies that n is always prime at step 6.[1]

Example 1: n = 31 is Prime[edit]

Input: integer n = 31 > 1.


   If ''n'' = ''a''<sup>''b''</sup> for integers ''a'' > 1 and ''b'' > 1, output ''composite''.
     For [ b=2, b <= log<sub>2</sub>(n), b++,
       If [ a is integer, Return[Composite]]
     a=n<sup>1/2</sup>...n<sup>1/4</sup>={5.568, 3.141, 2.360}


   Find the smallest ''r'' such that ''O''<sub>''r''</sub>(''n'') > (log<sub>''2''</sub> ''n'')<sup>2</sup>.
     maxk=⌊(log<sub>''2''</sub> n)<sup>2</sup>⌋;
     maxr=Max[3, ⌈(Log<sub>''2''</sub> n)<sup>5</sup>⌉]; (*maxr really isn't needed*)
     For [r=2, nextR && r < maxr, r++,
       For [k=1,(!nextR) &&k ≤ maxk, k++,
         nextR=(Mod[n<sup>k</sup>, r]==1 || Mod[n<sup>k</sup>, r]==0)
     r--; (*the loop over increments by one*)
     r = 29


   If 1 < [[greatest common divisor|gcd]](''a'',''n'') < ''n'' for some ''a'' ≤ ''r'', output ''composite''.
     For [a=r, a > 1, a--,
       If [(gcd=GCD[a,n]) > 1 && gcd < n, Return[Composite]]
     gcd={GCD(29,31)=1, GCD(28,31)=1, ..., GCD(2,31)=1} ≯ 1


   If ''n'' ≤ ''r'', output ''prime''.
     If [n ≤ r, Return[Prime]]; (* this step may be omitted if n > 5690034 *)
     31 > 29


   For ''a'' = 1 to <math>\scriptstyle{\left\lfloor\sqrt{\varphi(r)}\log_2(n)\right\rfloor}</math> do
     if (''X''+''a'')<sup>''n''</sup>≠ ''X''<sup>''n''</sup>+''a'' (mod ''X''<sup>''r''</sup> − 1,''n''), output ''composite'';
     PolyModulo[f_]:=PolynomialMod[ [[polynomial remainder|PolynomialRemainder]][f,x<sup>r</sup>-1,x],n];
     For[a=1, a ≤ max, a++,
       If[PolyModulo[(x+a)<sup>n</sup>]-PolynomialRemainder[x<sup>n</sup>+a, x<sup>r</sup>-1, x]≠0,
     (x+a)<sup>31</sup> =
       a<sup>31</sup> +31a<sup>30</sup>x +465a<sup>29</sup>x<sup>2</sup> +4495a<sup>28</sup>x<sup>3</sup> +31465a<sup>27</sup>x<sup>4</sup> +169911a<sup>26</sup>x<sup>5</sup> +736281a<sup>25</sup>x<sup>6</sup> +2629575a<sup>24</sup>x<sup>7</sup> +7888725a<sup>23</sup>x<sup>8</sup> +20160075a<sup>22</sup>x<sup>9</sup> +44352165a<sup>21</sup>x<sup>10</sup> +84672315a<sup>20</sup>x<sup>11</sup> +141120525a<sup>19</sup>x<sup>12</sup> +206253075a<sup>18</sup>x<sup>13</sup> +265182525a<sup>17</sup>x<sup>14</sup> +300540195a<sup>16</sup>x<sup>15</sup> +300540195a<sup>15</sup>x<sup>16</sup> +265182525a<sup>14</sup>x<sup>17</sup> +206253075a<sup>13</sup>x<sup>18</sup> +141120525a<sup>12</sup>x<sup>19</sup> +84672315a<sup>11</sup>x<sup>20</sup> +44352165a<sup>10</sup>x<sup>21</sup> +20160075a<sup>9</sup>x<sup>22</sup> +7888725a<sup>8</sup>x<sup>23</sup> +2629575a<sup>7</sup>x<sup>24</sup> +736281a<sup>6</sup>x<sup>25</sup> +169911a<sup>5</sup>x<sup>26</sup> +31465a<sup>4</sup>x<sup>27</sup> +4495a<sup>3</sup>x<sup>28</sup> +465a<sup>2</sup>x<sup>29</sup> +31ax<sup>30</sup> +x<sup>31</sup>
     PolynomialRemainder [(x+a)<sup>31</sup>, x<sup>29</sup>-1] =
       465a<sup>2</sup> +a<sup>31</sup> +(31a+31a<sup>30</sup>)x +(1+465a<sup>29</sup>)x<sup>2</sup> +4495a<sup>28</sup>x<sup>3</sup> +31465a<sup>27</sup>x<sup>4</sup> +169911a<sup>26</sup>x<sup>5</sup> +736281a<sup>25</sup>x<sup>6</sup> +2629575a<sup>24</sup>x<sup>7</sup> +7888725a<sup>23</sup>x<sup>8</sup> +20160075a<sup>22</sup>x<sup>9</sup> +44352165a<sup>21</sup>x<sup>10</sup> +84672315a<sup>20</sup>x<sup>11</sup> +141120525a<sup>19</sup>x<sup>12</sup> +206253075a<sup>18</sup>x<sup>13</sup> +265182525a<sup>17</sup>x<sup>14</sup> +300540195a<sup>16</sup>x<sup>15</sup> +300540195a<sup>15</sup>x<sup>16</sup> +265182525a<sup>14</sup>x<sup>17</sup> +206253075a<sup>13</sup>x<sup>18</sup> +141120525a<sup>12</sup>x<sup>19</sup> +84672315a<sup>11</sup>x<sup>20</sup> +44352165a<sup>10</sup>x<sup>21</sup> +20160075a<sup>9</sup>x<sup>22</sup> +7888725a<sup>8</sup>x<sup>23</sup> +2629575a<sup>7</sup>x<sup>24</sup> +736281a<sup>6</sup>x<sup>25</sup> +169911a<sup>5</sup>x<sup>26</sup> +31465a<sup>4</sup>x<sup>27</sup> +4495a<sup>3</sup>x<sup>28</sup>
     ({{EquationRef|A}}) PolynomialMod [PolynomialRemainder [(x+a)<sup>31</sup>, x<sup>29</sup>-1], 31] = a<sup>31</sup>+x<sup>2</sup>
     ({{EquationRef|B}}) PolynomialRemainder [x<sup>31</sup>+a, x<sup>29</sup>-1] = a+x<sup>2</sup>
     ({{EquationNote|A}}) - ({{EquationNote|B}}) = a<sup>31</sup>+x<sup>2</sup> - (a+x<sup>2</sup>) = a<sup>31</sup>-a
     max = <math>\left\lfloor\log_2 (31)\scriptstyle{\sqrt{\varphi(29)}}\right\rfloor</math> = 26
     {1<sup>31</sup>-1=0 (mod 31), 2<sup>31</sup>-2=0 (mod 31), 3<sup>31</sup>-3=0 (mod 31), ..., 26<sup>31</sup>-26=0 (mod 31)}


   Output ''prime''.
     31 Must be Prime

Where PolynomialMod is a term-wise modulo reduction of the polynomial. e.g. PolynomialMod[x+2x2+3x3, 3] = x+2x2+0x3

(See AKS Talk page for a discussion on why 'Example 2: n is not Prime past Step 4' is missing)


  1. ^ a b c d e f g Agrawal, Manindra; Kayal, Neeraj; Saxena, Nitin (2004). "PRIMES is in P" (PDF). Annals of Mathematics. 160 (2): 781–793. doi:10.4007/annals.2004.160.781. JSTOR 3597229. 
  2. ^ H. W. Lenstra Jr. and Carl Pomerance, "Primality testing with Gaussian periods", preliminary version July 20, 2005.
  3. ^ H. W. Lenstra jr. and Carl Pomerance, "Primality testing with Gaussian periods", version of April 12, 2011.
  4. ^ Daniel J. Bernstein, "Proving Primality After Agrawal-Kayal-Saxena", version of January 25, 2003.

Further reading[edit]

External links[edit]