From d5d779e27145f31e5a0b0a870411bee2a165dbd0 Mon Sep 17 00:00:00 2001 From: Marc Ilunga Date: Tue, 24 Nov 2026 11:09:07 +0100 Subject: [PATCH 1/2] feat(commons): retained-key hash presentation and one-query collision lifting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `Commons.Definitions.Hash.Keyed`, giving the keyed-collision DSL systems `Hash.Real` and `Hash.Ideal` a retained-key presentation (`Hash.Real.eq_sample`, `Hash.Ideal.eq_sample`) using CC's `sample k ←$ 𝒰[K]` notation via a shared `Hash.source_eq_ofProbAutomaton` lemma. Provide the stateless one-query pair-routing converter `Hash.extractConverter` for key-independent pair extractors `extract : M' Γ— M' β†’ M Γ— M` (covering unkeyed hashes and key-independent domain extensions), along with `Hash.extractConverter_smul_real`, `Hash.extractConverter_smul_ideal`, `Hash.CR.of_extractor`, and lossless advantage transport `Hash.CR.advantage_eq_of_extractor`. Add `Tests.HashExtractor` exercising domain extension, exact advantage transport, `cc_normalize` and `cc_calc mixed` tactic interoperability, and standard-axiom checks. Closes #456 --- src/Commons.lean | 1 + src/Commons/Definitions/Hash/Keyed.lean | 254 ++++++++++++++++++++++++ src/Tests.lean | 1 + src/Tests/HashExtractor.lean | 92 +++++++++ 4 files changed, 348 insertions(+) create mode 100644 src/Commons/Definitions/Hash/Keyed.lean create mode 100644 src/Tests/HashExtractor.lean diff --git a/src/Commons.lean b/src/Commons.lean index 1c3bff1..d1e87ac 100644 --- a/src/Commons.lean +++ b/src/Commons.lean @@ -15,6 +15,7 @@ public import Commons.Definitions.CollisionBound public import Commons.Definitions.DiffieHellman public import Commons.Definitions.Encryption public import Commons.Definitions.Hash +public import Commons.Definitions.Hash.Keyed public import Commons.Definitions.Ideal public import Commons.Definitions.LazyPermutation public import Commons.Definitions.KEM diff --git a/src/Commons/Definitions/Hash/Keyed.lean b/src/Commons/Definitions/Hash/Keyed.lean new file mode 100644 index 0000000..784b21d --- /dev/null +++ b/src/Commons/Definitions/Hash/Keyed.lean @@ -0,0 +1,254 @@ +module + +public import Commons.Definitions.Hash +public import ConstructiveCryptography.CryptographicAlgebra.DistinguisherAction +public import RandomSystems.Typed.KeyedAutomaton +public import RandomSystems.Typed.ProbProgramAttachment +public import RandomSystems.Typed.QueryRouting + +/-! +# Retained-key presentation and one-query collision lifting + +`Hash.Real.eq_sample` identifies the DSL system `Hash.Real H` with one uniform key `k ←$ 𝒰[K]` +retained for the whole interaction and the deterministic replies `Hash.reply H k`: `key()` +returns `k` and `collide(mβ‚€, m₁)` returns whether `H(k, mβ‚€) = H(k, m₁)`. +`Hash.Ideal.eq_sample` gives the ideal system the replies of the identity hash. + +The DSL program draws the key on its first query; `Hash.Real.body_probCombine` gives the +optional-key transition contract, and `TypedSystem.ofProbAutomaton_eq_keyed_of_optional_key` +moves the draw to initialization, as in `Commons.Definitions.MAC.Keyed`. + +`Hash.extractConverter extract` is a stateless one-query router (`TypedConverter.routing`) +forwarding `key()` and mapping `collide p` to `collide (extract p)`. Scope: `extract : M' Γ— M' β†’ +M Γ— M` is a key-independent pair extractor, covering unkeyed hashes (`K = Unit`) and +key-independent domain extensions rather than key-dependent or multi-query extraction. On the +retained-key presentation it transports both systems (`Hash.extractConverter_smul_real`, +`Hash.extractConverter_smul_ideal`), lifting collision resistance (`Hash.CR.of_extractor`) and +exact distinguisher advantage (`Hash.CR.advantage_eq_of_extractor`). + +```text + β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” + key() ──► β”‚ forward key() β”‚ ─ key() ─► β”‚ k ←$ 𝒰[K] β”‚ + collide p──► β”‚ forward collide(extract p) β”‚ ─ ... ─► β”‚ Hash.reply H kβ”‚ + β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ +``` + +## References + +1. D. Boneh and V. Shoup. *A Graduate Course in Applied Cryptography*. Version 0.6, 2023. +-/ + +@[expose] public section + +set_option autoImplicit false + +namespace Commons + +open ConstructiveCryptography ConstructiveCryptography.CryptographicAlgebra +open SystemAlgebra SystemAlgebra.DSL Probability Probability.Distribution +open scoped SystemAlgebra SystemAlgebra.DSL + +noncomputable section + +-- `K` carries keys and `M` messages; replies of `collide` are Booleans. +variable {K M T : Type} [Fintype K] [Nonempty K] [Fintype M] + +/-- Deterministic collision-system replies at key `k`: `key()` returns `k`, and +`collide(mβ‚€, m₁)` returns `decide (H k mβ‚€ = H k m₁)` [1, Attack Game 8.1, p. 290]. -/ +def Hash.reply (H : K β†’ M β†’ T) [DecidableEq T] (k : K) : + -- Budget `0` names the port alphabets, which are shared by every query budget. + βˆ€ i, (KeyedCollision K M 0).X i β†’ (KeyedCollision K M 0).Y i + | .key => fun _ => k + | .collide => fun p => decide (H k p.1 = H k p.2) + +omit [Nonempty K] in +/-- A single fresh source is a one-state fresh probabilistic automaton. -/ +lemma Hash.source_eq_ofProbAutomaton (P : ProbDist K) (n : β„•) : + TypedSystem.source P n = + TypedSystem.ofProbAutomaton (Interface.source K n) (ProbStep.fresh fun _ _ => P) + (ProbDist.single ()) := by + rw [TypedSystem.source_eq_ofMemoryless] + exact Subtype.ext (RandomSystem.ofProbAutomaton_eq_memoryless + (Interface.source K n).inputDomain (ProbStep.memoryless_fresh (fun _ _ => P)) + (ProbDist.single ())).symm + +/-- The DSL body draws a missing key once, then keeps it and replies by `Hash.reply H k`. -/ +lemma Hash.Real.body_probCombine + (H : K β†’ M β†’ T) [DecidableEq T] (keys : Option K) + (i : KeyedCollision.Port) (x : (KeyedCollision K M 0).X i) : + (Hash.Real.body H).probCombine (ProbStep.fresh fun _ _ => Hash.Real.law1 H) + (Hash.Real.bound_spec H) (keys, ()) i x = + match keys with + | none => (ProbDist.uniform K).bind fun k => + (ProbDist.single (Hash.reply H k i x)).map (fun y => ((some k, ()), y)) + | some k => (ProbDist.single (Hash.reply H k i x)).map (fun y => ((some k, ()), y)) := by + apply Subtype.ext + cases keys <;> cases i <;> + simp only [Program.probCombine, Program.probInvoke, Hash.Real.body, + callProg, returnProg, ProbStep.outcome, ProbStep.fresh, ProbDist.map_val, + fTransform_fTransform, Function.comp_def, ProbStep.tagged, + List.nil_append, ↓reduceDIte, Hash.reply, ProbDist.bind_val, + ProbDist.single, fTransform_single, bindK_single_comp] <;> rfl + +/-- **Retained-key presentation**: `Hash.Real H` samples one `k ←$ 𝒰[K]` and answers every +query by `Hash.reply H k`. -/ +theorem Hash.Real.eq_sample (H : K β†’ M β†’ T) [DecidableEq T] (q : β„•) : + Hash.Real (budget := q) H = + (sample k ←$ 𝒰[K]; + TypedSystem.ofFunction (A := KeyedCollision K M q) (Hash.reply H k)) := by + rw [Hash.Real, Hash.Real.program, Hash.Real.randomness, Hash.source_eq_ofProbAutomaton, + TypedConverter.ofProgram_smul_ofProbAutomaton] + have hi : (ProbDist.single ()).map (fun r => ((none : Option K), r)) = + ProbDist.single (none, ()) := Subtype.ext (fTransform_single _ _ _) + rw [hi] + exact (TypedSystem.ofProbAutomaton_eq_keyed_of_optional_key + (A := KeyedCollision K M q) (ProbDist.uniform K) + (fun k i x => ProbDist.single (Hash.reply H k i x)) _ (by + intro keys i x + cases keys <;> exact Hash.Real.body_probCombine H _ i x)).trans + (TypedSystem.keyed_single _ _) + +/-- The ideal DSL body draws a missing key once, then keeps it and replies by the identity +hash's `Hash.reply`. -/ +lemma Hash.Ideal.body_probCombine [DecidableEq M] (keys : Option K) + (i : KeyedCollision.Port) (x : (KeyedCollision K M 0).X i) : + (Hash.Ideal.body K M).probCombine (ProbStep.fresh fun _ _ => Hash.Ideal.law1 K M) + (Hash.Ideal.bound_spec K M) (keys, ()) i x = + match keys with + | none => (ProbDist.uniform K).bind fun k => + (ProbDist.single (Hash.reply (fun (_ : K) (m : M) => m) k i x)).map + (fun y => ((some k, ()), y)) + | some k => (ProbDist.single (Hash.reply (fun (_ : K) (m : M) => m) k i x)).map + (fun y => ((some k, ()), y)) := by + apply Subtype.ext + cases keys <;> cases i <;> + simp only [Program.probCombine, Program.probInvoke, Hash.Ideal.body, + callProg, returnProg, ProbStep.outcome, ProbStep.fresh, ProbDist.map_val, + fTransform_fTransform, Function.comp_def, ProbStep.tagged, + List.nil_append, ↓reduceDIte, Hash.reply, ProbDist.bind_val, + ProbDist.single, fTransform_single, bindK_single_comp] <;> rfl + +/-- **Retained-key presentation of `Hash.Ideal`**: one `k ←$ 𝒰[K]` with identity-hash replies. -/ +theorem Hash.Ideal.eq_sample [DecidableEq M] (q : β„•) : + Hash.Ideal K M (budget := q) = + (sample k ←$ 𝒰[K]; + TypedSystem.ofFunction (A := KeyedCollision K M q) + (Hash.reply (fun (_ : K) (m : M) => m) k)) := by + rw [Hash.Ideal, Hash.Ideal.program, Hash.Ideal.randomness, Hash.source_eq_ofProbAutomaton, + TypedConverter.ofProgram_smul_ofProbAutomaton] + have hi : (ProbDist.single ()).map (fun r => ((none : Option K), r)) = + ProbDist.single (none, ()) := Subtype.ext (fTransform_single _ _ _) + rw [hi] + exact (TypedSystem.ofProbAutomaton_eq_keyed_of_optional_key + (A := KeyedCollision K M q) (ProbDist.uniform K) + (fun k i x => ProbDist.single (Hash.reply (fun (_ : K) (m : M) => m) k i x)) _ (by + intro keys i x + cases keys <;> exact Hash.Ideal.body_probCombine _ i x)).trans + (TypedSystem.keyed_single _ _) + +/-! ### Key-independent one-query extraction converter -/ + +-- `M'` carries the outer messages whose candidate pairs are translated to pairs in `M`. +variable {M' : Type} [Fintype M'] + +/-- Forward `key()` and map `collide p` to `collide (extract p)`. -/ +def Hash.extractRequest (extract : M' Γ— M' β†’ M Γ— M) {q : β„•} : + (Ξ£ o, (KeyedCollision K M' q).X o) β†’ Ξ£ j, (KeyedCollision K M q).X j + | ⟨.key, u⟩ => ⟨.key, u⟩ + | ⟨.collide, p⟩ => ⟨.collide, extract p⟩ + +/-- Return the inside reply at the forwarded port unchanged (off-port branches are unreachable). -/ +def Hash.extractAnswer {q : β„•} : + (u : Ξ£ o, (KeyedCollision K M' q).X o) β†’ (Ξ£ j, (KeyedCollision K M q).Y j) β†’ + (KeyedCollision K M' q).Y u.1 + | ⟨.key, _⟩, ⟨.key, k⟩ => k + | ⟨.key, _⟩, ⟨.collide, _⟩ => Classical.arbitrary K + | ⟨.collide, _⟩, ⟨.collide, b⟩ => b + | ⟨.collide, _⟩, ⟨.key, _⟩ => false + +omit [Nonempty K] in +/-- Mapping each outside query to one inside query preserves the query-budget domain. -/ +lemma Hash.extractRequest_domain (extract : M' Γ— M' β†’ M Γ— M) {q : β„•} + (us : List (Ξ£ o, (KeyedCollision K M' q).X o)) (hus : (KeyedCollision K M' q).domain us) : + (KeyedCollision K M q).domain (us.map (Hash.extractRequest extract)) := + ⟨fun he => hus.1 (List.map_eq_nil_iff.mp he), (List.length_map _).trans_le hus.2⟩ + +/-- **Key-independent one-query extraction converter** (stateless router; covers unkeyed hashes +and key-independent domain extensions). -/ +def Hash.extractConverter (extract : M' Γ— M' β†’ M Γ— M) (q : β„•) : + KeyedCollision K M' q ⟢ KeyedCollision K M q := + TypedConverter.routing (Hash.extractRequest extract) Hash.extractAnswer + (Hash.extractRequest_domain extract) + +/-- Transport of fixed-key collision replies through `Hash.extractConverter`. -/ +lemma Hash.extractConverter_smul_ofFunction {T' : Type} [DecidableEq T] [DecidableEq T'] + (H : K β†’ M β†’ T) (G : K β†’ M' β†’ T') (extract : M' Γ— M' β†’ M Γ— M) (q : β„•) (k : K) + (hreal : βˆ€ p, H k (extract p).1 = H k (extract p).2 ↔ G k p.1 = G k p.2) : + Hash.extractConverter extract q β€’ TypedSystem.ofFunction (A := KeyedCollision K M q) + (Hash.reply H k) = + TypedSystem.ofFunction (A := KeyedCollision K M' q) (Hash.reply G k) := by + rw [Hash.extractConverter, TypedConverter.routing_smul_ofFunction] + congr 1 + funext o u + cases o + Β· rfl + Β· exact decide_eq_decide.mpr (hreal u) + +/-- **Real-system transport**: `Hash.extractConverter extract q β€’ Hash.Real H = Hash.Real G`. -/ +theorem Hash.extractConverter_smul_real {T' : Type} [DecidableEq T] [DecidableEq T'] + (H : K β†’ M β†’ T) (G : K β†’ M' β†’ T') (extract : M' Γ— M' β†’ M Γ— M) (q : β„•) + (hreal : βˆ€ k p, H k (extract p).1 = H k (extract p).2 ↔ G k p.1 = G k p.2) : + Hash.extractConverter extract q β€’ Hash.Real (budget := q) H = + Hash.Real (budget := q) G := by + rw [Hash.Real.eq_sample, Hash.Real.eq_sample, TypedConverter.smul_sample] + congr 1 + funext k + exact Hash.extractConverter_smul_ofFunction H G extract q k (hreal k) + +/-- **Ideal-system transport**: +`Hash.extractConverter extract q β€’ Hash.Ideal K M = Hash.Ideal K M'`. -/ +theorem Hash.extractConverter_smul_ideal [DecidableEq M] [DecidableEq M'] + (extract : M' Γ— M' β†’ M Γ— M) (q : β„•) + (hideal : βˆ€ p, (extract p).1 = (extract p).2 ↔ p.1 = p.2) : + Hash.extractConverter extract q β€’ Hash.Ideal K M (budget := q) = + Hash.Ideal K M' (budget := q) := by + rw [Hash.Ideal.eq_sample, Hash.Ideal.eq_sample, TypedConverter.smul_sample] + congr 1 + funext k + exact Hash.extractConverter_smul_ofFunction _ _ extract q k hideal + +/-! ### Collision lifting through the extraction converter -/ + +/-- **One-query collision lifting**: a key-independent collision/equality-preserving extractor +lifts `Hash.CR H error` to `Hash.CR G (d ↦ error (dΞ±))`. -/ +theorem Hash.CR.of_extractor {T' : Type} [DecidableEq M] [DecidableEq M'] + [DecidableEq T] [DecidableEq T'] [AdmissibleDistinguishers Interface TypedDistinguisher] + {q : β„•} + (H : K β†’ M β†’ T) (G : K β†’ M' β†’ T') (extract : M' Γ— M' β†’ M Γ— M) + (hreal : βˆ€ k p, H k (extract p).1 = H k (extract p).2 ↔ G k p.1 = G k p.2) + (hideal : βˆ€ p, (extract p).1 = (extract p).2 ↔ p.1 = p.2) + (closed : AdmissibleDistinguishers.ClosedUnder (Phi := TypedSystem) + (D := TypedDistinguisher) (Hash.extractConverter (K := K) extract q)) + {error : TypedDistinguisher (KeyedCollision K M q) β†’ ENNReal} (h : Hash.CR H error) : + Hash.CR G (fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter extract q))) := + AdmissibleDistinguishers.substitutesWithin_context (Hash.extractConverter extract q) closed h + (Hash.extractConverter_smul_real H G extract q hreal) + (Hash.extractConverter_smul_ideal extract q hideal) + +/-- **Lossless advantage transport** for every outer collision distinguisher `d`. -/ +theorem Hash.CR.advantage_eq_of_extractor {T' : Type} [DecidableEq M] [DecidableEq M'] + [DecidableEq T] [DecidableEq T'] {q : β„•} + (H : K β†’ M β†’ T) (G : K β†’ M' β†’ T') (extract : M' Γ— M' β†’ M Γ— M) + (hreal : βˆ€ k p, H k (extract p).1 = H k (extract p).2 ↔ G k p.1 = G k p.2) + (hideal : βˆ€ p, (extract p).1 = (extract p).2 ↔ p.1 = p.2) + (d : TypedDistinguisher (KeyedCollision K M' q)) : + d.advantage (Hash.Real (budget := q) G) (Hash.Ideal K M' (budget := q)) = + (TypedDistinguisher.absorb d (Hash.extractConverter extract q)).advantage + (Hash.Real (budget := q) H) (Hash.Ideal K M (budget := q)) := by + rw [← Hash.extractConverter_smul_real H G extract q hreal, + ← Hash.extractConverter_smul_ideal (K := K) extract q hideal] + rfl + +end + +end Commons diff --git a/src/Tests.lean b/src/Tests.lean index 2aa0e7e..c0ab5fa 100644 --- a/src/Tests.lean +++ b/src/Tests.lean @@ -91,6 +91,7 @@ public import Tests.RandomFunctionCollision public import Tests.RatioCost public import Tests.FilteredRatio public import Tests.CompositeNotation +public import Tests.HashExtractor /-! Axiom audits per layer and of the Commons library, the checks of the authenticated-encryption example, diff --git a/src/Tests/HashExtractor.lean b/src/Tests/HashExtractor.lean new file mode 100644 index 0000000..f3a1504 --- /dev/null +++ b/src/Tests/HashExtractor.lean @@ -0,0 +1,92 @@ +module + +public import Commons.Definitions.Hash.Keyed +public import ConstructiveCryptography.Notation +public import ConstructiveCryptography.Tactics.Substitution + +/-! +# Tests for retained-key hash presentations and one-query collision lifting + +Exercises: +* Retained-key presentations `Hash.Real.eq_sample` and `Hash.Ideal.eq_sample` using CC's + `sample k ←$ 𝒰[K]` notation. +* Key-independent domain extension `G k = H k ∘ f` via `Hash.extractConverter (mapPair f) q`, + `Hash.CR.of_extractor`, and `Hash.CR.advantage_eq_of_extractor`. +* Tactic interoperability: `cc_normalize` with `Hash.extractConverter` under parallel composition + and retained sampling, and `cc_calc mixed` chaining a lifted `Hash.CR` (`≃[...]`) step with a + statistical (`β‰ˆ[...]`) step. +-/ + +@[expose] public section + +set_option autoImplicit false + +namespace Tests.HashExtractor + +open CategoryTheory +open ConstructiveCryptography ConstructiveCryptography.CryptographicAlgebra +open SystemAlgebra Probability Commons +open scoped SystemAlgebra SystemAlgebra.DSL + +-- Every typed distinguisher is admitted, locally to this test module. +local instance allTests : AdmissibleDistinguishers Interface TypedDistinguisher := + AdmissibleDistinguishers.all + +variable {K M M' T : Type} [Fintype K] [Nonempty K] [Fintype M] [Fintype M'] + [DecidableEq M] [DecidableEq M'] [DecidableEq T] + +/-- Key-independent pair extractor for domain extension by `f`. -/ +def mapPair (f : M' β†’ M) (p : M' Γ— M') : M Γ— M := (f p.1, f p.2) + +/-- Domain extension preserves collision resistance with error `d ↦ error (dΞ±)`. -/ +lemma domainExtension_CR (H : K β†’ M β†’ T) (f : M' β†’ M) (hf : Function.Injective f) {q : β„•} + {error : TypedDistinguisher (KeyedCollision K M q) β†’ ENNReal} (h : Hash.CR H error) : + Hash.CR (fun k m => H k (f m)) + (fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q))) := + Hash.CR.of_extractor H _ (mapPair f) (fun _ _ => Iff.rfl) + (fun _ => hf.eq_iff) (fun _ _ => Set.mem_univ _) h + +/-- Lossless advantage transport for every outer distinguisher. -/ +example (H : K β†’ M β†’ T) (f : M' β†’ M) (hf : Function.Injective f) {q : β„•} + (d : TypedDistinguisher (KeyedCollision K M' q)) : + d.advantage (Hash.Real (budget := q) (fun k m => H k (f m))) + (Hash.Ideal K M' (budget := q)) = + (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q)).advantage + (Hash.Real (budget := q) H) (Hash.Ideal K M (budget := q)) := + Hash.CR.advantage_eq_of_extractor H _ (mapPair f) (fun _ _ => Iff.rfl) (fun _ => hf.eq_iff) d + +/-- `cc_normalize` distributes `Hash.extractConverter` across parallel composition and pushes it +inside the retained-key sample `sample k ←$ 𝒰[K]`. -/ +example {B : Interface} (H : K β†’ M β†’ T) (f : M' β†’ M) (q : β„•) (aux : TypedSystem B) : + (Hash.extractConverter (K := K) (mapPair f) q βŠ—β‚˜ πŸ™ B) β€’ + (Hash.Real (budget := q) H βˆ₯ aux) = + (sample k ←$ 𝒰[K]; + Hash.extractConverter (K := K) (mapPair f) q β€’ + TypedSystem.ofFunction (A := KeyedCollision K M q) (Hash.reply H k)) βˆ₯ aux := by + rw [Hash.Real.eq_sample] + cc_normalize + +/-- `cc_calc mixed` chains a lifted `Hash.CR` substitution step (`≃[...]`) with a statistical +distance step (`β‰ˆ[...]`) without leaving open goals. -/ +example (H : K β†’ M β†’ T) (f : M' β†’ M) (hf : Function.Injective f) {q : β„•} + {error : TypedDistinguisher (KeyedCollision K M q) β†’ ENNReal} {Ξ΄ : ENNReal} + {S : TypedSystem (KeyedCollision K M' q)} + (h : Hash.CR H error) + (hstat : Ξ” (Hash.Ideal K M' (budget := q)) S ≀ Ξ΄) : + Hash.Real (budget := q) (fun k m => H k (f m)) ≃[fun d => + error (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q)) + Ξ΄] S := by + cc_calc mixed + Hash.Real (budget := q) (fun k m => H k (f m)) + ≃[fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q))] + Hash.Ideal K M' (budget := q) := domainExtension_CR H f hf h + _ β‰ˆ[Ξ΄] S := hstat + +#print axioms Hash.source_eq_ofProbAutomaton +#print axioms Hash.Real.eq_sample +#print axioms Hash.Ideal.eq_sample +#print axioms Hash.extractConverter_smul_real +#print axioms Hash.extractConverter_smul_ideal +#print axioms Hash.CR.of_extractor +#print axioms Hash.CR.advantage_eq_of_extractor + +end Tests.HashExtractor From 545d1066f6ec1e268df00fd6afe29b8aac0d07d9 Mon Sep 17 00:00:00 2001 From: Marc Ilunga Date: Tue, 24 Nov 2026 13:25:20 +0100 Subject: [PATCH 2/2] Derive ideal collision transport from the identity hash --- src/Commons/Definitions/Hash/Keyed.lean | 74 ++++++------------------- src/RandomSystems/Typed/Memoryless.lean | 13 ++++- src/RandomSystems/Typed/README.md | 24 ++++---- src/Tests/HashExtractor.lean | 23 ++++---- 4 files changed, 51 insertions(+), 83 deletions(-) diff --git a/src/Commons/Definitions/Hash/Keyed.lean b/src/Commons/Definitions/Hash/Keyed.lean index 784b21d..578618a 100644 --- a/src/Commons/Definitions/Hash/Keyed.lean +++ b/src/Commons/Definitions/Hash/Keyed.lean @@ -12,17 +12,15 @@ public import RandomSystems.Typed.QueryRouting `Hash.Real.eq_sample` identifies the DSL system `Hash.Real H` with one uniform key `k ←$ 𝒰[K]` retained for the whole interaction and the deterministic replies `Hash.reply H k`: `key()` returns `k` and `collide(mβ‚€, m₁)` returns whether `H(k, mβ‚€) = H(k, m₁)`. -`Hash.Ideal.eq_sample` gives the ideal system the replies of the identity hash. +`Hash.Ideal.eq_sample` specializes this presentation to the identity hash. -The DSL program draws the key on its first query; `Hash.Real.body_probCombine` gives the +The DSL program draws the key on its first query. `Hash.Real.body_probCombine` gives the optional-key transition contract, and `TypedSystem.ofProbAutomaton_eq_keyed_of_optional_key` moves the draw to initialization, as in `Commons.Definitions.MAC.Keyed`. -`Hash.extractConverter extract` is a stateless one-query router (`TypedConverter.routing`) -forwarding `key()` and mapping `collide p` to `collide (extract p)`. Scope: `extract : M' Γ— M' β†’ -M Γ— M` is a key-independent pair extractor, covering unkeyed hashes (`K = Unit`) and -key-independent domain extensions rather than key-dependent or multi-query extraction. On the -retained-key presentation it transports both systems (`Hash.extractConverter_smul_real`, +`Hash.extractConverter extract` forwards `key()` and maps `collide p` to one inside query +`collide (extract p)`. The pair map `extract` is independent of the key. +The same converter transports both systems (`Hash.extractConverter_smul_real`, `Hash.extractConverter_smul_ideal`), lifting collision resistance (`Hash.CR.of_extractor`) and exact distinguisher advantage (`Hash.CR.advantage_eq_of_extractor`). @@ -61,17 +59,6 @@ def Hash.reply (H : K β†’ M β†’ T) [DecidableEq T] (k : K) : | .key => fun _ => k | .collide => fun p => decide (H k p.1 = H k p.2) -omit [Nonempty K] in -/-- A single fresh source is a one-state fresh probabilistic automaton. -/ -lemma Hash.source_eq_ofProbAutomaton (P : ProbDist K) (n : β„•) : - TypedSystem.source P n = - TypedSystem.ofProbAutomaton (Interface.source K n) (ProbStep.fresh fun _ _ => P) - (ProbDist.single ()) := by - rw [TypedSystem.source_eq_ofMemoryless] - exact Subtype.ext (RandomSystem.ofProbAutomaton_eq_memoryless - (Interface.source K n).inputDomain (ProbStep.memoryless_fresh (fun _ _ => P)) - (ProbDist.single ())).symm - /-- The DSL body draws a missing key once, then keeps it and replies by `Hash.reply H k`. -/ lemma Hash.Real.body_probCombine (H : K β†’ M β†’ T) [DecidableEq T] (keys : Option K) @@ -96,11 +83,13 @@ theorem Hash.Real.eq_sample (H : K β†’ M β†’ T) [DecidableEq T] (q : β„•) : Hash.Real (budget := q) H = (sample k ←$ 𝒰[K]; TypedSystem.ofFunction (A := KeyedCollision K M q) (Hash.reply H k)) := by - rw [Hash.Real, Hash.Real.program, Hash.Real.randomness, Hash.source_eq_ofProbAutomaton, + rw [Hash.Real, Hash.Real.program, Hash.Real.randomness, TypedSystem.source_eq_ofProbAutomaton, TypedConverter.ofProgram_smul_ofProbAutomaton] + -- Before the first query the hash holds no key and the fresh source has unit state. have hi : (ProbDist.single ()).map (fun r => ((none : Option K), r)) = ProbDist.single (none, ()) := Subtype.ext (fTransform_single _ _ _) rw [hi] + -- A first-query draw retained on every later query can be sampled at initialization. exact (TypedSystem.ofProbAutomaton_eq_keyed_of_optional_key (A := KeyedCollision K M q) (ProbDist.uniform K) (fun k i x => ProbDist.single (Hash.reply H k i x)) _ (by @@ -108,43 +97,13 @@ theorem Hash.Real.eq_sample (H : K β†’ M β†’ T) [DecidableEq T] (q : β„•) : cases keys <;> exact Hash.Real.body_probCombine H _ i x)).trans (TypedSystem.keyed_single _ _) -/-- The ideal DSL body draws a missing key once, then keeps it and replies by the identity -hash's `Hash.reply`. -/ -lemma Hash.Ideal.body_probCombine [DecidableEq M] (keys : Option K) - (i : KeyedCollision.Port) (x : (KeyedCollision K M 0).X i) : - (Hash.Ideal.body K M).probCombine (ProbStep.fresh fun _ _ => Hash.Ideal.law1 K M) - (Hash.Ideal.bound_spec K M) (keys, ()) i x = - match keys with - | none => (ProbDist.uniform K).bind fun k => - (ProbDist.single (Hash.reply (fun (_ : K) (m : M) => m) k i x)).map - (fun y => ((some k, ()), y)) - | some k => (ProbDist.single (Hash.reply (fun (_ : K) (m : M) => m) k i x)).map - (fun y => ((some k, ()), y)) := by - apply Subtype.ext - cases keys <;> cases i <;> - simp only [Program.probCombine, Program.probInvoke, Hash.Ideal.body, - callProg, returnProg, ProbStep.outcome, ProbStep.fresh, ProbDist.map_val, - fTransform_fTransform, Function.comp_def, ProbStep.tagged, - List.nil_append, ↓reduceDIte, Hash.reply, ProbDist.bind_val, - ProbDist.single, fTransform_single, bindK_single_comp] <;> rfl - /-- **Retained-key presentation of `Hash.Ideal`**: one `k ←$ 𝒰[K]` with identity-hash replies. -/ theorem Hash.Ideal.eq_sample [DecidableEq M] (q : β„•) : Hash.Ideal K M (budget := q) = (sample k ←$ 𝒰[K]; TypedSystem.ofFunction (A := KeyedCollision K M q) - (Hash.reply (fun (_ : K) (m : M) => m) k)) := by - rw [Hash.Ideal, Hash.Ideal.program, Hash.Ideal.randomness, Hash.source_eq_ofProbAutomaton, - TypedConverter.ofProgram_smul_ofProbAutomaton] - have hi : (ProbDist.single ()).map (fun r => ((none : Option K), r)) = - ProbDist.single (none, ()) := Subtype.ext (fTransform_single _ _ _) - rw [hi] - exact (TypedSystem.ofProbAutomaton_eq_keyed_of_optional_key - (A := KeyedCollision K M q) (ProbDist.uniform K) - (fun k i x => ProbDist.single (Hash.reply (fun (_ : K) (m : M) => m) k i x)) _ (by - intro keys i x - cases keys <;> exact Hash.Ideal.body_probCombine _ i x)).trans - (TypedSystem.keyed_single _ _) + (Hash.reply (fun (_ : K) (m : M) => m) k)) := + Hash.Real.eq_sample (fun _ : K => @id M) q /-! ### Key-independent one-query extraction converter -/ @@ -173,8 +132,7 @@ lemma Hash.extractRequest_domain (extract : M' Γ— M' β†’ M Γ— M) {q : β„•} (KeyedCollision K M q).domain (us.map (Hash.extractRequest extract)) := ⟨fun he => hus.1 (List.map_eq_nil_iff.mp he), (List.length_map _).trans_le hus.2⟩ -/-- **Key-independent one-query extraction converter** (stateless router; covers unkeyed hashes -and key-independent domain extensions). -/ +/-- Forward a key request unchanged and send each candidate pair `p` to `extract p` in one call. -/ def Hash.extractConverter (extract : M' Γ— M' β†’ M Γ— M) (q : β„•) : KeyedCollision K M' q ⟢ KeyedCollision K M q := TypedConverter.routing (Hash.extractRequest extract) Hash.extractAnswer @@ -211,11 +169,9 @@ theorem Hash.extractConverter_smul_ideal [DecidableEq M] [DecidableEq M'] (extract : M' Γ— M' β†’ M Γ— M) (q : β„•) (hideal : βˆ€ p, (extract p).1 = (extract p).2 ↔ p.1 = p.2) : Hash.extractConverter extract q β€’ Hash.Ideal K M (budget := q) = - Hash.Ideal K M' (budget := q) := by - rw [Hash.Ideal.eq_sample, Hash.Ideal.eq_sample, TypedConverter.smul_sample] - congr 1 - funext k - exact Hash.extractConverter_smul_ofFunction _ _ extract q k hideal + Hash.Ideal K M' (budget := q) := + Hash.extractConverter_smul_real (fun _ : K => @id M) (fun _ : K => @id M') extract q + (fun _ => hideal) /-! ### Collision lifting through the extraction converter -/ @@ -225,8 +181,10 @@ theorem Hash.CR.of_extractor {T' : Type} [DecidableEq M] [DecidableEq M'] [DecidableEq T] [DecidableEq T'] [AdmissibleDistinguishers Interface TypedDistinguisher] {q : β„•} (H : K β†’ M β†’ T) (G : K β†’ M' β†’ T') (extract : M' Γ— M' β†’ M Γ— M) + -- `hreal` preserves digest equality at each key, and `hideal` preserves message equality. (hreal : βˆ€ k p, H k (extract p).1 = H k (extract p).2 ↔ G k p.1 = G k p.2) (hideal : βˆ€ p, (extract p).1 = (extract p).2 ↔ p.1 = p.2) + -- `closed` admits each outside observer after it absorbs this router. (closed : AdmissibleDistinguishers.ClosedUnder (Phi := TypedSystem) (D := TypedDistinguisher) (Hash.extractConverter (K := K) extract q)) {error : TypedDistinguisher (KeyedCollision K M q) β†’ ENNReal} (h : Hash.CR H error) : diff --git a/src/RandomSystems/Typed/Memoryless.lean b/src/RandomSystems/Typed/Memoryless.lean index 73fe46e..0e3c5ef 100644 --- a/src/RandomSystems/Typed/Memoryless.lean +++ b/src/RandomSystems/Typed/Memoryless.lean @@ -10,7 +10,8 @@ public import RandomSystems.System.Memoryless `TypedSystem.ofMemoryless A Ξ½` answers each admitted query `i(x)` with a fresh reply `y ∼ Ξ½ i x`, independently of earlier exchanges [1, Definition 3.1, p. 55; Β§3.6.1, p. 65]. -`parallel_source_eq_ofProbAutomaton` presents two independent sources as one fresh automaton. +`source_eq_ofProbAutomaton` presents a source as a one-state automaton with fresh replies. +`parallel_source_eq_ofProbAutomaton` gives the corresponding presentation for two sources. `ofMemoryless_val` identifies this typed system with its cumulative behavior; `eq_memoryless_of_eq_ofMemoryless` projects a supplied typed contract to that behavior. @@ -156,6 +157,16 @@ lemma source_eq_ofMemoryless source law q = ofMemoryless (Interface.source Z q) (fun _ _ => law) := Subtype.ext (source_eq_memoryless law q) +/-- At each of the `q` allowed queries the source draws a fresh reply from `law` +and returns to its unique state. -/ +lemma source_eq_ofProbAutomaton {Z : Type} [Fintype Z] (law : ProbDist Z) (q : β„•) : + source law q = + ofProbAutomaton (Interface.source Z q) (ProbStep.fresh fun _ _ => law) + (ProbDist.single ()) := by + rw [source_eq_ofMemoryless] + exact Subtype.ext (RandomSystem.ofProbAutomaton_eq_memoryless _ + (ProbStep.memoryless_fresh (fun _ _ => law)) (ProbDist.single ())).symm + /-- Independent components reply according to the queried port's law, `parallelLaw ν₁ Ξ½β‚‚`. The raw parallel law is transported through the tensor's port labels. -/ lemma parallel_eq_memoryless diff --git a/src/RandomSystems/Typed/README.md b/src/RandomSystems/Typed/README.md index 2b3cf57..37c33bb 100644 --- a/src/RandomSystems/Typed/README.md +++ b/src/RandomSystems/Typed/README.md @@ -132,9 +132,10 @@ invariant at admitted queries gives equality of typed systems. [Point transitions][TypedSystem.ofProbAutomaton_ofDeterministic] recover `ofAutomaton`. A [**memoryless system**][TypedSystem.ofMemoryless] answers each admitted query `i(x)` with a fresh draw from `Ξ½ i x`. In `TypedSystem.ofMemoryless A Ξ½`, `A` selects the -interface and its declared domain. The [source][TypedSystem.source_eq_ofMemoryless] and -[parallel][TypedSystem.parallel_ofMemoryless] equations rewrite assemblies directly as typed -systems. A [bounded program][TypedConverter.ofProgram_smul_ofMemoryless], +interface and its declared domain. A [source][TypedSystem.source_eq_ofMemoryless] has a +[one-state automaton presentation][TypedSystem.source_eq_ofProbAutomaton]. +[Parallel composition][TypedSystem.parallel_ofMemoryless] combines the components' reply laws. +A [bounded program][TypedConverter.ofProgram_smul_ofMemoryless], [port-preserving program][TypedConverter.ofPreservingProgram_smul_ofMemoryless] or [costed program][TypedConverter.ofCostedProgram_smul_ofMemoryless] attached to `ofMemoryless B Ξ½` gives `ofMemoryless A ΞΊ` when its one-invocation reply law is `ΞΊ` in every program state. Supply @@ -311,14 +312,15 @@ typed system sampling the ideal function, under the fixed-query factorization. [TypedSystem.ofProbAutomaton]: ProbAutomaton.lean [TypedSystem.ofProbAutomaton_eq_of_simulation]: ProbAutomaton.lean [TypedSystem.ofProbAutomaton_ofDeterministic]: ProbAutomaton.lean -[TypedSystem.ofMemoryless]: Memoryless.lean#L59 -[TypedSystem.source_eq_ofMemoryless]: Memoryless.lean#L153 -[TypedSystem.parallel_ofMemoryless]: Memoryless.lean#L233 -[TypedSystem.parallel_eq_ofMemoryless]: Memoryless.lean#L258 -[TypedConverter.ofProgram_smul_ofMemoryless]: Memoryless.lean#L367 -[TypedConverter.ofPreservingProgram_smul_ofMemoryless]: Memoryless.lean#L385 -[TypedConverter.ofCostedProgram_smul_ofMemoryless]: Memoryless.lean#L406 -[TypedConverter.ofPreservingProgram_comp_rightContext_smul_memoryless]: Memoryless.lean#L338 +[TypedSystem.ofMemoryless]: Memoryless.lean#L60 +[TypedSystem.source_eq_ofMemoryless]: Memoryless.lean#L154 +[TypedSystem.source_eq_ofProbAutomaton]: Memoryless.lean#L162 +[TypedSystem.parallel_ofMemoryless]: Memoryless.lean#L244 +[TypedSystem.parallel_eq_ofMemoryless]: Memoryless.lean#L269 +[TypedConverter.ofProgram_smul_ofMemoryless]: Memoryless.lean#L378 +[TypedConverter.ofPreservingProgram_smul_ofMemoryless]: Memoryless.lean#L396 +[TypedConverter.ofCostedProgram_smul_ofMemoryless]: Memoryless.lean#L417 +[TypedConverter.ofPreservingProgram_comp_rightContext_smul_memoryless]: Memoryless.lean#L349 [TypedSystem.ofFunction]: Functional.lean#L62 [TypedSystem.ofConditional]: Functional.lean#L44 [TypedSystem.sample]: Functional.lean#L183 diff --git a/src/Tests/HashExtractor.lean b/src/Tests/HashExtractor.lean index f3a1504..039e647 100644 --- a/src/Tests/HashExtractor.lean +++ b/src/Tests/HashExtractor.lean @@ -10,7 +10,7 @@ public import ConstructiveCryptography.Tactics.Substitution Exercises: * Retained-key presentations `Hash.Real.eq_sample` and `Hash.Ideal.eq_sample` using CC's `sample k ←$ 𝒰[K]` notation. -* Key-independent domain extension `G k = H k ∘ f` via `Hash.extractConverter (mapPair f) q`, +* Key-independent domain extension `G k = H k ∘ f` via `Hash.extractConverter (Prod.map f f) q`, `Hash.CR.of_extractor`, and `Hash.CR.advantage_eq_of_extractor`. * Tactic interoperability: `cc_normalize` with `Hash.extractConverter` under parallel composition and retained sampling, and `cc_calc mixed` chaining a lifted `Hash.CR` (`≃[...]`) step with a @@ -35,15 +35,12 @@ local instance allTests : AdmissibleDistinguishers Interface TypedDistinguisher variable {K M M' T : Type} [Fintype K] [Nonempty K] [Fintype M] [Fintype M'] [DecidableEq M] [DecidableEq M'] [DecidableEq T] -/-- Key-independent pair extractor for domain extension by `f`. -/ -def mapPair (f : M' β†’ M) (p : M' Γ— M') : M Γ— M := (f p.1, f p.2) - /-- Domain extension preserves collision resistance with error `d ↦ error (dΞ±)`. -/ lemma domainExtension_CR (H : K β†’ M β†’ T) (f : M' β†’ M) (hf : Function.Injective f) {q : β„•} {error : TypedDistinguisher (KeyedCollision K M q) β†’ ENNReal} (h : Hash.CR H error) : Hash.CR (fun k m => H k (f m)) - (fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q))) := - Hash.CR.of_extractor H _ (mapPair f) (fun _ _ => Iff.rfl) + (fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter (Prod.map f f) q))) := + Hash.CR.of_extractor H _ (Prod.map f f) (fun _ _ => Iff.rfl) (fun _ => hf.eq_iff) (fun _ _ => Set.mem_univ _) h /-- Lossless advantage transport for every outer distinguisher. -/ @@ -51,17 +48,17 @@ example (H : K β†’ M β†’ T) (f : M' β†’ M) (hf : Function.Injective f) {q : β„•} (d : TypedDistinguisher (KeyedCollision K M' q)) : d.advantage (Hash.Real (budget := q) (fun k m => H k (f m))) (Hash.Ideal K M' (budget := q)) = - (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q)).advantage + (TypedDistinguisher.absorb d (Hash.extractConverter (Prod.map f f) q)).advantage (Hash.Real (budget := q) H) (Hash.Ideal K M (budget := q)) := - Hash.CR.advantage_eq_of_extractor H _ (mapPair f) (fun _ _ => Iff.rfl) (fun _ => hf.eq_iff) d + Hash.CR.advantage_eq_of_extractor H _ (Prod.map f f) (fun _ _ => Iff.rfl) (fun _ => hf.eq_iff) d /-- `cc_normalize` distributes `Hash.extractConverter` across parallel composition and pushes it inside the retained-key sample `sample k ←$ 𝒰[K]`. -/ example {B : Interface} (H : K β†’ M β†’ T) (f : M' β†’ M) (q : β„•) (aux : TypedSystem B) : - (Hash.extractConverter (K := K) (mapPair f) q βŠ—β‚˜ πŸ™ B) β€’ + (Hash.extractConverter (K := K) (Prod.map f f) q βŠ—β‚˜ πŸ™ B) β€’ (Hash.Real (budget := q) H βˆ₯ aux) = (sample k ←$ 𝒰[K]; - Hash.extractConverter (K := K) (mapPair f) q β€’ + Hash.extractConverter (K := K) (Prod.map f f) q β€’ TypedSystem.ofFunction (A := KeyedCollision K M q) (Hash.reply H k)) βˆ₯ aux := by rw [Hash.Real.eq_sample] cc_normalize @@ -74,14 +71,14 @@ example (H : K β†’ M β†’ T) (f : M' β†’ M) (hf : Function.Injective f) {q : β„•} (h : Hash.CR H error) (hstat : Ξ” (Hash.Ideal K M' (budget := q)) S ≀ Ξ΄) : Hash.Real (budget := q) (fun k m => H k (f m)) ≃[fun d => - error (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q)) + Ξ΄] S := by + error (TypedDistinguisher.absorb d (Hash.extractConverter (Prod.map f f) q)) + Ξ΄] S := by cc_calc mixed Hash.Real (budget := q) (fun k m => H k (f m)) - ≃[fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter (mapPair f) q))] + ≃[fun d => error (TypedDistinguisher.absorb d (Hash.extractConverter (Prod.map f f) q))] Hash.Ideal K M' (budget := q) := domainExtension_CR H f hf h _ β‰ˆ[Ξ΄] S := hstat -#print axioms Hash.source_eq_ofProbAutomaton +#print axioms TypedSystem.source_eq_ofProbAutomaton #print axioms Hash.Real.eq_sample #print axioms Hash.Ideal.eq_sample #print axioms Hash.extractConverter_smul_real