Probabilistic Hoare logic #
EasyCrypt's phoare[ c : P ==> Q ] ⋈ r (⋈ one of =, ≤, ≥): from every state satisfying
P, the probability that c ends in a state satisfying Q is ⋈ r.
The judgment is stated for an expectation g rather than an event: Stmt.ewp c g σ is the
expected value of g after running c from σ, and an event Q is its indicator
if Q then 1 else 0. The reason is the sequencing law phoareStmt_seq_iff, which is exact,
for all three relations: the judgment about p; q is the one about p with the expectation g
replaced by q.ewp g. So backwards reasoning only ever rewrites the expectation, as long as
the code has an exact wp (everything but loops, including calls to abstract procedures, whose
wp stays a symbolic term).
The relation ρ is Eq, LE.le or GE.ge (the constants, not lambdas, so that rules
and the printer can match on them); the bound r may depend on the initial
state.
The expected value of g after running c from σ.
Equations
- c.ewp g = (GaudisCrypt.programDenotation c).wp fun (r : Unit × GaudisCrypt.ProgramState) => g r.2
Instances For
The expected value of G (of the result and the globals) after running the procedure p on
args from the globals σ.
Equations
- p.ewp args G = (GaudisCrypt.procedureDenotation p args).wp fun (r : sig.ret × GaudisCrypt.VariableAssignment) => G r.1 r.2
Instances For
phoare[ A ==> g ] ρ r { c }: from every state satisfying A, the expected value of g
after c is in the relation ρ to r.
Equations
- GaudisCrypt.phoareStmt A c g ρ r = ∀ (σ : GaudisCrypt.ProgramState), A σ → ρ (c.ewp g σ) (r σ)
Instances For
phoare[ p : A ==> G ] ρ r: on all arguments and globals satisfying A, the expected value
of G after p is in the relation ρ to r.
Equations
- GaudisCrypt.phoareProc A p G ρ r = ∀ (args : sig.ParamType) (σ : GaudisCrypt.VariableAssignment), A args σ → ρ (p.ewp args G σ) (r args σ)
Instances For
Events and probabilities #
The indicator of a proposition, as an expectation: 1 if it holds, else 0 (EC's b2r).
Equations
- GaudisCrypt.indicatorE Q = if Q then 1 else 0
Instances For
The probability that the procedure p, run on args from the globals σ, ends with a
result and globals satisfying E — EC's Pr[p(args) @ &σ : E].
Equations
Instances For
A probability is the expectation of the event's indicator.
EC's byphoare: a probability, from a procedure judgment about the event.
EC's islossless p: p terminates with probability 1, on all arguments and globals.
Equations
- p.IsLossless = ∀ (args : sig.ParamType) (σ : GaudisCrypt.VariableAssignment), p.ewp args (fun (x : sig.ret) (x_1 : GaudisCrypt.VariableAssignment) => 1) σ = 1
Instances For
Hoare triples as probability-0 judgments #
hoare[ A ==> B ] { c } is phoare[ A ==> ¬B ] = 0 { c }.
The same for procedures.
Structural rules #
Sequencing is exact: the judgment about p; q is the one about p, with q's expected
value of g in place of g.
Strengthening the precondition.
An upper bound is monotone in the expectation, downwards.
A lower bound is monotone in the expectation, upwards.
An exact judgment gives both bounds.
Two bounds give an exact judgment.
At skip, the judgment is about g itself.
Exact expectations of the statements #
A call's expectation is the callee's, at the arguments, of g after writing the result and
the globals back. For an abstract callee this stays a symbolic term.
Expectation algebra #
What is left of a judgment once phoare_wp has run: sums and quotients of symbolic procedure
expectations. Linearity, losslessness, and the arithmetic of averages.
A lossless procedure's expectation of a constant is the constant.
The expectation of a constant is at most the constant (any procedure).
An event and its negation partition.
The union bound for two events.
A uniform draw hits a given point with chance 1 / |α|.
The average of a constant over a nonempty finite type is the constant.
Calls with a specification #
EC's call (_ : P ==> Q) for phoare: a trailing call x <- p(a) is closed by a judgment about
p, and the expectation before it becomes the judgment's bound — under the side condition that
the precondition holds at the arguments and the expectation after the call (the continuation
fun v gl => g (x.set v (globalL.set gl σ))) is related to the judgment's postcondition as the
relation needs. Where the condition fails, the bound is the trivial one (⊤ for ≤, 0 for
≥), or for = the call's own symbolic expectation, so that the = rule is exact.
The call rule for upper bounds.
The call rule for lower bounds.
The call rule for exact values: where the condition holds the call's expectation is the bound, elsewhere it stays as it was.
An exact procedure judgment gives both bounds.
A procedure judgment as a rewrite rule for the callee's expectation.
A conjunction of events is the product of their indicators.
Loops #
The rule for upper bounds: an expectation invariant I bounds a loop's expectation of g if one
iteration (from a state where the test passes) does not increase it, and g is below it where the
test fails — I is a pre-fixed point of the loop's wp functional (wp_while_invariant). A
qualitative invariant inv is folded in as if inv then ‹potential› else ⊤.
The loop's expectation of g is below the invariant I.
The loop rule for upper bounds, for a trailing loop: the body preserves the invariant I
(as an upper bound, from the states where the test passes), the expectation is below I where
the test fails, and the code before the loop is judged with I as its expectation.
Loops: termination and lower bounds #
An upper-bound invariant cannot see that a loop terminates — a loop that never does has
expectation 0, below everything. Lower bounds need termination: Stmt.ewp_while_one is the
zero-one argument (if every iteration exits with probability at least ε > 0, the loop
terminates almost surely), and Stmt.ewp_while_ge the lower-bound rule built on it, by
complement: with mass 1, Pr[g] = 1 − Pr[¬g], and 1 − I is an upper invariant for 1 − g.
The fixed-point equation of a loop.
Almost-sure termination (the zero-one argument): if the body is lossless and exits the
loop with probability at least ε > 0 from every state where the test passes, the loop
terminates with probability 1.
With mass 1, the expectation of 1 − g is 1 minus that of g (for g ≤ 1).
The lower-bound rule for a loop: an invariant I ≤ 1 that one iteration does not decrease
(where the test passes) and that is below g where the test fails is a lower bound for the
loop's expectation of g ≤ 1, provided the loop terminates almost surely
(Stmt.ewp_while_one).
An indicator is at most 1.
The loop rule for lower bounds, for a trailing loop: an invariant I ≤ 1 that the body
does not decrease (from the states where the test passes) and that is below g ≤ 1 where the
test fails, and almost-sure termination — the body is lossless and exits the loop with
probability at least ε > 0 (Stmt.ewp_while_one). The code before the loop is judged with
I as its expectation.
Lower bounds with a variant #
A counted loop (while (j < N)) exits with probability 0 in every round but the last, so the
exit-probability argument above does not apply. A variant does: a natural number that every
round strictly decreases, on every outcome of the body. Then the loop is lower-bounded by a
sub-invariant by well-founded induction on the variant — the body need not even be lossless
(the sub-invariant then accounts for the mass it loses). EC's while I V for phoare.
A statement's expectation is monotone in the function on the outcomes a Hoare triple allows.
Lower bounds with a variant: if every round strictly decreases the variant V (on every
outcome of the body), an I that the body does not decrease (from the states where the test
passes) and that is below g where it fails bounds the loop from below.
The loop rule for lower bounds, with a variant (Stmt.ewp_while_ge_variant), for a
trailing loop. The code before the loop is judged with I as its expectation.
Two procedure bounds give an exact judgment.
Procedure judgments as statement judgments #
A procedure judgment is the statement judgment about the one-line body res <- p(args);, as
for Hoare triples (hoareProc_as_hoareStmt).