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 of the page. For an integer , is the group of residues coprime to under multiplication modulo , and is its order. A liar for an odd composite is an with , and is the set of liars. A Carmichael number is a composite with for every coprime to .
Termination. The loop of lines L3 to L5 runs at most times, and each pass does one exponentiation, which ends. So the call ends.
Lemma 1 (a prime is accepted). If is prime, FERMAT-PRIMALITY-TEST returns true for every choice of the random bases.
Proof. If , line L1 returns true. Otherwise is an odd prime, so line L2 does not return. Each base of line L4 satisfies , so , and Fermat's little theorem gives . So line L5 never returns, and line L6 returns true.
Lemma 2 (the answer false is right). If FERMAT-PRIMALITY-TEST returns false, then is not prime.
Proof. Line L1 returns false only for , which are not prime. Line L2 returns false for an even , which has the factor 2 and is larger than 2. Line L5 returns false only when , and by Lemma 1 this is impossible for a prime .
Lemma 3 (liars are units and form a subgroup). Let be odd and composite. Each liar is coprime to . The set is a subgroup of , and it contains and .
Proof. If , then , so has an inverse modulo and . The residue is a liar. If , then , so is closed under products. A nonempty finite subset of a finite group that is closed under products is a subgroup, because each element has finite order and is a product of copies of . Finally , since is even.
Lemma 4 (at most half are liars). Let be odd, composite and not a Carmichael number. Then , and the number of liars in , divided by , is less than .
Proof. Since is not a Carmichael number, some coprime to has , so is a proper subgroup of by Lemma 3. By Lagrange's theorem its order divides and is smaller, so . The liars in are without and , which are distinct since , so there are of them. As ,
Lemma 5 (the number of liars). Let be odd and at least 3, with distinct primes . Then , where for a prime the set means all with .
Proof. By the Chinese remainder theorem, corresponds to the tuple , and holds if and only if for each . So is the product of the numbers of solutions of modulo . Each solution is a unit, as in Lemma 3. For an odd prime , the group is cyclic of order (see existence of primitive roots). In a cyclic group with generator and order , the element with satisfies if and only if , that is with , and exactly values of in have this property. So . Since , , so the factor does not change the gcd, and .
For a Carmichael number every unit is a liar, so ; this is consistent with Lemma 5 and Korselt's criterion that for each and is squarefree.
Theorem (error). Let be odd and composite, and let . Then FERMAT-PRIMALITY-TEST returns true with probability exactly . If is not a Carmichael number, then , and the probability is less than . If is a Carmichael number, then .
Proof. Lines L1 and L2 do not return for odd . The call returns true if and only if each of the bases drawn by line L4 is a liar. The draws are independent and uniform on the integers of , and of them are liars, by Lemma 3. So the probability is . Lemma 4 gives when is not a Carmichael number. For a Carmichael number, by the definition and Lemma 3.
Remark. The bound depends only on and ; no assumption on the distribution of inputs is used. It cannot be made uniform: for a Carmichael number with , , and tends to 1 as grows.