These are the human proofs of Baillie-PSW primality test, at full length.
Baillie-PSW never rejects a prime and always terminates full
This proof shows: correctness, termination.
We use the pseudocode BAILLIE-PSW(n) of the page. The claim proved here is the part of correctness that is known: the answer false is always right, and the call ends. That no composite returns true is an open problem, checked by computer for n<264 only.
Lemma 1 (the doubling formulas). Let α,β be the roots of x2−Px+Q in a commutative ring where α−β is a unit, with D=(α−β)2=P2−4Q. Then Uk=(αk−βk)/(α−β) and Vk=αk+βk, and
The same identities hold for the integer sequences, and hence modulo n.
Proof. Both closed forms satisfy the recurrence Xk+1=PXk−QXk−1, since α and β are roots of x2−Px+Q, and they agree with U0=0, U1=1, V0=2, V1=P. Then UkVk=(α2k−β2k)/(α−β)=U2k, and Vk2=α2k+β2k+2(αβ)k=V2k+2Qk. With α+β=P: PUk+Vk=((α+β)(αk−βk)+(α−β)(αk+βk))/(α−β)=2(αk+1−βk+1)/(α−β)=2Uk+1, and DUk+PVk=(α−β)(αk−βk)+(α+β)(αk+βk)=2Vk+1. For the integer sequences, take α,β in the complex numbers: they are distinct since D=0 (each D of the page is ≡1(mod4)), so the identities hold for the integers Uk and Vk, and reducing them gives them modulo n. In any ring as in the statement, the reductions of the integer sequences satisfy the same recurrence and initial values as the closed forms, so they are equal to them. □
So line L11, which reads the binary digits of d and applies the doubling step and, for a digit 1, the step k→k+1 with the halving done by the inverse of 2 modulo the odd n, computes Ud, Vd and Qd modulo n. Lines L13 to L15 compute V2rd for r=1,…,s−1, with W=Q2r−1d before each pass.
Lemma 2 (primes pass the strong test). If n is an odd prime, n passes the strong test of line L3.
Proof. Write n−1=2te with e odd. By Fermat's little theorem 22te≡1(modn). Modulo a prime the only square roots of 1 are ±1. So in the sequence 2e,22e,…,22te, either the first term is 1, or the term before the first 1 is −1, with index r<t. □
Lemma 3 (primes pass the strong Lucas test). Let n be an odd prime, let D be an integer with (nD)=−1, P=1 and Q=(1−D)/4. Write n+1=2sd with d odd. Then Ud≡0, or V2rd≡0(modn) for some 0≤r<s.
Proof. Since D is a quadratic non-residue modulo n, Fn(D)=Fn[x]/(x2−D) is a field with n2 elements. Also n∤Q: if n∣Q, then D=1−4Q≡1, a square. In this field put α=(1+D)/2 and β=(1−D)/2, the roots of x2−x+Q; then α−β=D=0 and αβ=Q=0. The map z↦zn is a ring map of the field that fixes Fn, and by Euler's criterion (D)n=D(n−1)/2D=−D. So αn=β and αn+1=αβ=Q; likewise βn+1=Q. Let γ=α/β. Then γn+1=1. By Lemma 1, Uk≡0(modn) if and only if αk=βk, that is γk=1; and Vk≡0 if and only if αk=−βk, that is γk=−1. In the sequence γd,γ2d,…,γ2sd=1, either the first term is 1, so Ud≡0, or there is a first index r+1≤s with γ2r+1d=1; then γ2rd is a square root of 1 in a field other than 1, so it is −1 and V2rd≡0, with r<s. □
Lemma 4 (the search for D ends). Let n≥3 be odd and not a perfect square. Then some D in the sequence 5,−7,9,−11,… has (nD)=−1.
Proof. The sequence consists of all integers D≡1(mod4) with ∣D∣≥5. For such D and odd m=∣D∣, the Jacobi symbol satisfies (nD)=(mn): for D=m>0 with m≡1(mod4) this is quadratic reciprocity; for D=−m with m≡3(mod4), (n−m)=(n−1)(nm)=(−1)(n−1)/2⋅(−1)(n−1)/2(mn)=(mn). So it is enough to find an odd m≥5 with (mn)=−1. As n is not a square, some prime q divides n to an odd power qa; write n=qan′ with q∤n′. Take a quadratic non-residue c modulo q, and by the Chinese remainder theorem an m≥5 with m≡c(modq) and m≡1(mod4n′). Then m is odd, m≡1(mod4), and by reciprocity (mn)=(nm)=(qm)a(n′m)=(−1)a⋅1=−1. □
Theorem. For every integer n, BAILLIE-PSW(n) terminates, and if n is prime it returns true. Equivalently, every answer false is right.
Proof.Termination. Lines L1 to L4 end. If the call reaches line L5, then n>97 is odd and not a square, so by Lemma 4 the loop of lines L6 to L8 meets a D with (nD)=−1 after finitely many steps, unless it returns earlier at line L7. Lines L10 to L16 run a bounded number of steps.
Primes return true. Let n be prime. If n≤97, line L2 returns true at p=n, and no smaller prime divides n. If n>97, no prime p≤97 divides n, so line L2 passes. By Lemma 2, line L3 passes. A prime is not a square, so line L4 passes. In the loop, (nD)=0 means n∣D, so ∣D∣≥n and line L7 does not return. So the loop ends with (nD)=−1, and by Lemma 1 lines L11 to L15 compute Ud and V2rd modulo n for r<s. By Lemma 3 one of them is 0, so line L12 or line L15 returns true, and line L16 is not reached.
False is right. Line L1 returns false only for n<2; line L2 only when a prime p=n divides n; line L4 only for a square n>1; line L7 only when 1<gcd(∣D∣,n)≤∣D∣<n. Lines L3 and L16 are not reached by primes, by the argument above. ■