Documentation

GaudisCrypt.Logic.PHoareWhileRev

pHL loops that terminate with probability 1: EC's while I V k ε #

EC's rule for phoare[ while b c : P ==> Q ] = r (ecPhlWhile.ml, t_bdhoare_while_rev_geq): an invariant I, an integer variant V bounded by k on I and positive where the test passes, and a probability ε > 0 with which one round decreases the variant. Then the loop terminates with probability 1 from I (Stmt.ewp_while_one_of_variant), and its expectation is the one solution of the loop's equation (phoareStmt_while_rev_eq).

The zero-one argument of Stmt.ewp_while_one (Logic/PHoare.lean) is the case I = True, k = 1: there, one round leaves the loop with probability ε.

theorem GaudisCrypt.Stmt.ewp_ge_of_certain (s : Stmt) {Ψ : ProgramState → Prop} {f h : ProgramState → ENNReal} {σ : ProgramState} (hΨ : s.ewp (fun (τ : ProgramState) => indicatorE (Ψ τ)) σ = 1) (hf : ∀ (τ : ProgramState), f τ ≤ 1) (hle : ∀ (τ : ProgramState), Ψ τ → f τ ≤ h τ) :
s.ewp f σ ≤ s.ewp h σ

An event of probability 1 may be assumed under an expectation at most 1.

theorem GaudisCrypt.Stmt.ewp_one_of_certain (s : Stmt) {Ψ : ProgramState → Prop} {σ : ProgramState} (hΨ : s.ewp (fun (τ : ProgramState) => indicatorE (Ψ τ)) σ = 1) :
s.ewp (fun (x : ProgramState) => 1) σ = 1

A statement that reaches an event with probability 1 is lossless.

theorem GaudisCrypt.Stmt.ewp_while_one_of_variant {c : Getter Bool ProgramState} {body : Stmt} (I : ProgramState → Prop) (V : ProgramState → ℤ) (k : ℤ) (ε : ENNReal) (hε0 : ε ≠ 0) (hterm : ∀ (σ : ProgramState), I σ → V σ ≤ k ∧ (V σ ≤ 0 → c.get σ = false)) (hinv : ∀ (σ : ProgramState), I σ → c.get σ = true → body.ewp (fun (τ : ProgramState) => indicatorE (I τ)) σ = 1) (hvar : ∀ (σ : ProgramState), I σ → c.get σ = true → ε ≤ body.ewp (fun (τ : ProgramState) => indicatorE (V τ < V σ)) σ) (σ : ProgramState) (hI : I σ) :
ewp (GaudiProg[ while (§c) { stmt body; } ]) (fun (x : ProgramState) => 1) σ = 1

