Documentation

CslibFrontier.Crypto.Primitives.PRG.LinearCongruential

Linear congruential generator distinguisher and seed search #

The linear congruential generator G_lcg(s) = (⌊s / w⌋, a * s + b mod q) with state space S_q = {0, …, q - 1} and output space R = {0, …, ⌊(q - 1) / w⌋} ([BS23], Section 3.7.1, pp. 70–71).

Given (r, s'), LinearCongruential.test recovers s from s' using BinaryGcd.modInv and checks ⌊s / w⌋ = r. On LinearCongruential.generator : Generator (Fin q) (Fin |R| × Fin q), when a is coprime to q, generator is injective (generator_injective) and test computes cslib's range test Generator.rangeTest (rangeTest_generator), so advantage_generator_test gives exact distinguishing advantage 1 - 1 / |R|. Note the boundary: Boneh–Shoup call G_lcg "clearly insecure" for every positive w ≤ q. That holds when 0 < w < q (advantage at least 1 / 2) and fails at w = q: then |R| = 1 and the advantage is 0, as it is for the values w = 0 and w > q that the book excludes. The advantage theorem targets the finite typed game (which has no adversary cost model); computableInPolyTime_test separately certifies test on encoded natural numbers in binary.

For the multi-iteration generator outputting consecutive ⌊s_i / w⌋ values, LinearCongruential.seedSearch checks all w candidates r₁ * w + e with e < w below q whose second and third outputs match r₂ and r₃, returning the greatest passing candidate or r₁ * w if none passes (Boneh–Shoup do not specify a tie-breaking or fallback value). seedSearch_eq proves that the search returns the seed s < q when 0 < w and the condition hshort holds: no nonzero d with |d| < w has a * d within less than w of a multiple of q. Under 0 < w and hshort two outputs already determine the state (eq_of_div_eq_of_next_div_eq). hshort is not in the book, which describes the search for the Java 8 parameters only. It is sufficient and not necessary, and some condition is needed: with a = b = 1, q = 16, w = 4, which meet the book's conditions on the parameters, the seeds 0 and 1 have the same three outputs. computableInPolyTime_seedSearch certifies seedSearch in time polynomial in w (given in unary via encUnary, so exponential in the bit length of w) and the binary lengths of the other inputs. java8 records Boneh–Shoup's parameters for Java 8 Math.random, and seedSearch_java8 discharges hshort for every seed s < 2 ^ 48.

Differences from [BS23]: LinearCongruential omits a, b < q and 0 < w ≤ q, taking only the hypothesis each result uses; G_lcg^(n) as a generator and Lemma 3.7 are omitted.

Main definitions #

Main results #

References #

The public system parameters of a linear congruential generator ([BS23], Section 3.7.1, p. 70).

  • q : ℕ

    The modulus q: the states are the numbers below q.

  • a : ℕ

    The multiplier a.

  • b : ℕ

    The increment b.

  • w : ℕ

    The divisor w: an iteration in state s outputs s / w.

theorem Cslib.Crypto.PRG.encLinearCongruential_a_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → LinearCongruential} :
theorem Cslib.Crypto.PRG.encLinearCongruential_q_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → LinearCongruential} :
theorem Cslib.Crypto.PRG.encLinearCongruential_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 T0 S0 x1 T1 S1 x2 T2 S2 x3 T3 S3 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn Turing.MultiTapeTM.encNat T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn Turing.MultiTapeTM.encNat T1 S1) (h2 : Turing.MultiTapeTM.ComputableInO x2 encIn Turing.MultiTapeTM.encNat T2 S2) (h3 : Turing.MultiTapeTM.ComputableInO x3 encIn Turing.MultiTapeTM.encNat T3 S3) :
Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { q := x0 x, a := x1 x, b := x2 x, w := x3 x }) encIn encLinearCongruential (fun (x : γ) => T0 x + (T1 x + (T2 x + T3 x))) fun (x : γ) => S0 x + (S1 x + (S2 x + S3 x))

The encoding of the parameters of a linear congruential generator: q, a, b and w in binary, each framed except the last (encLinearCongruential_apply).

theorem Cslib.Crypto.PRG.encLinearCongruential_b_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → LinearCongruential} :
theorem Cslib.Crypto.PRG.encLinearCongruential_w_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → LinearCongruential} :

The state update s ↦ a * s + b mod q.

