Generic UC Monad Typeclass #
This file abstracts the 4 axioms that any monad needs for the UC factorization framework to work:
- Coproducts (A1):
ucMapSum— parallel composition onSum - Affine/failure (A2):
ucFail— abort/discard - Pseudometric (A3):
ucSdist— statistical distance with PPL - Monad laws (A4):
LawfulMonad— associativity and unit laws
All factorization theorems from UCFactorization.lean, UC.lean, and UCAlg.lean
are reproved generically over any monad satisfying these axioms.
Two-Layer Architecture #
The theory splits cleanly into two layers:
Standard layer (A1+A2+A4 — any affine monad): Protocol categories, simulator composition, exact (ungraded) simulator-existence preorder, and exact UC emulation, for any monad with coproducts and failure. Captures deterministic simulation and compiler correctness.
Metric-enriched layer (adds A3 — requires probabilistic/quantitative structure):
Approximate factorization, ε-bounds, game hopping, statistical UC emulation.
Makes Kl(T) into a Lawvere metric space (ℝ≥0∞-enriched category, BMPP 2022).
This is the discriminating axiom: only monads with a probabilistic or
nondeterministic base give meaningful intermediate distances.
Main definitions #
Standard layer (no metric) #
UCMonadBase— A1 + A4: lawful monad with coproduct structureUCMonadAffine— A2: failure/abortUCPure— purity predicate / central submonad (Carette-Lemonnier-Zamdzhiev 2022)UCMonad.ProtObj— generic protocol objectUCMonad.ProtMor— generic protocol morphism (simulator adapter)UCMonad.ExactUCEmulates— exact UC emulation (no metric needed)
Metric-enriched layer #
UCMonadMetric— A3: Lawvere metric onKl(T)with PPL (non-expansiveness)UCMonad— all four axioms combinedUCMonad.approxFact— ε-approximate factorizationUCMonad.UCEmulates— generic UC emulation with ε-boundUCMonad.SimConstruction— algebraic simulator mapUCMonad.UCEmulatesAlg— algebraic UC emulation
Main results #
Standard layer #
protMor_comp,protMor_comp_assoc— simulator compositionfactPre_refl,factPre_trans— exact factorization preorderExactUCEmulates_refl,ExactUCEmulates_trans— exact UC emulationExactUCEmulates_of_factPre— factorization → exact UC emulation
Metric-enriched layer #
approxFact_refl,approxFact_trans,approxFact_monoapproxFact_implies_UCEmulates— factorization → UC emulationUCEmulates_refl,UCEmulates_trans,UCEmulates_monoUCEmulatesAlg.forget,.refl,.trans,.trans_generalUCEmulates_of_ExactUCEmulates— exact → approximate (ε = 0)
Design #
This file has no CatCrypt imports — it is self-contained, depending only on
Mathlib for ℝ≥0∞. The concrete SPComp instance is in UCMonad/SPCompInstance.lean.
Section 1: Typeclass Hierarchy #
A1 + A4: Lawful monad with coproduct structure (mapSum).
ucMapSum f g applies f on .inl and g on .inr, with functoriality
laws ensuring it respects pure and Kleisli composition.
- seq_assoc {α β γ : Type} (x : T α) (g : T (α → β)) (h : T (β → γ)) : h <*> (g <*> x) = Function.comp <$> h <*> g <*> x
Parallel composition on
Sum: appliesfto.inl,gto.inr.- ucMapSum_inl {α β γ δ : Type} (f : α → T β) (g : γ → T δ) (a : α) : ucMapSum f g (Sum.inl a) = do let b ← f a pure (Sum.inl b)
ucMapSum f g (.inl a) = f a >>= fun b => pure (.inl b) - ucMapSum_inr {α β γ δ : Type} (f : α → T β) (g : γ → T δ) (c : γ) : ucMapSum f g (Sum.inr c) = do let d ← g c pure (Sum.inr d)
ucMapSum f g (.inr c) = g c >>= fun d => pure (.inr d) Identity:
ucMapSum pure pure = pure- ucMapSum_pure_kleisli {α β γ δ : Type} (f : β → T γ) (g : γ → T δ) : (fun (x : α ⊕ β) => ucMapSum pure f x >>= ucMapSum pure g) = ucMapSum pure fun (b : β) => f b >>= g
Kleisli functoriality:
(id ⊕ f) ∘ₖ (id ⊕ g) = id ⊕ (f ∘ₖ g)
Instances
A3: Statistical distance pseudometric with post-processing lemmas (PPL).
Categorically, this makes Kl(T) into a Lawvere metric space
(ℝ≥0∞-enriched category) where hom-objects are extended non-negative reals
and composition is non-expansive (Lawvere 1973, BMPP 2022). The PPL axioms
(ucSdist_comp_right, ucSdist_comp_left) express that Kleisli composition
is a non-expansive map on hom-objects — the enriched functoriality condition.
This is the discriminating axiom: A1, A2, A4 hold for any monad with exceptions, but A3 requires a quantitative/probabilistic structure that gives meaningful intermediate distances (0 < ε < ∞). Deterministic monads (StateM, ReaderT, Id) only see distance 0 or ∞, making the enrichment trivial.
Without A3, the exact factorization (Sections 1–3) still works — only the approximate/graded factorization (Section 4+) requires the metric.
- seq_assoc {α β γ : Type} (x : T α) (g : T (α → β)) (h : T (β → γ)) : h <*> (g <*> x) = Function.comp <$> h <*> g <*> x
Statistical distance between Kleisli morphisms.
Reflexivity:
ucSdist f f = 0Symmetry
Triangle inequality
- ucSdist_comp_right {α β γ : Type} (f g : α → T β) (k : β → T γ) : (ucSdist (fun (a : α) => f a >>= k) fun (a : α) => g a >>= k) ≤ ucSdist f g
Right PPL: post-composition doesn't increase distance.
- ucSdist_comp_left {α β γ : Type} (f : α → T β) (g₁ g₂ : β → T γ) : (ucSdist (fun (a : α) => f a >>= g₁) fun (a : α) => f a >>= g₂) ≤ ucSdist g₁ g₂
Left PPL: pre-composition doesn't increase distance.
Instances
All four axioms combined: lawful monad + coproducts + failure + pseudometric.
- seq_assoc {α β γ : Type} (x : T α) (g : T (α → β)) (h : T (β → γ)) : h <*> (g <*> x) = Function.comp <$> h <*> g <*> x
Instances
Purity predicate: marks computations as state-independent.
For T = SPComp, this corresponds to SPComp.IsPure.
Pure computations commute with all others (Fubini property).
In the directed type theory, purity is the !A (of-course) modality.
Categorically, UCPure is the center (central submonad) of T
in the sense of Carette-Lemonnier-Zamdzhiev (LICS 2022). The key
property is isPure_comm: pure computations commute with all others,
which is equivalent to saying that the pure fragment forms a
commutative submonad (the premonoidal center of Kl(T)).
The purity predicate on individual computations.
pureis always pure.- isPure_bind {α β : Type} (c : T α) (f : α → T β) : isPure c → (∀ (a : α), isPure (f a)) → isPure (c >>= f)
bindpreserves purity. - isPure_comm {α β γ : Type} (c : T α) (d : T β) (k : α → β → T γ) : isPure c → (do let a ← c let b ← d k a b) = do let b ← d let a ← c k a b
Commutativity: pure computations commute with all others (Fubini).
This is the defining property of the central submonad (Carette-Lemonnier-Zamdzhiev, LICS 2022): a computation
cis central iffc >>= (fun a => d >>= fun b => k a b)equalsd >>= (fun b => c >>= fun a => k a b)for alldandk.
Instances
Section 2: Helper Lemmas #
Section 3: Generic Protocol Category #
A protocol object bundles an interface type with a protocol morphism.
Parameterized over any monad T satisfying UCMonadBase.
- iface : Type
The adversary/simulator interface type
The protocol: maps honest inputs to honest output + interface
Instances For
A protocol morphism from P to Q consists of a simulator transforming
P's interface into Q's interface, with a commutativity proof.
The simulator: transforms
P's interface toQ's interface
Instances For
Identity morphism: the pure simulator.
Equations
- UCMonad.protMor_id P = { sim := pure, comm := ⋯ }
Instances For
Composition of protocol morphisms: Kleisli-compose the simulators.
Instances For
Left identity law for protocol morphism composition.
Right identity law for protocol morphism composition.
Associativity of protocol morphism composition.
Exact factorization preorder (opposite to morphism direction).
Equations
- UCMonad.factPre P Q = Nonempty (UCMonad.ProtMor Q P)
Instances For
Section 3b: Exact UC Emulation (Standard Layer — no metric) #
The exact factorization and UC emulation depend only on A1+A2+A4 (any affine monad). This is the standard layer of the theory: protocol categories, simulator composition, and exact simulation are available for any monad with coproducts and failure, without requiring a probabilistic metric.
It captures deterministic simulation, compiler correctness, and robust compilation (Patrignani et al., TOPLAS 2024).
The metric-enriched layer (Section 4+) adds A3 to get ε-approximate factorization, statistical distance bounds, and game hopping — requiring a quantitative/probabilistic structure (BMPP 2022).
Exact UC emulation: for all adversaries A, there exists a simulator S such that the real and ideal views are equal (not just close in distance).
This depends only on UCMonadBase (A1+A4) — no metric needed.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Exact factorization implies exact UC emulation (no metric needed).
Exact UC emulation is reflexive.
Exact UC emulation is transitive (same-interface).
Section 4: Generic Approximate Factorization #
ε-approximate factorization preorder.
approxFact ε P Q means: there exists a simulator S : Q.iface → T P.iface
such that P.prot is within statistical distance ε of (id ⊕ S) ∘ₖ Q.prot.
Equations
- UCMonad.approxFact ε P Q = ∃ (S : Q.iface → T P.iface), (UCMonad.UCMonadMetric.ucSdist P.prot fun (a : hon) => Q.prot a >>= UCMonad.UCMonadBase.ucMapSum pure S) ≤ ε
Instances For
Reflexivity: identity simulator gives distance 0.
Monotonicity: tighter bound implies looser bound.
Transitivity: simulator chaining gives additive bound.
Exact factorization implies approximate factorization at ε = 0.
Same-interface sdist bound gives approximate factorization.
Section 5: Generic UC Emulation #
Protocol π UC-emulates ideal functionality F with statistical error ε.
For ALL adversaries A, there EXISTS a simulator S such that the ucSdist of the post-adversary views is ≤ ε.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Perfect UC emulation: ε = 0.
Equations
- UCMonad.UCEmulatesPerfect view π F = UCMonad.UCEmulates 0 π F
Instances For
Same-interface UC: if ucSdist π F ≤ ε, then UCEmulates ε with S = A.
Monotonicity: UC emulation with tighter bound implies looser bound.
Reflexivity: any protocol UC-emulates itself perfectly (same-interface).
Transitivity of UC emulation (same-interface case).
Approximate factorization implies UC emulation.
Exact factorization implies perfect UC emulation.
Bridge: exact → approximate #
Exact UC emulation implies approximate UC emulation at ε = 0.
Section 6: Generic Algebraic UC (Explicit Simulator Construction) #
A simulator construction: a functorial map from adversaries to simulators.
Equations
- UCMonad.SimConstruction T leak sim_if view = ((leak → T view) → sim_if → T view)
Instances For
Identity simulator construction (same-interface).
Equations
Instances For
Composition of simulator constructions.
Instances For
Algebraic UC emulation with explicit simulator construction.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Forget the algebraic witness: UCEmulatesAlg → UCEmulates.
Monotonicity for algebraic UC.
Reflexivity: any protocol emulates itself with Sim = id.
From sdist: same-interface with Sim = id.
Same-interface transitivity via simulator composition.
General transitivity through different interface types.