Algorithm Search⌘K

Proofs: Pepin's test

These are the human proofs of Pepin's test, at full length.

Pépin's test returns true exactly when 2^(2^k) + 1 is prime full

This proof shows: correctness, termination.

We use the pseudocode PEPIN(k)(k) of the page, for an integer k≥0k \ge 0, with F=Fk=22k+1F = F_k = 2^{2^k} + 1 and e=2ke = 2^k. So F−1=2eF - 1 = 2^e and (F−1)/2=2e−1(F - 1)/2 = 2^{e-1}.

Termination. The loop of line L4 runs e−1e - 1 times, and each pass does one squaring and one reduction. The reduction ends (Lemma 1). So the call ends.

Lemma 1 (the reduction). Let x≥0x \ge 0 and write x=h⋅2e+ℓx = h \cdot 2^e + \ell with 0≤ℓ<2e0 \le \ell < 2^e. Then x≡ℓ−h(modF)x \equiv \ell - h \pmod F. If moreover x≤(F−1)2x \le (F - 1)^2, then −F<ℓ−h<F-F < \ell - h < F, so ℓ−h\ell - h, plus FF if it is negative, is x mod Fx \bmod F.

Proof. 2e=F−1≡−1(modF)2^e = F - 1 \equiv -1 \pmod F, so h⋅2e+ℓ≡ℓ−hh \cdot 2^e + \ell \equiv \ell - h. If x≤(F−1)2=22ex \le (F - 1)^2 = 2^{2e}, then h=⌊x/2e⌋≤2e=F−1h = \lfloor x / 2^e \rfloor \le 2^e = F - 1, so ℓ−h≥−(F−1)>−F\ell - h \ge -(F - 1) > -F. Also ℓ−h≤ℓ<2e<F\ell - h \le \ell < 2^e < F. So ℓ−h\ell - h lies in (−F,F)(-F, F) and is congruent to xx, and adding FF when it is negative gives the residue in [0,F)[0, F). □\square

Lemma 2 (the loop invariant). For k≥1k \ge 1, after the ii-th pass of line L5, 0≤x<F0 \le x < F and x≡32i(modF)x \equiv 3^{2^i} \pmod F. So after the loop, x=3(F−1)/2 mod Fx = 3^{(F-1)/2} \bmod F.

Proof. Before the loop, x=3=320x = 3 = 3^{2^0} and 3<F3 < F since F≥5F \ge 5. If x≡32ix \equiv 3^{2^i} with 0≤x<F0 \le x < F, then x2≡32i+1x^2 \equiv 3^{2^{i+1}} and x2≤(F−1)2x^2 \le (F - 1)^2, so by Lemma 1 line L5 stores its residue in [0,F)[0, F). After e−1e - 1 passes, x≡32e−1=3(F−1)/2x \equiv 3^{2^{e-1}} = 3^{(F-1)/2}. □\square

Lemma 3 (the congruence implies primality). Let k≥1k \ge 1. If 3(F−1)/2≡−1(modF)3^{(F-1)/2} \equiv -1 \pmod F, then FF is prime.

Proof. Let qq be a prime factor of FF. Reducing modulo qq gives 3(F−1)/2≡−1(modq)3^{(F-1)/2} \equiv -1 \pmod q. If q=3q = 3, the left side is 0 and the right side is 2 modulo 3, which is impossible; so q≠3q \ne 3 and 3 is a unit modulo qq. Squaring gives 3F−1≡1(modq)3^{F-1} \equiv 1 \pmod q. Let dd be the multiplicative order of 3 modulo qq. Then d∣F−1=2ed \mid F - 1 = 2^e, so dd is a power of 2. If d∣(F−1)/2=2e−1d \mid (F - 1)/2 = 2^{e-1}, then 3(F−1)/2≡1(modq)3^{(F-1)/2} \equiv 1 \pmod q, so 1≡−11 \equiv -1 and q=2q = 2, but FF is odd. Hence d=2e=F−1d = 2^e = F - 1. By Fermat's little theorem, d∣q−1d \mid q - 1, so q≥Fq \ge F. As q∣Fq \mid F, q=Fq = F, and FF is prime. □\square

Lemma 4 (primality implies the congruence). Let k≥1k \ge 1. If FF is prime, then 3(F−1)/2≡−1(modF)3^{(F-1)/2} \equiv -1 \pmod F.

Proof. As k≥1k \ge 1, e≥2e \ge 2, so 4∣2e4 \mid 2^e and F≡1(mod4)F \equiv 1 \pmod 4. As ee is even, 2e=4e/2≡1(mod3)2^e = 4^{e/2} \equiv 1 \pmod 3, so F≡2(mod3)F \equiv 2 \pmod 3; in particular F≠3F \ne 3, and FF is an odd prime with 3∤F3 \nmid F. By the law of quadratic reciprocity, since F≡1(mod4)F \equiv 1 \pmod 4,

(3F)=(F3)=(23)=−1,\left(\frac{3}{F}\right) = \left(\frac{F}{3}\right) = \left(\frac{2}{3}\right) = -1,

the last because the squares modulo 3 are 0 and 1. By Euler's criterion, 3(F−1)/2≡(3F)=−1(modF)3^{(F-1)/2} \equiv \left(\frac{3}{F}\right) = -1 \pmod F. □\square

Theorem. For each integer k≥0k \ge 0, PEPIN(k)(k) returns true if and only if FkF_k is prime.

Proof. If k=0k = 0, then F0=3F_0 = 3 is prime, and line L1 returns true. Let k≥1k \ge 1. By Lemma 2, line L6 returns true if and only if 3(F−1)/2 mod F=F−13^{(F-1)/2} \bmod F = F - 1, that is, 3(F−1)/2≡−1(modF)3^{(F-1)/2} \equiv -1 \pmod F. By Lemmas 3 and 4, this holds if and only if FF is prime. ■\blacksquare

Back to Pepin's test