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:
- modulo an odd composite
nthe strong liars lie in a proper subgroup of the units (ZMod.exists_subgroup_ne_top_isStrongLiar), so at most half of the units are strong liars (ZMod.two_mul_card_isStrongLiar_le). This also covers odd prime powers, so the test needs no perfect-power check, unlike Algorithm 9.44 of [KL20]; - the coRP verifier
Complexity.MillerRabin.verifierreads2 |n|random bitsr, takes the base1 + (r mod (n - 1))and declares an oddn > 2composite when that base is a witness.Complexity.MillerRabin.decidesWithOneSidedErrorbounds its error; the bias of the reduction modulon - 1is absorbed there.
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 #
Cslib.Crypto.millerRabinRound: one round onn, answeringtruefor "prime".Cslib.Crypto.millerRabinTest:tindependent rounds onn.
Main results #
Cslib.Crypto.eval_millerRabinTest_true:trounds all pass with thet-th power of the probability that one round passes.Cslib.Crypto.eval_millerRabinTest_of_prime: a prime always passes.Cslib.Crypto.eval_millerRabinTest_le_of_not_prime: a number that is not prime passestrounds with probability at most2⁻ᵗ.Cslib.Crypto.isPPTOn_millerRabinTest: the test is a strict PPT program in|n|andt.
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.
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).
Efficiency #
A round is a strict PPT program in the binary length of n.
The test is a strict PPT program in the binary length of n and the number of rounds
t, given in unary.