Documentation

CslibFrontier.Crypto.Computational.Encryption.RandomFunctionHybrid

Lazy-vs-Fresh Random Function Hybrid and Shared Block-Mode CPA Utilities #

Shared random-function hybrid and left-or-right block-mode CPA experiment machinery for symmetric block-cipher modes of operation (such as OFB, CTR and CBC):

Main definitions #

Main results #

References #

@[reducible, inline]

A lazily sampled random-function table and a flag recording a repeated input.

@[reducible, inline]

Whether a FlagState has recorded a repeated oracle input.

noncomputable def Cslib.Crypto.lazyStep {n : ℕ} (x : BitString n) :

Lazy sampling of a uniform function: a cached input returns its cached value and raises the flag, and a new input draws a uniform value.

noncomputable def Cslib.Crypto.freshStep {n : ℕ} (x : BitString n) :

Fresh sampling of a uniform function: every input draws a new uniform value, and a repeated input raises the flag.

PRF.idealExperiment against a uniform random function on BitString n equals the output of ProbOracleComp.runWith lazyStep program ([], false).

The advantage between PRF.idealExperiment and fresh independent sampling freshStep is bounded by the probability of raising the repetition flag under freshStep.

theorem Cslib.Crypto.advantage_le_two (g₀ g₁ g₂ : Game) :
↑(g₀.advantage g₂) ≤ ↑(g₀.advantage g₁) + ↑(g₂.advantage g₁)

Two-hop triangle inequality for real-valued Game.advantage.

theorem Cslib.Crypto.advantage_le_four (g₀ g₁ g₂ g₃ g₄ : Game) :
↑(g₀.advantage g₄) ≤ ↑(g₀.advantage g₁) + ↑(g₁.advantage g₂) + ↑(g₃.advantage g₂) + ↑(g₄.advantage g₃)

Four-hop symmetric triangle inequality for real-valued Game.advantage.

Shared Left-or-Right Block-Mode CPA Experiment #

@[reducible, inline]

A left-or-right query: two candidate plaintexts, each a list of n-bit blocks.

@[reducible, inline]

The answer to a left-or-right block-mode query: an initialization vector and ciphertext blocks.

@[reducible, inline]

An adaptive adversary in a left-or-right block-mode experiment, with private coins.

The plaintext encrypted for query q under challenge bit b: the right message if b = true and the left one otherwise. Queries whose messages differ in length or exceed ℓ blocks encrypt the empty message.

The number of blocks encrypted for query q: the common length of a valid query, or 0.

@[simp]

The encrypted plaintext has blockModeBlocks ℓ q blocks, regardless of the challenge bit.

theorem Cslib.Crypto.blockModeBlocks_le {n : ℕ} (ℓ : ℕ) (q : BlockModeQuery n) :

Every query encrypts at most ℓ blocks.

The cost of a call in a left-or-right block-mode experiment: private draws are free and each encryption query costs its block length.

noncomputable def Cslib.Crypto.blockModeOracle {n : ℕ} (enc : BitString n → List (BitString n) → List (BitString n)) (ℓ : ℕ) (b : Bool) (q : BlockModeQuery n) :

Generic random-IV left-or-right encryption oracle for block encryption operator enc and challenge bit b.

noncomputable def Cslib.Crypto.blockModeIdealOracle {n : ℕ} (ℓ : ℕ) (q : BlockModeQuery n) :

The ideal left-or-right oracle: a uniform initialization vector and blockModeBlocks ℓ q independent uniform ciphertext blocks, independent of the challenge bit.

noncomputable def Cslib.Crypto.blockModeCpaExperiment {n : ℕ} {Key : Type} [Fintype Key] [Nonempty Key] (enc : Key → BitString n → List (BitString n) → List (BitString n)) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) :

Generic random-IV left-or-right experiment for keyed block encryption enc and challenge bit b: sample one uniform key and answer every query with blockModeOracle (enc key) ℓ b ([KL20], Definition 3.22).

noncomputable def Cslib.Crypto.blockModeIdealExperiment {n : ℕ} (ℓ : ℕ) (adversary : BlockModeAdversary n) :

The ideal left-or-right experiment: the adversary receives fresh uniform ciphertext blocks.

noncomputable def Cslib.Crypto.blockModeQueryProgram {n : ℕ} (keystream : BitString n → ℕ → ProbOracleComp (fun (x : BitString n) => BitString n) (List (BitString n))) (ℓ : ℕ) (b : Bool) (q : BlockModeQuery n) :

One block-mode encryption computed from a keystream oracle program keystream iv k.

noncomputable def Cslib.Crypto.blockModeReduction {n : ℕ} (keystream : BitString n → ℕ → ProbOracleComp (fun (x : BitString n) => BitString n) (List (BitString n))) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) :

Generic PRF distinguisher built from a left-or-right block-mode adversary and keystream program: the adversary with each encryption query replaced by blockModeQueryProgram.

theorem Cslib.Crypto.blockModeCpaExperiment_eq_realExperiment_simulate {n : ℕ} {Key : Type} [Fintype Key] [Nonempty Key] (enc : Key → BitString n → List (BitString n) → List (BitString n)) (program : BlockModeQuery n → ProbOracleComp (fun (x : BitString n) => BitString n) (BitString n × List (BitString n))) (F : Key → BitString n → BitString n) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) (hprogram : ∀ (key : Key) (q : BlockModeQuery n), ((program q).run fun (y : BitString n) => pure (F key y)) = blockModeOracle (enc key) ℓ b q) :
blockModeCpaExperiment enc ℓ b adversary = PRF.realExperiment F (ProbOracleComp.simulate program adversary)