Almost-sure termination from a variant (EC's while I V k ε): from the invariant I, with a variant V that is at most k on I and positive where the test passes, if one round keeps I with probability 1 and decreases V with probability at least ε > 0, the loop terminates with probability 1.

noncomputable def GaudisCrypt.Stmt.jumpWith (J : ProgramState → ENNReal) (hJ : ∀ (σ : ProgramState), J σ ≤ 1) (τ₁ : ProgramState) :

A statement with a prescribed expectation of g: go to the state τ₁ with probability J σ, fail otherwise. (The stand-in for EC's abstract statement in the loop rule below.)

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem GaudisCrypt.Stmt.ewp_jumpWith (J : ProgramState → ENNReal) (hJ : ∀ (σ : ProgramState), J σ ≤ 1) (τ₁ : ProgramState) (g : ProgramState → ENNReal) (σ : ProgramState) :
    (jumpWith J hJ τ₁).ewp g σ = J σ * g τ₁
    theorem GaudisCrypt.phoareStmt_while_rev_eq {P : ProgramState → Prop} {body : Stmt} {c : Getter Bool ProgramState} {g bd : ProgramState → ENNReal} (I : ProgramState → Prop) (V : ProgramState → ℤ) (k : ℤ) (ε : ENNReal) (hpre : ∀ (σ : ProgramState), P σ → I σ) (hbound : ∀ (σ : ProgramState), P σ → c.get σ = false → bd σ = g σ) (hterm : ∀ (σ : ProgramState), I σ → V σ ≤ k ∧ (V σ ≤ 0 → c.get σ = false)) (hbody : ∀ (w : Stmt), phoareStmt P w g Eq bd → phoareStmt (fun (σ : ProgramState) => P σ ∧ c.get σ = true) (GaudiProg[ stmt body; stmt w; ]) g Eq bd) (hinv : phoareStmt (fun (σ : ProgramState) => I σ ∧ c.get σ = true) body (fun (τ : ProgramState) => indicatorE (I τ)) Eq fun (_τ : ProgramState) => 1) (hε0 : ε ≠ 0) (hvar : ∀ (z : ℤ), phoareStmt (fun (σ : ProgramState) => I σ ∧ c.get σ = true ∧ V σ = z) body (fun (τ : ProgramState) => indicatorE (V τ < z)) GE.ge fun (_τ : ProgramState) => ε) (hg1 : ∀ (τ : ProgramState), g τ ≤ 1) (hbd1 : ∀ (σ : ProgramState), P σ → bd σ ≤ 1) (hsat : ∃ (τ : ProgramState), g τ = 1) :
    phoareStmt P (GaudiProg[ while (§c) { stmt body; } ]) g Eq bd

    EC's while I V k ε on phoare[ while b c : P ==> g ] = bd (ecPhlWhile.ml, t_bdhoare_while_rev_geq), EC's six conditions in its order:

    • hpre: the precondition gives the invariant;
    • hbound: where the loop is not entered, the bound is the postcondition;
    • hterm: on the invariant the variant is at most k, and positive where the test passes;
    • hbody: one round followed by any statement w that meets the judgment meets it (EC's abstract statement standing for the loop);
    • hinv: one round keeps the invariant, with probability 1;
    • hε0, hvar: one round decreases the variant with probability at least ε > 0.

    Beyond EC's: g ≤ 1 and bd ≤ 1 (an event and a probability), and some state satisfies the postcondition (hsat). The last is needed here because w is a quantified statement, not EC's abstract one: if no state satisfies the postcondition, no w meets the judgment with a bound other than 0, and hbody holds for want of one — while (b) b <- false from b with the postcondition false would pass the six conditions for any bound. (In EC the judgment about w is a hypothesis that can only be used, not refuted.)

    theorem GaudisCrypt.phoareStmt_while_rev_eq_ofReal {P : ProgramState → Prop} {body : Stmt} {c : Getter Bool ProgramState} {g bd : ProgramState → ENNReal} (I : ProgramState → Prop) (V : ProgramState → ℤ) (k : ℤ) (ε : ℝ) (hpre : ∀ (σ : ProgramState), P σ → I σ) (hbound : ∀ (σ : ProgramState), P σ → c.get σ = false → bd σ = g σ) (hterm : ∀ (σ : ProgramState), I σ → V σ ≤ k ∧ (V σ ≤ 0 → c.get σ = false)) (hbody : ∀ (w : Stmt), phoareStmt P w g Eq bd → phoareStmt (fun (σ : ProgramState) => P σ ∧ c.get σ = true) (GaudiProg[ stmt body; stmt w; ]) g Eq bd) (hinv : phoareStmt (fun (σ : ProgramState) => I σ ∧ c.get σ = true) body (fun (τ : ProgramState) => indicatorE (I τ)) Eq fun (_τ : ProgramState) => 1) (hε0 : 0 < ε) (hvar : ∀ (z : ℤ), phoareStmt (fun (σ : ProgramState) => I σ ∧ c.get σ = true ∧ V σ = z) body (fun (τ : ProgramState) => indicatorE (V τ < z)) GE.ge fun (_τ : ProgramState) => ENNReal.ofReal ε) (hg1 : ∀ (τ : ProgramState), g τ ≤ 1) (hbd1 : ∀ (σ : ProgramState), P σ → bd σ ≤ 1) (hsat : ∃ (τ : ProgramState), g τ = 1) :
    phoareStmt P (GaudiProg[ while (§c) { stmt body; } ]) g Eq bd

    phoareStmt_while_rev_eq with the rate a real, as EC writes it.

    EC's while I V k ε on phoareStmt P (while c body) g = bd (phoareStmt_while_rev_eq): EC's goals in its order — pre_inv, pre_bound, term, body, inv, rate (0 < ε), variant — then post_le_one, bound_le_one, sat. The goals that are not judgments are closed when trivial (EC's => //).

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      EC's while I V k ε on phoare[ while b c : P ==> Q ] = r (the loop alone): the invariant I and the integer variant V written like the judgment's conditions, k an integer and ε a real. See phoareStmt_while_rev_eq for the goals.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For