Documentation

CslibFrontier.Computability.WordRAM.Randomized.SymbolicExec

Symbolic execution of structured randomized word-RAM code #

Weakest-precondition wrappers and symbolic-execution tactics extending Cslib.WordRAM.WP (wram_exec) to structured randomized execution:

The tactic wram_rand_exec steps straight-line deterministic instructions (Code.op), conditionals (Code.branch, or stops with -split), and uniform draws (Code.rand, both at a leaf and at the head of Code.seq), reassociating left-nested Code.seq automatically and reusing the register/memory simprocs and fits discharger of wram_exec.

The tactic wram_rand_call consumes a proved deterministic (Exec, ExecWithin) or randomized (ExecRand, ExecRandWithin, ExecCodeDist) contract at the head of a WP.Rand, WP.RandWithin, or WP.Dist goal (including across Code.rename f via renamed hf), exposing a WP continuation whose residual ProbComp or PMF family unifies by higher-order pattern matching without manual m₂ or μ₂ annotations.

Main definitions #

Main results #

Shared structural lemmas for ExecRand, ExecRandWithin, and ExecCodeDist #

theorem Cslib.WordRAM.ExecRand.seq_det {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {m₁ : ProbComp (Data r × ℕ)} {f : Data r → Data r} {n₂ : Data r → ℕ} (h₁ : ExecRand w c₁ x m₁) (h₂ : ∀ (y : Data r) (n : ℕ), (y, n) ∈ OracleComp.support m₁ → Exec w c₂ y (f y) (n₂ y)) :
ExecRand w (c₁.seq c₂) x ((fun (z : Data r × ℕ) => (f z.1, z.2 + n₂ z.1)) <$> m₁)

Post-compose a deterministic continuation Exec w c₂ y (f y) (n₂ y) onto an exact randomized execution ExecRand w c₁ x m₁.

theorem Cslib.WordRAM.ExecCodeDist.of_execRand_le {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {m : ProbComp (Data r × ℕ)} {T : ℕ} (h : ExecRand w c x m) (hT : ∀ z ∈ OracleComp.support m, z.2 ≤ T) :

Lift an ExecRand execution with pathwise step bound ≤ T directly into ExecCodeDist.

WP.Rand: weakest preconditions for ExecRand #

def Cslib.WordRAM.WP.Rand {B : Basis} {r : ℕ} (w : ℕ) (c : Code B r) (x : Data r) (Q : ProbComp (Data r × ℕ) → Prop) :

WP.Rand w c x Q: at word size w, the structured randomized program c executes cleanly from x yielding some ProbComp (Data r × ℕ) computation m satisfying Q m.

theorem Cslib.WordRAM.WP.Rand.execRand {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {m : ProbComp (Data r × ℕ)} (h : Rand w c x fun (x : ProbComp (Data r × ℕ)) => m = x) :
ExecRand w c x m

Extract an exact ExecRand w c x m certificate from WP.Rand w c x (m = ·).

theorem Cslib.WordRAM.WP.Rand.exists_execRand {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} (h : Rand w c x Q) :
∃ (m : ProbComp (Data r × ℕ)), ExecRand w c x m ∧ Q m

Extract an existential ∃ m, ExecRand w c x m ∧ Q m from WP.Rand w c x Q.

theorem Cslib.WordRAM.WP.Rand.mono {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {Q Q' : ProbComp (Data r × ℕ) → Prop} (h : Rand w c x Q) (hQ : ∀ (m : ProbComp (Data r × ℕ)), Q m → Q' m) :
Rand w c x Q'

Weaken the postcondition of WP.Rand.

theorem Cslib.WordRAM.WP.Rand.of_execRand {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {m : ProbComp (Data r × ℕ)} {Q : ProbComp (Data r × ℕ) → Prop} (h : ExecRand w c x m) (hQ : Q m) :
Rand w c x Q

Continue from an explicit ExecRand execution of c.

theorem Cslib.WordRAM.WP.Rand.of_exec {B : Basis} {r w : ℕ} {c : Code B r} {x y : Data r} {n : ℕ} {Q : ProbComp (Data r × ℕ) → Prop} (h : Exec w c x y n) (hQ : Q (pure (y, n))) :
Rand w c x Q

Continue from an exact deterministic Exec execution of c.

theorem Cslib.WordRAM.WP.Rand.of_wp_exact {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} (h : Exact w c x fun (y : Data r) (n : ℕ) => Q (pure (y, n))) :
Rand w c x Q

Lift a deterministic WP.Exact specification into WP.Rand.

theorem Cslib.WordRAM.WP.Rand.of_execRand_rename {B : Basis} {r r' w : ℕ} {f : Fin r → Fin r'} (hf : Function.Injective f) {c : Code B r} {x : Data r'} {m : ProbComp (Data r × ℕ)} {Q : ProbComp (Data r' × ℕ) → Prop} (h : ExecRand w c (Data.comap f x) m) (hQ : Q ((fun (z : Data r × ℕ) => (Data.extend f z.1 x, z.2)) <$> m)) :
Rand w (Code.rename f c) x Q

Continue from a renamed ExecRand execution at a leaf c.rename f.

theorem Cslib.WordRAM.WP.Rand.seq_assoc {B : Basis} {r w : ℕ} {c₁ c₂ c₃ : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} (h : Rand w (c₁.seq (c₂.seq c₃)) x Q) :
Rand w ((c₁.seq c₂).seq c₃) x Q

Reassociate a left-nested .seq (.seq c₁ c₂) c₃ to .seq c₁ (.seq c₂ c₃).

theorem Cslib.WordRAM.WP.Rand.seq_exact {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} (h : Exact w c₁ x fun (y : Data r) (n₁ : ℕ) => Rand w c₂ y fun (m₂ : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, n₁ + z.2)) <$> m₂)) :
Rand w (c₁.seq c₂) x Q

Execute a deterministic prefix c₁ via WP.Exact, shifting the step count of c₂.

theorem Cslib.WordRAM.WP.Rand.seq_of_exec {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x y : Data r} {n : ℕ} {Q : ProbComp (Data r × ℕ) → Prop} (h₁ : Exec w c₁ x y n) (h₂ : Rand w c₂ y fun (m₂ : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, n + z.2)) <$> m₂)) :
Rand w (c₁.seq c₂) x Q

