Algorithm Search⌘K

Proofs: APR-CL primality test

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)(N) of the page and its notation. After line L4, N≥2N \ge 2 is odd and coprime to tst s, so NN is coprime to every qq of the plan and to every prime p∣tp \mid t. The plan has s2>Ns^2 > N, t=lcm⁡(qi−1)t = \operatorname{lcm}(q_i - 1) with t/2t/2 odd, and every odd qq of the plan has q≡3(mod4)q \equiv 3 \pmod 4. For a prime pp and an integer x≠0x \ne 0, vp(x)v_p(x) is the exponent of pp in xx.

1. Rings and sums for one pair

Fix a prime q∤Nq \nmid N and an odd prime p∣q−1p \mid q - 1, with m=pkm = p^k the exact power of pp in q−1q - 1. Let ζ=ζm\zeta = \zeta_m, ω=ζq\omega = \zeta_q, K=Q(ζ,ω)K = \mathbb{Q}(\zeta, \omega) and R=Z[ζ,ω]R = \mathbb{Z}[\zeta, \omega]. As gcd⁡(m,q)=1\gcd(m, q) = 1, R=Z[ζmq]R = \mathbb{Z}[\zeta_{mq}], it is a free Z\mathbb{Z}-module, and it is a free Z[ζ]\mathbb{Z}[\zeta]-module with basis 1,ω,…,ωq−21, \omega, \ldots, \omega^{q-2}. The automorphisms of KK are σcρd\sigma_c \rho_d for cc prime to mm and dd prime to qq, with σc(ζ)=ζc\sigma_c(\zeta) = \zeta^c, σc(ω)=ω\sigma_c(\omega) = \omega, ρd(ζ)=ζ\rho_d(\zeta) = \zeta and ρd(ω)=ωd\rho_d(\omega) = \omega^d. They map RR onto RR.

Let gg be the least primitive root modulo qq, χ(gx)=ζx\chi(g^x) = \zeta^x and χ(0)=0\chi(0) = 0. As mm is odd and m∣q−1m \mid q - 1, m∣(q−1)/2m \mid (q-1)/2, so χ(−1)=ζ(q−1)/2=1\chi(-1) = \zeta^{(q-1)/2} = 1. Let

τ=∑x=1q−1χ(x)ωx,τˉ=∑x=1q−1χ(x)−1ω−x.\tau = \sum_{x = 1}^{q-1} \chi(x) \omega^x, \qquad \bar\tau = \sum_{x = 1}^{q-1} \chi(x)^{-1} \omega^{-x}.

Lemma 1. (a) ττˉ=q\tau \bar\tau = q. (b) ρd(τ)=χ(d)−1τ\rho_d(\tau) = \chi(d)^{-1} \tau and σc(τ)=∑xχ(x)cωx\sigma_c(\tau) = \sum_x \chi(x)^c \omega^x. (c) τ2=Jσ2(τ)\tau^2 = J \sigma_2(\tau), with J=∑x=2q−1χ(x)χ(1−x)J = \sum_{x=2}^{q-1} \chi(x) \chi(1 - x).

Proof. (a) ττˉ=∑x,y≠0χ(xy−1)ωx−y\tau\bar\tau = \sum_{x, y \ne 0} \chi(x y^{-1}) \omega^{x - y}. Put x=cyx = cy: this is ∑c≠0χ(c)∑y≠0ω(c−1)y\sum_{c \ne 0} \chi(c) \sum_{y \ne 0} \omega^{(c-1)y}. The inner sum is q−1q - 1 for c=1c = 1 and −1-1 otherwise, as the qq-th roots of unity add up to 0. So ττˉ=(q−1)−∑c≠0,1χ(c)=q\tau\bar\tau = (q - 1) - \sum_{c \ne 0, 1} \chi(c) = q, since ∑c≠0χ(c)=0\sum_{c \ne 0} \chi(c) = 0 for the nontrivial χ\chi. (b) ρd(τ)=∑xχ(x)ωdx=∑yχ(d−1y)ωy\rho_d(\tau) = \sum_x \chi(x) \omega^{dx} = \sum_y \chi(d^{-1} y) \omega^y with y=dxy = dx. The second claim holds because σc\sigma_c maps χ(x)=ζind⁡x\chi(x) = \zeta^{\operatorname{ind} x} to χ(x)c\chi(x)^c. (c) τ2=∑aωa∑x+y=aχ(x)χ(y)\tau^2 = \sum_a \omega^a \sum_{x + y = a} \chi(x) \chi(y). For a=0a = 0 the inner sum is χ(−1)∑xχ2(x)=0\chi(-1) \sum_x \chi^2(x) = 0, as χ2\chi^2 is nontrivial (mm is odd and m>1m > 1). For a≠0a \ne 0 put x=ax′x = a x', y=ay′y = a y': the inner sum is χ2(a)J\chi^2(a) J. So τ2=J∑aχ2(a)ωa=Jσ2(τ)\tau^2 = J \sum_a \chi^2(a) \omega^a = J \sigma_2(\tau) by (b). □\square

