The AKS primality test on the word RAM #
The AKS algorithm Agrawal, Kayal, Saxena, Section 4 decides whether n is prime. This
program runs it on the word RAM, with the input n in memory cell 1 and the integer thresholds
c = ⌈log₂ n⌉ and L = c² (Cslib.AKS.PassesUpTo):
- if
n < 2, answer0; - if
nis a perfect power, answer0; - find the least
r ≥ 2withord_r(n) > L; - if
1 < gcd(a, n) < nfor somea ≤ r, answer0; - if
n ≤ r, answer1; - answer
1exactly when(X + a)^n ≡ X^n + a (mod X^r - 1, n)for every1 ≤ a ≤ ⌊√(φ(r) L)⌋.
Each step is a subroutine run on the low registers. The search in step 2 stops by
Cslib.AKS.exists_lt_orderOf, at some r ≤ 4 c⁵, and the answer is correct by Theorem 4.1 of the
paper with integer thresholds (Cslib.AKS.prime_iff_passesUpTo_numTestsNat).
The word size must hold the numbers the steps compute; for an input of s bits, the width
8 (s + 1) suffices. The program then answers within time s ≤ 4096 (s + 1)¹⁵ instructions
(Cslib.Algorithms.WordRAM.AKS.time_le). The dominant term is step 5: at most 2 s⁴ values of
a, each tested by s rounds of schoolbook multiplication of polynomials with r ≤ 4 s⁵
coefficients.
Theorem 5.1 of the paper bounds the bit complexity of the algorithm by Õ(log^{21/2} n), using
fast multiplication of integers and polynomials.
Main definitions #
Cslib.Algorithms.WordRAM.AKS.program: the AKS algorithm.Cslib.Algorithms.WordRAM.AKS.time: its instruction bound on inputs ofsbits.
Main results #
Cslib.Algorithms.WordRAM.AKS.answers: at a word size of at least8 (size n + 1), the program answers whethernis prime withintime (size n)instructions.Cslib.Algorithms.WordRAM.AKS.decidesInTime: its compiled program decides the primes.Cslib.Algorithms.WordRAM.AKS.time_le,Cslib.Algorithms.WordRAM.AKS.polynomiallyBounded_time: the instruction bound is at most4096 (s + 1)¹⁵, so it is polynomial.
References #
Registers #
Constant one, which is also the address of the input.
The bound L = ⌈log₂ n⌉² on the order.
The input of the perfect-power test.
The answer of the perfect-power test.
The argument of the binary logarithm.
The number n of the search for r.
The bound L of the search for r.
The result r of the search.
The totient computed in step 3.
The argument of the square root.
The result block of step 5.
The scratch block of step 5.
The number r of coefficients in step 5.
The last tested value A of step 5.
The program #
Step 1, the perfect-power test.
The binary logarithm ⌈log₂ n⌉.
Step 2, the search for r.
Step 3, and the totient φ(r).
The integer square root.
Step 5, the congruences.
Steps 4 and 5: answer 1 if n ≤ r, and otherwise test the congruence for
1 ≤ a ≤ ⌊√(φ(r) L)⌋, with the blocks at 0, r and 2 r.
Steps 2 to 5, for an n ≥ 2 that is not a perfect power.
The AKS algorithm. Read n; answer 0 if n < 2 or n is a perfect power, and
otherwise run steps 2 to 5. The answer is in ans.
The instruction bound of program on an input of s bits: the perfect-power test, the
logarithm, step 2 with L ≤ s² and r ≤ 4 s⁵, step 3, the square root, and step 5 with at most
2 s⁴ tests.
The instruction bound is polynomial in the number of bits.
Word sizes #
Correctness #
The AKS algorithm decides the primes. Its compiled program, given n as one word at a word
size of at least 8 (size n + 1), answers whether n is prime within time (size n) + 1
instructions.