Consume a proved deterministic Exec contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.Rand.seq_of_exec_spec {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {n : ℕ} {Q : ProbComp (Data r × ℕ) → Prop} {P : Data r → Prop} (h₁ : ∃ (y : Data r), Exec w c₁ x y n ∧ P y) (h₂ : ∀ (y : Data r), P y → Rand w c₂ y fun (m₂ : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, n + z.2)) <$> m₂)) :
Rand w (c₁.seq c₂) x Q

Consume an existential deterministic Exec contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.Rand.seq_of_execRand {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} {m₁ : ProbComp (Data r × ℕ)} {m₂ : Data r → ProbComp (Data r × ℕ)} (h₁ : ExecRand w c₁ x m₁) (h₂ : ∀ (y : Data r) (n : ℕ), (y, n) ∈ OracleComp.support m₁ → Rand w c₂ y fun (x : ProbComp (Data r × ℕ)) => m₂ y = x) (hQ : Q do let z₁ ← m₁ let z₂ ← m₂ z₁.1 pure (z₂.1, z₁.2 + z₂.2)) :
Rand w (c₁.seq c₂) x Q

Consume an explicit ExecRand contract at the head of .seq c₁ c₂, exposing (y, n) in m₁.support so m₂ unifies by higher-order pattern matching.