The group ring. Let G=(Z/m)∗G = (\mathbb{Z}/m)^* and YY the representatives 1≤y<m1 \le y < m, p∤yp \nmid y. The ring Z[G]\mathbb{Z}[G] acts on K∗K^* by x∑acσc=∏σc(x)acx^{\sum a_c \sigma_c} = \prod \sigma_c(x)^{a_c}; this action is multiplicative in the exponent and commutes with every ρd\rho_d. Let

θ=∑y∈Yy σy−1,αc=∑y∈Y⌊cy/m⌋σy−1(c≥0).\theta = \sum_{y \in Y} y \, \sigma_y^{-1}, \qquad \alpha_c = \sum_{y \in Y} \lfloor c y / m \rfloor \sigma_y^{-1} \quad (c \ge 0).

Lemma 2. For cc prime to mm, (c−σc)θ=mαc(c - \sigma_c) \theta = m \alpha_c. Hence (2−σ2)αN=(N−σN)α2(2 - \sigma_2) \alpha_N = (N - \sigma_N) \alpha_2.

Proof. cy=m⌊cy/m⌋+(cy mod m)c y = m \lfloor cy/m \rfloor + (cy \bmod m), so cθ=mαc+∑y(cy mod m)σy−1c\theta = m\alpha_c + \sum_y (cy \bmod m) \sigma_y^{-1}. Put y′=cy mod my' = cy \bmod m, which runs over YY; then σy−1=σcσy′−1\sigma_y^{-1} = \sigma_{c} \sigma_{y'}^{-1}, so the last sum is σcθ\sigma_c \theta. For the second claim, m(2−σ2)αN=(2−σ2)(N−σN)θ=m(N−σN)α2m (2 - \sigma_2) \alpha_N = (2 - \sigma_2)(N - \sigma_N)\theta = m (N - \sigma_N) \alpha_2, and Z[G]\mathbb{Z}[G] has no torsion. □\square

Let u=τα2=∏y∈Y, y>m/2σy−1(τ)u = \tau^{\alpha_2} = \prod_{y \in Y, \, y > m/2} \sigma_{y^{-1}}(\tau), an element of RR, and w=∑y∈Y, y>m/2y−1 mod mw = \sum_{y \in Y, \, y > m/2} y^{-1} \bmod m.

Lemma 3. (a) S=JαNS = J^{\alpha_N}, and SσN(u)=uNS \sigma_N(u) = u^N in RR. (b) ρd(u)=χ(d)−wu\rho_d(u) = \chi(d)^{-w} u.

Proof. (a) The code computes ∏yσy−1(J)⌊Ny/m⌋=JαN\prod_y \sigma_{y^{-1}}(J)^{\lfloor Ny/m \rfloor} = J^{\alpha_N}, by the identity ⌊Ny/m⌋=⌊N/m⌋y+⌊(N mod m)y/m⌋\lfloor Ny/m \rfloor = \lfloor N/m \rfloor y + \lfloor (N \bmod m) y/m \rfloor of the page. By Lemma 1(c), J=τ2−σ2J = \tau^{2 - \sigma_2} in K∗K^*, so by Lemma 2, JαN=τ(N−σN)α2=uN/σN(u)J^{\alpha_N} = \tau^{(N - \sigma_N)\alpha_2} = u^N / \sigma_N(u). Both SσN(u)S \sigma_N(u) and uNu^N lie in RR and are equal in KK. (b) By Lemma 1(b), ρd(σc(τ))=σc(χ(d)−1τ)=χ(d)−cσc(τ)\rho_d(\sigma_{c}(\tau)) = \sigma_c(\chi(d)^{-1}\tau) = \chi(d)^{-c} \sigma_c(\tau). Multiply over c=y−1c = y^{-1} with y>m/2y > m/2. □\square

