Algorithm Search⌘K

Proofs: AKS primality test

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)(n) of the page, after Agrawal, Kayal and Saxena (Annals of Mathematics 160, 2004, Section 4). Write lg⁡\lg for the base 2 logarithm, or(m)o_r(m) for the order of mm modulo rr when gcd⁡(m,r)=1\gcd(m, r) = 1, and φ\varphi for Euler's totient. "f≡g(modXr−1,m)f \equiv g \pmod{X^r - 1, m}" means that f−gf - g lies in the ideal generated by Xr−1X^r - 1 and mm in Z[X]\mathbb{Z}[X].

Lemma 1 (termination of line L3). For n≥2n \ge 2 there is an r≥2r \ge 2 with gcd⁡(r,n)=1\gcd(r, n) = 1 and or(n)>(lg⁡n)2o_r(n) > (\lg n)^2, so line L3 ends.

Proof. Let k=⌊(lg⁡n)2⌋k = \lfloor (\lg n)^2 \rfloor and N=n∏i=1k(ni−1)N = n \prod_{i=1}^{k} (n^i - 1), a positive integer since n≥2n \ge 2. Take a prime rr that does not divide NN. Then r∤nr \nmid n, so gcd⁡(r,n)=1\gcd(r, n) = 1, and ni≢1(modr)n^i \not\equiv 1 \pmod r for 1≤i≤k1 \le i \le k, so or(n)>ko_r(n) > k, that is, or(n)>(lg⁡n)2o_r(n) > (\lg n)^2 as or(n)o_r(n) is an integer. The search of line L3 tries r=2,3,…r = 2, 3, \ldots in order, so it stops at the least such rr. (Lemma 4.3 of the paper shows that r≤max⁡(3,⌈(lg⁡n)5⌉)r \le \max(3, \lceil (\lg n)^5 \rceil).) □\square

Lemma 2 (polynomial Fermat for primes). If nn is prime, then (X+a)n≡Xn+a(modn)(X + a)^n \equiv X^n + a \pmod n for every integer aa.

Proof. By the binomial theorem (X+a)n=∑i=0n(ni)an−iXi(X + a)^n = \sum_{i=0}^{n} \binom{n}{i} a^{n-i} X^i. For 0<i<n0 < i < n, (ni)=n!i!(n−i)!\binom{n}{i} = \frac{n!}{i!(n-i)!} and the prime nn divides the numerator but not the denominator, so n∣(ni)n \mid \binom{n}{i}. The two outer terms are XnX^n and an≡aa^n \equiv a, by Fermat's little theorem. □\square

Lemma 3 (primes return true). If nn is prime, AKS(n)(n) returns true.

Proof. Line L1 does not fire as n≥2n \ge 2, and line L2 does not fire, as a prime is not aba^b with a,b≥2a, b \ge 2. In line L4, gcd⁡(a,n)∈{1,n}\gcd(a, n) \in \{1, n\} for every aa, so it does not fire. If n≤rn \le r, line L5 returns true. Otherwise, for each aa, Lemma 2 gives (X+a)n≡Xn+a(modn)(X + a)^n \equiv X^n + a \pmod n, hence also modulo (Xr−1,n)(X^r - 1, n), so line L8 does not fire and line L9 returns true. □\square

From now on, suppose that AKS(n)(n) returns true. If it does so at line L5, then n≤rn \le r and line L4 found no a≤ra \le r with 1<gcd⁡(a,n)<n1 < \gcd(a, n) < n; in particular no aa with 2≤a≤n−12 \le a \le n - 1 divides nn, so nn is prime. So assume it returns true at line L9. Then n≥2n \ge 2, nn is not a perfect power, n>rn > r, gcd⁡(r,n)=1\gcd(r, n) = 1, or(n)>(lg⁡n)2o_r(n) > (\lg n)^2, gcd⁡(a,n)=1\gcd(a, n) = 1 for 1≤a≤r1 \le a \le r, and, with ℓ=⌊φ(r)lg⁡n⌋\ell = \lfloor \sqrt{\varphi(r)} \lg n \rfloor,

