Documentation

CslibFrontier.Crypto.Computational.ModulusGeneration

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:

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 #

Main results #

References #

noncomputable def Cslib.Crypto.primeCandidate (n : ℕ) :

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.

noncomputable def Cslib.Crypto.primeAttempt (n : ℕ) :

One attempt of the generator: a uniform n-bit candidate p and the verdict of the Miller–Rabin test with n rounds on p.

noncomputable def Cslib.Crypto.randomPrime (n : ℕ) :

The random prime generator of [KL20], Algorithm 9.34: up to 3 n² attempts, returning the first candidate that passes the test, or none when every attempt fails.

noncomputable def Cslib.Crypto.genModulus (n : ℕ) :

The modulus generator suggested in [KL20], Section 9.2.3: two independent runs of the random prime generator, multiplied. It returns (N, p, q) with N = p q, or none when either run fails.

Candidates #

theorem Cslib.Crypto.mem_support_primeCandidate {n p : ℕ} (hn : 0 < n) (hp : p ∈ (primeCandidate n).eval.support) :
2 ^ (n - 1) ≤ p ∧ p < 2 ^ n

Every candidate is an n-bit number.

theorem Cslib.Crypto.eval_primeCandidate_apply {n p : ℕ} (hn : 0 < n) (hlow : 2 ^ (n - 1) ≤ p) (hhigh : p < 2 ^ n) :
(primeCandidate n).eval p = (2 ^ (n - 1))⁻¹

Candidates are uniform over the n-bit numbers.

Attempts #

theorem Cslib.Crypto.eval_primeAttempt (n : ℕ) :
(primeAttempt n).eval = (primeCandidate n).eval.bind fun (p : ℕ) => PMF.map (fun (verdict : Bool) => (p, verdict)) (millerRabinTest p n).eval

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

A uniform n-bit candidate is not prime with probability at most 1 - 1 / (3 n) for 1 < n.

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⁻ⁿ.

theorem Cslib.Crypto.mem_support_randomPrime {n p : ℕ} (hp : some p ∈ (randomPrime n).eval.support) :
2 ^ (n - 1) ≤ p ∧ p < 2 ^ n

Every output of the generator is an n-bit number.

theorem Cslib.Crypto.eval_randomPrime_some {n p : ℕ} (hlow : 2 ^ (n - 1) ≤ p) (hhigh : p < 2 ^ n) (hp : Nat.Prime p) :
(randomPrime n).eval (some p) = (2 ^ (n - 1))⁻¹ * ∑ i ∈ Finset.range (3 * n ^ 2), (primeAttempt n).eval.toOuterMeasure {a : ℕ × Bool | ¬a.2 = true} ^ i

The probability of returning an n-bit prime p is the probability 2⁻⁽ⁿ⁻¹⁾ of drawing p as a candidate, times the expected number of attempts. It does not depend on p.

theorem Cslib.Crypto.eval_randomPrime_some_eq {n p q : ℕ} (hp : Nat.Prime p) (hq : Nat.Prime q) (hp_low : 2 ^ (n - 1) ≤ p) (hp_high : p < 2 ^ n) (hq_low : 2 ^ (n - 1) ≤ q) (hq_high : q < 2 ^ n) :

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 #

theorem Cslib.Crypto.toOuterMeasure_genModulus_invalid_le (n : ℕ) :
(genModulus n).eval.toOuterMeasure {out : Option (ℕ × ℕ × ℕ) | ¬∃ (p : ℕ) (q : ℕ), out = some (p * q, p, q) ∧ Nat.Prime p ∧ Nat.Prime q ∧ 2 ^ (n - 1) ≤ p ∧ p < 2 ^ n ∧ 2 ^ (n - 1) ≤ q ∧ q < 2 ^ n} ≤ 2 * ((primeCandidate n).eval.toOuterMeasure {p : ℕ | ¬Nat.Prime p} ^ (3 * n ^ 2) + 3 * ↑n ^ 2 * (2 ^ n)⁻¹)

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.

theorem Cslib.Crypto.toOuterMeasure_genModulus_invalid_le_inv_two_pow {n : ℕ} (hn : 1 < n) :
(genModulus n).eval.toOuterMeasure {out : Option (ℕ × ℕ × ℕ) | ¬∃ (p : ℕ) (q : ℕ), out = some (p * q, p, q) ∧ Nat.Prime p ∧ Nat.Prime q ∧ 2 ^ (n - 1) ≤ p ∧ p < 2 ^ n ∧ 2 ^ (n - 1) ≤ q ∧ q < 2 ^ n} ≤ (6 * ↑n ^ 2 + 2) * (2 ^ n)⁻¹

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 #