Miller-Rabin never rejects a prime, and accepts an odd composite with probability below 4^-k full
This proof shows: correctness, termination, error.
We use the pseudocode MILLER-RABIN(n,k) of the page. For odd n≥3 write n−1=2sd with d odd. A base a∈[1,n−1] is a strong liar for n if ad≡1(modn) or a2jd≡−1(modn) for some 0≤j<s; S(n) is the set of strong liars. φ is Euler's totient function, and v2(m) is the exponent of 2 in m.
Termination. Line L3 halves n−1 at most lgn times. The loop of line L4 runs at most k times, and each pass does one exponentiation and at most s−1 squarings. So the call ends.
Lemma 1 (a round passes exactly for strong liars). Let n≥5 be odd and a∈[2,n−2]. The round of lines L5 to L11 with the base a ends at line L7 or L10 if and only if a∈S(n), and otherwise it returns false at line L11.
Proof. After line L6, x=admodn, and after the j-th pass of line L9, x=a2jdmodn, by induction on j. Line L7 passes exactly when ad≡±1, which covers the cases ad≡1 and j=0. Line L10 passes in pass j exactly when a2jd≡−1 for some 1≤j≤s−1. So the round passes if and only if one of the defining conditions of a strong liar holds. □
Lemma 2 (no nontrivial square roots of 1 modulo a prime). If p is prime and x2≡1(modp), then x≡±1(modp).
Proof. p∣x2−1=(x−1)(x+1), so p divides one of the factors. □
Lemma 3 (a prime is accepted). If n is prime, MILLER-RABIN(n,k) returns true for every choice of the bases.
Proof. For n∈{2,3} line L1 returns true. Otherwise n≥5 is odd, and line L2 does not return. Let a∈[2,n−2], so n∤a. Put xj=a2jdmodn for 0≤j≤s. By Fermat's little theorem xs=1. Let t be the least index with xt=1. If t=0, then ad≡1 and a∈S(n). If t>0, then xt−12=xt=1 and xt−1=1, so xt−1≡−1 by Lemma 2, with 0≤t−1<s, and a∈S(n). By Lemma 1 every round passes, and line L12 returns true. □
Lemma 4 (false is right). If MILLER-RABIN(n,k) returns false, then n is not prime.
Proof. Line L1 returns false only for n∈{0,1}. Line L2 returns false for even n≥4. Line L11 returns false only when a round fails, which by Lemma 3 does not happen for a prime. □
Lemma 5 (counting in a cyclic group). Let G be a cyclic group of order N and m≥1. Then xm=1 has gcd(m,N) solutions in G. If N is even and −1 denotes the unique element of order 2, then xm=−1 has a solution if and only if v2(m)<v2(N), and then it has gcd(m,N) solutions.
Proof. Let g generate G and x=gt, 0≤t<N. Then xm=1 if and only if N∣tm, which has gcd(m,N) solutions t. Next −1=gN/2, and xm=−1 if and only if tm≡N/2(modN). With c=gcd(m,N) this is solvable if and only if c∣N/2, with c solutions. As v2(c)=min(v2(m),v2(N)), c∣N/2 holds if and only if v2(m)<v2(N). □
Lemma 6 (Monier's count). Let n=∏i=1rpiei be odd and composite with distinct primes pi, vi=v2(pi−1) and ν=minivi. Then
∣S(n)∣=(1+j=0∑ν−12jr)i=1∏rgcd(d,pi−1). Proof. Each (Z/pieiZ)∗ is cyclic of order Ni=piei−1(pi−1), with v2(Ni)=vi. For m∣n−1 we have pi∤m, so gcd(m,Ni)=gcd(m,pi−1). By the Chinese remainder theorem the number of solutions of am≡±1(modn) is the product of the numbers of solutions modulo the piei. By Lemma 5, ad≡1 has T=∏igcd(d,pi−1) solutions. For 0≤j<s, a2jd≡−1 has ∏i2jgcd(d,pi−1)=2jrT solutions if j<ν, since then j<vi and gcd(2jd,pi−1)=2jgcd(d,pi−1); it has none if j≥ν. As every pi≡1(mod2ν), n≡1(mod2ν), so ν≤s. The cases are disjoint: if a2jd≡−1, then a2j′d≡1 for j′>j, and if ad≡1 then no power a2jd is −1. Adding the counts gives the formula. □
Lemma 7 (Monier-Rabin). If n>9 is odd and composite, then ∣S(n)∣≤φ(n)/4.
Proof. Keep the notation of Lemma 6. Since d is odd, gcd(d,pi−1) divides the odd part (pi−1)/2vi. With φ(n)=∏ipiei−1(pi−1) this gives
φ(n)∣S(n)∣≤2v1+⋯+vr1+∑j<ν2jr⋅∏ipiei−11,(1) with equality in the first factor when each gcd(d,pi−1) is the whole odd part. Since ∑ivi≥rν, ν≥1 and ∑j<ν2jr=(2rν−1)/(2r−1),
2rν1+∑j<ν2jr=2r−11+2−rν2r−12r−2≤2r−11+2−r2r−12r−2=21−r.(2) Case r≥3. By (1) and (2), ∣S(n)∣/φ(n)≤21−r≤1/4.
Case r=2, n not squarefree. The last factor in (1) is at most 1/3, so ∣S(n)∣/φ(n)≤21⋅31<41.
Case n=pq with primes p<q. If v1+v2>2ν, the first factor in (1) is at most half of the left side of (2), so ∣S(n)∣/φ(n)≤21⋅21. If v1=v2=ν, let q′=(q−1)/2ν be the odd part of q−1. If q′∣d, then q−1=2νq′ divides 2sd=n−1, because ν≤s. But n−1=p(q−1)+(p−1), so then q−1∣p−1, which is false as 0<p−1<q−1. So gcd(d,q−1) is a proper divisor of the odd number q′, hence at most q′/3, and the bound (1) improves by the factor 1/3: ∣S(n)∣/φ(n)≤31⋅21<41.
Case r=1, n=pe with e≥2. Lemma 6 gives ∣S(n)∣=2νgcd(d,p−1)≤2ν(p−1)/2ν=p−1, so ∣S(n)∣/φ(n)≤p1−e. This is at most 1/5 if p≥5, and at most 1/9 if p=3 and e≥3. The remaining case n=9 is excluded. □
Theorem (error). Let n≥5 be odd and composite. Then MILLER-RABIN(n,k) returns true with probability exactly qk, where q=(∣S(n)∣−2)/(n−3), and q<1/4. So the probability is less than 4−k.
Proof. Lines L1 and L2 do not return. By Lemma 1 the call returns true if and only if each of the k bases drawn in line L5 is in S(n). The bases are independent and uniform on the n−3 integers of [2,n−2]. The liars 1 and n−1 are not in this range, and they are distinct, so ∣S(n)∣−2 of the n−3 bases are liars, and each round passes with probability q. For n=9, S(9)={1,8}, so q=0. For n>9, Lemma 7 and φ(n)≤n−1 give
q≤n−3(n−1)/4−2=4(n−3)n−9<41. The k rounds are independent, so the probability of true is qk<4−k. ■
Remark. The probability is over the random bases only. It holds for every odd composite n, with no assumption on how n was chosen. Together with Lemma 3 this shows that the test has one-sided error: a wrong answer is always true, on a composite input.