Lemma 4. w mod pw \bmod p does not depend on kk, and the code checks p∤wp \nmid w for every pair of the plan. So p∤wp \nmid w for every pair used, including those of line L16.

Proof. Modulo pp, y−1 mod my^{-1} \bmod m is the inverse of y mod py \bmod p. The yy in YY with y>m/2y > m/2 are the numbers from (m+1)/2(m+1)/2 to m−1m - 1 that are prime to pp. Their count is (m−1)/2=p(pk−1−1)/2+(p−1)/2(m - 1)/2 = p (p^{k-1} - 1)/2 + (p - 1)/2. Take the last (p−1)/2(p-1)/2 of them, with residues (p+1)/2,…,p−1(p + 1)/2, \ldots, p - 1; the others form a run of whole blocks of pp consecutive numbers, and each block adds ∑c=1p−1c−1≡0(modp)\sum_{c=1}^{p-1} c^{-1} \equiv 0 \pmod p. So w≡∑c=(p+1)/2p−1c−1(modp)w \equiv \sum_{c = (p+1)/2}^{p-1} c^{-1} \pmod p for every kk. The function check_units raises an error if p∣wp \mid w for a pair of the plan; the extra pairs of line L16 use a prime p∣tp \mid t, which already occurs in the plan. □\square

2. The semigroup of one prime factor

Fix a prime r∣Nr \mid N and let A=R/rRA = R / rR. As RR is a free Z\mathbb{Z}-module, AA is a finite ring that is not 0. As r∤qr \nmid q, qq is a unit of AA, so by Lemma 1(a) τ\tau and all σc(τ)\sigma_c(\tau) are units of AA, and so is uu.

Lemma 5. The powers ζi\zeta^i, 0≤i<m0 \le i < m, are distinct in AA.

