Documentation

GaudisCrypt.Logic.PRHL2Upto

pRHL, version 2 — synchronized while with an exit #

ProgramDenotation.prhl2.while_loop (PRHL2.lean) couples two loops whose guards agree at every iteration. This module proves ProgramDenotation.prhl2.while_upto, the variant needed for an EasyCrypt-style up-to-bad call rule: at the start of any iteration where the Exit predicate holds (together with the invariant), the guard/body couplings are abandoned and the two remaining whole loops are coupled by a separately supplied coupling h_exit (after the bad event, the runs are typically coupled as a product, and their guards need no longer agree).

Proof outline #

The witness is again the least fixed point of a coupling transformer Φ, which at a state pair runs the exit coupling if Exit holds and otherwise the guard coupling followed (while it fires) by the body coupling and a recursive call. The support follows by induction on the Kleene iterates. The marginals are no longer the loop's Kleene iterates (in the exit branch the marginal is the whole remaining loop), so instead each iterate's marginal is sandwiched, in expectation, between the loop's n-th Kleene iterate and the loop itself (while_iterate_le_while_loop, while_loop_eq_iteration); the suprema then agree.

theorem GaudisCrypt.SubProbability.expected_mono_of_satisfies {X : Type u} {μ : SubProbability X} {P : X → Prop} (hP : μ.satisfies P) {F G : X → ENNReal} (h : ∀ (x : X), P x → F x ≤ G x) :

Monotonicity of expected on the support: an integrand that is pointwise smaller on the support integrates to a smaller value.

theorem GaudisCrypt.while_loop_eq_iteration {s : Type u} (cond : ProgramDenotation s Bool) (body : ProgramDenotation s Unit) (σ : s) :
while_loop cond body σ = (while_iteration cond body) (fun (x : Unit) => while_loop cond body) () σ

The while loop is a fixed point of its iteration functional, at every state.

theorem GaudisCrypt.expected_while_iteration {s : Type u} (cond : ProgramDenotation s Bool) (body : ProgramDenotation s Unit) (fp : Unit → ProgramDenotation s Unit) (σ : s) (G : Unit × s → ENNReal) :
((while_iteration cond body) fp () σ).expected G = (cond σ).expected fun (b : Bool × s) => if b.1 = true then (body b.2).expected fun (u : Unit × s) => (fp () u.2).expected G else G ((), b.2)

One step of the while_iteration functional, in expectation.

theorem GaudisCrypt.while_iterate_le_while_loop {s : Type u} (cond : ProgramDenotation s Bool) (body : ProgramDenotation s Unit) (n : ℕ) (σ : s) (G : Unit × s → ENNReal) :
((⇑(while_iteration cond body))^[n] ⊥ () σ).expected G ≤ (while_loop cond body σ).expected G

Every Kleene iterate of the loop is below the loop, in expectation.

theorem GaudisCrypt.ProgramDenotation.prhl2.while_upto {s₁ s₂ : Type u} {cond₁ : ProgramDenotation s₁ Bool} {body₁ : ProgramDenotation s₁ Unit} {cond₂ : ProgramDenotation s₂ Bool} {body₂ : ProgramDenotation s₂ Unit} {Inv Exit : s₁ → s₂ → Prop} {PostC : Bool → s₁ → s₂ → Prop} {PostE : Unit × s₁ → Unit × s₂ → Prop} (h_cond : prhl2 (fun (σ₁ : s₁) (σ₂ : s₂) => Inv σ₁ σ₂ ∧ ¬Exit σ₁ σ₂) cond₁ cond₂ fun (u : Bool × s₁) (v : Bool × s₂) => u.1 = v.1 ∧ PostC u.1 u.2 v.2) (h_body : prhl2 (PostC true) body₁ body₂ fun (u : Unit × s₁) (v : Unit × s₂) => Inv u.2 v.2) (h_exit : prhl2 (fun (σ₁ : s₁) (σ₂ : s₂) => Inv σ₁ σ₂ ∧ Exit σ₁ σ₂) (GaudisCrypt.while_loop cond₁ body₁) (GaudisCrypt.while_loop cond₂ body₂) PostE) :
prhl2 Inv (GaudisCrypt.while_loop cond₁ body₁) (GaudisCrypt.while_loop cond₂ body₂) fun (u : Unit × s₁) (v : Unit × s₂) => PostC false u.2 v.2 ∨ PostE u v

Synchronized while rule with an exit. As ProgramDenotation.prhl2.while_loop: under the invariant (and while Exit fails) the guards are coupled to agree, refining the invariant to PostC, and the bodies preserve the invariant from PostC true. In addition, at the start of any iteration where Exit holds, the two remaining whole loops are related by the coupling h_exit with post PostE. The loops then relate either at PostC false (both guards failed together, before an exit) or at PostE (an exit was taken).

The witness is Φ.lfp, the least fixed point of the coupling transformer that runs the exit coupling if Exit holds and otherwise the guard coupling and, while it fires, the body coupling. Its marginals are sandwiched between the loop's Kleene iterates and the loop.