Documentation

CslibFrontier.Algorithms.WordRAM.AKS

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):

  1. if n < 2, answer 0;
  2. if n is a perfect power, answer 0;
  3. find the least r ≥ 2 with ord_r(n) > L;
  4. if 1 < gcd(a, n) < n for some a ≤ r, answer 0;
  5. if n ≤ r, answer 1;
  6. answer 1 exactly when (X + a)^n ≡ X^n + a (mod X^r - 1, n) for every 1 ≤ 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 #

Main results #

References #

Registers #

def Cslib.Algorithms.WordRAM.AKS.low {k : ℕ} (hk : k ≤ 29) :
Fin k → Fin 36

Subroutines run on the low registers 0, …, k - 1, below the registers of the top level.

@[reducible, inline]

The input n.

@[reducible, inline]

Constant one, which is also the address of the input.

@[reducible, inline]

Constant two.

@[reducible, inline]

The bound L = ⌈log₂ n⌉² on the order.

@[reducible, inline]

The modulus r of step 2.

@[reducible, inline]

Euler's totient φ(r).

@[reducible, inline]

The answer.

@[reducible, inline]

The input of the perfect-power test.

@[reducible, inline]

The answer of the perfect-power test.

@[reducible, inline]

The argument of the binary logarithm.

@[reducible, inline]

The binary logarithm.

@[reducible, inline]

The number n of the search for r.

@[reducible, inline]

The bound L of the search for r.

@[reducible, inline]

The result r of the search.

@[reducible, inline]

The number n of step 3.

@[reducible, inline]

The modulus r of step 3.

@[reducible, inline]

The answer of step 3.

@[reducible, inline]

The totient computed in step 3.

@[reducible, inline]

The argument of the square root.

@[reducible, inline]

The square root.

@[reducible, inline]

The result block of step 5.

@[reducible, inline]

The base block of step 5.

@[reducible, inline]

The scratch block of step 5.

@[reducible, inline]

The number r of coefficients in step 5.

@[reducible, inline]

The modulus n of step 5.

@[reducible, inline]

The last tested value A of step 5.

@[reducible, inline]

The answer of step 5.

The program #

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.

theorem Cslib.Algorithms.WordRAM.AKS.time_le (s : ℕ) :
time s ≤ 4096 * (s + 1) ^ 15

The instruction bound is a polynomial of degree 15: time s ≤ 4096 (s + 1)¹⁵.

The instruction bound is polynomial in the number of bits.

Word sizes #

Correctness #

theorem Cslib.Algorithms.WordRAM.AKS.answers {w : ℕ} (N : ℕ) (hw : 8 * (N.size + 1) ≤ w) :

The AKS algorithm is correct. At a word size of at least 8 (size n + 1), the program leaves 1 in ans if n is prime and 0 otherwise, within time (size n) instructions.

theorem Cslib.Algorithms.WordRAM.AKS.decidesInTime :
(program.program ans).DecidesInTime (fun (x n : ℕ) => [n]) {n : ℕ | Nat.Prime n} (fun (n x : ℕ) => time n.size + 1) fun (n w : ℕ) => 8 * (n.size + 1) ≤ w

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.