Documentation

GaudisCrypt.Logic.PHoareWp

Backwards reasoning for probabilistic Hoare judgments #

The tactics of Logic/HoareWp.lean and Logic/Inline.lean, for phoareStmt:

Judgments about equivalent statements #

theorem GaudisCrypt.phoareStmt_iff_of_equivInLens {A : ProgramState → Prop} {s s' : Stmt} {g : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} {r : ProgramState → ENNReal} {l : Lens ProgramState ProgramState} (h : StmtWithHoles.EquivInLens s s' l) :
phoareStmt A s g ρ r ↔ phoareStmt (fun (τ : ProgramState) => A (l.get τ)) s' (fun (τ : ProgramState) => g (l.get τ)) ρ fun (τ : ProgramState) => r (l.get τ)

A judgment about an equivalent statement: along s.EquivInLens s' l, the judgment about s is the one about s' with the conditions, the expectation and the bound read through l.

theorem GaudisCrypt.phoareStmt_of_equivInLens {A A' : ProgramState → Prop} {s s' : Stmt} {g g' r r' : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} {l : Lens ProgramState ProgramState} (h : StmtWithHoles.EquivInLens s s' l) (hA : (fun (τ : ProgramState) => A (l.get τ)) = A') (hg : (fun (τ : ProgramState) => g (l.get τ)) = g') (hr : (fun (τ : ProgramState) => r (l.get τ)) = r') (h' : phoareStmt A' s' g' ρ r') :
phoareStmt A s g ρ r

phoareStmt_iff_of_equivInLens, backwards, with the conditions given up to an equation: what phoare_inline applies.

theorem GaudisCrypt.phoareStmt_of_equiv {A : ProgramState → Prop} {s s' : Stmt} {g r : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} (h : StmtWithHoles.Equiv s s') (h' : phoareStmt A s' g ρ r) :
phoareStmt A s g ρ r

A judgment about an equivalent statement, without renaming.

theorem GaudisCrypt.StmtWithHoles.ewp_call {sig : ProcedureSignature} (x : Setter sig.ret ProgramState) (p : Procedure sig) (args : Getter sig.ParamType ProgramState) (g : ProgramState → ENNReal) :
Stmt.ewp (GaudiProg[ x <- call p (§args); ]) g = fun (σ : ProgramState) => p.ewp (args.get σ) (fun (v : sig.ret) (gl : VariableAssignment) => g (x.set v (ProgramState.globalL.set gl σ))) σ.globals

Stmt.ewp_call, at the spelling the statements carry.

wp rules for the generic driver (Logic/JudgmentWp.lean) #

theorem GaudisCrypt.phoareStmt_seq_det {A : ProgramState → Prop} {s d : Stmt} {upd : ProgramState → ProgramState} {g r : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} (hd : d.Det upd) (h : phoareStmt A s (fun (σ : ProgramState) => g (upd σ)) ρ r) :
phoareStmt A (GaudiProg[ stmt s; stmt d; ]) g ρ r

Trailing deterministic code (Stmt.Det): the expectation after the update.

theorem GaudisCrypt.phoareStmt_seq_sample {α : Type} {A : ProgramState → Prop} {s : Stmt} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} {g r : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} (h : phoareStmt A s (fun (σ : ProgramState) => (e.get σ).expected fun (a : α) => g (x.set a σ)) ρ r) :
phoareStmt A (GaudiProg[ stmt s; x <$ §e; ]) g ρ r

A trailing sampling: the average over the distribution.

