Algorithm Search⌘K

Proofs: Fermat primality test

These are the human proofs of Fermat primality test, at full length.

The Fermat test never rejects a prime, and its error on a composite is q^k with q given by the number of liars full

This proof shows: correctness, termination, error.

We use the pseudocode FERMAT-PRIMALITY-TEST(n,k)(n, k) of the page. For an integer n≥2n \ge 2, (Z/nZ)∗(\mathbb{Z}/n\mathbb{Z})^* is the group of residues coprime to nn under multiplication modulo nn, and φ(n)\varphi(n) is its order. A liar for an odd composite nn is an a∈[1,n−1]a \in [1, n-1] with an−1≡1(modn)a^{n-1} \equiv 1 \pmod n, and L(n)L(n) is the set of liars. A Carmichael number is a composite nn with an−1≡1(modn)a^{n-1} \equiv 1 \pmod n for every aa coprime to nn.

Termination. The loop of lines L3 to L5 runs at most kk times, and each pass does one exponentiation, which ends. So the call ends.

Lemma 1 (a prime is accepted). If nn is prime, FERMAT-PRIMALITY-TEST(n,k)(n, k) returns true for every choice of the random bases.

Proof. If n∈{2,3}n \in \{2, 3\}, line L1 returns true. Otherwise n≥5n \ge 5 is an odd prime, so line L2 does not return. Each base of line L4 satisfies 2≤a≤n−22 \le a \le n - 2, so n∤an \nmid a, and Fermat's little theorem gives an−1≡1(modn)a^{n-1} \equiv 1 \pmod n. So line L5 never returns, and line L6 returns true. □\square

Lemma 2 (the answer false is right). If FERMAT-PRIMALITY-TEST(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\}, which are not prime. Line L2 returns false for an even n≥4n \ge 4, which has the factor 2 and is larger than 2. Line L5 returns false only when an−1≢1(modn)a^{n-1} \not\equiv 1 \pmod n, and by Lemma 1 this is impossible for a prime nn. □\square

Lemma 3 (liars are units and form a subgroup). Let n≥5n \ge 5 be odd and composite. Each liar is coprime to nn. The set L(n)L(n) is a subgroup of (Z/nZ)∗(\mathbb{Z}/n\mathbb{Z})^*, and it contains 11 and n−1n - 1.

Proof. If an−1≡1(modn)a^{n-1} \equiv 1 \pmod n, then a⋅an−2≡1a \cdot a^{n-2} \equiv 1, so aa has an inverse modulo nn and gcd⁡(a,n)=1\gcd(a, n) = 1. The residue 11 is a liar. If a,b∈L(n)a, b \in L(n), then (ab)n−1=an−1bn−1≡1(ab)^{n-1} = a^{n-1} b^{n-1} \equiv 1, so L(n)L(n) is closed under products. A nonempty finite subset of a finite group that is closed under products is a subgroup, because each element xx has finite order mm and x−1=xm−1x^{-1} = x^{m-1} is a product of copies of xx. Finally (n−1)n−1≡(−1)n−1=1(n-1)^{n-1} \equiv (-1)^{n-1} = 1, since n−1n - 1 is even. □\square

Lemma 4 (at most half are liars). Let n≥5n \ge 5 be odd, composite and not a Carmichael number. Then ∣L(n)∣≤φ(n)/2|L(n)| \le \varphi(n)/2, and the number of liars in [2,n−2][2, n-2], divided by n−3n - 3, is less than 1/21/2.

Proof. Since nn is not a Carmichael number, some bb coprime to nn has bn−1≢1b^{n-1} \not\equiv 1, so L(n)L(n) is a proper subgroup of (Z/nZ)∗(\mathbb{Z}/n\mathbb{Z})^* by Lemma 3. By Lagrange's theorem its order divides φ(n)\varphi(n) and is smaller, so ∣L(n)∣≤φ(n)/2|L(n)| \le \varphi(n)/2. The liars in [2,n−2][2, n-2] are L(n)L(n) without 11 and n−1n - 1, which are distinct since n≥5n \ge 5, so there are ∣L(n)∣−2≤φ(n)/2−2|L(n)| - 2 \le \varphi(n)/2 - 2 of them. As φ(n)≤n−1\varphi(n) \le n - 1,

