Documentation

CslibFrontier.Crypto.Computational.MillerRabin

The Miller–Rabin primality test as a probabilistic program #

This file packages the Miller–Rabin test ([KL20], Section 9.2.2) as a probabilistic program with an explicit coin budget, and proves the guarantee of [KL20], Theorem 9.33: a prime always passes, and a composite passes t independent rounds with probability at most 2⁻ᵗ. The test is a strict PPT program in the binary length of the candidate and the number of rounds, given in unary.

The number theory is already in the library and is reused here, not reproved:

A round (millerRabinRound) flips the 2 |n| coins and negates the verifier, so it answers true for "prime". The test (millerRabinTest) runs t independent rounds and answers "prime" when all of them do.

Main definitions #

Main results #

References #

One Miller–Rabin round on n, answering true for "prime". It flips 2 |n| fair coins and negates the coRP verifier Complexity.MillerRabin.verifier: 2 passes, 0, 1 and the even numbers above 2 fail, and an odd n > 2 passes exactly when the base 1 + (r mod (n - 1)), for the coin word r, is a strong liar for n.

noncomputable def Cslib.Crypto.millerRabinTest (n t : ℕ) :

The Miller–Rabin test with t rounds: run t independent rounds on n and answer "prime" (true) when every round does. This is Algorithm 9.44 of [KL20] without the perfect-power check, which the strong-liar bound makes unnecessary, and with the base drawn as in Complexity.MillerRabin.verifier.

One round #

The law of a round: the image of 2 |n| uniform coins under the negated verifier.

A prime passes every round.

A number that is not prime passes a round with probability at most 1 / 2. For an odd composite this is the strong-liar bound ZMod.two_mul_card_isStrongLiar_le, through Complexity.MillerRabin.decidesWithOneSidedError; 0, 1 and the even numbers above 2 never pass.

Independent rounds #

t rounds all pass with the t-th power of the probability that one round passes.

A prime always passes the test ([KL20], Theorem 9.33).

A composite passes t rounds with probability at most 2⁻ᵗ ([KL20], Theorem 9.33). The bound holds for every n that is not prime, including 0 and 1.

Efficiency #

The test is a strict PPT program in the binary length of n and the number of rounds t, given in unary.