theorem Cslib.WordRAM.WP.Rand.seq_of_exec_rename {B : Basis} {r r' w : ℕ} {f : Fin r → Fin r'} (hf : Function.Injective f) {c₁ : Code B r} {c₂ : Code B r'} {x : Data r'} {y : Data r} {n : ℕ} {Q : ProbComp (Data r' × ℕ) → Prop} (h₁ : Exec w c₁ (Data.comap f x) y n) (h₂ : Rand w c₂ (Data.extend f y x) fun (m₂ : ProbComp (Data r' × ℕ)) => Q ((fun (z : Data r' × ℕ) => (z.1, n + z.2)) <$> m₂)) :
Rand w ((Code.rename f c₁).seq c₂) x Q

Consume a renamed deterministic Exec execution at the head of .seq (c₁.rename f) c₂.

theorem Cslib.WordRAM.WP.Rand.seq_of_execRand_rename {B : Basis} {r r' w : ℕ} {f : Fin r → Fin r'} (hf : Function.Injective f) {c₁ : Code B r} {c₂ : Code B r'} {x : Data r'} {m₁ : ProbComp (Data r × ℕ)} {m₂ : Data r → ProbComp (Data r' × ℕ)} {Q : ProbComp (Data r' × ℕ) → Prop} (h₁ : ExecRand w c₁ (Data.comap f x) m₁) (h₂ : ∀ (y : Data r) (n : ℕ), (y, n) ∈ OracleComp.support m₁ → Rand w c₂ (Data.extend f y x) fun (x : ProbComp (Data r' × ℕ)) => m₂ y = x) (hQ : Q do let z₁ ← m₁ let z₂ ← m₂ z₁.1 pure (z₂.1, z₁.2 + z₂.2)) :
Rand w ((Code.rename f c₁).seq c₂) x Q

Consume a renamed ExecRand execution at the head of .seq (c₁.rename f) c₂, indexing m₂ directly by the inner state y : Data r so m₂ unifies without comap/extend inversions.

theorem Cslib.WordRAM.WP.Rand.rand {B : Basis} {r w : ℕ} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} {dst src : Fin r} {a : ℕ} (hs : x.regs src = a) (hfit : Fits w a) (h : Q do let k ← ProbComp.unifFin a pure (x.set dst ↑k, 1)) :
Rand w (Code.rand dst src) x Q

A leaf Code.rand dst src instruction with the bound register named in closed form.

theorem Cslib.WordRAM.WP.Rand.seq_rand {B : Basis} {r w : ℕ} {c₂ : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} {dst src : Fin r} {a : ℕ} (hs : x.regs src = a) (hfit : Fits w a) {m₂ : Fin (a + 1) → ProbComp (Data r × ℕ)} (h₂ : ∀ (k : Fin (a + 1)), Rand w c₂ (x.set dst ↑k) fun (x : ProbComp (Data r × ℕ)) => m₂ k = x) (h : Q do let k ← ProbComp.unifFin a let z₂ ← m₂ k pure (z₂.1, 1 + z₂.2)) :
Rand w ((Code.rand dst src).seq c₂) x Q

A Code.rand dst src instruction followed by a WP.Rand continuation c₂.

theorem Cslib.WordRAM.WP.Rand.seq_rand_det {B : Basis} {r w : ℕ} {c₂ : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} {dst src : Fin r} {a : ℕ} (hs : x.regs src = a) (hfit : Fits w a) {f : Fin (a + 1) → Data r} {n : Fin (a + 1) → ℕ} (h₂ : ∀ (k : Fin (a + 1)), Exec w c₂ (x.set dst ↑k) (f k) (n k)) (h : Q do let k ← ProbComp.unifFin a pure (f k, 1 + n k)) :
Rand w ((Code.rand dst src).seq c₂) x Q

A Code.rand dst src instruction followed by a deterministic Exec continuation c₂.

theorem Cslib.WordRAM.WP.Rand.branch {B : Basis} {r w : ℕ} {pos neg : Code B r} {cond : Cond r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} (hpos : cond.Holds x → Rand w pos x fun (m : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, z.2 + 1)) <$> m)) (hneg : ¬cond.Holds x → Rand w neg x fun (m : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, z.2 + 2)) <$> m)) :
Rand w (Code.branch cond pos neg) x Q

A conditional on cond.Holds x, by cases.

theorem Cslib.WordRAM.WP.Rand.branch_lt {B : Basis} {r w : ℕ} {pos neg : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} {i j : Fin r} {a b : ℕ} (hi : x.regs i = a) (hj : x.regs j = b) (hpos : a < b → Rand w pos x fun (m : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, z.2 + 1)) <$> m)) (hneg : ¬a < b → Rand w neg x fun (m : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, z.2 + 2)) <$> m)) :
Rand w (Code.branch (Cond.lt i j) pos neg) x Q