(X+a)n≡Xn+a(modXr−1,n)(0≤a≤ℓ),(X + a)^n \equiv X^n + a \pmod{X^r - 1, n} \qquad (0 \le a \le \ell),

where a=0a = 0 holds trivially. We show that nn is prime.

Setup. Since or(n)>(lg⁡n)2≥1o_r(n) > (\lg n)^2 \ge 1, n≢1(modr)n \not\equiv 1 \pmod r. If every prime factor qq of nn had q≡1(modr)q \equiv 1 \pmod r, then n≡1n \equiv 1; so some prime p∣np \mid n has p≢1(modr)p \not\equiv 1 \pmod r, that is, or(p)>1o_r(p) > 1 (note gcd⁡(p,r)=1\gcd(p, r) = 1 as gcd⁡(n,r)=1\gcd(n, r) = 1). Since gcd⁡(a,n)=1\gcd(a, n) = 1 for all a≤ra \le r, p>rp > r. Also or(n)≤φ(r)o_r(n) \le \varphi(r), so φ(r)>lg⁡n\sqrt{\varphi(r)} > \lg n and ℓ<φ(r)<r<p\ell < \varphi(r) < r < p. Reducing the congruences above modulo pp:

(X+a)n≡Xn+a(modXr−1,p)(0≤a≤ℓ).(1)(X + a)^n \equiv X^n + a \pmod{X^r - 1, p} \qquad (0 \le a \le \ell). \tag{1}

By Lemma 2 for the prime pp:

(X+a)p≡Xp+a(modXr−1,p)(0≤a≤ℓ).(2)(X + a)^p \equiv X^p + a \pmod{X^r - 1, p} \qquad (0 \le a \le \ell). \tag{2}

Work in Fp[X]\mathbb{F}_p[X]. Call an integer m≥1m \ge 1 introspective for f∈Fp[X]f \in \mathbb{F}_p[X] if f(X)m≡f(Xm)(modXr−1)f(X)^m \equiv f(X^m) \pmod{X^r - 1}.

Lemma 4. If mm and m′m' are introspective for ff, so is mm′m m'. If mm is introspective for ff and for gg, it is introspective for fgf g.

Proof. Substituting XmX^m for XX in f(X)m′≡f(Xm′)(modXr−1)f(X)^{m'} \equiv f(X^{m'}) \pmod{X^r - 1} gives f(Xm)m′≡f(Xmm′)(modXmr−1)f(X^m)^{m'} \equiv f(X^{m m'}) \pmod{X^{mr} - 1}, and Xr−1X^r - 1 divides Xmr−1X^{mr} - 1, so this also holds modulo Xr−1X^r - 1. Hence f(X)mm′=(f(X)m)m′≡f(Xm)m′≡f(Xmm′)f(X)^{m m'} = (f(X)^m)^{m'} \equiv f(X^m)^{m'} \equiv f(X^{m m'}). The second claim is (fg)(X)m=f(X)mg(X)m≡f(Xm)g(Xm)=(fg)(Xm)(fg)(X)^m = f(X)^m g(X)^m \equiv f(X^m) g(X^m) = (fg)(X^m). □\square

Lemma 5. The numbers pp and n/pn / p are introspective for each X+aX + a, 0≤a≤ℓ0 \le a \le \ell.

Proof. For pp this is (2). For m=n/pm = n/p, let u=(X+a)m−(Xm+a)u = (X + a)^m - (X^m + a). In Fp[X]\mathbb{F}_p[X] the map g↦gpg \mapsto g^p is a ring homomorphism with cp=cc^p = c on Fp\mathbb{F}_p, so up=(X+a)n−(Xn+a)u^p = (X + a)^n - (X^n + a), which is 00 modulo Xr−1X^r - 1 by (1). Since p∤rp \nmid r, Xr−1X^r - 1 and its derivative rXr−1r X^{r-1} are coprime, so Xr−1X^r - 1 is squarefree in Fp[X]\mathbb{F}_p[X]. Each irreducible factor hh of Xr−1X^r - 1 divides upu^p, hence uu; as these factors are distinct, their product Xr−1X^r - 1 divides uu. □\square

