Documentation

CslibFrontier.Languages.Probabilistic.Oracle.Lens

Lawful state lenses for composite stateful oracle handlers #

Cryptographic games and reductions often combine several stateful components—an association-list cache, a ciphertext log, a query countdown, a sticky Boolean bad flag—inside a single documented structure rather than nested anonymous tuples (((j, (false, [])), queried)). A StateLens S Part packages a lawful getter/setter pair (get_set, set_get, set_set) focusing on one field or sub-record Part inside S.

StateLens.zoom lifts any StateT Part m action or oracle handler onto S while leaving the unfocused fields invariant:

Main definitions #

Main results #

structure Cslib.StateLens (S : Type u_1) (Part : Type u_2) :
Type (max u_1 u_2)

A lawful lens focusing on a Part component inside a composite state S.

  • get : S → Part

    Read the focused component from S.

  • set : S → Part → S

    Replace the focused component in S.

  • get_set (s : S) (p : Part) : self.get (self.set s p) = p

    Reading immediately after writing recovers the written component.

  • set_get (s : S) : self.set s (self.get s) = s

    Writing back the current component leaves the composite state unchanged.

  • set_set (s : S) (p₁ p₂ : Part) : self.set (self.set s p₁) p₂ = self.set s p₂

    Writing twice overwrites the first write.

def Cslib.StateLens.id (S : Type u_1) :

Identity lens on S.

def Cslib.StateLens.comp {S : Type u_1} {Mid : Type u_2} {Part : Type u_3} (outer : StateLens S Mid) (inner : StateLens Mid Part) :
StateLens S Part

Compose an outer lens S → Mid with an inner lens Mid → Part.

def Cslib.StateLens.fst (A : Type u_1) (B : Type u_2) :
StateLens (A × B) A

Lens focusing on the first component of a pair A × B.

def Cslib.StateLens.snd (A : Type u_1) (B : Type u_2) :
StateLens (A × B) B

Lens focusing on the second component of a pair A × B.

theorem Cslib.StateLens.set_injective {S : Type u_1} {Part : Type u_2} (l : StateLens S Part) (s : S) :

Fixing the surrounding composite state s, l.set s is injective.

def Cslib.StateLens.zoom {S Part : Type u} {m : Type u → Type v} [Functor m] (l : StateLens S Part) {α : Type u} (act : StateT Part m α) :
StateT S m α

Lift a StateT Part m action onto the composite state S, leaving everything outside l unchanged.

Zooming oracle executions and IdenticalUntilBad #

theorem Cslib.StateLens.run_interpret_zoom {ι S Part α : Type} {spec : ι → Type} (l : StateLens S Part) (handler : (q : ι) → StateT Part PMF (spec q)) (program : OracleComp spec α) (s : S) :
(OracleComp.interpret (fun (q : ι) => l.zoom (handler q)) program).run s = PMF.map (fun (x : α × Part) => (x.1, l.set s x.2)) ((OracleComp.interpret handler program).run (l.get s))

Running an OracleComp against a zoomed PMF handler executes against the inner handler from l.get s and writes the final inner state back into the initial frame s.

theorem Cslib.StateLens.runWith_zoom {Query S Part α : Type} {qspec : Query → Type} (l : StateLens S Part) (handler : (q : Query) → StateT Part PMF (qspec q)) (program : ProbOracleComp qspec α) (s : S) :
ProbOracleComp.runWith (fun (q : Query) => l.zoom (handler q)) program s = PMF.map (fun (x : α × Part) => (x.1, l.set s x.2)) (ProbOracleComp.runWith handler program (l.get s))

Running a ProbOracleComp with runWith against a zoomed PMF handler executes against the inner handler from l.get s and writes the final inner state back into s.

theorem Cslib.StateLens.identicalUntilBad_zoom {ι S Part : Type} {spec : ι → Type} {first second : (q : ι) → StateT Part PMF (spec q)} {bad : Part → Prop} (l : StateLens S Part) (h : OracleComp.IdenticalUntilBad first second bad) :
OracleComp.IdenticalUntilBad (fun (q : ι) => l.zoom (first q)) (fun (q : ι) => l.zoom (second q)) fun (s : S) => bad (l.get s)