A conditional on x.regs i < x.regs j, with the compared values in closed form.

theorem Cslib.WordRAM.WP.Rand.branch_eq {B : Basis} {r w : ℕ} {pos neg : Code B r} {x : Data r} {Q : ProbComp (Data r × ℕ) → Prop} {i j : Fin r} {a b : ℕ} (hi : x.regs i = a) (hj : x.regs j = b) (hpos : a = b → Rand w pos x fun (m : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, z.2 + 1)) <$> m)) (hneg : ¬a = b → Rand w neg x fun (m : ProbComp (Data r × ℕ)) => Q ((fun (z : Data r × ℕ) => (z.1, z.2 + 2)) <$> m)) :
Rand w (Code.branch (Cond.eq i j) pos neg) x Q

A conditional on x.regs i = x.regs j, with the compared values in closed form.

WP.RandWithin: weakest preconditions for ExecRandWithin #

def Cslib.WordRAM.WP.RandWithin {B : Basis} {r : ℕ} (w : ℕ) (c : Code B r) (x : Data r) (P : Data r → Prop) (Q : ℕ → ℕ → Prop) :

WP.RandWithin w c x P Q: at word size w, the structured randomized program c terminates cleanly from x achieving postcondition P within some step bound t and rand-call bound q satisfying Q t q.