Let I={(n/p)ipj:i,j≥0}I = \{ (n/p)^i p^j : i, j \ge 0 \} and P={∏a=0ℓ(X+a)ea:ea≥0}P = \{ \prod_{a=0}^{\ell} (X + a)^{e_a} : e_a \ge 0 \}. By Lemmas 4 and 5, every m∈Im \in I is introspective for every f∈Pf \in P.

Let GG be the set of residues modulo rr of the elements of II. All of them are units modulo rr, and GG is closed under products, so GG is a subgroup of (Z/rZ)×(\mathbb{Z}/r\mathbb{Z})^\times. Let t=∣G∣t = |G|. Then t≤φ(r)t \le \varphi(r), and GG contains the powers of n=(n/p)⋅pn = (n/p) \cdot p, so t≥or(n)>(lg⁡n)2t \ge o_r(n) > (\lg n)^2.

Let QrQ_r be the rr-th cyclotomic polynomial. It divides Xr−1X^r - 1, and over Fp\mathbb{F}_p it is a product of irreducible factors of degree or(p)>1o_r(p) > 1. Fix one such factor hh and let F=Fp[X]/(h)F = \mathbb{F}_p[X] / (h), a finite field in which the class ξ\xi of XX is a primitive rr-th root of unity. Let G\mathcal{G} be the set of images of PP in FF. No X+aX + a maps to 0, since deg⁡h>1\deg h > 1, so G\mathcal{G} is a subset of F×F^\times closed under products, that is, a subgroup.

Lemma 6 (lower bound). ∣G∣≥(t+ℓt−1)|\mathcal{G}| \ge \binom{t + \ell}{t - 1}.

Proof. Let f,g∈Pf, g \in P have degree below tt and the same image in FF. For m∈Im \in I, f(X)m≡f(Xm)f(X)^m \equiv f(X^m) and g(X)m≡g(Xm)g(X)^m \equiv g(X^m) modulo Xr−1X^r - 1, hence modulo hh, so f(ξm)=f(ξ)m=g(ξ)m=g(ξm)f(\xi^m) = f(\xi)^m = g(\xi)^m = g(\xi^m) in FF. As ξr=1\xi^r = 1, ξm\xi^m depends only on m mod rm \bmod r, and as ξ\xi has order rr, the tt residues in GG give tt distinct elements ξm\xi^m. So f(Y)−g(Y)f(Y) - g(Y), of degree below tt, has tt roots in the field FF, and f=gf = g. Now the numbers 0,1,…,ℓ0, 1, \ldots, \ell are distinct modulo pp, as ℓ<p\ell < p, so the polynomials ∏a(X+a)ea\prod_a (X + a)^{e_a} with ∑aea≤t−1\sum_a e_a \le t - 1 are distinct elements of PP of degree below tt, by unique factorization in Fp[X]\mathbb{F}_p[X]. There are ((t−1)+(ℓ+1)t−1)=(t+ℓt−1)\binom{(t - 1) + (\ell + 1)}{t - 1} = \binom{t + \ell}{t - 1} of them, and their images are distinct. □\square

Lemma 7 (upper bound). If nn is not a power of pp, then ∣G∣≤nt|\mathcal{G}| \le n^{\sqrt t}.

