The Miller–Rabin test: PRIMES is in coRP #
The Miller–Rabin test Rabin is a polynomial-time verifier that puts the non-primes
(the composite numbers, 0 and 1), given in binary, in RP, and so the primes in coRP. On an
odd n > 2 it reads a random word r of 2 |n| bits, where |n| is the length of the binary
encoding of n. It takes the base a = 1 + (value of r mod (n - 1)), in [1, n - 1], and
accepts, declaring n composite, when a is not a strong liar for n (ZMod.IsStrongLiar).
Numbers below 2 and even numbers other than 2 are accepted outright, and 2 is rejected.
- A prime is never accepted: modulo a prime every nonzero residue is a strong liar
(
ZMod.isStrongLiar_of_prime). - An odd composite
nis accepted on at least half of the random words. At mostφ(n) / 2 ≤ (n - 2) / 2residues are strong liars (ZMod.two_mul_card_isStrongLiar_le), and each residue modulon - 1is hit by at most2 ^ (2 |n|) / (n - 1) + 1words. The bias from reducing a random word modulon - 1is absorbed because2 ^ (2 |n|) ≥ n ^ 2.
The test MillerRabin.passes m a checks the strong-liar condition for the base a modulo m + 1
without computing the decomposition m = 2 ^ s * t, t odd: it scans every j < |m| with
2 ^ j ∣ m and accepts when m / 2 ^ j is odd and a ^ (m / 2 ^ j) ≡ 1, or when j > 0 and
a ^ (m / 2 ^ j) ≡ -1. The powers use polynomial-time square-and-multiply
(Turing.MultiTapeTM.ComputableInPolyTime.binary_powMod).
Membership of the primes in coRP already follows from the AKS theorem: primes_mem_P (PRIMES is
in P) with P_subset_coRP. The point of this file is the explicit randomized verifier and its
error analysis, which are independent of AKS.
Main definitions #
Cslib.Complexity.MillerRabin.passes: one Miller–Rabin round for the candidatem + 1.Cslib.Complexity.MillerRabin.verifier: the one-sided-error verifier for the non-primes.
Main results #
Cslib.Complexity.MillerRabin.passes_eq_true_iff: a round passes exactly on strong liars.Cslib.Complexity.MillerRabin.verifier_polyTime: the verifier runs in polynomial time.Cslib.Complexity.MillerRabin.decidesWithOneSidedError: the verifier decides the non-primes with one-sided error, using2 |n|random bits.Cslib.Complexity.MillerRabin.primes_mem_coRP: the primes are incoRP.
References #
One Miller–Rabin round for the candidate m + 1 with base a: some j < |m| with
2 ^ j ∣ m has m / 2 ^ j odd and a ^ (m / 2 ^ j) ≡ 1, or j > 0 and a ^ (m / 2 ^ j) ≡ -1,
modulo m + 1. For m > 0 this holds exactly when a is a strong liar for m + 1
(passes_eq_true_iff).
A Miller–Rabin round is computable in polynomial time.
The Miller–Rabin verifier is computable in polynomial time.
For m > 0, a Miller–Rabin round for m + 1 passes exactly on the strong liars.
The Miller–Rabin test decides the non-primes with one-sided error, reading 2 |n| random
bits on the input n: 0, 1 and even composites are always accepted, an odd composite is
accepted on at least half of the random words, and a prime on none.