theorem Cslib.WordRAM.WP.RandWithin.execRandWithin {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {P : Data r → Prop} {T R : ℕ} (h : RandWithin w c x P fun (t q : ℕ) => t ≤ T ∧ q ≤ R) :
ExecRandWithin w c x P T R

Extract ExecRandWithin w c x P T R.

theorem Cslib.WordRAM.WP.RandWithin.of_execRandWithin {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {P P₁ : Data r → Prop} {T₁ R₁ : ℕ} {Q : ℕ → ℕ → Prop} (h : ExecRandWithin w c x P₁ T₁ R₁) (hP : ∀ (y : Data r), P₁ y → P y) (hQ : Q T₁ R₁) :
RandWithin w c x P Q

Continue from an explicit ExecRandWithin certificate.

theorem Cslib.WordRAM.WP.RandWithin.of_wp {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} (h : WP w c x fun (y : Data r) (t : ℕ) => P y ∧ Q t 0) :
RandWithin w c x P Q

Lift a deterministic WP execution into WP.RandWithin (0 rand calls).

theorem Cslib.WordRAM.WP.RandWithin.of_execWithin {B : Basis} {r w : ℕ} {c : Code B r} {x y : Data r} {P : Data r → Prop} {T₁ : ℕ} {Q : ℕ → ℕ → Prop} (h : ExecWithin w c x y T₁) (hP : P y) (hQ : Q T₁ 0) :
RandWithin w c x P Q

Lift a deterministic ExecWithin specification into WP.RandWithin.

theorem Cslib.WordRAM.WP.RandWithin.of_execRandWithin_rename {B : Basis} {r r' w : ℕ} {f : Fin r → Fin r'} (hf : Function.Injective f) {c : Code B r} {x : Data r'} {P : Data r' → Prop} {P₁ : Data r → Prop} {T₁ R₁ : ℕ} {Q : ℕ → ℕ → Prop} (h : ExecRandWithin w c (Data.comap f x) P₁ T₁ R₁) (hP : ∀ (y : Data r), P₁ y → P (Data.extend f y x)) (hQ : Q T₁ R₁) :
RandWithin w (Code.rename f c) x P Q

Continue from an explicit ExecRandWithin contract renamed via an injective register map.

theorem Cslib.WordRAM.WP.RandWithin.seq_assoc {B : Basis} {r w : ℕ} {c₁ c₂ c₃ : Code B r} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} (h : RandWithin w (c₁.seq (c₂.seq c₃)) x P Q) :
RandWithin w ((c₁.seq c₂).seq c₃) x P Q

Reassociate left-nested sequencing.

theorem Cslib.WordRAM.WP.RandWithin.seq_wp {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} (h : WP w c₁ x fun (y : Data r) (t₁ : ℕ) => RandWithin w c₂ y P fun (t₂ q : ℕ) => Q (t₂ + t₁) q) :
RandWithin w (c₁.seq c₂) x P Q

Execute a deterministic prefix c₁ via WP, adding its step count to the continuation.

theorem Cslib.WordRAM.WP.RandWithin.seq_of_execWithin {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x y : Data r} {P : Data r → Prop} {T₁ : ℕ} {Q : ℕ → ℕ → Prop} (h₁ : ExecWithin w c₁ x y T₁) (h₂ : RandWithin w c₂ y P fun (t₂ q : ℕ) => Q (t₂ + T₁) q) :
RandWithin w (c₁.seq c₂) x P Q

Consume a deterministic ExecWithin at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.RandWithin.seq_of_execWithin_spec {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {P P₁ : Data r → Prop} {T₁ : ℕ} {Q : ℕ → ℕ → Prop} (h₁ : ∃ (y : Data r), ExecWithin w c₁ x y T₁ ∧ P₁ y) (h₂ : ∀ (y : Data r), P₁ y → RandWithin w c₂ y P fun (t₂ q : ℕ) => Q (t₂ + T₁) q) :
RandWithin w (c₁.seq c₂) x P Q

Consume an existential deterministic ExecWithin contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.RandWithin.seq_of_exec {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x y : Data r} {P : Data r → Prop} {T₁ : ℕ} {Q : ℕ → ℕ → Prop} (h₁ : Exec w c₁ x y T₁) (h₂ : RandWithin w c₂ y P fun (t₂ q : ℕ) => Q (t₂ + T₁) q) :
RandWithin w (c₁.seq c₂) x P Q

Consume a deterministic Exec contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.RandWithin.seq_of_execRandWithin {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {P P₁ : Data r → Prop} {T₁ R₁ T₂ R₂ : ℕ} {Q : ℕ → ℕ → Prop} (h₁ : ExecRandWithin w c₁ x P₁ T₁ R₁) (h₂ : ∀ (y : Data r), P₁ y → RandWithin w c₂ y P fun (t₂ q₂ : ℕ) => t₂ ≤ T₂ ∧ q₂ ≤ R₂) (hQ : Q (T₁ + T₂) (R₁ + R₂)) :
RandWithin w (c₁.seq c₂) x P Q

Consume a randomized ExecRandWithin contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.RandWithin.rand {B : Basis} {r w : ℕ} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} {dst src : Fin r} {a : ℕ} (hs : x.regs src = a) (hfit : Fits w a) (hP : ∀ (k : Fin (a + 1)), P (x.set dst ↑k)) (hQ : Q 1 1) :
RandWithin w (Code.rand dst src) x P Q

A leaf Code.rand dst src instruction.

theorem Cslib.WordRAM.WP.RandWithin.seq_rand {B : Basis} {r w : ℕ} {c₂ : Code B r} {x : Data r} {P : Data r → Prop} {T₂ R₂ : ℕ} {Q : ℕ → ℕ → Prop} {dst src : Fin r} {a : ℕ} (hs : x.regs src = a) (hfit : Fits w a) (h₂ : ∀ (k : Fin (a + 1)), RandWithin w c₂ (x.set dst ↑k) P fun (t₂ q₂ : ℕ) => t₂ ≤ T₂ ∧ q₂ ≤ R₂) (hQ : Q (1 + T₂) (1 + R₂)) :
RandWithin w ((Code.rand dst src).seq c₂) x P Q

A Code.rand dst src instruction followed by a WP.RandWithin continuation.

theorem Cslib.WordRAM.WP.RandWithin.branch {B : Basis} {r w : ℕ} {pos neg : Code B r} {cond : Cond r} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} (hpos : cond.Holds x → RandWithin w pos x P fun (t q : ℕ) => Q (t + 1) q) (hneg : ¬cond.Holds x → RandWithin w neg x P fun (t q : ℕ) => Q (t + 2) q) :
RandWithin w (Code.branch cond pos neg) x P Q

Conditional on cond.Holds x.

theorem Cslib.WordRAM.WP.RandWithin.branch_lt {B : Basis} {r w : ℕ} {pos neg : Code B r} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} {i j : Fin r} {a b : ℕ} (hi : x.regs i = a) (hj : x.regs j = b) (hpos : a < b → RandWithin w pos x P fun (t q : ℕ) => Q (t + 1) q) (hneg : ¬a < b → RandWithin w neg x P fun (t q : ℕ) => Q (t + 2) q) :
RandWithin w (Code.branch (Cond.lt i j) pos neg) x P Q

