Documentation

CslibFrontier.Crypto.Computational.Encryption.NonceBased

Nonce-based symmetric encryption and chosen-plaintext security #

A nonce-based cipher ([BS23], Section 5.5, printed pp. 201–202) consists of deterministic encryption c = E(k, m, N) and deterministic decryption m = D(k, c, N) taking an explicit nonce N alongside the message m, with correctness requiring D(k, E(k, m, N), N) = m (printed p. 202). Section 9.5 (printed p. 378) extends both algorithms with an associated-data input d, writing c = E(k, m, d, N) and D(k, c, d, N) (which outputs a message or reject). NonceCipher K M D C N represents this general syntax with Option M decryption; plain nonce-based encryption without associated data is the specialization D = Unit.

Unlike symmetricCPAGameOn / SymmetricINDCPAOn (SymmetricCPA.lean) and SymmetricCipher.cpaGame / SymmetricCipher.INDCPA (Authenticated.lean), which model randomized encryption without nonces (where multi-query CPA security requires ciphertext expansion, SymmetricCipher.not_indcpa_of_ciphertext_length_eq), a NonceCipher is deterministic and relies on nonce uniqueness across encryption queries to achieve CPA security with zero ciphertext expansion.

Attack Game 5.3 ([BS23], Section 5.5, printed p. 202) and its associated-data extension (Section 9.5, Definition 9.7, printed p. 378) restrict encryption queries to equal-length message pairs in M and pairwise distinct nonces N_i ∈ N \ {N_1, …, N_{i-1}}. To totalize this game for arbitrary adversaries without partial oracles or [Inhabited C], the typed left-or-right CPA experiment NonceCipher.cpaExperiment maintains (ok, seen) : Bool × List N, starting from (true, []):

For every admissible, nonce-respecting adversary, ok stays true and every query returns some (E.encrypt k (pair.select b) d nonce). Any run that submits an out-of-domain message pair, an out-of-domain nonce, or a repeated nonce sets ok := false independently of the challenge bit b, never calls E.encrypt outside its valid domain, requires no [Inhabited C], and outputs false under both b = false and b = true, contributing zero distinguishing advantage.

NonceCipher.INDCPA defines asymptotic uniform chosen-plaintext security at D = Unit over bit words (List Bool), adapting IsOraclePPT encBool distinguishers through the fixed canonical word codec NonceCipher.unitCPACodec, which encodes (pair, (), nonce) as frame pair.msg₀ ++ frame pair.msg₁ ++ nonce and encodes optional ciphertext replies via encOption encBits. Fixing the codec avoids free codec parameters whose padding could make runtime budgets vacuous.

