Documentation

CslibFrontier.Computability.Complexity.Problems.Primes.MillerRabin

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.

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 #

Main results #

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).

The Miller–Rabin verifier for the non-primes. It accepts n < 2 and even n ≠ 2, rejects 2, and on an odd n > 2 accepts exactly when the base 1 + (ofBits r mod (n - 1)) fails the round.

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.

PRIMES is in coRP, by the Miller–Rabin test. This membership also follows from AKS (primes_mem_P with P_subset_coRP); this proof uses only the randomized verifier.