theorem GaudisCrypt.phoareStmt_seq_call {sig : ProcedureSignature} {A : ProgramState → Prop} {s : Stmt} {x : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {g r : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} (h : phoareStmt A s (fun (σ : ProgramState) => p.ewp (args.get σ) (fun (v : sig.ret) (gl : VariableAssignment) => g (x.set v (ProgramState.globalL.set gl σ))) σ.globals) ρ r) :
phoareStmt A (GaudiProg[ stmt s; x <- call p (§args); ]) g ρ r

A trailing call, without a specification: the callee's expectation, symbolically.

theorem GaudisCrypt.phoareStmt_seq_if {A : ProgramState → Prop} {s t e : Stmt} {c : Getter Bool ProgramState} {g r : ProgramState → ENNReal} {ρ : ENNReal → ENNReal → Prop} (h : phoareStmt A s (fun (σ : ProgramState) => if c.get σ = true then t.ewp g σ else e.ewp g σ) ρ r) :
phoareStmt A (GaudiProg[ stmt s; if (§c) { stmt t; } else { stmt e; } ]) g ρ r

A trailing conditional with calls or samplings in its branches: the branches' expectations, by cases on the condition (postSimp then computes them).

Conditionals with samplings or calls in Hoare triples #

A Hoare triple is a pHL judgment with bound 0 (hoareStmt_iff_phoareStmt), so a conditional whose branches sample or call gets its wp from the expectation calculus: each branch's outcomes off the postcondition have probability 0, an expectation postSimp computes with the Stmt.ewp lemmas and turns back into a condition on the support (expected_eq_zero_iff).

theorem GaudisCrypt.SubProbability.expected_eq_zero_iff {α : Type u_1} (μ : SubProbability α) (f : α → ENNReal) :
μ.expected f = 0 ↔ ∀ a ∈ μ.support, f a = 0

An expectation is 0 iff the function vanishes on the support (the measure is discrete).

theorem GaudisCrypt.ite_eq_zero_iff' {c : Prop} [Decidable c] (a b : ENNReal) :
(if c then a else b) = 0 ↔ (c → a = 0) ∧ (¬c → b = 0)
theorem GaudisCrypt.hoare_seq_if {A B : ProgramState → Prop} {s t e : Stmt} {c : Getter Bool ProgramState} (h : hoareStmt A s fun (σ : ProgramState) => if c.get σ = true then t.ewp (fun (τ : ProgramState) => indicatorE ¬B τ) σ = 0 else e.ewp (fun (τ : ProgramState) => indicatorE ¬B τ) σ = 0) :
hoareStmt A (GaudiProg[ stmt s; if (§c) { stmt t; } else { stmt e; } ]) B

The wp rule for a trailing conditional with samplings or calls in its branches, for Hoare triples: in each branch, the outcomes off B have probability 0.

What postSimp adds for Hoare triples, to compute a branch's expectation (hoare_seq_if): the Stmt.ewp lemmas, and back from an expectation = 0 to a condition.

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

    Apply the right-to-left direction of the equivalence lem, its arguments by unification.

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

      The parts of a goal phoareStmt A s g ρ r.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        @[reducible, inline]

        phoareOnStmt is the generic Judgment.onStmt (kept for the name).

        Equations
        Instances For
          @[reducible, inline]

          phoare_split k: regroup the statement as {x₁; …; xₙ₋ₖ}; {xₙ₋ₖ₊₁; …; xₙ} — the generic HoareSplit.hoareSplit, as for every registered judgment.

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

              The simp set computing an expectation: the exact expectations of the statements, the reads of simpReads, and the uniform distribution's average.

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

                M.procedure for a procedure module M that evaluates to Module.proc q (the inliner's module evaluation, Flatten.moduleStep?) ↦ q, the spelling call sites carry after inlining (FairCoin.flip.procedure, say). Anything else, an abstract module say, is left as it is.

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

                  M.procedure ↦ q where the procedure module M evaluates to Module.proc q (procedureConst): one spelling for a callee, whether it came from the notation or from inlining (where it is often a moduletype accessor applied to a record).

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

                    The default simprocs, and reduceProcedureConst.

                    Equations
                    Instances For

                      Simplify e with simp only over decls (theorems rewritten with, definitions unfolded) and the default simprocs.

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

                        Simplify the expectation of a goal phoareStmt A s g ρ r (ewpDecls, ewpSimprocs).

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[reducible, inline]

                          phoareInline is the generic Flatten.judgmentInline: the conditions and the bound are read through the renaming, cleaned by the inliner, then simplified like an expectation (the condSimp this file registers for phoareStmt).

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

                              phoare_wp k's step: the last k statements by wp — the judgment-generic Judgment.wpStep, with the phoare rules registered above (deterministic code, an exact average for a sampling, a symbolic expectation for a call), the expectation simplified by ewpDecls after each step.

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

                                  h : f a₀ … aₙ with argument i replaced by the result r of simplifying it.

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

                                    A procedure judgment phoareProc P p G ρ R with its conditions P, G, R in the normal form phoare_wp leaves expectations in (ewpDecls): the phoare[ ] notation wraps them in lets and GaudiExpr getters. The procedure is left as it is, so that the judgment still unifies with a call statement.

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

                                      One backwards step over a call, on a goal phoareStmt A {x₁; …; xₙ₋₁; x <- call p (a)} g ρ r and a judgment h about p: close the call by phoareStmt_call_le/_ge/_eq (by the goal's relation; an exact h serves for all three), and continue with the judgment about x₁; …; xₙ₋₁, its expectation the bound of h under the side condition, simplified (ewpDecls). When the continuation is literally h's postcondition and h's precondition simplifies to True, the condition disappears.

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

                                        phoare_call h: phoareCallStep h on the main goal — the last statement is a call, closed by the procedure judgment h about the callee (EC's call (_ : P ==> Q)), or by its losslessness h : islossless M.f (EC's call H).

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

                                          A procedure judgment phoareProc P p G Eq R as a conditional rewrite rule ∀ args σ, P args σ → p.ewp args G σ = R args σ, normalized like the goal (ewpDecls, ewpSimprocs). Bounds (≤, ≥) are not rewrite rules and are rejected.

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

                                            phoare_simp [h₁, …]: simplify with the simp set of phoare_wp (ewpDecls) and the exact procedure judgments hᵢ as rewrite rules for their callees' expectations (specRewrite). On a goal phoareStmt A s g ρ r only the expectation g is simplified; on any other goal (what phoare_skip leaves, say) the whole goal.

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

                                              A loop step, on a goal phoareStmt A {x₁; …; xₙ₋₁; while c body} g ≤ r and an expectation invariant I: phoareStmt_while_le closes the loop, leaving three goals, in order: body (one iteration does not increase I, from the states where the test passes), exit (g ≤ I where the test fails, simplified, reads generalized), and the judgment about x₁; …; xₙ₋₁ with I as its expectation.

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

                                                phoare_while_le I: phoareWhileLeStep on the main goal, with the expectation invariant I written like a condition of a phoare[ ] judgment (an ENNReal expression; §x reads the program variable x, or a global). Goals: body, exit, then the code before the loop.

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

                                                  Elaborate a function of the state with values in T, written like a condition of a phoare[ ] judgment about the goal's program (elabGoalExpectation at any type).

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

                                                    Elaborate an expectation written like a condition of a phoare[ ] judgment about the goal's program: §x reads the program variable x (bound to its slot, as the notation binds it) or a global.

                                                    Equations
                                                    Instances For

                                                      A goal ∀ σ, … about conditions and expectations: β-reduced, simplified like an expectation (ewpDecls), reads generalized.

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

                                                        A loop step for a lower bound, on phoareStmt A {x₁; …; xₙ₋₁; while c body} g ≥ r, with the invariant I and the exit probability ε (phoareStmt_while_ge). Goals, in order: body, exit, the code before the loop, lossless, progress, and whichever of I ≤ 1, g ≤ 1, ε ≠ 0, ε ≤ 1 simp does not close.

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

                                                          phoare_while_le I for lower bounds: phoare_while_ge I using ε (phoareWhileGeStep), I written like a condition of a phoare[ ] judgment, ε a plain ENNReal.

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

                                                            A loop step for a lower bound with a variant, on phoareStmt A {x₁; …; xₙ₋₁; while c body} g ≥ r, with the invariant I and the variant V : ProgramState → ℕ (phoareStmt_while_ge_variant). Goals, in order: body (the body does not decrease I), exit, the code before the loop, and decreases (∀ k, a Hoare triple: from the test and V = k, the body ends with V < k).

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

                                                              phoare_while_ge I variant V: the lower-bound loop rule with a variant (phoareWhileGeVariantStep) — for counted loops, which exit with probability 0 in all rounds but the last. I and V written like conditions of a phoare[ ] judgment, V natural.

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

                                                                The end of a backwards proof, on a goal phoareStmt A skip g ρ r: the relation itself, ∀ σ, A σ → ρ (g σ) (r σ) (phoareStmt_skip_iff), β-reduced, with the reads of the state generalized to plain values (generalizeReads).

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