Proof. Let s=⌊t⌋s = \lfloor \sqrt t \rfloor and I^={(n/p)ipj:0≤i,j≤s}\hat I = \{ (n/p)^i p^j : 0 \le i, j \le s \}. If (n/p)ipj=(n/p)i′pj′(n/p)^i p^j = (n/p)^{i'} p^{j'} with (i,j)≠(i′,j′)(i, j) \ne (i', j'), then i≠i′i \ne i', and (n/p)∣i−i′∣(n/p)^{|i - i'|} is a power of pp, so n/pn/p and nn are powers of pp. So, as nn is not, I^\hat I has (s+1)2>t(s + 1)^2 > t elements, and two of them, m1>m2m_1 > m_2, have the same residue modulo rr. Then for every f∈Pf \in P, f(X)m1≡f(Xm1)≡f(Xm2)≡f(X)m2(modXr−1)f(X)^{m_1} \equiv f(X^{m_1}) \equiv f(X^{m_2}) \equiv f(X)^{m_2} \pmod{X^r - 1}, since Xm1≡Xm2(modXr−1)X^{m_1} \equiv X^{m_2} \pmod{X^r - 1}. So every element of G\mathcal{G} is a root in FF of Ym1−Ym2Y^{m_1} - Y^{m_2}, a nonzero polynomial of degree m1m_1, and ∣G∣≤m1≤(n/p)sps=ns≤nt|\mathcal{G}| \le m_1 \le (n/p)^s p^s = n^s \le n^{\sqrt t}. □\square

Lemma 8. ∣G∣>nt|\mathcal{G}| > n^{\sqrt t}.

Proof. Let B=⌊tlg⁡n⌋B = \lfloor \sqrt t \lg n \rfloor. Since t>(lg⁡n)2t > (\lg n)^2, t>lg⁡n\sqrt t > \lg n, so t>tlg⁡n≥Bt > \sqrt t \lg n \ge B and t−1≥Bt - 1 \ge B. Since t≤φ(r)t \le \varphi(r), ℓ=⌊φ(r)lg⁡n⌋≥B\ell = \lfloor \sqrt{\varphi(r)} \lg n \rfloor \ge B. Also n>r≥2n > r \ge 2 and (lg⁡n)2<t≤φ(r)<r<n(\lg n)^2 < t \le \varphi(r) < r < n; for n≥3n \ge 3, tlg⁡n>(lg⁡n)2>2\sqrt t \lg n > (\lg n)^2 > 2, so B≥2B \ge 2. The binomial coefficient (t+ℓt−1)=((ℓ+1)+(t−1)t−1)\binom{t + \ell}{t - 1} = \binom{(\ell + 1) + (t - 1)}{t - 1} grows with t−1t - 1 and with ℓ\ell, so by Lemma 6

∣G∣≥(ℓ+1+BB)≥(2B+1B).|\mathcal{G}| \ge \binom{\ell + 1 + B}{B} \ge \binom{2B + 1}{B}.

For B≥2B \ge 2, (2B+1B)>2B+1\binom{2B + 1}{B} > 2^{B + 1}: it holds for B=2B = 2 (10>810 > 8), and going from BB to B+1B + 1 multiplies the left side by (2B+3)(2B+2)(B+2)(B+1)=2(2B+3)B+2>2\frac{(2B + 3)(2B + 2)}{(B + 2)(B + 1)} = \frac{2(2B + 3)}{B + 2} > 2. Finally B+1>tlg⁡nB + 1 > \sqrt t \lg n, so 2B+1>2tlg⁡n=nt2^{B + 1} > 2^{\sqrt t \lg n} = n^{\sqrt t}. □\square

Theorem. For every integer n≥0n \ge 0, AKS(n)(n) ends, and it returns true if and only if nn is prime.

Proof. Termination. Line L1 handles n<2n < 2. Line L2 tries finitely many exponents, line L3 ends by Lemma 1, and the loops of lines L4 and L7 are bounded by rr and ℓ\ell. Each check in line L8 is a power computed with ⌊lg⁡n⌋\lfloor \lg n \rfloor squarings.

Primes. By Lemma 3 a prime returns true.

Composites. Suppose a composite nn returns true. A return at line L5 forces nn prime, as shown above, so the return is at line L9, and the setup applies with a prime p∣np \mid n. Lemmas 7 and 8 together force nn to be a power of pp, say n=pkn = p^k, and k≥2k \ge 2 as nn is composite. Then n=pkn = p^k is a perfect power and line L2 would have returned false, a contradiction. So a composite nn returns false, and so do 00 and 11, at line L1. ■\blacksquare

Back to AKS primality test