These are the human proofs of AKS primality test, at full length.
The AKS test returns true exactly when n is prime full
This proof shows: correctness, termination.
We use the pseudocode AKS(n) of the page, after Agrawal, Kayal and Saxena (Annals of Mathematics 160, 2004, Section 4). Write lg for the base 2 logarithm, or(m) for the order of m modulo r when gcd(m,r)=1, and φ for Euler's totient. "f≡g(modXr−1,m)" means that f−g lies in the ideal generated by Xr−1 and m in Z[X].
Lemma 1 (termination of line L3). For n≥2 there is an r≥2 with gcd(r,n)=1 and or(n)>(lgn)2, so line L3 ends.
Proof. Let k=⌊(lgn)2⌋ and N=n∏i=1k(ni−1), a positive integer since n≥2. Take a prime r that does not divide N. Then r∤n, so gcd(r,n)=1, and ni≡1(modr) for 1≤i≤k, so or(n)>k, that is, or(n)>(lgn)2 as or(n) is an integer. The search of line L3 tries r=2,3,… in order, so it stops at the least such r. (Lemma 4.3 of the paper shows that r≤max(3,⌈(lgn)5⌉).) □
Lemma 2 (polynomial Fermat for primes). If n is prime, then (X+a)n≡Xn+a(modn) for every integer a.
Proof. By the binomial theorem (X+a)n=∑i=0n(in)an−iXi. For 0<i<n, (in)=i!(n−i)!n! and the prime n divides the numerator but not the denominator, so n∣(in). The two outer terms are Xn and an≡a, by Fermat's little theorem. □
Lemma 3 (primes return true). If n is prime, AKS(n) returns true.
Proof. Line L1 does not fire as n≥2, and line L2 does not fire, as a prime is not ab with a,b≥2. In line L4, gcd(a,n)∈{1,n} for every a, so it does not fire. If n≤r, line L5 returns true. Otherwise, for each a, Lemma 2 gives (X+a)n≡Xn+a(modn), hence also modulo (Xr−1,n), so line L8 does not fire and line L9 returns true. □
From now on, suppose that AKS(n) returns true. If it does so at line L5, then n≤r and line L4 found no a≤r with 1<gcd(a,n)<n; in particular no a with 2≤a≤n−1 divides n, so n is prime. So assume it returns true at line L9. Then n≥2, n is not a perfect power, n>r, gcd(r,n)=1, or(n)>(lgn)2, gcd(a,n)=1 for 1≤a≤r, and, with ℓ=⌊φ(r)lgn⌋,
(X+a)n≡Xn+a(modXr−1,n)(0≤a≤ℓ),
where a=0 holds trivially. We show that n is prime.
Setup. Since or(n)>(lgn)2≥1, n≡1(modr). If every prime factor q of n had q≡1(modr), then n≡1; so some prime p∣n has p≡1(modr), that is, or(p)>1 (note gcd(p,r)=1 as gcd(n,r)=1). Since gcd(a,n)=1 for all a≤r, p>r. Also or(n)≤φ(r), so φ(r)>lgn and ℓ<φ(r)<r<p. Reducing the congruences above modulo p:
(X+a)n≡Xn+a(modXr−1,p)(0≤a≤ℓ).(1)
By Lemma 2 for the prime p:
(X+a)p≡Xp+a(modXr−1,p)(0≤a≤ℓ).(2)
Work in Fp[X]. Call an integer m≥1introspective for f∈Fp[X] if f(X)m≡f(Xm)(modXr−1).
Lemma 4. If m and m′ are introspective for f, so is mm′. If m is introspective for f and for g, it is introspective for fg.
Proof. Substituting Xm for X in f(X)m′≡f(Xm′)(modXr−1) gives f(Xm)m′≡f(Xmm′)(modXmr−1), and Xr−1 divides Xmr−1, so this also holds modulo Xr−1. Hence f(X)mm′=(f(X)m)m′≡f(Xm)m′≡f(Xmm′). The second claim is (fg)(X)m=f(X)mg(X)m≡f(Xm)g(Xm)=(fg)(Xm). □
Lemma 5. The numbers p and n/p are introspective for each X+a, 0≤a≤ℓ.
Proof. For p this is (2). For m=n/p, let u=(X+a)m−(Xm+a). In Fp[X] the map g↦gp is a ring homomorphism with cp=c on Fp, so up=(X+a)n−(Xn+a), which is 0 modulo Xr−1 by (1). Since p∤r, Xr−1 and its derivative rXr−1 are coprime, so Xr−1 is squarefree in Fp[X]. Each irreducible factor h of Xr−1 divides up, hence u; as these factors are distinct, their product Xr−1 divides u. □
Let I={(n/p)ipj:i,j≥0} and P={∏a=0ℓ(X+a)ea:ea≥0}. By Lemmas 4 and 5, every m∈I is introspective for every f∈P.
Let G be the set of residues modulo r of the elements of I. All of them are units modulo r, and G is closed under products, so G is a subgroup of (Z/rZ)×. Let t=∣G∣. Then t≤φ(r), and G contains the powers of n=(n/p)⋅p, so t≥or(n)>(lgn)2.
Let Qr be the r-th cyclotomic polynomial. It divides Xr−1, and over Fp it is a product of irreducible factors of degree or(p)>1. Fix one such factor h and let F=Fp[X]/(h), a finite field in which the class ξ of X is a primitive r-th root of unity. Let G be the set of images of P in F. No X+a maps to 0, since degh>1, so G is a subset of F× closed under products, that is, a subgroup.
Lemma 6 (lower bound).∣G∣≥(t−1t+ℓ).
Proof. Let f,g∈P have degree below t and the same image in F. For m∈I, f(X)m≡f(Xm) and g(X)m≡g(Xm) modulo Xr−1, hence modulo h, so f(ξm)=f(ξ)m=g(ξ)m=g(ξm) in F. As ξr=1, ξm depends only on mmodr, and as ξ has order r, the t residues in G give t distinct elements ξm. So f(Y)−g(Y), of degree below t, has t roots in the field F, and f=g. Now the numbers 0,1,…,ℓ are distinct modulo p, as ℓ<p, so the polynomials ∏a(X+a)ea with ∑aea≤t−1 are distinct elements of P of degree below t, by unique factorization in Fp[X]. There are (t−1(t−1)+(ℓ+1))=(t−1t+ℓ) of them, and their images are distinct. □
Lemma 7 (upper bound). If n is not a power of p, then ∣G∣≤nt.
Proof. Let s=⌊t⌋ and I^={(n/p)ipj:0≤i,j≤s}. If (n/p)ipj=(n/p)i′pj′ with (i,j)=(i′,j′), then i=i′, and (n/p)∣i−i′∣ is a power of p, so n/p and n are powers of p. So, as n is not, I^ has (s+1)2>t elements, and two of them, m1>m2, have the same residue modulo r. Then for every f∈P, f(X)m1≡f(Xm1)≡f(Xm2)≡f(X)m2(modXr−1), since Xm1≡Xm2(modXr−1). So every element of G is a root in F of Ym1−Ym2, a nonzero polynomial of degree m1, and ∣G∣≤m1≤(n/p)sps=ns≤nt. □
Lemma 8.∣G∣>nt.
Proof. Let B=⌊tlgn⌋. Since t>(lgn)2, t>lgn, so t>tlgn≥B and t−1≥B. Since t≤φ(r), ℓ=⌊φ(r)lgn⌋≥B. Also n>r≥2 and (lgn)2<t≤φ(r)<r<n; for n≥3, tlgn>(lgn)2>2, so B≥2. The binomial coefficient (t−1t+ℓ)=(t−1(ℓ+1)+(t−1)) grows with t−1 and with ℓ, so by Lemma 6
∣G∣≥(Bℓ+1+B)≥(B2B+1).
For B≥2, (B2B+1)>2B+1: it holds for B=2 (10>8), and going from B to B+1 multiplies the left side by (B+2)(B+1)(2B+3)(2B+2)=B+22(2B+3)>2. Finally B+1>tlgn, so 2B+1>2tlgn=nt. □
Theorem. For every integer n≥0, AKS(n) ends, and it returns true if and only if n is prime.
Proof.Termination. Line L1 handles n<2. Line L2 tries finitely many exponents, line L3 ends by Lemma 1, and the loops of lines L4 and L7 are bounded by r and ℓ. Each check in line L8 is a power computed with ⌊lgn⌋ squarings.
Primes. By Lemma 3 a prime returns true.
Composites. Suppose a composite n returns true. A return at line L5 forces n prime, as shown above, so the return is at line L9, and the setup applies with a prime p∣n. Lemmas 7 and 8 together force n to be a power of p, say n=pk, and k≥2 as n is composite. Then n=pk is a perfect power and line L2 would have returned false, a contradiction. So a composite n returns false, and so do 0 and 1, at line L1. ■