The state before s': a⁻¹ * (s' - b) mod q, with a⁻¹ computed by the extended Euclidean algorithm. It inverts next on states when a is coprime to q (prev_next).

theorem Cslib.Crypto.PRG.LinearCongruential.prev_next (P : LinearCongruential) (ha : P.a.Coprime P.q) {s : ℕ} (hs : s < P.q) :
P.prev (P.next s) = s

The state update can be undone: prev recovers the state s from a * s + b mod q.

The test of [BS23], p. 70, on numbers: given (r, s'), recover the state s before s' and accept when s / w = r.

The test runs in time polynomial in the length of the parameters and the challenge, all in binary.

The generator G_lcg : S_q → R × S_q of [BS23], p. 70: s ↦ (⌊s / w⌋, a * s + b mod q), where R has the ⌊(q - 1) / w⌋ + 1 elements 0, …, ⌊(q - 1) / w⌋.

theorem Cslib.Crypto.PRG.LinearCongruential.rangeTest_generator (P : LinearCongruential) (ha : P.a.Coprime P.q) (y : Fin ((P.q - 1) / P.w + 1) × Fin P.q) :
P.generator.rangeTest y = P.test ↑y.1 ↑y.2

The book's test is the range test of G_lcg.

theorem Cslib.Crypto.PRG.LinearCongruential.advantage_generator_test (P : LinearCongruential) [NeZero P.q] (ha : P.a.Coprime P.q) :
(P.generator.advantage fun (y : Fin ((P.q - 1) / P.w + 1) × Fin P.q) => PMF.pure (P.test ↑y.1 ↑y.2)) = 1 - 1 / ↑((P.q - 1) / P.w + 1)

The seed-recovery test has advantage exactly 1 - 1 / |R| against G_lcg ([BS23], Section 3.7.1, p. 70): the test that recovers the state s from a * s + b mod q and compares ⌊s / w⌋, run on a challenge in R × S_q, has advantage exactly 1 - 1 / |R|, where |R| = ⌊(q - 1) / w⌋ + 1. The advantage is at least 1 / 2 when 0 < w < q; otherwise R has one element and the advantage is 0. Boneh–Shoup call G_lcg clearly insecure for every positive w ≤ q; that fails at w = q, where G_lcg(s) is exactly uniform.

theorem Cslib.Crypto.PRG.LinearCongruential.eq_of_div_eq_of_next_div_eq (P : LinearCongruential) (hw : 0 < P.w) (hshort : ∀ (d d' : ℤ), |d| < ↑P.w → |d'| < ↑P.w → ↑P.q ∣ ↑P.a * d - d' → d = 0) {s t : ℕ} (h₁ : s / P.w = t / P.w) (h₂ : P.next s / P.w = P.next t / P.w) :
s = t

Under hshort, a state is determined by its output and the output of its successor.

The exhaustive search of [BS23], p. 71, for a seed with first outputs r₁, r₂, r₃. The candidates are the w numbers r₁ * w + e with e < w, the numbers whose first output is r₁. The result is the greatest candidate below q whose second and third outputs are r₂ and r₃, and r₁ * w if there is none.

theorem Cslib.Crypto.PRG.LinearCongruential.seedSearch_eq (P : LinearCongruential) (hw : 0 < P.w) (hshort : ∀ (d d' : ℤ), |d| < ↑P.w → |d'| < ↑P.w → ↑P.q ∣ ↑P.a * d - d' → d = 0) {s : ℕ} (hs : s < P.q) :
P.seedSearch (s / P.w) (P.next s / P.w) (P.next (P.next s) / P.w) = s

The search recovers the seed from its first three outputs under hshort: no nonzero d with |d| < w has a * d within w of a multiple of q. Then no two states have the same first two outputs. The condition is sufficient and not necessary.

The search runs in time polynomial in w and in the length of the other inputs in binary: the caller supplies w in unary (so time is exponential in the bit length of w).

The parameters of Math.random in the Java 8 Development Kit as stated by [BS23], pp. 70–71: q = 2 ^ 48, a = 0x5DEECE66D, b = 0x0B and w = 2 ^ 22.

Seed recovery in Boneh–Shoup's Java 8 LCG model ([BS23], p. 71): with the parameters that the book gives for Java 8 Math.random, the search over the 2 ^ 22 candidates returns the seed from the model's integer outputs ⌊s_i / w⌋, for every seed. Java's Math.random returns a double; no correspondence with that interface is formalized here. The proof uses the first two outputs only.