Algorithm Search⌘K

Proofs: Baillie-PSW primality test

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)(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<264n < 2^{64} only.

Lemma 1 (the doubling formulas). Let α,β\alpha, \beta be the roots of x2−Px+Qx^2 - Px + Q in a commutative ring where α−β\alpha - \beta is a unit, with D=(α−β)2=P2−4QD = (\alpha - \beta)^2 = P^2 - 4Q. Then Uk=(αk−βk)/(α−β)U_k = (\alpha^k - \beta^k)/(\alpha - \beta) and Vk=αk+βkV_k = \alpha^k + \beta^k, and

U2k=UkVk,V2k=Vk2−2Qk,2Uk+1=PUk+Vk,2Vk+1=DUk+PVk.U_{2k} = U_k V_k, \quad V_{2k} = V_k^2 - 2Q^k, \quad 2U_{k+1} = P U_k + V_k, \quad 2V_{k+1} = D U_k + P V_k.

The same identities hold for the integer sequences, and hence modulo nn.

Proof. Both closed forms satisfy the recurrence Xk+1=PXk−QXk−1X_{k+1} = P X_k - Q X_{k-1}, since α\alpha and β\beta are roots of x2−Px+Qx^2 - Px + Q, and they agree with U0=0U_0 = 0, U1=1U_1 = 1, V0=2V_0 = 2, V1=PV_1 = P. Then UkVk=(α2k−β2k)/(α−β)=U2kU_k V_k = (\alpha^{2k} - \beta^{2k})/(\alpha - \beta) = U_{2k}, and Vk2=α2k+β2k+2(αβ)k=V2k+2QkV_k^2 = \alpha^{2k} + \beta^{2k} + 2(\alpha\beta)^k = V_{2k} + 2Q^k. With α+β=P\alpha + \beta = P: PUk+Vk=((α+β)(αk−βk)+(α−β)(αk+βk))/(α−β)=2(αk+1−βk+1)/(α−β)=2Uk+1P U_k + V_k = ((\alpha + \beta)(\alpha^k - \beta^k) + (\alpha - \beta)(\alpha^k + \beta^k))/(\alpha - \beta) = 2(\alpha^{k+1} - \beta^{k+1})/(\alpha - \beta) = 2U_{k+1}, and DUk+PVk=(α−β)(αk−βk)+(α+β)(αk+βk)=2Vk+1D U_k + P V_k = (\alpha - \beta)(\alpha^k - \beta^k) + (\alpha + \beta)(\alpha^k + \beta^k) = 2V_{k+1}. For the integer sequences, take α,β\alpha, \beta in the complex numbers: they are distinct since D≠0D \ne 0 (each DD of the page is ≡1(mod4)\equiv 1 \pmod 4), so the identities hold for the integers UkU_k and VkV_k, and reducing them gives them modulo nn. 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. □\square

So line L11, which reads the binary digits of dd and applies the doubling step and, for a digit 1, the step k→k+1k \to k + 1 with the halving done by the inverse of 2 modulo the odd nn, computes UdU_d, VdV_d and QdQ^d modulo nn. Lines L13 to L15 compute V2rdV_{2^r d} for r=1,…,s−1r = 1, \ldots, s - 1, with W=Q2r−1dW = Q^{2^{r-1} d} before each pass.

Lemma 2 (primes pass the strong test). If nn is an odd prime, nn passes the strong test of line L3.

Proof. Write n−1=2ten - 1 = 2^t e with ee odd. By Fermat's little theorem 22te≡1(modn)2^{2^t e} \equiv 1 \pmod n. Modulo a prime the only square roots of 1 are ±1\pm 1. So in the sequence 2e,22e,…,22te2^e, 2^{2e}, \ldots, 2^{2^t e}, either the first term is 1, or the term before the first 1 is −1-1, with index r<tr < t. □\square

Lemma 3 (primes pass the strong Lucas test). Let nn be an odd prime, let DD be an integer with (Dn)=−1\left(\frac{D}{n}\right) = -1, P=1P = 1 and Q=(1−D)/4Q = (1 - D)/4. Write n+1=2sdn + 1 = 2^s d with dd odd. Then Ud≡0U_d \equiv 0, or V2rd≡0(modn)V_{2^r d} \equiv 0 \pmod n for some 0≤r<s0 \le r < s.

Proof. Since DD is a quadratic non-residue modulo nn, Fn(D)=Fn[x]/(x2−D)\mathbb{F}_n(\sqrt D) = \mathbb{F}_n[x]/(x^2 - D) is a field with n2n^2 elements. Also n∤Qn \nmid Q: if n∣Qn \mid Q, then D=1−4Q≡1D = 1 - 4Q \equiv 1, a square. In this field put α=(1+D)/2\alpha = (1 + \sqrt D)/2 and β=(1−D)/2\beta = (1 - \sqrt D)/2, the roots of x2−x+Qx^2 - x + Q; then α−β=D≠0\alpha - \beta = \sqrt D \ne 0 and αβ=Q≠0\alpha \beta = Q \ne 0. The map z↦znz \mapsto z^n is a ring map of the field that fixes Fn\mathbb{F}_n, and by Euler's criterion (D)n=D(n−1)/2D=−D(\sqrt D)^n = D^{(n-1)/2} \sqrt D = -\sqrt D. So αn=β\alpha^n = \beta and αn+1=αβ=Q\alpha^{n+1} = \alpha \beta = Q; likewise βn+1=Q\beta^{n+1} = Q. Let γ=α/β\gamma = \alpha / \beta. Then γn+1=1\gamma^{n+1} = 1. By Lemma 1, Uk≡0(modn)U_k \equiv 0 \pmod n if and only if αk=βk\alpha^k = \beta^k, that is γk=1\gamma^k = 1; and Vk≡0V_k \equiv 0 if and only if αk=−βk\alpha^k = -\beta^k, that is γk=−1\gamma^k = -1. In the sequence γd,γ2d,…,γ2sd=1\gamma^d, \gamma^{2d}, \ldots, \gamma^{2^s d} = 1, either the first term is 1, so Ud≡0U_d \equiv 0, or there is a first index r+1≤sr + 1 \le s with γ2r+1d=1\gamma^{2^{r+1} d} = 1; then γ2rd\gamma^{2^r d} is a square root of 1 in a field other than 1, so it is −1-1 and V2rd≡0V_{2^r d} \equiv 0, with r<sr < s. □\square

Lemma 4 (the search for DD ends). Let n≥3n \ge 3 be odd and not a perfect square. Then some DD in the sequence 5,−7,9,−11,…5, -7, 9, -11, \ldots has (Dn)=−1\left(\frac{D}{n}\right) = -1.

Proof. The sequence consists of all integers D≡1(mod4)D \equiv 1 \pmod 4 with ∣D∣≥5|D| \ge 5. For such DD and odd m=∣D∣m = |D|, the Jacobi symbol satisfies (Dn)=(nm)\left(\frac{D}{n}\right) = \left(\frac{n}{m}\right): for D=m>0D = m > 0 with m≡1(mod4)m \equiv 1 \pmod 4 this is quadratic reciprocity; for D=−mD = -m with m≡3(mod4)m \equiv 3 \pmod 4, (−mn)=(−1n)(mn)=(−1)(n−1)/2⋅(−1)(n−1)/2(nm)=(nm)\left(\frac{-m}{n}\right) = \left(\frac{-1}{n}\right)\left(\frac{m}{n}\right) = (-1)^{(n-1)/2} \cdot (-1)^{(n-1)/2} \left(\frac{n}{m}\right) = \left(\frac{n}{m}\right). So it is enough to find an odd m≥5m \ge 5 with (nm)=−1\left(\frac{n}{m}\right) = -1. As nn is not a square, some prime qq divides nn to an odd power qaq^a; write n=qan′n = q^a n' with q∤n′q \nmid n'. Take a quadratic non-residue cc modulo qq, and by the Chinese remainder theorem an m≥5m \ge 5 with m≡c(modq)m \equiv c \pmod q and m≡1(mod4n′)m \equiv 1 \pmod{4n'}. Then mm is odd, m≡1(mod4)m \equiv 1 \pmod 4, and by reciprocity (nm)=(mn)=(mq)a(mn′)=(−1)a⋅1=−1\left(\frac{n}{m}\right) = \left(\frac{m}{n}\right) = \left(\frac{m}{q}\right)^a \left(\frac{m}{n'}\right) = (-1)^a \cdot 1 = -1. □\square

