Documentation

CslibFrontier.Crypto.Computational.UnitSampling

Sampling units modulo a product of two primes #

Paillier and Goldwasser–Micali encryption over a modulus N = p * q draw their randomness with ProbComp.sampleModNat (n + |N|) N and use it as a unit modulo N, while the security proofs reason about a uniform unit. The draw is within N / 2 ^ (n + |N|) ≤ 1 / 2 ^ n of a uniform residue modulo N, and a uniform residue has statistical distance (N - φ(N)) / N from a uniform unit. When p and q have at least n bits, this fraction is strictly less than 4 / 2 ^ n.

A draw r that is not a unit is replaced by 1 (unitOrOne N r), so the randomness that is used is always a unit, and the replacement is computable in polynomial time.

Main definitions #

Main results #

For any positive modulus N, the statistical distance between a uniform residue in ZMod N and the image of a uniform unit in (ZMod N)ˣ is the fraction (N - φ(N)) / N of non-units.

Restricting a uniform residue modulo N to the units changes the advantage of any continuation by at most the fraction (N - φ(N)) / N of non-units.

theorem Cslib.Crypto.sub_totient_div_lt {p q n : ℕ} (hp : Nat.Prime p) (hq : Nat.Prime q) (hpq : p ≠ q) (hpn : n ≤ p.size) (hqn : n ≤ q.size) :
↑(p * q - (p * q).totient) / ↑(p * q) < 4 / 2 ^ n

For distinct primes p and q of at least n bits, the non-units modulo p * q are strictly less than a 4 / 2 ^ n fraction of the residues.

theorem Cslib.Crypto.Game.advantage_sampleModNat_units_le {p q n : ℕ} [NeZero (p * q)] (hp : Nat.Prime p) (hq : Nat.Prime q) (hpq : p ≠ q) (hpn : n ≤ p.size) (hqn : n ≤ q.size) (k : ℕ → Game) :
advantage ((ProbComp.sampleModNat (n + (Turing.MultiTapeTM.encNat (p * q)).length) (p * q)).eval.bind k) ((Probability.PMF.uniformOfFintype (ZMod (p * q))ˣ).bind fun (u : (ZMod (p * q))ˣ) => k (↑u).val) ≤ 5 / 2 ^ n

For N = p * q with distinct primes p and q of at least n bits, drawing r ← ProbComp.sampleModNat (n + |N|) N instead of a uniform unit u modulo N changes the advantage of any continuation k by at most 5 / 2 ^ n: at most 1 / 2 ^ n from the residue sampler and at most 4 / 2 ^ n from the non-units.

Replacing a non-unit draw #

The randomness that is used: r itself when it is a unit modulo N, and 1 otherwise.

theorem Cslib.Crypto.unitOrOne_val_coe_unit {N : ℕ} (u : (ZMod N)ˣ) :
unitOrOne N (↑u).val = (↑u).val

The residue of a unit modulo N is kept.