OracleComp.IdenticalUntilBad lifts along a lawful StateLens.

theorem Cslib.StateLens.probIdenticalUntilBad_zoom {Query S Part : Type} {qspec : Query → Type} {first second : (q : Query) → StateT Part PMF (qspec q)} {bad : Part → Prop} (l : StateLens S Part) (h : ProbOracleComp.IdenticalUntilBad first second bad) :
ProbOracleComp.IdenticalUntilBad (fun (q : Query) => l.zoom (first q)) (fun (q : Query) => l.zoom (second q)) fun (s : S) => bad (l.get s)

ProbOracleComp.IdenticalUntilBad lifts along a lawful StateLens.

Zooming memoize onto a composite state #

theorem Cslib.StateLens.zoom_memoize_of_some {S Key Value : Type u} {m : Type u → Type v} [Monad m] [LawfulMonad m] [BEq Key] (l : StateLens S (List (Key × Value))) (compute : Key → m Value) (key : Key) (s : S) (value : Value) (h : List.lookup key (l.get s) = some value) :
l.zoom (memoize compute key) s = pure (value, s)

On a cache hit, l.zoom (memoize compute key) returns the cached value and leaves the entire composite state s unchanged.

theorem Cslib.StateLens.zoom_memoize_of_none {S Key Value : Type u} {m : Type u → Type v} [Monad m] [LawfulMonad m] [BEq Key] (l : StateLens S (List (Key × Value))) (compute : Key → m Value) (key : Key) (s : S) (h : List.lookup key (l.get s) = none) :
l.zoom (memoize compute key) s = (fun (value : Value) => (value, l.set s ((key, value) :: l.get s))) <$> compute key

On a cache miss, l.zoom (memoize compute key) runs compute key and prepends (key, value) to l.get s inside s.

theorem Cslib.StateLens.length_get_of_mem_support_zoom_memoize {S Key Value : Type} [BEq Key] (l : StateLens S (List (Key × Value))) (compute : Key → PMF Value) (key : Key) (s : S) {out : Value × S} (hout : out ∈ (l.zoom (memoize compute key) s).support) :
(l.get out.2).length ≤ (l.get s).length + 1

Every supported transition of l.zoom (memoize compute key) in PMF grows the focused cache by at most one entry.

Sticky Boolean bad-flag latching on a named structure state #

@[reducible, inline]
abbrev Cslib.StateLens.Bad {S : Type} (flag : StateLens S Bool) (s : S) :

Bad-state predicate testing whether a focused Boolean field flag : StateLens S Bool is true.

noncomputable def Cslib.StateLens.latch {ι S : Type} {spec : ι → Type} (flag : StateLens S Bool) (step : (q : ι) → S → PMF (spec q × S)) (q : ι) :
StateT S PMF (spec q)

Latch a Boolean bad-flag field flag : StateLens S Bool across oracle calls: evaluate step on the unflagged state flag.set s false and combine the prior flag flag.get s with the per-call indicator flag.get s' via ||.

@[simp]
theorem Cslib.StateLens.latch_of_false {ι S : Type} {spec : ι → Type} (flag : StateLens S Bool) (step : (q : ι) → S → PMF (spec q × S)) (q : ι) {s : S} (hs : flag.get s = false) :
flag.latch step q s = step q s

From an unflagged state (flag.get s = false), one call of flag.latch step has the exact law step q s.

theorem Cslib.StateLens.absorb_latch {ι S : Type} {spec : ι → Type} (flag : StateLens S Bool) (step : (q : ι) → S → PMF (spec q × S)) (q : ι) (s : S) :
flag.Bad s → ∀ x ∈ (flag.latch step q s).support, flag.Bad x.2

flag.Bad is absorbing for every handler built with flag.latch.

