These are the human proofs of APR-CL primality test, at full length.
APR-CL is always right and always stops full
This proof shows: correctness, termination.
We use the pseudocode APR-CL(N) of the page and its notation. After line L4, N≥2 is odd and coprime to ts, so N is coprime to every q of the plan and to every prime p∣t. The plan has s2>N, t=lcm(qi−1) with t/2 odd, and every odd q of the plan has q≡3(mod4). For a prime p and an integer x=0, vp(x) is the exponent of p in x.
1. Rings and sums for one pair
Fix a prime q∤N and an odd prime p∣q−1, with m=pk the exact power of p in q−1. Let ζ=ζm, ω=ζq, K=Q(ζ,ω) and R=Z[ζ,ω]. As gcd(m,q)=1, R=Z[ζmq], it is a free Z-module, and it is a free Z[ζ]-module with basis 1,ω,…,ωq−2. The automorphisms of K are σcρd for c prime to m and d prime to q, with σc(ζ)=ζc, σc(ω)=ω, ρd(ζ)=ζ and ρd(ω)=ωd. They map R onto R.
Let g be the least primitive root modulo q, χ(gx)=ζx and χ(0)=0. As m is odd and m∣q−1, m∣(q−1)/2, so χ(−1)=ζ(q−1)/2=1. Let
τ=x=1∑q−1χ(x)ωx,τˉ=x=1∑q−1χ(x)−1ω−x.
Lemma 1. (a) ττˉ=q. (b) ρd(τ)=χ(d)−1τ and σc(τ)=∑xχ(x)cωx. (c) τ2=Jσ2(τ), with J=∑x=2q−1χ(x)χ(1−x).
Proof. (a) ττˉ=∑x,y=0χ(xy−1)ωx−y. Put x=cy: this is ∑c=0χ(c)∑y=0ω(c−1)y. The inner sum is q−1 for c=1 and −1 otherwise, as the q-th roots of unity add up to 0. So ττˉ=(q−1)−∑c=0,1χ(c)=q, since ∑c=0χ(c)=0 for the nontrivial χ.
(b) ρd(τ)=∑xχ(x)ωdx=∑yχ(d−1y)ωy with y=dx. The second claim holds because σc maps χ(x)=ζindx to χ(x)c.
(c) τ2=∑aωa∑x+y=aχ(x)χ(y). For a=0 the inner sum is χ(−1)∑xχ2(x)=0, as χ2 is nontrivial (m is odd and m>1). For a=0 put x=ax′, y=ay′: the inner sum is χ2(a)J. So τ2=J∑aχ2(a)ωa=Jσ2(τ) by (b). □
The group ring. Let G=(Z/m)∗ and Y the representatives 1≤y<m, p∤y. The ring Z[G] acts on K∗ by x∑acσc=∏σc(x)ac; this action is multiplicative in the exponent and commutes with every ρd. Let
θ=y∈Y∑yσy−1,αc=y∈Y∑⌊cy/m⌋σy−1(c≥0).
Lemma 2. For c prime to m, (c−σc)θ=mαc. Hence (2−σ2)αN=(N−σN)α2.
Proof.cy=m⌊cy/m⌋+(cymodm), so cθ=mαc+∑y(cymodm)σy−1. Put y′=cymodm, which runs over Y; then σy−1=σcσy′−1, so the last sum is σcθ. For the second claim, m(2−σ2)αN=(2−σ2)(N−σN)θ=m(N−σN)α2, and Z[G] has no torsion. □
Let u=τα2=∏y∈Y,y>m/2σy−1(τ), an element of R, and w=∑y∈Y,y>m/2y−1modm.
Lemma 3. (a) S=JαN, and SσN(u)=uN in R. (b) ρd(u)=χ(d)−wu.
Proof. (a) The code computes ∏yσy−1(J)⌊Ny/m⌋=JαN, by the identity ⌊Ny/m⌋=⌊N/m⌋y+⌊(Nmodm)y/m⌋ of the page. By Lemma 1(c), J=τ2−σ2 in K∗, so by Lemma 2, JαN=τ(N−σN)α2=uN/σN(u). Both SσN(u) and uN lie in R and are equal in K.
(b) By Lemma 1(b), ρd(σc(τ))=σc(χ(d)−1τ)=χ(d)−cσc(τ). Multiply over c=y−1 with y>m/2. □
Lemma 4.wmodp does not depend on k, and the code checks p∤w for every pair of the plan. So p∤w for every pair used, including those of line L16.
Proof. Modulo p, y−1modm is the inverse of ymodp. The y in Y with y>m/2 are the numbers from (m+1)/2 to m−1 that are prime to p. Their count is (m−1)/2=p(pk−1−1)/2+(p−1)/2. Take the last (p−1)/2 of them, with residues (p+1)/2,…,p−1; the others form a run of whole blocks of p consecutive numbers, and each block adds ∑c=1p−1c−1≡0(modp). So w≡∑c=(p+1)/2p−1c−1(modp) for every k. The function check_units raises an error if p∣w for a pair of the plan; the extra pairs of line L16 use a prime p∣t, which already occurs in the plan. □
2. The semigroup of one prime factor
Fix a prime r∣N and let A=R/rR. As R is a free Z-module, A is a finite ring that is not 0. As r∤q, q is a unit of A, so by Lemma 1(a) τ and all σc(τ) are units of A, and so is u.
Lemma 5. The powers ζi, 0≤i<m, are distinct in A.
Proof.∏j=1m−1(1−ζj)=m, the value at x=1 of (xm−1)/(x−1)=∏j=1m−1(x−ζj). If ζi=ζi′ in A with i=i′, then 1−ζi′−i=0 in A, so m=0 in A. But r∤m, so m is a unit of A, and A=0. □
Lemma 6 (Frobenius). In A, ur=χ(r)−wrσr(u).
Proof. For x=∑cijζiωj∈R, the multinomial theorem and Fermat's little theorem give xr≡∑cijζriωrj=σrρr(x)(modrR). With Lemma 3(b), ur=σr(χ(r)−wu)=χ(r)−wrσr(u). □
Let Γ be the set of integers a≥1 with p∤a for which ua=εσa(u) in A for some ε=ζi. As σa(u) is a unit, ε is unique by Lemma 5; call it ε(a), and let λ(a)=ε(a)a′, where aa′≡1(modm).
Lemma 7. (a) 1∈Γ, and if a,b∈Γ then ab∈Γ and λ(ab)=λ(a)λ(b). (b) r∈Γ with λ(r)=χ(r)−w. (c) If the check of line L11 passes for the pair, with S≡ζh(modN), then N∈Γ with λ(N)=ζhN′, NN′≡1(modm). (d) There is an e≥0 such that for a,b∈Γ with a≡b(modpE) and E≥max(e,k), λ(a)=λ(b).
Proof. (a) uab=(ε(a)σa(u))b=ε(a)bσa(ub)=ε(a)bε(b)aσab(u), since σa is a ring map of A with σa(ζi)=ζai. So ε(ab)=ε(a)bε(b)a, and raising to (ab)′ gives λ(ab)=ε(a)a′ε(b)b′.
(b) Lemma 6 gives ε(r)=χ(r)−wr.
(c) S≡ζh modulo NZ[ζ], so modulo rR, and Lemma 3(a) gives uN=ζhσN(u) in A.
(d) A is finite, so u has a finite order pec with p∤c. Let v=uc, of order pe. For a∈Γ, va=ε(a)cσa(v). If a≡b(modpE), then va=vb, and σa=σb as m∣pE. So ε(a)c=ε(b)c, hence ε(a)=ε(b) by Lemma 5, as c is invertible modulo m. Also a′≡b′(modm). □
Fact F. Let p be odd, f≥1, and a an integer with vp(a−1)=f. For E≥f, the order of a modulo pE is pE−f, and the powers of a modulo pE are exactly the residues ≡1(modpf).
Proof. If a=1+pfx with p∤x, then ap=1+pf+1x+(2p)p2fx2+…≡1+pf+1x(modpf+2), as p is odd. By induction vp(api−1)=f+i, so the order is pE−f. The residues ≡1(modpf) form a group of pE−f elements that contains the powers of a. □
3. The conditions Lp for odd p
For odd p∣t, Lp is: vp(rp−1−1)≥vp(Np−1−1) for every prime r∣N. Write f=vp(Np−1−1)≥1.
Lemma 8 (proving Lp).Lp holds if Np−1≡1(modp2), or if for some prime q∤N with p∣q−1 the check gives S≡ζh with p∤h.
Proof. In the first case f=1, and vp(rp−1−1)≥1 by Fermat. In the second case fix r∣N, suppose g=vp(rp−1−1)<f, and work in A for this pair. Take E≥max(e,k,f). By Fact F for b=rp−1, Np−1≡bj(modpE) for some j≥0, and as bj≡1(modpf) and b has order pf−g modulo pf, p∣j. By Lemma 7, Np−1 and bj lie in Γ, and λ(N)p−1=λ(r)(p−1)j=χ(r)−w(p−1)j. The left side is ζhN′(p−1), of order exactly m, as p∤hN′(p−1). The right side is a p-th power in the group of order m, so its order divides m/p. This is a contradiction, so g≥f. □
Lemma 9. Suppose Lp holds and every pair (p,q) with q in the plan passes line L11. Then there are integers l(r), one for each prime r∣N, such that χp,q(r)=χp,q(N)l(r) for every odd q of the plan with p∣q−1, where χp,q is the character χ of that pair.
Proof. There are finitely many primes r∣N and pairs (p,q); take E at least f+k and at least the e of Lemma 7(d) for each of them. By Lp and Fact F for a=Np−1, each r has an l(r) with rp−1≡N(p−1)l(r)(modpE), unique modulo pE−f.
Fix a pair and r, and work in A. Both sides lie in Γ, so by Lemma 7, χ(r)−w(p−1)=λ(N)(p−1)l(r). As p−1 and w are invertible modulo m, χ(r)−w=λ(N)l(r) and χ(r)=ψl(r) with ψ=λ(N)−w−1=ζ−hN′w−1, where w−1 is the inverse modulo m. This ψ depends only on the pair and h, not on r.
Now write N=∏riei. Then Np−1=∏ri(p−1)ei≡N(p−1)∑eil(ri)(modpE), and as Np−1 has order pE−f modulo pE, ∑eil(ri)≡1(modpE−f), so also modulo m. Hence χ(N)=∏χ(ri)ei=ψ∑eil(ri)=ψ, and χ(r)=χ(N)l(r). □
4. The case p=2
Here L2 is: v2(r−1)≥v2(N−1) for every prime r∣N. Let σ=v2(N−1)≥1. For an odd q of the plan, the character of order 2=2v2(q−1) is the Legendre symbol (⋅/q), and Eq=(−q)(N−1)/2modN.
Lemma 10. (a) If some Eq=N−1, then L2 holds, and L2 holds if N≡3(mod4). (b) If N is not a square, (z/N)=−1 and z(N−1)/2≡−1(modN), then L2 holds.
Proof. If x(N−1)/2≡−1(modr), the order o of x modulo r divides N−1=2σn′ but not (N−1)/2, so v2(o)=σ; as o∣r−1, v2(r−1)≥σ. Apply this to x=−q for (a) and to x=z for (b). If N≡3(mod4), σ=1, and r−1 is even. □
Lemma 11. Suppose L2 holds and every odd q of the plan has Eq=±1. Let l(r)=1 if v2(r−1)=σ and l(r)=0 otherwise. Then (r/q)=(N/q)l(r) for every prime r∣N and every odd q of the plan.
Proof. Let o be the order of −q modulo r and ρ=v2(r−1)≥σ. By Euler's criterion, (−q/r)=−1 exactly when o∤(r−1)/2, that is when v2(o)=ρ. If Eq=−1, v2(o)=σ as in Lemma 10, so (−q/r)=(−1)l(r). If Eq=1, o∣(N−1)/2, so v2(o)≤σ−1<ρ and (−q/r)=1. In both cases (−q/r)=Eql(r). By quadratic reciprocity, (−q/r)=(−1)(r−1)/2(q/r)=(−1)(r−1)/2(1+(q−1)/2)(r/q)=(r/q), as (q−1)/2 is odd. So (r/q)=Eql(r).
Write N=∏riei. By L2, ri≡1+2σl(ri)(mod2σ+1), and as 2σ≥σ+1, N≡1+2σ∑eil(ri)(mod2σ+1). Also N≡1+2σ(mod2σ+1), so ∑eil(ri) is odd, and (N/q)=∏(ri/q)ei=Eq∑eil(ri)=Eq. Hence (r/q)=(N/q)l(r). □
5. The final search
Lemma 12. Suppose N reaches line L17. Then every prime r∣N has r≡Nl(mods) for some 0≤l<t.
Proof. Lines L11 and L14 passed for every pair of the plan. After line L16, done[p] is true for every prime p∣t, each set by line L5, L6, L12, L15 or the extra check, so by Lemmas 8 and 10 every Lp holds (for the extra check of an odd p with a prime q not in the plan, Lemma 8 applies with that q, as q∤N is checked; Lemma 4 gives p∤w). Lemmas 9 and 11 give numbers lp(r) for every p∣t. By the Chinese remainder theorem choose 0≤l<t with l≡lp(r)(modpvp(t)) for every p∣t.
Fix an odd q of the plan and pk∥q−1. As pk∣pvp(t) and χp,q has order pk, χp,q(r)=χp,q(N)l for every p∣q−1, so indgr≡lindgN(modpk) for every such p, hence modulo q−1, and r≡Nl(modq). Also r≡Nl≡1(mod2). As s is the product of these distinct primes, r≡Nl(mods). □
Theorem 1 (prime answers are right). If APR-CL(N) returns prime, N is prime.
Proof. Line L3 returns prime only for a prime of the plan or a prime factor of t. If the extra check of line L16 met a prime q∣N, it continued only when N=q, which is prime. Otherwise the answer comes from line L20 and Lemma 12 applies. Suppose N is composite there, and let r be its least prime factor, so r≤N<s. By Lemma 12, r≡Nl(mods) with 0≤l<t. As 1<r<s, r=Nlmods, and l=0 since r=1. Lines L17 and L18 compute Nimods for every 1≤i<t, so at i=l or before, line L19 finds a number r′ with 1<r′<N and r′∣N, and returns composite. This contradicts the answer prime. □
Theorem 2 (composite answers are right). If APR-CL(N) returns composite, N is composite.
Proof. We show that a prime N never reaches a "composite". Line L1: N≥2. Line L4: a prime that is not in line L3 is coprime to ts. Line L11: in A=R/NR, Lemma 6 and Lemma 3(a) give SσN(u)=uN=χ(N)−wNσN(u), so S≡χ(N)−wN modulo NR, hence modulo NZ[ζ], as R is free over Z[ζ] with a basis that contains 1. Line L14: Euler's criterion gives E=(−q/N)=±1. Line L16 for p=2: a prime is not a square, the Jacobi symbol is the Legendre symbol, (z/N)=0 is impossible for z<N, and z(N−1)/2≡(z/N)=−1. Line L16 for odd p: a prime q∣N means N=q, which returns "proved", not composite, and the check of line L11 passes as above. Line L19: a prime has no factor r with 1<r<N. □
Theorem 3 (termination). APR-CL(N) stops.
Proof. The plan comes from a finite table, and lines L7 to L15 run over finitely many pairs. Each power is a loop of O(logN) steps, and the search for h at line L11 tries m values. In the extra check for p=2, a non-square N=∏piei has some ei odd; by the Chinese remainder theorem there is a z<N that is a non-residue modulo pi and ≡1 modulo the other prime powers, and (z/N)=−1. So the loop over z stops at the latest at the least such z. The extra check for odd p stops after 60 primes q that it tests. The search of lines L17 to L19 has t−1 steps. So every run stops, with prime, composite, or no answer from the cap of line L16. □