Conditional on x.regs i < x.regs j.

theorem Cslib.WordRAM.WP.RandWithin.branch_eq {B : Basis} {r w : ℕ} {pos neg : Code B r} {x : Data r} {P : Data r → Prop} {Q : ℕ → ℕ → Prop} {i j : Fin r} {a b : ℕ} (hi : x.regs i = a) (hj : x.regs j = b) (hpos : a = b → RandWithin w pos x P fun (t q : ℕ) => Q (t + 1) q) (hneg : ¬a = b → RandWithin w neg x P fun (t q : ℕ) => Q (t + 2) q) :
RandWithin w (Code.branch (Cond.eq i j) pos neg) x P Q

Conditional on x.regs i = x.regs j.

WP.Dist: weakest preconditions for ExecCodeDist #

def Cslib.WordRAM.WP.Dist {B : Basis} {r : ℕ} (w : ℕ) (c : Code B r) (x : Data r) (Q : PMF (Data r) → ENNReal → Prop) :

WP.Dist w c x Q: at word size w, the structured randomized program c executes cleanly from x with exit distribution μ : PMF (Data r) and expected instruction bound T : ℝ≥0∞ satisfying Q μ T.

theorem Cslib.WordRAM.WP.Dist.execCodeDist {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {μ : PMF (Data r)} {T : ENNReal} (h : Dist w c x fun (μ' : PMF (Data r)) (T' : ENNReal) => μ = μ' ∧ T' ≤ T) :
ExecCodeDist w c x μ T

Extract ExecCodeDist w c x μ T.

theorem Cslib.WordRAM.WP.Dist.mono {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {Q Q' : PMF (Data r) → ENNReal → Prop} (h : Dist w c x Q) (hQ : ∀ (μ : PMF (Data r)) (T : ENNReal), Q μ T → Q' μ T) :
Dist w c x Q'

Weaken the postcondition of WP.Dist.

theorem Cslib.WordRAM.WP.Dist.of_execCodeDist {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {μ : PMF (Data r)} {T : ENNReal} {Q : PMF (Data r) → ENNReal → Prop} (h : ExecCodeDist w c x μ T) (hQ : Q μ T) :
Dist w c x Q

Continue from an explicit ExecCodeDist certificate.

theorem Cslib.WordRAM.WP.Dist.of_wp {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {Q : PMF (Data r) → ENNReal → Prop} (h : WP w c x fun (y : Data r) (t : ℕ) => Q (PMF.pure y) ↑t) :
Dist w c x Q

Lift a deterministic WP execution into WP.Dist.

theorem Cslib.WordRAM.WP.Dist.of_execWithin {B : Basis} {r w : ℕ} {c : Code B r} {x y : Data r} {n : ℕ} {Q : PMF (Data r) → ENNReal → Prop} (h : ExecWithin w c x y n) (hQ : Q (PMF.pure y) ↑n) :
Dist w c x Q

Continue from a deterministic ExecWithin execution.

theorem Cslib.WordRAM.WP.Dist.of_exec {B : Basis} {r w : ℕ} {c : Code B r} {x y : Data r} {n : ℕ} {Q : PMF (Data r) → ENNReal → Prop} (h : Exec w c x y n) (hQ : Q (PMF.pure y) ↑n) :
Dist w c x Q

Continue from a deterministic Exec execution.

theorem Cslib.WordRAM.WP.Dist.of_execRand_le {B : Basis} {r w : ℕ} {c : Code B r} {x : Data r} {n : ℕ} {Q : PMF (Data r) → ENNReal → Prop} {m : ProbComp (Data r × ℕ)} (h : ExecRand w c x m) (hT : ∀ z ∈ OracleComp.support m, z.2 ≤ n) (hQ : Q (PMF.map Prod.fst m.eval) ↑n) :
Dist w c x Q

Continue from an ExecRand execution with pathwise step bound ≤ n.

theorem Cslib.WordRAM.WP.Dist.seq_wp {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {Q : PMF (Data r) → ENNReal → Prop} (h : WP w c₁ x fun (y : Data r) (t₁ : ℕ) => Dist w c₂ y fun (μ : PMF (Data r)) (t₂ : ENNReal) => Q μ (↑t₁ + t₂)) :
Dist w (c₁.seq c₂) x Q

Execute a deterministic prefix c₁ via WP, adding its step count to the continuation.

theorem Cslib.WordRAM.WP.Dist.seq_of_execWithin {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x y : Data r} {n : ℕ} {Q : PMF (Data r) → ENNReal → Prop} (h₁ : ExecWithin w c₁ x y n) (h₂ : Dist w c₂ y fun (μ : PMF (Data r)) (t₂ : ENNReal) => Q μ (↑n + t₂)) :
Dist w (c₁.seq c₂) x Q

Consume a deterministic ExecWithin at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.Dist.seq_of_execWithin_spec {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {n : ℕ} {Q : PMF (Data r) → ENNReal → Prop} {P : Data r → Prop} (h₁ : ∃ (y : Data r), ExecWithin w c₁ x y n ∧ P y) (h₂ : ∀ (y : Data r), P y → Dist w c₂ y fun (μ : PMF (Data r)) (t₂ : ENNReal) => Q μ (↑n + t₂)) :
Dist w (c₁.seq c₂) x Q

Consume an existential deterministic ExecWithin contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.Dist.seq_of_exec {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x y : Data r} {n : ℕ} {Q : PMF (Data r) → ENNReal → Prop} (h₁ : Exec w c₁ x y n) (h₂ : Dist w c₂ y fun (μ : PMF (Data r)) (t₂ : ENNReal) => Q μ (↑n + t₂)) :
Dist w (c₁.seq c₂) x Q

Consume a deterministic Exec contract at the head of .seq c₁ c₂.

theorem Cslib.WordRAM.WP.Dist.seq_of_execCodeDist_const {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {μ₁ : PMF (Data r)} {T₁ : ENNReal} {Q : PMF (Data r) → ENNReal → Prop} {μ₂ : Data r → PMF (Data r)} {B₂ : ENNReal} (h₁ : ExecCodeDist w c₁ x μ₁ T₁) (h₂ : ∀ y ∈ μ₁.support, Dist w c₂ y fun (μ' : PMF (Data r)) (T' : ENNReal) => μ₂ y = μ' ∧ T' ≤ B₂) (hQ : Q (μ₁.bind μ₂) (T₁ + B₂)) :
Dist w (c₁.seq c₂) x Q

Consume an ExecCodeDist at the head of .seq c₁ c₂ with a uniform continuation step bound B₂, leaving a WP.Dist continuation so μ₂ unifies by higher-order pattern matching.

theorem Cslib.WordRAM.WP.Dist.rand {B : Basis} {r w : ℕ} {x : Data r} {Q : PMF (Data r) → ENNReal → Prop} {dst src : Fin r} {a : ℕ} (hs : x.regs src = a) (hfit : Fits w a) (hQ : Q (PMF.map (fun (k : Fin (a + 1)) => x.set dst ↑k) (ProbComp.unifFin a).eval) 1) :
Dist w (Code.rand dst src) x Q

A leaf Code.rand dst src instruction.

theorem Cslib.WordRAM.WP.Dist.branch {B : Basis} {r w : ℕ} {pos neg : Code B r} {cond : Cond r} {x : Data r} {Q : PMF (Data r) → ENNReal → Prop} (hpos : cond.Holds x → Dist w pos x fun (μ : PMF (Data r)) (T : ENNReal) => Q μ (T + 1)) (hneg : ¬cond.Holds x → Dist w neg x fun (μ : PMF (Data r)) (T : ENNReal) => Q μ (T + 2)) :
Dist w (Code.branch cond pos neg) x Q

Conditional on cond.Holds x.

theorem Cslib.WordRAM.WP.Dist.branch_lt {B : Basis} {r w : ℕ} {pos neg : Code B r} {x : Data r} {Q : PMF (Data r) → ENNReal → Prop} {i j : Fin r} {a b : ℕ} (hi : x.regs i = a) (hj : x.regs j = b) (hpos : a < b → Dist w pos x fun (μ : PMF (Data r)) (T : ENNReal) => Q μ (T + 1)) (hneg : ¬a < b → Dist w neg x fun (μ : PMF (Data r)) (T : ENNReal) => Q μ (T + 2)) :
Dist w (Code.branch (Cond.lt i j) pos neg) x Q

Conditional on x.regs i < x.regs j.

theorem Cslib.WordRAM.WP.Dist.branch_eq {B : Basis} {r w : ℕ} {pos neg : Code B r} {x : Data r} {Q : PMF (Data r) → ENNReal → Prop} {i j : Fin r} {a b : ℕ} (hi : x.regs i = a) (hj : x.regs j = b) (hpos : a = b → Dist w pos x fun (μ : PMF (Data r)) (T : ENNReal) => Q μ (T + 1)) (hneg : ¬a = b → Dist w neg x fun (μ : PMF (Data r)) (T : ENNReal) => Q μ (T + 2)) :
Dist w (Code.branch (Cond.eq i j) pos neg) x Q

Conditional on x.regs i = x.regs j.

Randomized symbolic execution tactic (wram_rand_exec) #

wram_rand_exec symbolically executes the straight-line, branching, and rand parts of a structured randomized word-RAM program on an ExecRand, WP.Rand, ExecRandWithin, WP.RandWithin, ExecCodeDist, or WP.Dist goal.

It reassociates left-nested Code.seq, collapses deterministic .op steps using wram_exec's simplifier and discharger, steps Code.branch (or splits unless -split is passed), and steps Code.rand dst src (both as a leaf and as .seq (.rand dst src) c₂, introducing k when c₂ contains calls or loops). Stops cleanly at loops and unexpanded subroutines, leaving a WP.Rand, WP.RandWithin, or WP.Dist goal ready for wram_rand_call. Unlike wram_exec, it does not open a program named among the hints: unfold the program first.

wram_rand_call h applies a proved deterministic (Exec / ExecWithin) or randomized (ExecRand / ExecRandWithin / ExecCodeDist) contract h at the head of a WP.Rand, WP.RandWithin, or WP.Dist goal (either a leaf call c or a sequenced call .seq c₁ c₂), and exposes the continuation. Pass renamed hf to lift a contract across Code.rename f via hf : f.Injective.

wram_rand_call h applies a proved deterministic (Exec / ExecWithin) or randomized (ExecRand / ExecRandWithin / ExecCodeDist) contract h at the head of a WP.Rand, WP.RandWithin, or WP.Dist goal (either a leaf call c or a sequenced call .seq c₁ c₂), and exposes the continuation. Pass renamed hf to lift a contract across Code.rename f via hf : f.Injective.

wram_rand_call h applies a proved deterministic (Exec / ExecWithin) or randomized (ExecRand / ExecRandWithin / ExecCodeDist) contract h at the head of a WP.Rand, WP.RandWithin, or WP.Dist goal (either a leaf call c or a sequenced call .seq c₁ c₂), and exposes the continuation. Pass renamed hf to lift a contract across Code.rename f via hf : f.Injective.

wram_rand_call h applies a proved deterministic (Exec / ExecWithin) or randomized (ExecRand / ExecRandWithin / ExecCodeDist) contract h at the head of a WP.Rand, WP.RandWithin, or WP.Dist goal (either a leaf call c or a sequenced call .seq c₁ c₂), and exposes the continuation. Pass renamed hf to lift a contract across Code.rename f via hf : f.Injective.