Algorithm Search⌘K

Proofs: Miller-Rabin primality test

These are the human proofs of Miller-Rabin primality test, at full length.

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)(n, k) of the page. For odd n≥3n \ge 3 write n−1=2sdn - 1 = 2^s d with dd odd. A base a∈[1,n−1]a \in [1, n - 1] is a strong liar for nn if ad≡1(modn)a^d \equiv 1 \pmod n or a2jd≡−1(modn)a^{2^j d} \equiv -1 \pmod n for some 0≤j<s0 \le j < s; S(n)S(n) is the set of strong liars. φ\varphi is Euler's totient function, and v2(m)v_2(m) is the exponent of 2 in mm.

Termination. Line L3 halves n−1n - 1 at most lg⁡n\lg n times. The loop of line L4 runs at most kk times, and each pass does one exponentiation and at most s−1s - 1 squarings. So the call ends.

Lemma 1 (a round passes exactly for strong liars). Let n≥5n \ge 5 be odd and a∈[2,n−2]a \in [2, n - 2]. The round of lines L5 to L11 with the base aa ends at line L7 or L10 if and only if a∈S(n)a \in S(n), and otherwise it returns false at line L11.

Proof. After line L6, x=ad mod nx = a^d \bmod n, and after the jj-th pass of line L9, x=a2jd mod nx = a^{2^j d} \bmod n, by induction on jj. Line L7 passes exactly when ad≡±1a^d \equiv \pm 1, which covers the cases ad≡1a^d \equiv 1 and j=0j = 0. Line L10 passes in pass jj exactly when a2jd≡−1a^{2^j d} \equiv -1 for some 1≤j≤s−11 \le j \le s - 1. So the round passes if and only if one of the defining conditions of a strong liar holds. □\square

Lemma 2 (no nontrivial square roots of 1 modulo a prime). If pp is prime and x2≡1(modp)x^2 \equiv 1 \pmod p, then x≡±1(modp)x \equiv \pm 1 \pmod p.

Proof. p∣x2−1=(x−1)(x+1)p \mid x^2 - 1 = (x - 1)(x + 1), so pp divides one of the factors. □\square

Lemma 3 (a prime is accepted). If nn is prime, MILLER-RABIN(n,k)(n, k) returns true for every choice of the bases.

Proof. For n∈{2,3}n \in \{2, 3\} line L1 returns true. Otherwise n≥5n \ge 5 is odd, and line L2 does not return. Let a∈[2,n−2]a \in [2, n - 2], so n∤an \nmid a. Put xj=a2jd mod nx_j = a^{2^j d} \bmod n for 0≤j≤s0 \le j \le s. By Fermat's little theorem xs=1x_s = 1. Let tt be the least index with xt=1x_t = 1. If t=0t = 0, then ad≡1a^d \equiv 1 and a∈S(n)a \in S(n). If t>0t > 0, then xt−12=xt=1x_{t-1}^2 = x_t = 1 and xt−1≠1x_{t-1} \ne 1, so xt−1≡−1x_{t-1} \equiv -1 by Lemma 2, with 0≤t−1<s0 \le t - 1 < s, and a∈S(n)a \in S(n). By Lemma 1 every round passes, and line L12 returns true. □\square

Lemma 4 (false is right). If MILLER-RABIN(n,k)(n, k) returns false, then nn is not prime.

Proof. Line L1 returns false only for n∈{0,1}n \in \{0, 1\}. Line L2 returns false for even n≥4n \ge 4. Line L11 returns false only when a round fails, which by Lemma 3 does not happen for a prime. □\square

Lemma 5 (counting in a cyclic group). Let GG be a cyclic group of order NN and m≥1m \ge 1. Then xm=1x^m = 1 has gcd⁡(m,N)\gcd(m, N) solutions in GG. If NN is even and −1-1 denotes the unique element of order 2, then xm=−1x^m = -1 has a solution if and only if v2(m)<v2(N)v_2(m) < v_2(N), and then it has gcd⁡(m,N)\gcd(m, N) solutions.