This module provides definitions, the canonical word codec, and the ProbOracleComp.runWith evaluation equation for cpaExperiment. Nonce-based counter mode, Theorem 5.6, and the counter-overlap separation for unscaled nonces are in CslibFrontier.Crypto.Computational.Encryption.NonceCTR (#5793); nonce-based ciphertext integrity and AEAD (Attack Game 9.3, Definitions 9.8–9.9, printed p. 379) are deferred to their first consumer in #5865.

Main definitions #

References #

structure Cslib.Crypto.NonceCipher (K M D C N : Type) :

A deterministic nonce-based symmetric cipher with key space K, message space M, associated-data space D, ciphertext space C, and nonce space N ([BS23], Section 5.5, printed pp. 201–202, for D = Unit; Section 9.5, printed p. 378, with associated data).

  • encrypt : K → M → D → N → C

    Deterministically encrypt message m with associated data d and nonce nonce under key k.

  • decrypt : K → C → D → N → Option M

    Deterministically decrypt ciphertext c with associated data d and nonce nonce under key k, returning none on rejection.

def Cslib.Crypto.NonceCipher.Correct {K M D C N : Type} (E : NonceCipher K M D C N) :

Perfect decryption correctness ([BS23], Section 5.5, printed p. 202; Section 9.5, printed p. 378): decrypting a ciphertext under the same key, associated data, and nonce recovers the plaintext.

def Cslib.Crypto.NonceCipher.cpaOracle {K M D C N : Type} [DecidableEq N] (E : NonceCipher K M D C N) (valid : M → M → Bool) (validNonce : N → Bool) (k : K) (b : Bool) (query : MessagePair M × D × N) :

Stateful left-or-right challenge oracle for NonceCipher.cpaExperiment, totalizing the equal-length and unique-nonce query rules of [BS23], Section 5.5, Attack Game 5.3 (printed p. 202) and Section 9.5, Definition 9.7 (printed p. 378). On state (ok, seen):

  • if ok, valid pair.msg₀ pair.msg₁, validNonce nonce, and decide (nonce ∉ seen) hold, record nonce :: seen and return some (E.encrypt k (pair.select b) d nonce);
  • otherwise set ok := false and return none without invoking E.encrypt.
noncomputable def Cslib.Crypto.NonceCipher.cpaExperiment {K M D C N : Type} [DecidableEq N] (E : NonceCipher K M D C N) (sampleKey : ProbComp K) (valid : M → M → Bool) (validNonce : N → Bool) (adversary : ProbOracleComp (fun (x : MessagePair M × D × N) => Option C) Bool) (b : Bool) :

Typed left-or-right chosen-plaintext experiment for a nonce-based cipher, totalizing [BS23], Section 5.5, Attack Game 5.3 (printed p. 202) and Section 9.5, Definition 9.7 (printed p. 378). Sample k ← sampleKey, run adversary from initial state (true, []) against E.cpaOracle valid validNonce k b, and output ok && guess from the final (guess, ok, seen).

For admissible, nonce-respecting executions, ok remains true and every query returns some (E.encrypt k (pair.select b) d nonce). Any execution submitting an inadmissible pair, an invalid nonce, or a repeated nonce sets ok := false independently of b, never calls E.encrypt outside its valid domain, requires no [Inhabited C], and outputs false for both challenge bits b = false and b = true, contributing zero distinguishing advantage.

theorem Cslib.Crypto.NonceCipher.eval_cpaExperiment {K M D C N : Type} [DecidableEq N] (E : NonceCipher K M D C N) (sampleKey : ProbComp K) (valid : M → M → Bool) (validNonce : N → Bool) (adversary : ProbOracleComp (fun (x : MessagePair M × D × N) => Option C) Bool) (b : Bool) :
(E.cpaExperiment sampleKey valid validNonce adversary b).eval = sampleKey.eval.bind fun (k : K) => PMF.map (fun (x : Bool × Bool × List N) => match x with | (guess, ok, snd) => ok && guess) (ProbOracleComp.runWith (fun (q : MessagePair M × D × N) (state : Bool × List N) => (E.cpaOracle valid validNonce k b q state).eval) adversary (true, []))

Output distribution of NonceCipher.cpaExperiment via ProbOracleComp.runWith.

Canonical word codec and asymptotic CPA security #

Parse one framed prefix frame w from v, returning some (w, u) when frame w ++ u = v and none when v does not begin with a complete frame.

@[simp]

decodeFramed succeeds with (w, u) if and only if frame w ++ u = v.

Canonical parameter-free word codec for D = Unit nonce-based CPA queries: encodes (pair, (), nonce) on the single word channel as frame pair.msg₀ ++ frame pair.msg₁ ++ nonce, decodes two framed message prefixes followed by the raw nonce suffix via decodeFramed, and encodes Option (List Bool) replies with encOption encBits (none ↦ [], some ct ↦ true :: ct).

def Cslib.Crypto.NonceCipher.INDCPA (E : ℕ → NonceCipher (List Bool) (List Bool) Unit (List Bool) (List Bool)) (sampleKey : ℕ → ProbComp (List Bool)) (valid : ℕ → List Bool → List Bool → Bool) (validNonce : ℕ → List Bool → Bool) :

Adaptive chosen-plaintext security for a family of word-level nonce-based ciphers without associated data ([BS23], Section 5.5, Attack Game 5.3 and Definition 5.3, printed p. 202), against uniform strict IsOraclePPT encBool distinguishers adapted via unitCPACodec.