Theorem. For every integer nn, BAILLIE-PSW(n)(n) terminates, and if nn 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>97n > 97 is odd and not a square, so by Lemma 4 the loop of lines L6 to L8 meets a DD with (Dn)=−1\left(\frac{D}{n}\right) = -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 nn be prime. If n≤97n \le 97, line L2 returns true at p=np = n, and no smaller prime divides nn. If n>97n > 97, no prime p≤97p \le 97 divides nn, so line L2 passes. By Lemma 2, line L3 passes. A prime is not a square, so line L4 passes. In the loop, (Dn)=0\left(\frac{D}{n}\right) = 0 means n∣Dn \mid D, so ∣D∣≥n|D| \ge n and line L7 does not return. So the loop ends with (Dn)=−1\left(\frac{D}{n}\right) = -1, and by Lemma 1 lines L11 to L15 compute UdU_d and V2rdV_{2^r d} modulo nn for r<sr < 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<2n < 2; line L2 only when a prime p≠np \ne n divides nn; line L4 only for a square n>1n > 1; line L7 only when 1<gcd⁡(∣D∣,n)≤∣D∣<n1 < \gcd(|D|, n) \le |D| < n. Lines L3 and L16 are not reached by primes, by the argument above. ■\blacksquare

Back to Baillie-PSW primality test