Proof. Let gg generate GG and x=gtx = g^t, 0≤t<N0 \le t < N. Then xm=1x^m = 1 if and only if N∣tmN \mid tm, which has gcd⁡(m,N)\gcd(m, N) solutions tt. Next −1=gN/2-1 = g^{N/2}, and xm=−1x^m = -1 if and only if tm≡N/2(modN)tm \equiv N/2 \pmod N. With c=gcd⁡(m,N)c = \gcd(m, N) this is solvable if and only if c∣N/2c \mid N/2, with cc solutions. As v2(c)=min⁡(v2(m),v2(N))v_2(c) = \min(v_2(m), v_2(N)), c∣N/2c \mid N/2 holds if and only if v2(m)<v2(N)v_2(m) < v_2(N). □\square

Lemma 6 (Monier's count). Let n=∏i=1rpiein = \prod_{i=1}^r p_i^{e_i} be odd and composite with distinct primes pip_i, vi=v2(pi−1)v_i = v_2(p_i - 1) and ν=min⁡ivi\nu = \min_i v_i. Then

∣S(n)∣=(1+∑j=0ν−12jr)∏i=1rgcd⁡(d,pi−1).|S(n)| = \Big(1 + \sum_{j=0}^{\nu - 1} 2^{jr}\Big) \prod_{i=1}^r \gcd(d, p_i - 1).

Proof. Each (Z/pieiZ)∗(\mathbb{Z}/p_i^{e_i}\mathbb{Z})^* is cyclic of order Ni=piei−1(pi−1)N_i = p_i^{e_i - 1}(p_i - 1), with v2(Ni)=viv_2(N_i) = v_i. For m∣n−1m \mid n - 1 we have pi∤mp_i \nmid m, so gcd⁡(m,Ni)=gcd⁡(m,pi−1)\gcd(m, N_i) = \gcd(m, p_i - 1). By the Chinese remainder theorem the number of solutions of am≡±1(modn)a^m \equiv \pm 1 \pmod n is the product of the numbers of solutions modulo the pieip_i^{e_i}. By Lemma 5, ad≡1a^d \equiv 1 has T=∏igcd⁡(d,pi−1)T = \prod_i \gcd(d, p_i - 1) solutions. For 0≤j<s0 \le j < s, a2jd≡−1a^{2^j d} \equiv -1 has ∏i2jgcd⁡(d,pi−1)=2jrT\prod_i 2^j \gcd(d, p_i - 1) = 2^{jr} T solutions if j<νj < \nu, since then j<vij < v_i and gcd⁡(2jd,pi−1)=2jgcd⁡(d,pi−1)\gcd(2^j d, p_i - 1) = 2^j \gcd(d, p_i - 1); it has none if j≥νj \ge \nu. As every pi≡1(mod2ν)p_i \equiv 1 \pmod{2^\nu}, n≡1(mod2ν)n \equiv 1 \pmod{2^\nu}, so ν≤s\nu \le s. The cases are disjoint: if a2jd≡−1a^{2^j d} \equiv -1, then a2j′d≡1a^{2^{j'} d} \equiv 1 for j′>jj' > j, and if ad≡1a^d \equiv 1 then no power a2jda^{2^j d} is −1-1. Adding the counts gives the formula. □\square

Lemma 7 (Monier-Rabin). If n>9n > 9 is odd and composite, then ∣S(n)∣≤φ(n)/4|S(n)| \le \varphi(n)/4.

Proof. Keep the notation of Lemma 6. Since dd is odd, gcd⁡(d,pi−1)\gcd(d, p_i - 1) divides the odd part (pi−1)/2vi(p_i - 1)/2^{v_i}. With φ(n)=∏ipiei−1(pi−1)\varphi(n) = \prod_i p_i^{e_i - 1}(p_i - 1) this gives

∣S(n)∣φ(n)≤1+∑j<ν2jr2v1+⋯+vr⋅1∏ipiei−1,(1)\frac{|S(n)|}{\varphi(n)} \le \frac{1 + \sum_{j<\nu} 2^{jr}}{2^{v_1 + \cdots + v_r}} \cdot \frac{1}{\prod_i p_i^{e_i - 1}}, \tag{1}

with equality in the first factor when each gcd⁡(d,pi−1)\gcd(d, p_i - 1) is the whole odd part. Since ∑ivi≥rν\sum_i v_i \ge r\nu, ν≥1\nu \ge 1 and ∑j<ν2jr=(2rν−1)/(2r−1)\sum_{j<\nu} 2^{jr} = (2^{r\nu} - 1)/(2^r - 1),

1+∑j<ν2jr2rν=12r−1+2−rν2r−22r−1≤12r−1+2−r2r−22r−1=21−r.(2)\frac{1 + \sum_{j<\nu} 2^{jr}}{2^{r\nu}} = \frac{1}{2^r - 1} + 2^{-r\nu} \frac{2^r - 2}{2^r - 1} \le \frac{1}{2^r - 1} + 2^{-r} \frac{2^r - 2}{2^r - 1} = 2^{1-r}. \tag{2}

Case r≥3r \ge 3. By (1) and (2), ∣S(n)∣/φ(n)≤21−r≤1/4|S(n)|/\varphi(n) \le 2^{1-r} \le 1/4.

Case r=2r = 2, nn not squarefree. The last factor in (1) is at most 1/31/3, so ∣S(n)∣/φ(n)≤12⋅13<14|S(n)|/\varphi(n) \le \frac12 \cdot \frac13 < \frac14.

Case n=pqn = pq with primes p<qp < q. If v1+v2>2νv_1 + v_2 > 2\nu, the first factor in (1) is at most half of the left side of (2), so ∣S(n)∣/φ(n)≤12⋅12|S(n)|/\varphi(n) \le \frac12 \cdot \frac12. If v1=v2=νv_1 = v_2 = \nu, let q′=(q−1)/2νq' = (q - 1)/2^\nu be the odd part of q−1q - 1. If q′∣dq' \mid d, then q−1=2νq′q - 1 = 2^\nu q' divides 2sd=n−12^s d = n - 1, because ν≤s\nu \le s. But n−1=p(q−1)+(p−1)n - 1 = p(q - 1) + (p - 1), so then q−1∣p−1q - 1 \mid p - 1, which is false as 0<p−1<q−10 < p - 1 < q - 1. So gcd⁡(d,q−1)\gcd(d, q - 1) is a proper divisor of the odd number q′q', hence at most q′/3q'/3, and the bound (1) improves by the factor 1/31/3: ∣S(n)∣/φ(n)≤13⋅12<14|S(n)|/\varphi(n) \le \frac13 \cdot \frac12 < \frac14.

Case r=1r = 1, n=pen = p^e with e≥2e \ge 2. Lemma 6 gives ∣S(n)∣=2νgcd⁡(d,p−1)≤2ν(p−1)/2ν=p−1|S(n)| = 2^\nu \gcd(d, p - 1) \le 2^\nu (p - 1)/2^\nu = p - 1, so ∣S(n)∣/φ(n)≤p1−e|S(n)|/\varphi(n) \le p^{1-e}. This is at most 1/51/5 if p≥5p \ge 5, and at most 1/91/9 if p=3p = 3 and e≥3e \ge 3. The remaining case n=9n = 9 is excluded. □\square

Theorem (error). Let n≥5n \ge 5 be odd and composite. Then MILLER-RABIN(n,k)(n, k) returns true with probability exactly qkq^k, where q=(∣S(n)∣−2)/(n−3)q = (|S(n)| - 2)/(n - 3), and q<1/4q < 1/4. So the probability is less than 4−k4^{-k}.

Proof. Lines L1 and L2 do not return. By Lemma 1 the call returns true if and only if each of the kk bases drawn in line L5 is in S(n)S(n). The bases are independent and uniform on the n−3n - 3 integers of [2,n−2][2, n - 2]. The liars 11 and n−1n - 1 are not in this range, and they are distinct, so ∣S(n)∣−2|S(n)| - 2 of the n−3n - 3 bases are liars, and each round passes with probability qq. For n=9n = 9, S(9)={1,8}S(9) = \{1, 8\}, so q=0q = 0. For n>9n > 9, Lemma 7 and φ(n)≤n−1\varphi(n) \le n - 1 give

q≤(n−1)/4−2n−3=n−94(n−3)<14.q \le \frac{(n - 1)/4 - 2}{n - 3} = \frac{n - 9}{4(n - 3)} < \frac14.

The kk rounds are independent, so the probability of true is qk<4−kq^k < 4^{-k}. ■\blacksquare

Remark. The probability is over the random bases only. It holds for every odd composite nn, with no assumption on how nn 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.

Back to Miller-Rabin primality test