Random prime and modulus generation #
This file implements the random prime generator of [KL20], Algorithm 9.34, on top of
the Miller–Rabin test of CslibFrontier/Crypto/Computational/MillerRabin.lean. On input n, given
in unary, it draws up to 3 n² uniform n-bit candidates, runs the Miller–Rabin test with n
rounds on each, and returns the first candidate that passes, or none when every candidate fails.
The guarantees of [KL20], Section 9.2.1, proved here are:
- the generator returns a number that is not prime with probability at most
3 n² 2⁻ⁿ: by a union bound over the attempts, each of which passes a composite with probability at most2⁻ⁿ; - it returns
nonewith probability at mostρ ^ (3 n²), whereρis the probability that a uniformn-bit candidate is not prime, since a prime candidate always passes; - for
n > 1,ρ ≤ 1 - 1 / (3 n)by the density of then-bit primes ([KL20], Theorem 9.32, inCslibFrontier/Foundations/NumberTheory/PrimeDensity.lean), so the generator returnsnonewith probability at most2⁻ⁿ; - every output is an
n-bit number, and alln-bit primes are returned with the same probability, so conditioned on returning a prime the output is a uniformn-bit prime; - the generator is a strict PPT program in
n.
Two independent runs of the generator, multiplied, give the modulus generator GenModulus that
[KL20], Section 9.2.3, suggests: it returns (p q, p, q) for n-bit primes p and
q except with probability at most 2 ρ ^ (3 n²) + 6 n² 2⁻ⁿ, hence at most (6 n² + 2) 2⁻ⁿ for
n > 1, and it is a strict PPT program in n.
Main definitions #
Cslib.Crypto.primeCandidate: a uniformn-bit number.Cslib.Crypto.primeAttempt: a candidate together with the verdict of the test on it.Cslib.Crypto.randomPrime: the generator of Algorithm 9.34.Cslib.Crypto.genModulus: the modulus generator of Section 9.2.3.
Main results #
Cslib.Crypto.eval_primeCandidate_apply: candidates are uniform over then-bit numbers.Cslib.Crypto.toOuterMeasure_randomPrime_not_prime_le: the generator returns a number that is not prime with probability at most3 n² 2⁻ⁿ.Cslib.Crypto.eval_randomPrime_none_le: the generator fails with probability at mostρ ^ (3 n²).Cslib.Crypto.inv_three_mul_le_toOuterMeasure_primeCandidate_prime: forn > 1, a candidate is prime with probability at least1 / (3 n).Cslib.Crypto.eval_randomPrime_none_le_inv_two_pow: forn > 1, the generator fails with probability at most2⁻ⁿ.Cslib.Crypto.mem_support_randomPrime: every output is ann-bit number.Cslib.Crypto.eval_randomPrime_some_eq: alln-bit primes are equally likely outputs.Cslib.Crypto.isPPTOn_randomPrime: the generator is a strict PPT program inn.Cslib.Crypto.toOuterMeasure_genModulus_invalid_le: the modulus generator returns(p q, p, q)forn-bit primespandqexcept with probability at most2 ρ ^ (3 n²) + 6 n² 2⁻ⁿ.Cslib.Crypto.toOuterMeasure_genModulus_invalid_le_inv_two_pow: forn > 1, this probability is at most(6 n² + 2) 2⁻ⁿ.Cslib.Crypto.isPPTOn_genModulus: the modulus generator is a strict PPT program inn.
References #
A uniform n-bit candidate: n - 1 fair coins below a leading one, read least
significant bit first. For n = 0 the candidate is 1.
Candidates #
The law of a candidate.
Attempts #
The law of an attempt: a candidate p, then the verdict of the test on p.
The generator #
The generator returns a number that is not prime with probability at most 3 n² 2⁻ⁿ
([KL20], Section 9.2.1): by a union bound, each of the 3 n² attempts passes the
test on a number that is not prime with probability at most 2⁻ⁿ.
The generator fails with probability at most ρ ^ (3 n²), where ρ is the probability
that a uniform n-bit candidate is not prime: an attempt on a prime candidate always passes.
A uniform n-bit candidate is prime with probability at least 1 / (3 n) for 1 < n,
by the density of the n-bit primes ([KL20], Theorem 9.32).
For 1 < n, the generator fails with probability at most 2⁻ⁿ. A candidate is prime
with probability at least 1 / (3 n), so all 3 n² attempts fail with probability at most
(1 - 1 / (3 n)) ^ (3 n²) ≤ 2⁻ⁿ.
Conditioned on returning a prime, the output is a uniform n-bit prime
([KL20], Section 9.2.1): any two n-bit primes are returned with the same
probability.
Moduli #
The modulus generator returns (p q, p, q) for n-bit primes p and q
([KL20], Section 9.2.3) except with probability at most 2 ρ ^ (3 n²) + 6 n² 2⁻ⁿ,
where ρ is the probability that a uniform n-bit candidate is not prime: by a union bound over
its two runs.
For 1 < n, the modulus generator returns (p q, p, q) for n-bit primes p and q
except with probability at most (6 n² + 2) 2⁻ⁿ, which is negligible in n.
Efficiency #
Candidates are a strict PPT program in n, given in unary.
An attempt is a strict PPT program in n, given in unary.
The generator is a strict PPT program in n, given in unary.
The modulus generator is a strict PPT program in n, given in unary.