Proofs: Frobenius primality test
These are the human proofs of Frobenius primality test, at full length.
The Frobenius test never rejects a prime, and its false answers are proofs full
This proof shows: correctness, termination.
We use the pseudocode FROBENIUS of the page, with , and . For an odd , is the Jacobi symbol. We prove that the test ends with probability 1, that it returns true for every prime, and that every false answer is right. The bound on the probability that a composite passes is Grantham's theorem (Journal of Number Theory 72, 1998; Crandall and Pomerance, Theorem 3.6.10) and is quoted, not proved, here.
Lemma 1 (trial division). Lines L1 to L3 return true only for primes and false only for non-primes, and they return for every .
Proof. Line L1 returns false for . In line L2 the primes are tried in increasing order. If is found, the answer is , which is right: a prime has no prime divisor other than itself, and a composite with is not . If is reached first, no prime below divides , so has no prime factor up to , and is prime. If the loop ends without a return, no prime up to divides ; if then , any prime factor would have , so there is none and is prime, as line L3 returns.
From now on, , has no prime factor up to , and in particular is odd.
Lemma 2 (squares). Line L4 returns false only for a composite. If passes line L4, there is an integer with such that , and are all coprime to .
Proof. A square with is composite. If is not a square, some prime divides to an odd power ; write with . As , there are quadratic non-residues modulo , so one of them, , has ; also , as 0 and 1 are not non-residues. By the Chinese remainder theorem take and . Then . Modulo the numbers are nonzero by the choice of , and modulo every prime they are , which are nonzero as . So all three are coprime to .
Lemma 3 (line L5 ends). If passes line L4, the random search of line L5 ends with probability 1.
Proof. Take as in Lemma 2. As is odd, 2 and 4 are invertible modulo ; let and in be the residues of and , and let be the residue of . By Lemma 2, , and are units modulo , so . Then , so and , and with . So is an allowed pair. Each try draws it with probability , independently of the other tries, so with probability 1 some try ends the search, at this pair or earlier.
Lemma 4 (line L6). If line L6 returns false, is composite.
Proof. A divisor of with exists.
Lemma 5 (the field). Let be an odd prime, and let be integers with and . Let and write for the class of in . Then:
- is a field with elements, and is a ring automorphism of that fixes exactly ;
- and ;
- lies in and ;
- with , odd, either or for some .
Proof. (1) A root of modulo would give a square modulo , against . So is irreducible of degree 2 over the field , and is a field with elements. In characteristic , and , so is a ring homomorphism; it is injective, as is a field, hence bijective. Its fixed points are the roots of , at most of them, and every is one, by Fermat's little theorem. So the fixed points are exactly .
(2) Since has coefficients in , , so is a root of in . The roots of are and , since and . If , then by (1), and would have a root in . So and .
(3) . As and is prime, for some . In the field , has only the roots , so .
(4) The group has order , so ; note as in . Also , by Euler's criterion. Since is odd, and . Let , so and . If , let be the largest index with ; then and . In a field, has only the roots , so .
Lemma 6 (the ring code is right). Lines L7 and L10 compute in correctly.
Proof. Each element of has a unique form with , as is monic of degree 2. Since , and . The left-to-right binary method keeps the invariant that after the digits of have been read, the value is (the number with those binary digits): a squaring doubles the exponent and a product by adds one. After the last digit the value is .
Theorem. For every , FROBENIUS ends with probability 1. If is prime it returns true. If it returns false, is not prime.
Proof. Termination. Line L2 has at most 5133 passes, line L5 ends with probability 1 by Lemma 3, and lines L7, L10 and L12 are loops of length .
Primes. Let be prime. If , Lemma 1 gives true. Otherwise is not a square, so line L4 does not fire, and line L6 does not fire, as . The pair drawn has and , so by Lemmas 5 and 6, with , and lines L8 and L9 pass. By Lemma 5 (4), either and line L11 returns true, or for some ; the loop of line L12 checks in order and returns true at the first . So line L13 is never reached.
False answers. Lines L1 to L3 are right by Lemma 1, line L4 by Lemma 2, and line L6 by Lemma 4. Lines L8, L9 and L13 return false only when a property fails that, by Lemma 5, every prime above has for every allowed pair; so is composite.
The error on composites. A composite can pass only through lines L11 and L12, after lines L1 to L6. If has no prime factor up to 50000 and is not a square, Grantham's theorem bounds the fraction of allowed pairs for which it passes by . Line L5 draws pairs uniformly and keeps the first allowed one, so the kept pair is uniform among the allowed pairs, and the probability of a true answer is at most .