Documentation

GaudisCrypt.Logic.PHoareRnd

pHL on an exact bound: EC's rnd E, conseq, islossless #

EC proves phoare[ c; x <$ d : P ==> Q ] = r by

rnd E.                      (* c : P ==> mu d E = r /\ forall v, v \in d => (E v <=> Q v) *)
conseq (: _ ==> true).      (* the condition holds in every state *)
islossless.                 (* c is lossless *)

The judgments of Logic/PHoare.lean are about expectations, and their rules for a sampling (phoare_auto, rnd_pred1) work on the expectation it leaves, for upper bounds. Here are EC's three steps for an exact bound, on the judgment itself.

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

An expectation that is r on an event of probability 1 (and at most 1 elsewhere) has expected value r.

theorem GaudisCrypt.phoareStmt_rnd_eq {A : ProgramState → Prop} {c : Stmt} {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} (E : ProgramState → α → Prop) {Q : ProgramState → Prop} {r : ENNReal} (h : phoare[ A CurrentState.state ==> ((e.get CurrentState.state).expected fun (v : α) => indicatorE (E CurrentState.state v)) = r ∧ ∀ v ∈ (e.get CurrentState.state).support, E CurrentState.state v ↔ Q (x.set v CurrentState.state) ] = 1 { stmt c; }) :
phoare[ A CurrentState.state ==> Q CurrentState.state ] = r { stmt c; x <$ §e; }

EC's pHL rnd E on an exact bound: the code before the sampling reaches, with probability 1, a state in which E has probability r under the distribution and, on its support, E is the postcondition.

