These are the human proofs of Lucas-Lehmer test, at full length.
The Lucas-Lehmer test returns true exactly when 2^p - 1 is prime full
This proof shows: correctness, termination.
We use the pseudocode LUCAS-LEHMER(p) of the page, for an integer p≥2, with Mp=2p−1. Define the integers s0=4 and si+1=si2−2.
Termination. The loop of line L4 runs p−2 times, and each pass does one squaring and one reduction. So the call ends.
Lemma 1 (the loop computes sp−2). For p≥3, after the i-th pass of line L5, s≡si(modMp). So line L6 returns true if and only if Mp∣sp−2.
Proof. Before the loop s=4=s0, and 4<Mp as p≥3. If s≡si, then s2−2≡si2−2=si+1, and line L5 keeps the residue. After p−2 passes, s is the residue of sp−2 in [0,Mp), which is 0 if and only if Mp∣sp−2. □
The ring. For an odd prime q, let Rq=Zq[x]/(x2−3), and write 3 for the class of x. Its elements are a+b3 with a,b∈Zq, so ∣Rq∣=q2, and the units of Rq number at most q2−1, since 0 is not a unit. Put ω=2+3 and ωˉ=2−3. Then ωωˉ=4−3=1, so both are units and ωˉ=ω−1.
Lemma 2 (closed form). In Rq, the image of si is ω2i+ωˉ2i for each i≥0.
Proof. For i=0, ω+ωˉ=4. If the claim holds for i, then si+1=si2−2=ω2i+1+2(ωωˉ)2i+ωˉ2i+1−2=ω2i+1+ωˉ2i+1. □
Lemma 3 (if Mp∣sp−2, then Mp is prime). Let p≥3 and Mp∣sp−2. Then Mp is prime.
Proof. Suppose Mp is composite, and let q be its smallest prime factor. Then q2≤Mp, and q is odd since Mp is odd. As q∣sp−2, Lemma 2 gives ω2p−2+ωˉ2p−2=0 in Rq. Multiplying by ω2p−2 and using ωωˉ=1 gives ω2p−1+1=0, so ω2p−1=−1 and ω2p=1. Since q is odd, −1=1 in Rq, so the order of ω in the unit group divides 2p but not 2p−1, and it is 2p. The order of an element is at most the size of the group, so 2p≤q2−1≤Mp−1=2p−2, which is false. □
Lemma 4 (residue facts). Let p≥3 and let q=Mp be prime. Then p is odd, q≡7(mod8) and q≡1(mod3). Hence 2 is a quadratic residue modulo q and 3 is not.
Proof. If p were even, 3=22−1 would divide 2p−1, and q>3; so p is odd. As p≥3, 2p≡0(mod8), so q≡−1≡7(mod8). As p is odd, 2p≡(−1)p=−1≡2(mod3), so q≡1(mod3). By the second supplement of quadratic reciprocity, 2 is a residue modulo q because q≡±1(mod8). Since q≡3(mod4) and 3≡3(mod4), quadratic reciprocity gives (q3)=−(3q)=−(31)=−1. □
Lemma 5 (if Mp is prime, then Mp∣sp−2). Let p≥3 and let q=Mp be prime. Then q∣sp−2.
Proof. By Lemma 4, x2−3 has no root modulo q, so it is irreducible over Zq and Rq is a field with q2 elements. In it, u↦uq is a ring homomorphism (the binomial coefficients (jq) with 0<j<q vanish), it fixes Zq by Fermat's little theorem, and (3)q=3(q−1)/23=−3 by Euler's criterion and Lemma 4. So (a+b3)q=a−b3.
Let τ=1+3. Then τ2=4+23=2ω, and τq+1=τ⋅τq=(1+3)(1−3)=−2. On the other hand, τq+1=(τ2)(q+1)/2=2(q+1)/2ω(q+1)/2, and 2(q+1)/2=2⋅2(q−1)/2=2 by Euler's criterion, since 2 is a residue. Hence 2ω(q+1)/2=−2, and as 2 is invertible, ω(q+1)/2=−1. Here (q+1)/2=2p−1. So ω2p−1=−1, and
sp−2=ω2p−2+ωˉ2p−2=ωˉ2p−2(ω2p−1+1)=0
in Rq, using ω2p−2=ωˉ2p−2ω2p−1. As sp−2 is an integer whose image in Rq is 0, q∣sp−2. □
Theorem. For each integer p≥2, LUCAS-LEHMER(p) returns true if and only if Mp is prime.
Proof. For p=2, line L1 returns true, and M2=3 is prime. For p≥3, by Lemma 1 the call returns true if and only if Mp∣sp−2. By Lemma 3 this implies that Mp is prime, and by Lemma 5 it follows from it. ■
Remark. Lemma 3 does not use that p is prime. So for a composite p≥4 the test returns false, which is right because Mp is then composite.