Proof. ∏j=1m−1(1−ζj)=m\prod_{j=1}^{m-1} (1 - \zeta^j) = m, the value at x=1x = 1 of (xm−1)/(x−1)=∏j=1m−1(x−ζj)(x^m - 1)/(x - 1) = \prod_{j=1}^{m-1}(x - \zeta^j). If ζi=ζi′\zeta^i = \zeta^{i'} in AA with i≠i′i \ne i', then 1−ζi′−i=01 - \zeta^{i'-i} = 0 in AA, so m=0m = 0 in AA. But r∤mr \nmid m, so mm is a unit of AA, and A≠0A \ne 0. □\square

Lemma 6 (Frobenius). In AA, ur=χ(r)−wrσr(u)u^r = \chi(r)^{-wr} \sigma_r(u).

Proof. For x=∑cijζiωj∈Rx = \sum c_{ij} \zeta^i \omega^j \in R, the multinomial theorem and Fermat's little theorem give xr≡∑cijζriωrj=σrρr(x)(modrR)x^r \equiv \sum c_{ij} \zeta^{ri} \omega^{rj} = \sigma_r \rho_r(x) \pmod{rR}. With Lemma 3(b), ur=σr(χ(r)−wu)=χ(r)−wrσr(u)u^r = \sigma_r(\chi(r)^{-w} u) = \chi(r)^{-wr} \sigma_r(u). □\square

Let Γ\Gamma be the set of integers a≥1a \ge 1 with p∤ap \nmid a for which ua=εσa(u)u^a = \varepsilon \sigma_a(u) in AA for some ε=ζi\varepsilon = \zeta^i. As σa(u)\sigma_a(u) is a unit, ε\varepsilon is unique by Lemma 5; call it ε(a)\varepsilon(a), and let λ(a)=ε(a)a′\lambda(a) = \varepsilon(a)^{a'}, where aa′≡1(modm)a a' \equiv 1 \pmod m.

Lemma 7. (a) 1∈Γ1 \in \Gamma, and if a,b∈Γa, b \in \Gamma then ab∈Γab \in \Gamma and λ(ab)=λ(a)λ(b)\lambda(ab) = \lambda(a)\lambda(b). (b) r∈Γr \in \Gamma with λ(r)=χ(r)−w\lambda(r) = \chi(r)^{-w}. (c) If the check of line L11 passes for the pair, with S≡ζh(modN)S \equiv \zeta^h \pmod N, then N∈ΓN \in \Gamma with λ(N)=ζhN′\lambda(N) = \zeta^{h N'}, NN′≡1(modm)N N' \equiv 1 \pmod m. (d) There is an e≥0e \ge 0 such that for a,b∈Γa, b \in \Gamma with a≡b(modpE)a \equiv b \pmod{p^E} and E≥max⁡(e,k)E \ge \max(e, k), λ(a)=λ(b)\lambda(a) = \lambda(b).

Proof. (a) uab=(ε(a)σa(u))b=ε(a)bσa(ub)=ε(a)bε(b)aσab(u)u^{ab} = (\varepsilon(a) \sigma_a(u))^b = \varepsilon(a)^b \sigma_a(u^b) = \varepsilon(a)^b \varepsilon(b)^a \sigma_{ab}(u), since σa\sigma_a is a ring map of AA with σa(ζi)=ζai\sigma_a(\zeta^i) = \zeta^{ai}. So ε(ab)=ε(a)bε(b)a\varepsilon(ab) = \varepsilon(a)^b \varepsilon(b)^a, and raising to (ab)′(ab)' gives λ(ab)=ε(a)a′ε(b)b′\lambda(ab) = \varepsilon(a)^{a'} \varepsilon(b)^{b'}. (b) Lemma 6 gives ε(r)=χ(r)−wr\varepsilon(r) = \chi(r)^{-wr}. (c) S≡ζhS \equiv \zeta^h modulo NZ[ζ]N \mathbb{Z}[\zeta], so modulo rRrR, and Lemma 3(a) gives uN=ζhσN(u)u^N = \zeta^h \sigma_N(u) in AA. (d) AA is finite, so uu has a finite order pecp^e c with p∤cp \nmid c. Let v=ucv = u^c, of order pep^e. For a∈Γa \in \Gamma, va=ε(a)cσa(v)v^a = \varepsilon(a)^c \sigma_a(v). If a≡b(modpE)a \equiv b \pmod{p^E}, then va=vbv^a = v^b, and σa=σb\sigma_a = \sigma_b as m∣pEm \mid p^E. So ε(a)c=ε(b)c\varepsilon(a)^c = \varepsilon(b)^c, hence ε(a)=ε(b)\varepsilon(a) = \varepsilon(b) by Lemma 5, as cc is invertible modulo mm. Also a′≡b′(modm)a' \equiv b' \pmod m. □\square

Fact F. Let pp be odd, f≥1f \ge 1, and aa an integer with vp(a−1)=fv_p(a - 1) = f. For E≥fE \ge f, the order of aa modulo pEp^E is pE−fp^{E - f}, and the powers of aa modulo pEp^E are exactly the residues ≡1(modpf)\equiv 1 \pmod{p^f}.

Proof. If a=1+pfxa = 1 + p^f x with p∤xp \nmid x, then ap=1+pf+1x+(p2)p2fx2+…≡1+pf+1x(modpf+2)a^p = 1 + p^{f+1} x + \binom p2 p^{2f} x^2 + \ldots \equiv 1 + p^{f+1} x \pmod{p^{f+2}}, as pp is odd. By induction vp(api−1)=f+iv_p(a^{p^i} - 1) = f + i, so the order is pE−fp^{E-f}. The residues ≡1(modpf)\equiv 1 \pmod{p^f} form a group of pE−fp^{E-f} elements that contains the powers of aa. □\square

3. The conditions LpL_p for odd pp

For odd p∣tp \mid t, LpL_p is: vp(rp−1−1)≥vp(Np−1−1)v_p(r^{p-1} - 1) \ge v_p(N^{p-1} - 1) for every prime r∣Nr \mid N. Write f=vp(Np−1−1)≥1f = v_p(N^{p-1} - 1) \ge 1.

Lemma 8 (proving LpL_p). LpL_p holds if Np−1≢1(modp2)N^{p-1} \not\equiv 1 \pmod{p^2}, or if for some prime q∤Nq \nmid N with p∣q−1p \mid q - 1 the check gives S≡ζhS \equiv \zeta^h with p∤hp \nmid h.

Proof. In the first case f=1f = 1, and vp(rp−1−1)≥1v_p(r^{p-1} - 1) \ge 1 by Fermat. In the second case fix r∣Nr \mid N, suppose g=vp(rp−1−1)<fg = v_p(r^{p-1} - 1) < f, and work in AA for this pair. Take E≥max⁡(e,k,f)E \ge \max(e, k, f). By Fact F for b=rp−1b = r^{p-1}, Np−1≡bj(modpE)N^{p-1} \equiv b^j \pmod{p^E} for some j≥0j \ge 0, and as bj≡1(modpf)b^j \equiv 1 \pmod{p^f} and bb has order pf−gp^{f-g} modulo pfp^f, p∣jp \mid j. By Lemma 7, Np−1N^{p-1} and bjb^j lie in Γ\Gamma, and λ(N)p−1=λ(r)(p−1)j=χ(r)−w(p−1)j\lambda(N)^{p-1} = \lambda(r)^{(p-1)j} = \chi(r)^{-w(p-1)j}. The left side is ζhN′(p−1)\zeta^{hN'(p-1)}, of order exactly mm, as p∤hN′(p−1)p \nmid h N' (p-1). The right side is a pp-th power in the group of order mm, so its order divides m/pm/p. This is a contradiction, so g≥fg \ge f. □\square

Lemma 9. Suppose LpL_p holds and every pair (p,q)(p, q) with qq in the plan passes line L11. Then there are integers l(r)l(r), one for each prime r∣Nr \mid N, such that χp,q(r)=χp,q(N)l(r)\chi_{p,q}(r) = \chi_{p,q}(N)^{l(r)} for every odd qq of the plan with p∣q−1p \mid q - 1, where χp,q\chi_{p,q} is the character χ\chi of that pair.

Proof. There are finitely many primes r∣Nr \mid N and pairs (p,q)(p, q); take EE at least f+kf + k and at least the ee of Lemma 7(d) for each of them. By LpL_p and Fact F for a=Np−1a = N^{p-1}, each rr has an l(r)l(r) with rp−1≡N(p−1)l(r)(modpE)r^{p-1} \equiv N^{(p-1) l(r)} \pmod{p^E}, unique modulo pE−fp^{E - f}.

Fix a pair and rr, and work in AA. Both sides lie in Γ\Gamma, so by Lemma 7, χ(r)−w(p−1)=λ(N)(p−1)l(r)\chi(r)^{-w(p-1)} = \lambda(N)^{(p-1) l(r)}. As p−1p - 1 and ww are invertible modulo mm, χ(r)−w=λ(N)l(r)\chi(r)^{-w} = \lambda(N)^{l(r)} and χ(r)=ψl(r)\chi(r) = \psi^{l(r)} with ψ=λ(N)−w−1=ζ−hN′w−1\psi = \lambda(N)^{-w^{-1}} = \zeta^{-hN' w^{-1}}, where w−1w^{-1} is the inverse modulo mm. This ψ\psi depends only on the pair and hh, not on rr.

Now write N=∏rieiN = \prod r_i^{e_i}. Then Np−1=∏ri(p−1)ei≡N(p−1)∑eil(ri)(modpE)N^{p-1} = \prod r_i^{(p-1)e_i} \equiv N^{(p-1) \sum e_i l(r_i)} \pmod{p^E}, and as Np−1N^{p-1} has order pE−fp^{E-f} modulo pEp^E, ∑eil(ri)≡1(modpE−f)\sum e_i l(r_i) \equiv 1 \pmod{p^{E-f}}, so also modulo mm. Hence χ(N)=∏χ(ri)ei=ψ∑eil(ri)=ψ\chi(N) = \prod \chi(r_i)^{e_i} = \psi^{\sum e_i l(r_i)} = \psi, and χ(r)=χ(N)l(r)\chi(r) = \chi(N)^{l(r)}. □\square

4. The case p=2p = 2

Here L2L_2 is: v2(r−1)≥v2(N−1)v_2(r - 1) \ge v_2(N - 1) for every prime r∣Nr \mid N. Let σ=v2(N−1)≥1\sigma = v_2(N - 1) \ge 1. For an odd qq of the plan, the character of order 2=2v2(q−1)2 = 2^{v_2(q-1)} is the Legendre symbol (⋅/q)(\cdot/q), and Eq=(−q)(N−1)/2 mod NE_q = (-q)^{(N-1)/2} \bmod N.

Lemma 10. (a) If some Eq=N−1E_q = N - 1, then L2L_2 holds, and L2L_2 holds if N≡3(mod4)N \equiv 3 \pmod 4. (b) If NN is not a square, (z/N)=−1(z/N) = -1 and z(N−1)/2≡−1(modN)z^{(N-1)/2} \equiv -1 \pmod N, then L2L_2 holds.

Proof. If x(N−1)/2≡−1(modr)x^{(N-1)/2} \equiv -1 \pmod r, the order oo of xx modulo rr divides N−1=2σn′N - 1 = 2^\sigma n' but not (N−1)/2(N - 1)/2, so v2(o)=σv_2(o) = \sigma; as o∣r−1o \mid r - 1, v2(r−1)≥σv_2(r - 1) \ge \sigma. Apply this to x=−qx = -q for (a) and to x=zx = z for (b). If N≡3(mod4)N \equiv 3 \pmod 4, σ=1\sigma = 1, and r−1r - 1 is even. □\square

Lemma 11. Suppose L2L_2 holds and every odd qq of the plan has Eq=±1E_q = \pm 1. Let l(r)=1l(r) = 1 if v2(r−1)=σv_2(r - 1) = \sigma and l(r)=0l(r) = 0 otherwise. Then (r/q)=(N/q)l(r)(r/q) = (N/q)^{l(r)} for every prime r∣Nr \mid N and every odd qq of the plan.

Proof. Let oo be the order of −q-q modulo rr and ρ=v2(r−1)≥σ\rho = v_2(r - 1) \ge \sigma. By Euler's criterion, (−q/r)=−1(-q/r) = -1 exactly when o∤(r−1)/2o \nmid (r-1)/2, that is when v2(o)=ρv_2(o) = \rho. If Eq=−1E_q = -1, v2(o)=σv_2(o) = \sigma as in Lemma 10, so (−q/r)=(−1)l(r)(-q/r) = (-1)^{l(r)}. If Eq=1E_q = 1, o∣(N−1)/2o \mid (N-1)/2, so v2(o)≤σ−1<ρv_2(o) \le \sigma - 1 < \rho and (−q/r)=1(-q/r) = 1. In both cases (−q/r)=Eql(r)(-q/r) = E_q^{l(r)}. By quadratic reciprocity, (−q/r)=(−1)(r−1)/2(q/r)=(−1)(r−1)/2 (1+(q−1)/2)(r/q)=(r/q)(-q/r) = (-1)^{(r-1)/2} (q/r) = (-1)^{(r-1)/2 \, (1 + (q-1)/2)} (r/q) = (r/q), as (q−1)/2(q - 1)/2 is odd. So (r/q)=Eql(r)(r/q) = E_q^{l(r)}.

Write N=∏rieiN = \prod r_i^{e_i}. By L2L_2, ri≡1+2σl(ri)(mod2σ+1)r_i \equiv 1 + 2^\sigma l(r_i) \pmod{2^{\sigma+1}}, and as 2σ≥σ+12\sigma \ge \sigma + 1, N≡1+2σ∑eil(ri)(mod2σ+1)N \equiv 1 + 2^\sigma \sum e_i l(r_i) \pmod{2^{\sigma+1}}. Also N≡1+2σ(mod2σ+1)N \equiv 1 + 2^\sigma \pmod{2^{\sigma+1}}, so ∑eil(ri)\sum e_i l(r_i) is odd, and (N/q)=∏(ri/q)ei=Eq∑eil(ri)=Eq(N/q) = \prod (r_i/q)^{e_i} = E_q^{\sum e_i l(r_i)} = E_q. Hence (r/q)=(N/q)l(r)(r/q) = (N/q)^{l(r)}. □\square

Lemma 12. Suppose NN reaches line L17. Then every prime r∣Nr \mid N has r≡Nl(mods)r \equiv N^l \pmod s for some 0≤l<t0 \le l < t.

Proof. Lines L11 and L14 passed for every pair of the plan. After line L16, done[pp] is true for every prime p∣tp \mid t, each set by line L5, L6, L12, L15 or the extra check, so by Lemmas 8 and 10 every LpL_p holds (for the extra check of an odd pp with a prime qq not in the plan, Lemma 8 applies with that qq, as q∤Nq \nmid N is checked; Lemma 4 gives p∤wp \nmid w). Lemmas 9 and 11 give numbers lp(r)l_p(r) for every p∣tp \mid t. By the Chinese remainder theorem choose 0≤l<t0 \le l < t with l≡lp(r)(modpvp(t))l \equiv l_p(r) \pmod{p^{v_p(t)}} for every p∣tp \mid t.

Fix an odd qq of the plan and pk∥q−1p^k \| q - 1. As pk∣pvp(t)p^k \mid p^{v_p(t)} and χp,q\chi_{p,q} has order pkp^k, χp,q(r)=χp,q(N)l\chi_{p,q}(r) = \chi_{p,q}(N)^l for every p∣q−1p \mid q - 1, so ind⁡gr≡lind⁡gN(modpk)\operatorname{ind}_g r \equiv l \operatorname{ind}_g N \pmod{p^k} for every such pp, hence modulo q−1q - 1, and r≡Nl(modq)r \equiv N^l \pmod q. Also r≡Nl≡1(mod2)r \equiv N^l \equiv 1 \pmod 2. As ss is the product of these distinct primes, r≡Nl(mods)r \equiv N^l \pmod s. □\square

Theorem 1 (prime answers are right). If APR-CL(N)(N) returns prime, NN is prime.

Proof. Line L3 returns prime only for a prime of the plan or a prime factor of tt. If the extra check of line L16 met a prime q∣Nq \mid N, it continued only when N=qN = q, which is prime. Otherwise the answer comes from line L20 and Lemma 12 applies. Suppose NN is composite there, and let rr be its least prime factor, so r≤N<sr \le \sqrt N < s. By Lemma 12, r≡Nl(mods)r \equiv N^l \pmod s with 0≤l<t0 \le l < t. As 1<r<s1 < r < s, r=Nl mod sr = N^l \bmod s, and l≠0l \ne 0 since r≠1r \ne 1. Lines L17 and L18 compute Ni mod sN^i \bmod s for every 1≤i<t1 \le i < t, so at i=li = l or before, line L19 finds a number r′r' with 1<r′<N1 < r' < N and r′∣Nr' \mid N, and returns composite. This contradicts the answer prime. □\square

Theorem 2 (composite answers are right). If APR-CL(N)(N) returns composite, NN is composite.

Proof. We show that a prime NN never reaches a "composite". Line L1: N≥2N \ge 2. Line L4: a prime that is not in line L3 is coprime to tsts. Line L11: in A=R/NRA = R/NR, Lemma 6 and Lemma 3(a) give SσN(u)=uN=χ(N)−wNσN(u)S \sigma_N(u) = u^N = \chi(N)^{-wN} \sigma_N(u), so S≡χ(N)−wNS \equiv \chi(N)^{-wN} modulo NRNR, hence modulo NZ[ζ]N\mathbb{Z}[\zeta], as RR is free over Z[ζ]\mathbb{Z}[\zeta] with a basis that contains 1. Line L14: Euler's criterion gives E=(−q/N)=±1E = (-q/N) = \pm 1. Line L16 for p=2p = 2: a prime is not a square, the Jacobi symbol is the Legendre symbol, (z/N)=0(z/N) = 0 is impossible for z<Nz < N, and z(N−1)/2≡(z/N)=−1z^{(N-1)/2} \equiv (z/N) = -1. Line L16 for odd pp: a prime q∣Nq \mid N means N=qN = q, which returns "proved", not composite, and the check of line L11 passes as above. Line L19: a prime has no factor rr with 1<r<N1 < r < N. □\square

Theorem 3 (termination). APR-CL(N)(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(log⁡N)O(\log N) steps, and the search for hh at line L11 tries mm values. In the extra check for p=2p = 2, a non-square N=∏pieiN = \prod p_i^{e_i} has some eie_i odd; by the Chinese remainder theorem there is a z<Nz < N that is a non-residue modulo pip_i and ≡1\equiv 1 modulo the other prime powers, and (z/N)=−1(z/N) = -1. So the loop over zz stops at the latest at the least such zz. The extra check for odd pp stops after 60 primes qq that it tests. The search of lines L17 to L19 has t−1t - 1 steps. So every run stops, with prime, composite, or no answer from the cap of line L16. □\square

■\blacksquare

Back to APR-CL primality test