theorem GaudisCrypt.phoareStmt_conseq_eq_one {A : ProgramState → Prop} {c : Stmt} {Q : ProgramState → Prop} (Q' : ProgramState → Prop) (hQ : ∀ (τ : ProgramState), Q' τ → Q τ) (h : phoare[ A CurrentState.state ==> Q' CurrentState.state ] = 1 { stmt c; }) :
phoare[ A CurrentState.state ==> Q CurrentState.state ] = 1 { stmt c; }

EC's conseq (: _ ==> Q') on phoare[ … ] = 1: a weaker condition that is still reached with probability 1.

noncomputable def GaudisCrypt.EC.mu {α : Type} (d : SubProbability α) (E : α → Prop) :

EC's mu d E: the probability of the event E under d, a real.

Equations
Instances For
    theorem GaudisCrypt.EC.mu_pred1 {α : Type} (d : SubProbability α) (c : α) :
    mu d (pred1 c) = mu1 d c

    EC's mu1 d c is mu d (pred1 c).

    theorem GaudisCrypt.EC.dbool1E_mu1 (b : Bool) :
    mu1 dbool b = 1 / 2

    EC's dbool1E, over the reals: mu1 {0,1} b = 1%r / 2%r.

    theorem GaudisCrypt.phoareStmt_rnd_eq_ofReal {A : ProgramState → Prop} {c : Stmt} {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} (E : ProgramState → α → Prop) {Q : ProgramState → Prop} {r : ℝ} (h : phoare[ A CurrentState.state ==> EC.mu (e.get CurrentState.state) (E CurrentState.state) = r ∧ ∀ v ∈ (e.get CurrentState.state).support, E CurrentState.state v ↔ Q (x.set v CurrentState.state) ] = 1 { stmt c; }) :
    phoare[ A CurrentState.state ==> Q CurrentState.state ] = (ENNReal.ofReal r) { stmt c; x <$ §e; }

    phoareStmt_rnd_eq for a real bound (Pr[ … ] = r): the event's probability as EC's mu d E.

    theorem GaudisCrypt.phoareStmt_rnd_ge_ofReal {A : ProgramState → Prop} {c : Stmt} {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} (E : ProgramState → α → Prop) {Q : ProgramState → Prop} {r : ℝ} (h : phoare[ A CurrentState.state ==> r ≤ EC.mu (e.get CurrentState.state) (E CurrentState.state) ∧ ∀ v ∈ (e.get CurrentState.state).support, E CurrentState.state v → Q (x.set v CurrentState.state) ] = 1 { stmt c; }) :
    phoare[ A CurrentState.state ==> Q CurrentState.state ] ≥ (ENNReal.ofReal r) { stmt c; x <$ §e; }

    EC's pHL rnd E on a lower bound, phoare[ c; x <$ d : P ==> Q ] ≥ r: the code before the sampling reaches, with probability 1, a state in which E has probability at least r and, on the distribution's support, implies the postcondition.

    theorem GaudisCrypt.phoareStmt_rnd_ge_post {A : ProgramState → Prop} {c : Stmt} {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} {Q : ProgramState → Prop} {r : ℝ} (h : phoare[ A CurrentState.state ==> r ≤ EC.mu (e.get CurrentState.state) fun (v : α) => Q (x.set v CurrentState.state) ] = 1 { stmt c; }) :
    phoare[ A CurrentState.state ==> Q CurrentState.state ] ≥ (ENNReal.ofReal r) { stmt c; x <$ §e; }

    phoareStmt_rnd_ge_ofReal with the postcondition itself as the event (EC's rnd on a lower bound).

    Events reached with probability 1 #

    phoare[ c : P ==> Q ] = 1 with Q an event: EC's rules keep it an event (a lossless call leaves ∀ result, Q, a sampling is_lossless d ∧ ∀ v ∈ d, Q), where the rules on expectations (Logic/PHoareWp.lean) would leave a product of indicators, a sum, or a callee's expectation.

    theorem GaudisCrypt.phoareStmt_call_lossless_eq_one {sig : ProcedureSignature} {A : ProgramState → Prop} {pre : Stmt} {x : Setter sig.ret ProgramState} {p : Procedure sig} {a : Getter sig.ParamType ProgramState} {Q : ProgramState → Prop} (hp : p.IsLossless) (h : phoare[ A CurrentState.state ==> ∀ (v : sig.ret) (gl : VariableAssignment), Q (x.set v (ProgramState.globalL.set gl CurrentState.state)) ] = 1 { stmt pre; }) :
    phoare[ A CurrentState.state ==> Q CurrentState.state ] = 1 { stmt pre; x <- call p (§a); }

    EC's call H with H : islossless f, on phoare[ c; x <@ f(a) : P ==> Q ] = 1: the code before the call reaches, with probability 1, a state from which Q holds whatever the call returns (and whatever globals it leaves).

    theorem GaudisCrypt.phoareStmt_rnd_lossless_eq_one {A : ProgramState → Prop} {c : Stmt} {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} {Q : ProgramState → Prop} (h : phoare[ A CurrentState.state ==> EC.is_lossless (e.get CurrentState.state) ∧ ∀ v ∈ (e.get CurrentState.state).support, Q (x.set v CurrentState.state) ] = 1 { stmt c; }) :
    phoare[ A CurrentState.state ==> Q CurrentState.state ] = 1 { stmt c; x <$ §e; }

    EC's pHL rnd (rnd predT) on phoare[ c; x <$ d : P ==> Q ] = 1: the distribution is lossless and Q holds on its support.

    theorem GaudisCrypt.phoareStmt_skip_event {A Q : ProgramState → Prop} (h : ∀ (σ : ProgramState), A σ → Q σ) :
    phoare[ A CurrentState.state ==> Q CurrentState.state ] = 1 { skip; }

    The end of a proof of phoare[ skip : P ==> Q ] = 1: P implies Q.

    theorem GaudisCrypt.phoareStmt_one_iff_event {A : ProgramState → Prop} {c : Stmt} {ρ : ENNReal → ENNReal → Prop} {r : ProgramState → ENNReal} :
    phoareStmt A c (fun (_τ : ProgramState) => 1) ρ r ↔ phoareStmt A c (fun (_τ : ProgramState) => indicatorE True) ρ r

    The expectation 1 is the event True.

    The expectation of the phoare judgment g as one event, fun τ => indicatorE (Q τ) (the rules on expectations split a conjunction into a product, a conditional into two indicators); with one, the expectation 1 is the event True. none if it is not an event.

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

        Is the judgment g an exact one with the bound 1 (ENNReal.ofReal 1, as byphoare leaves it, is rewritten to 1)? none, the state restored, if not.

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

            The event of the judgment m (fun τ => indicatorE (P τ)) simplified under the indicator (ewpDecls: the reads through the last statement's write), so that it stays one event; keep, if given, is abstracted meanwhile (a bound, kept as it is written).

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

              A rule about c; s (its hypothesis h the judgment about c) matched with a goal: the goal split before its last statement, the rule applied to metavariables, and those by name.

              Instances For

                Match the rule lem with the goal g, split before its last statement. Nothing is assigned yet but the split (EventRule.close); none, the state restored, if the rule does not fit.

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

                  Apply the matched rule: the new goal is its hypothesis h, tagged judgment.

                  Equations
                  Instances For

                    EC's call H, H : islossless f, on an event reached with probability 1 (phoareStmt_call_lossless_eq_one).

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

                      EC's pHL rnd on an event reached with probability 1 (phoareStmt_rnd_lossless_eq_one).

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

                        EC's auto on phoare[ c : P ==> Q ] = 1, Q an event: backwards over the code to the last call (an assignment or a conditional by wp, a sampling by rnd), the judgment an event throughout; skip when the code is used up. false if the goal is not such a judgment.

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

                          EC's pHL rnd E on an exact bound, phoare[ c; x <$ d : P ==> Q ] = r: the goal becomes phoare[ c : P ==> mu d E = r ∧ ∀ v ∈ d, (E v ↔ Q v) ] = 1 (phoareStmt_rnd_eq). E is a predicate on the sampled value and may read the program's variables (pred1 §b').

                          EC's rnd (no event: rnd predT), on phoare[ c; x <$ d : P ==> Q ] = 1: the goal becomes phoare[ c : P ==> is_lossless d ∧ ∀ v ∈ d, Q v ] = 1 (phoareStmt_rnd_lossless_eq_one).

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            theorem GaudisCrypt.phoareStmt_conseq_ge {A A' : ProgramState → Prop} {c : Stmt} {Q Q' : ProgramState → Prop} {r : ProgramState → ENNReal} (hpre : ∀ (σ : ProgramState), A σ → A' σ) (hpost : ∀ (σ : ProgramState), A σ → ∀ (τ : ProgramState), Q' τ → Q τ) (h : phoare[ A' CurrentState.state ==> Q' CurrentState.state ] ≥ (r CurrentState.state) { stmt c; }) :
                            phoare[ A CurrentState.state ==> Q CurrentState.state ] ≥ (r CurrentState.state) { stmt c; }

                            EC's conseq (: P' ==> Q') on a lower bound phoare[ c : P ==> Q ] ≥ r: a weaker precondition and a stronger event; what the precondition says may be used for the events.

                            theorem GaudisCrypt.phoareStmt_eq_one_of_hoare_lossless {A : ProgramState → Prop} {c : Stmt} {Q : ProgramState → Prop} (hl : phoare[ A CurrentState.state ==> 1 ] = 1 { stmt c; }) (hh : hoare[ A CurrentState.state ==> Q CurrentState.state ] { stmt c; }) :
                            phoare[ A CurrentState.state ==> Q CurrentState.state ] = 1 { stmt c; }

                            EC's conseq (: _ ==> true) (: _ ==> Q) on phoare[ c : P ==> Q ] = 1: the code is lossless, and Q holds after it (a Hoare triple).

                            A side condition about two states, ∀ σ, A σ → ∀ τ, B τ: over values, the reads of σ generalized first, then those of τ.

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

                              EC's conseq (: _ ==> true) (: _ ==> Q) on phoare[ c : P ==> Q ] = 1 (phoareStmt_eq_one_of_hoare_lossless): goals hoare (the triple hoare[ c : P ==> Q ]), then lossless (phoare[ c : P ==> true ] = 1) — EC's order.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                theorem GaudisCrypt.ENNReal.sum_one_div_card_eq_one (α : Type) [Fintype α] [Nonempty α] :
                                ∑ _x : α, 1 / ↑(Fintype.card α) = 1

                                The uniform distribution on a finite nonempty type is lossless, as the sum phoare_auto leaves at a sampling from it.

                                The procedure the last statement of s calls, if it is a call.

                                The number of statements of s (under the lets of its local variables).

                                EC's islossless on phoare[ c : P ==> 1 ] = 1: the code statement by statement from the end (phoare_seq_lossless … : True), an assignment or a sampling by phoare_auto, a call by a new goal islossless p (phoare_call). The calls' goals, the last call first.

                                EC's islossless: a statement without loops is lossless when its samplings and the procedures it calls are. On phoare[ c : P ==> true ] = 1 (and on islossless M.f): a call of a concrete procedure is opened in turn; one goal islossless p per call of an abstract one, the last call first (EC's order).

                                Equations
                                Instances For