Documentation

GaudisCrypt.Logic.LoopTransform

Loop transformations and statement-level transitivity #

EasyCrypt rewrites loops in a relational judgment before reasoning about them: splitwhile{i} k : e (a loop while c b becomes while (c /\ e) b; while c b) and unroll{i} k (one iteration peeled off as a conditional). Here both are denotational equivalences of statements (StmtWithHoles.Equiv), moved into a judgment with prhlStmt_of_equiv_left/_right.

splitwhile is proven for loops whose first part terminates by a variant (every SHA-3 loop is bounded): the remaining loop's expectation is then a fixed point of the first part's loop, from above (Stmt.ewp_while_le) and from below (Stmt.ewp_while_ge_variant).

EasyCrypt's transitivity{i} { code } P Q at the statement level is prhlStmt.trans.

Equivalence from denotations #

Statements with the same run are equivalent.

theorem GaudisCrypt.Stmt.denotation_eq_of_ewp {a b : Stmt} (h : ∀ (g : ProgramState → ENNReal) (σ : ProgramState), a.ewp g σ = b.ewp g σ) (σ : ProgramState) :

Statements with the same expectations have the same run.

splitwhile #

The guard c /\ e of the first loop of a split.

Equations
Instances For
    @[simp]
    theorem GaudisCrypt.Getter.andB_get (c e : Getter Bool ProgramState) (σ : ProgramState) :
    (c.andB e).get σ = (c.get σ && e.get σ)
    theorem GaudisCrypt.Stmt.ewp_splitwhile (c e : Getter Bool ProgramState) (body : Stmt) (V : ProgramState → ℕ) (hvar : ∀ (k : ℕ), hoareStmt (fun (σ : ProgramState) => (c.andB e).get σ = true ∧ V σ = k) body fun (τ : ProgramState) => V τ < k) (g : ProgramState → ENNReal) (σ : ProgramState) :
    ewp (StmtWithHoles.while c body) g σ = ewp ((StmtWithHoles.while (c.andB e) body).seq (StmtWithHoles.while c body)) g σ

    splitwhile, on expectations: a loop is the same loop run first while e also holds, then on. V decreases on every round of the first loop.

    theorem GaudisCrypt.Stmt.Equiv.splitwhile (c e : Getter Bool ProgramState) (body : Stmt) (V : ProgramState → ℕ) (hvar : ∀ (k : ℕ), hoareStmt (fun (σ : ProgramState) => (c.andB e).get σ = true ∧ V σ = k) body fun (τ : ProgramState) => V τ < k) :

    splitwhile (EC's splitwhile … : e), as an equivalence of statements.

    unroll #

    unroll (EC's unroll): one round of a loop, peeled off as a conditional.

    A conditional followed by code #

    The code after a conditional goes into both branches (what EC's if does with the rest of the code).

    In judgments, for a trailing loop #

    theorem GaudisCrypt.prhlStmt_seq_splitwhile_left {A B : ProgramState → ProgramState → Prop} {s₁ s₂ body : Stmt} {c : Getter Bool ProgramState} (e : Getter Bool ProgramState) (V : ProgramState → ℕ) (hvar : ∀ (k : ℕ), hoareStmt (fun (σ : ProgramState) => (c.andB e).get σ = true ∧ V σ = k) body fun (τ : ProgramState) => V τ < k) (h : prhlStmt A (StmtWithHoles.seq s₁ ((StmtWithHoles.while (c.andB e) body).seq (StmtWithHoles.while c body))) s₂ B) :

    splitwhile{1} on a trailing loop.

    theorem GaudisCrypt.prhlStmt_seq_splitwhile_right {A B : ProgramState → ProgramState → Prop} {s₁ s₂ body : Stmt} {c : Getter Bool ProgramState} (e : Getter Bool ProgramState) (V : ProgramState → ℕ) (hvar : ∀ (k : ℕ), hoareStmt (fun (σ : ProgramState) => (c.andB e).get σ = true ∧ V σ = k) body fun (τ : ProgramState) => V τ < k) (h : prhlStmt A s₁ (StmtWithHoles.seq s₂ ((StmtWithHoles.while (c.andB e) body).seq (StmtWithHoles.while c body))) B) :

    splitwhile{2} on a trailing loop.

    unroll{1} on a trailing loop.

    theorem GaudisCrypt.prhlStmt_splitwhile_left {A B : ProgramState → ProgramState → Prop} {s₂ body : Stmt} {c : Getter Bool ProgramState} (e : Getter Bool ProgramState) (V : ProgramState → ℕ) (hvar : ∀ (k : ℕ), hoareStmt (fun (σ : ProgramState) => (c.andB e).get σ = true ∧ V σ = k) body fun (τ : ProgramState) => V τ < k) (h : prhlStmt A ((StmtWithHoles.while (c.andB e) body).seq (StmtWithHoles.while c body)) s₂ B) :
    prhlStmt A (StmtWithHoles.while c body) s₂ B

    splitwhile{1} on a loop that is the whole left statement.

    theorem GaudisCrypt.prhlStmt_splitwhile_right {A B : ProgramState → ProgramState → Prop} {s₁ body : Stmt} {c : Getter Bool ProgramState} (e : Getter Bool ProgramState) (V : ProgramState → ℕ) (hvar : ∀ (k : ℕ), hoareStmt (fun (σ : ProgramState) => (c.andB e).get σ = true ∧ V σ = k) body fun (τ : ProgramState) => V τ < k) (h : prhlStmt A s₁ ((StmtWithHoles.while (c.andB e) body).seq (StmtWithHoles.while c body)) B) :
    prhlStmt A s₁ (StmtWithHoles.while c body) B

    splitwhile{2} on a loop that is the whole right statement.

    unroll{1} on a loop that is the whole left statement.

    unroll{2} on a loop that is the whole right statement.

    Induction #

    theorem GaudisCrypt.prhlStmt_of_forall_nat {A B : ProgramState → ProgramState → Prop} {c₁ c₂ : Stmt} (f : ProgramState → ProgramState → ℕ) (h : ∀ (k : ℕ), prhlStmt (fun (σ₁ σ₂ : ProgramState) => A σ₁ σ₂ ∧ f σ₁ σ₂ = k) c₁ c₂ B) :
    prhlStmt A c₁ c₂ B

    A judgment by cases on a measure of the initial states (for an induction on it, e.g. on the rounds a loop has left: EC's elim on an integer in a proof by induction).

    Transitivity #

    theorem GaudisCrypt.prhlStmt.trans {A B A₂ B₂ : ProgramState → ProgramState → Prop} {c₁ c₂ c₃ : Stmt} (h₁ : prhlStmt A c₁ c₂ B) (h₂ : prhlStmt A₂ c₂ c₃ B₂) :
    prhlStmt (fun (σ₁ σ₃ : ProgramState) => ∃ (σ₂ : ProgramState), A σ₁ σ₂ ∧ A₂ σ₂ σ₃) c₁ c₃ fun (τ₁ τ₃ : ProgramState) => ∃ (τ₂ : ProgramState), B τ₁ τ₂ ∧ B₂ τ₂ τ₃

    Transitivity of statement judgments (EC's transitivity{i} { code } P Q, with code the middle statement): the preconditions and postconditions compose through a middle state.