The real experiment of a block-mode query program. If program q, run with F key as its oracle, is the encryption oracle blockModeOracle (enc key) ℓ b q, then blockModeCpaExperiment enc is PRF.realExperiment F for the adversary run with program.

theorem Cslib.Crypto.blockModeCpaExperiment_eq_realExperiment {n : ℕ} {Key : Type} [Fintype Key] [Nonempty Key] (enc : Key → BitString n → List (BitString n) → List (BitString n)) (keystream : BitString n → ℕ → ProbOracleComp (fun (x : BitString n) => BitString n) (List (BitString n))) (F : Key → BitString n → BitString n) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) (hks : ∀ (key : Key) (iv : BitString n) (ms : List (BitString n)), ∃ (raw : List (BitString n)), ((keystream iv ms.length).run fun (y : BitString n) => pure (F key y)) = pure raw ∧ List.zipWith (fun (x1 x2 : BitString n) => x1 + x2) raw ms = enc key iv ms) :
blockModeCpaExperiment enc ℓ b adversary = PRF.realExperiment F (blockModeReduction keystream ℓ b adversary)

When keystream iv ms.length evaluates with F key to the mask XORed by enc key iv ms, blockModeCpaExperiment enc is PRF.realExperiment F (blockModeReduction keystream ℓ b adversary).

theorem Cslib.Crypto.isQueryBounded_blockModeReduction {n : ℕ} (keystream : BitString n → ℕ → ProbOracleComp (fun (x : BitString n) => BitString n) (List (BitString n))) (ℓ : ℕ) (b : Bool) {adversary : BlockModeAdversary n} {K : ℕ} (hks : ∀ (iv : BitString n), ∀ k ≤ ℓ, (keystream iv k).IsQueryBounded k) (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) :

If keystream iv k makes at most k oracle calls for every k ≤ ℓ, and adversary asks at most K blocks in total, then blockModeReduction keystream ℓ b adversary makes at most K oracle calls.

The law of the ideal oracle: a uniform initialization vector and blockModeBlocks ℓ q independent uniform blocks.

Fresh sampling of a block-mode query program. If every answer of program, under fresh sampling and with the table forgotten, has the law of the ideal oracle, then the adversary run with program under fresh sampling is blockModeIdealExperiment.

theorem Cslib.Crypto.freshGame_eq_blockModeIdealExperiment {n : ℕ} (keystream : BitString n → ℕ → ProbOracleComp (fun (x : BitString n) => BitString n) (List (BitString n))) (ℓ : ℕ) (b : Bool) (adversary : BlockModeAdversary n) (hks : ∀ (q : BlockModeQuery n) (iv : BitString n) (s : FlagState n), PMF.map Prod.fst (ProbOracleComp.runWith freshStep (keystream iv (blockModeBlocks ℓ q)) s) = uniformBlocks (blockModeBlocks ℓ q)) :

Under fresh sampling, whenever the keystream program outputs uniform blocks on every query, blockModeReduction has the exact same output distribution as blockModeIdealExperiment.

theorem Cslib.Crypto.advantage_idealExperiment_simulate_le {n : ℕ} (program : BlockModeQuery n → ProbOracleComp (fun (x : BitString n) => BitString n) (BitString n × List (BitString n))) (ℓ : ℕ) (hprogram : ∀ (q : BlockModeQuery n) (s : FlagState n), PMF.map (fun (x : (BitString n × List (BitString n)) × FlagState n) => (x.1, ())) (ProbOracleComp.runWith freshStep (program q) s) = PMF.map (fun (x : BitString n × List (BitString n)) => (x, ())) (blockModeIdealOracle ℓ q).eval) (hbad : ∀ (q : BlockModeQuery n) (s : FlagState n), ¬s.repeated = true → (ProbOracleComp.runWith freshStep (program q) s).toOuterMeasure {x : (BitString n × List (BitString n)) × FlagState n | x.2.repeated = true} ≤ ↑(blockModeBlocks ℓ q * s.1.length + (blockModeBlocks ℓ q).choose 2) * (2 ^ n)⁻¹) (hlen : ∀ (q : BlockModeQuery n) (s : FlagState n) (a : BitString n × List (BitString n)) (t : List (BitString n × BitString n)) (f : Bool), (a, t, f) ∈ (ProbOracleComp.runWith freshStep (program q) s).support → t.length ≤ s.1.length + blockModeBlocks ℓ q) {adversary : BlockModeAdversary n} {K : ℕ} (h : OracleComp.IsCostBounded (blockModeCost ℓ) adversary K) :

A block-mode query program against a uniform random function. Suppose that under fresh sampling every answer of program has the law of the ideal oracle, adds at most k = blockModeBlocks ℓ q entries to the table, and from a table of t entries raises the flag with probability at most (k t + k.choose 2) / 2 ^ n. Then against a uniform random function the adversary run with program, if its queries ask at most K blocks in total, is within K.choose 2 / 2 ^ n of blockModeIdealExperiment.