theorem Cslib.StateLens.identicalUntilBad_latch {ι S : Type} {spec : ι → Type} (flag : StateLens S Bool) (step₁ step₂ : (q : ι) → S → PMF (spec q × S)) (hagree : ∀ (q : ι) (s : S), flag.get s = false → ∀ (a : spec q) (s' : S), flag.get s' = false → (step₁ q s) (a, s') = (step₂ q s) (a, s')) :
OracleComp.IdenticalUntilBad (flag.latch step₁) (flag.latch step₂) flag.Bad

Two flag.latch handlers satisfy OracleComp.IdenticalUntilBad whenever their underlying step kernels assign equal mass to unflagged transitions from unflagged states.

theorem Cslib.StateLens.probIdenticalUntilBad_latch {Query S : Type} {qspec : Query → Type} (flag : StateLens S Bool) (step₁ step₂ : (q : Query) → S → PMF (qspec q × S)) (hagree : ∀ (q : Query) (s : S), flag.get s = false → ∀ (a : qspec q) (s' : S), flag.get s' = false → (step₁ q s) (a, s') = (step₂ q s) (a, s')) :
ProbOracleComp.IdenticalUntilBad (flag.latch step₁) (flag.latch step₂) flag.Bad

Two flag.latch handlers satisfy ProbOracleComp.IdenticalUntilBad whenever their underlying step kernels assign equal mass to unflagged transitions from unflagged states.

theorem Cslib.StateLens.runWith_latch_map {Query S Base α : Type} {qspec : Query → Type} (flag : StateLens S Bool) (step : (q : Query) → S → PMF (qspec q × S)) (unflagged : (q : Query) → StateT Base PMF (qspec q)) (proj : S → Base) (hproj : ∀ (s : S) (b : Bool), proj (flag.set s b) = proj s) (hstep : ∀ (q : Query) (s : S), flag.get s = false → PMF.map (Prod.map id proj) (step q s) = unflagged q (proj s)) (program : ProbOracleComp qspec α) (s : S) :
PMF.map (Prod.map id proj) (ProbOracleComp.runWith (flag.latch step) program s) = ProbOracleComp.runWith unflagged program (proj s)

Projecting a flag.latch step execution along proj : S → Base that ignores flag recovers ProbOracleComp.runWith unflagged whenever each unflagged step projects to unflagged.

theorem Cslib.StateLens.toOuterMeasure_latch_le {Query S α : Type} {qspec : Query → Type} (flag : StateLens S Bool) {step : (q : Query) → S → PMF (qspec q × S)} (cost : Query → ℕ) (budget : S → ENNReal) (δ : ENNReal) (hstep : ∀ (q : Query) (s : S), flag.get s = false → (step q s).toOuterMeasure {x : qspec q × S | flag.Bad x.2} ≤ ↑(cost q) * budget s) (hgrow : ∀ (q : Query) (s : S), flag.get s = false → ∀ x ∈ (step q s).support, flag.get x.2 = false → budget x.2 ≤ budget s + δ) {program : ProbOracleComp qspec α} {n k : ℕ} (hn : program.IsQueryBounded n) (hk : OracleComp.IsCostBounded (Sum.elim (fun (x : ℕ) => 0) cost) program k) {s : S} (hs : flag.get s = false) :
(ProbOracleComp.runWith (flag.latch step) program s).toOuterMeasure {x : α × S | flag.Bad x.2} ≤ ↑k * (budget s + ↑n * δ)

Linear bad-event union bound for ProbOracleComp.runWith (flag.latch step), stated directly in terms of unflagged step transitions (flag.get s = false).

theorem Cslib.StateLens.toOuterMeasure_latch_choose_two_le {Query S α : Type} {qspec : Query → Type} (flag : StateLens S Bool) {step : (q : Query) → S → PMF (qspec q × S)} (size : S → ℕ) (δ : ENNReal) (hstep : ∀ (q : Query) (s : S), flag.get s = false → (step q s).toOuterMeasure {x : qspec q × S | flag.Bad x.2} ≤ ↑(size s) * δ) (hgrow : ∀ (q : Query) (s : S), flag.get s = false → ∀ x ∈ (step q s).support, flag.get x.2 = false → size x.2 ≤ size s + 1) {program : ProbOracleComp qspec α} {n : ℕ} (hn : program.IsQueryBounded n) {s : S} (hs : flag.get s = false) :
(ProbOracleComp.runWith (flag.latch step) program s).toOuterMeasure {x : α × S | flag.Bad x.2} ≤ ↑(n * size s + n.choose 2) * δ

Triangular (n * size s + n.choose 2) bad-event union bound for ProbOracleComp.runWith (flag.latch step), stated directly in terms of unflagged step transitions (flag.get s = false).