Structured randomized word-RAM programs #
Cslib.WordRAM.ExecRand w c x m is the randomized counterpart of Cslib.WordRAM.Exec: at word
size w, running the structured program c : Code B r from x : Data r terminates without
faulting on every path and yields the ProbComp (Data r × ℕ) computation m, pairing each final
machine data y with the exact number of compiled instructions n executed along that path.
Cslib.WordRAM.ExecRandWithin w c x P T R bundles ExecRand with a pathwise postcondition P, a
worst-case instruction budget T, and a worst-case rand-call budget R (IsQueryBounded),
supporting Hoare-style rules for data instructions, rand, sequencing, branches, and potential
loops (ExecRandWithin.loop_potential).
Every structured randomized execution refines Code.assemble (ExecRand.runRand_assemble) and
compiles via Code.program out to a flat Program B r establishing RandHaltsWithin,
RandCallsBounded, and the memory postcondition P directly on RandCfg.halted
(ExecRandWithin.program).
Main definitions #
Cslib.WordRAM.ExecRand: exact randomized execution ofCode B rintoProbComp (Data r × ℕ).Cslib.WordRAM.codeComp: canonicalProbComp (Data r × ℕ)associated with(w, c, x).Cslib.WordRAM.ExecRandWithin: pathwise postcondition, worst-case instruction bound, andrand-call budget onCode B r.
Main results #
Cslib.WordRAM.ExecRand.unique:mis uniquely determined by(w, c, x).Cslib.WordRAM.Exec.execRand,Cslib.WordRAM.ExecWithin.execRandWithin: deterministic structured executions lift with zerorandcalls.Cslib.WordRAM.ExecRandWithin.loop_potential: potential-based termination, worst-case time, andrand-call rule for randomized loops.Cslib.WordRAM.ExecRand.runRand_assemble,Cslib.WordRAM.ExecRand.runRand_program,Cslib.WordRAM.Exec.runRand_program,Cslib.WordRAM.ExecRandWithin.program: assembly refinement into flatrunRand,RandHaltsWithin, andRandCallsBounded.
References #
Structured randomized executions #
ExecRand w c x m: at word size w, the structured program c runs from data x without
faulting on any path and induces the ProbComp (Data r × ℕ) computation m of (finalData, steps)
matching the exact instruction counts of Code.assemble.
- op {B : Basis} {r w : ℕ} {o : Op B r} {x y : Data r} (h : Op.eval w o x = some y) : ExecRand w (Code.op o) x (pure (y, 1))
A data instruction executes in
1instruction and issues0calls. - rand {B : Basis} {r w : ℕ} {dst src : Fin r} {x : Data r} (h : Fits w (x.regs src)) : ExecRand w (Code.rand dst src) x do
let k ← ProbComp.unifFin (x.regs src)
pure (x.set dst ↑k, 1)
Code.rand dst srcdraws uniformly from[0, x.regs src]in1instruction when the bound fits a word. - seq {B : Basis} {r w : ℕ} {c₁ c₂ : Code B r} {x : Data r} {m₁ : ProbComp (Data r × ℕ)} {m₂ : Data r → ProbComp (Data r × ℕ)} (h₁ : ExecRand w c₁ x m₁) (h₂ : ∀ z ∈ OracleComp.support m₁, ExecRand w c₂ z.1 (m₂ z.1)) : ExecRand w (c₁.seq c₂) x do
let z₁ ← m₁
let z₂ ← m₂ z₁.1
pure (z₂.1, z₁.2 + z₂.2)
Sequencing composes the two computations and adds their instruction counts.
- branch_pos {B : Basis} {r w : ℕ} {c : Cond r} {pos neg : Code B r} {x : Data r} {m : ProbComp (Data r × ℕ)} (hc : c.Holds x) (h : ExecRand w pos x m) : ExecRand w (Code.branch c pos neg) x ((fun (z : Data r × ℕ) => (z.1, z.2 + 1)) <$> m)
Taking the positive branch adds
1instruction for the branch test. - branch_neg {B : Basis} {r w : ℕ} {c : Cond r} {pos neg : Code B r} {x : Data r} {m : ProbComp (Data r × ℕ)} (hc : ¬c.Holds x) (h : ExecRand w neg x m) : ExecRand w (Code.branch c pos neg) x ((fun (z : Data r × ℕ) => (z.1, z.2 + 2)) <$> m)
Taking the negative branch adds
2instructions (branch test and jump pastpos). - loop_exit {B : Basis} {r w : ℕ} {c : Cond r} {body : Code B r} {x : Data r} (hc : ¬c.Holds x) : ExecRand w (Code.loop c body) x (pure (x, 2))
Exiting a loop when the condition fails takes
2instructions (branch test and exit jump). - loop_iter {B : Basis} {r w : ℕ} {c : Cond r} {body : Code B r} {x : Data r} {mb : ProbComp (Data r × ℕ)} {ml : Data r → ProbComp (Data r × ℕ)} (hc : c.Holds x) (hb : ExecRand w body x mb) (hl : ∀ z ∈ OracleComp.support mb, ExecRand w (Code.loop c body) z.1 (ml z.1)) : ExecRand w (Code.loop c body) x do
let z₁ ← mb
let z₂ ← ml z₁.1
pure (z₂.1, z₁.2 + z₂.2 + 2)
Entering a loop executes
body, jumps back to the header, and continues the loop, adding2instructions for the branch test and back-jump.
Instances For
Structured randomized executions preserve word-boundedness on every supported path.
A register that c does not write retains its value on every supported path.
Renaming registers in structured randomized executions #
Running a structured randomized program through an injective register map f transforms its
outputs via Data.extend f x, preserving instruction counts and untouched registers.
Hoare-style bounded structured execution (ExecRandWithin) #
ExecRandWithin w c x P T R: at word size w, running c from x terminates cleanly on every
path (ExecRand), makes at most R rand calls on every path, and every supported outcome
(y, n) satisfies P y and n ≤ T.
Instances For
Every deterministic ExecWithin lifts to ExecRandWithin with 0 rand calls.
Monotonicity / consequence rule for ExecRandWithin.
Strengthening P with Data.Bounded w when x.Bounded w holds initially.
Strengthening P with preservation of a register i not written by c.
Code.rand dst src runs in 1 step and 1 rand call when x.regs src fits a word.
Sequencing rule: composes ExecRandWithin specs and sums both step and call budgets.
Positive branch rule (+1 step, same call budget).
Negative branch rule (+2 steps, same call budget).
Combined branch rule with common budgets (T + 2, R).
One iteration of a loop followed by the rest adds T₁ + 2 steps and R₁ + R₂ calls.
Potential rule for randomized loops. Suppose every state x satisfying I x and
cond.Holds x runs body within some T_b instructions and R_b rand calls such that every
supported successor y satisfies I y, T_b + 2 + Φ y ≤ Φ x, and R_b + Ψ y ≤ Ψ x. Then from
any x satisfying I x, .loop cond body terminates within Φ x + 2 instructions and Ψ x
rand calls, reaching a state y with I y ∧ ¬cond.Holds y.
Renaming a ExecRandWithin specification along an injective register map f.
Compilation to flat runRand, RandHaltsWithin, and RandCallsBounded #
Randomized assembly is exact. Running a flat program p containing c.assemble pc at pc
for T steps (where every path of m uses at most T steps) is equal in ProbComp to sampling
(y, n) ← m and continuing from ⟨pc + c.size, y⟩ for the remaining T - n steps.
Compiling c with Code.program out maps every (y, n) of m to .halted (y.regs out) y
once T ≥ n + 1 on every path.
Structured deterministic corollary: a compiled Exec w c x y n halts cleanly under runRand
with output y.regs out and final machine data y at any fuel T ≥ n + 1.
Compiling an ExecRandWithin w c x P T R certificate via c.program out yields
RandHaltsWithin, RandCallsBounded, and pathwise P on the halting data at any fuel
T' ≥ T + 1.