φ(n)/2−2n−3≤(n−1)/2−2n−3=n−52(n−3)<12.□\frac{\varphi(n)/2 - 2}{n - 3} \le \frac{(n-1)/2 - 2}{n - 3} = \frac{n - 5}{2(n - 3)} < \frac{1}{2}. \qquad \square

Lemma 5 (the number of liars). Let n=∏i=1rpiein = \prod_{i=1}^{r} p_i^{e_i} be odd and at least 3, with distinct primes pip_i. Then ∣L(n)∣=∏i=1rgcd⁡(n−1,pi−1)|L(n)| = \prod_{i=1}^{r} \gcd(n - 1, p_i - 1), where for a prime nn the set L(n)L(n) means all a∈[1,n−1]a \in [1, n-1] with an−1≡1a^{n-1} \equiv 1.

Proof. By the Chinese remainder theorem, a mod na \bmod n corresponds to the tuple (a mod piei)i(a \bmod p_i^{e_i})_i, and an−1≡1(modn)a^{n-1} \equiv 1 \pmod n holds if and only if an−1≡1(modpiei)a^{n-1} \equiv 1 \pmod{p_i^{e_i}} for each ii. So ∣L(n)∣|L(n)| is the product of the numbers NiN_i of solutions of xn−1≡1x^{n-1} \equiv 1 modulo pieip_i^{e_i}. Each solution is a unit, as in Lemma 3. For an odd prime pp, the group (Z/peZ)∗(\mathbb{Z}/p^e\mathbb{Z})^* is cyclic of order m=pe−1(p−1)m = p^{e-1}(p - 1) (see existence of primitive roots). In a cyclic group with generator gg and order mm, the element gjg^j with 0≤j<m0 \le j < m satisfies (gj)t=1(g^j)^t = 1 if and only if m∣jtm \mid jt, that is md∣j\frac{m}{d} \mid j with d=gcd⁡(t,m)d = \gcd(t, m), and exactly dd values of jj in [0,m)[0, m) have this property. So Ni=gcd⁡(n−1,piei−1(pi−1))N_i = \gcd(n - 1, p_i^{e_i - 1}(p_i - 1)). Since pi∣np_i \mid n, pi∤n−1p_i \nmid n - 1, so the factor piei−1p_i^{e_i - 1} does not change the gcd, and Ni=gcd⁡(n−1,pi−1)N_i = \gcd(n - 1, p_i - 1). □\square

For a Carmichael number every unit is a liar, so ∣L(n)∣=φ(n)|L(n)| = \varphi(n); this is consistent with Lemma 5 and Korselt's criterion that pi−1∣n−1p_i - 1 \mid n - 1 for each ii and nn is squarefree.

Theorem (error). Let n≥5n \ge 5 be odd and composite, and let q=(∣L(n)∣−2)/(n−3)q = (|L(n)| - 2)/(n - 3). Then FERMAT-PRIMALITY-TEST(n,k)(n, k) returns true with probability exactly qkq^k. If nn is not a Carmichael number, then q<1/2q < 1/2, and the probability is less than 2−k2^{-k}. If nn is a Carmichael number, then q=(φ(n)−2)/(n−3)q = (\varphi(n) - 2)/(n - 3).

Proof. Lines L1 and L2 do not return for odd n≥5n \ge 5. The call returns true if and only if each of the kk bases drawn by line L4 is a liar. The kk draws are independent and uniform on the n−3n - 3 integers of [2,n−2][2, n-2], and ∣L(n)∣−2|L(n)| - 2 of them are liars, by Lemma 3. So the probability is qkq^k. Lemma 4 gives q<1/2q < 1/2 when nn is not a Carmichael number. For a Carmichael number, ∣L(n)∣=φ(n)|L(n)| = \varphi(n) by the definition and Lemma 3. ■\blacksquare

Remark. The bound depends only on nn and kk; no assumption on the distribution of inputs is used. It cannot be made uniform: for a Carmichael number n=p1p2p3n = p_1 p_2 p_3 with p1<p2<p3p_1 < p_2 < p_3, q≥(φ(n)−2)/nq \ge (\varphi(n) - 2)/n, and φ(n)/n=∏(1−1/pi)≥(1−1/p1)3\varphi(n)/n = \prod (1 - 1/p_i) \ge (1 - 1/p_1)^3 tends to 1 as p1p_1 grows.

Back to Fermat primality test