Swapping statements (EC's swap) #
Two adjacent statements can be exchanged when neither sees the other's effect. Two cases cover what game hops need:
- a deterministic statement
d(Stmt.Det d u: it runs as the updateu) and a statementsthat commutes withu(Stmt.CommutesWith s u):d; s ≡ s; d(Stmt.swap_det); - a sampling
x <$ efrom a distribution that does not depend on the state, and a statement that commutes with every write tox:x <$ e; s ≡ s; x <$ e(Stmt.swap_sample, by Fubini for the two independent draws).
Stmt.CommutesWith s u — running s from u σ is running it from σ and applying u after —
is stated on expectations, and proved syntax-directed (assignments, samplings, calls,
conditionals, sequences) from state equations that simp settles for distinct variables:
the update leaves what s reads alone, and commutes with what s writes. A call commutes with
an update of the caller's locals that does not touch the result variable or the arguments: the
callee runs on its own locals.
Like split and inline, a swap is an equivalence of statements, so it works on every
registered judgment (stmt_swap). Also here: splitting a tuple assignment into one assignment
per component (Stmt.split_pair), which is how a swap reaches one parameter of an inlined call.
s commutes with the state update u: from u σ it does what it does from σ, followed by
u.
Equations
- s.CommutesWith u = ∀ (g : GaudisCrypt.ProgramState → ENNReal) (σ : GaudisCrypt.ProgramState), s.ewp g (u σ) = s.ewp (fun (τ : GaudisCrypt.ProgramState) => g (u τ)) σ
Instances For
A program in the footprint R, started after a deterministic update f from Rᶜ, ends as
if f were applied after it (ProgramDenotation.inFootprint at the kernel pure ∘ f).
A loop commutes with u when its guard does not see u and its body commutes with it
(ProgramDenotation.CommutesU.while_loop, the Kleene argument).
A call commutes with an update of the globals from outside the callee's footprint (EC's
swap past an abstract call, justified by the callee's memory restriction): the callee lies in R
on every argument, the update u applies f to the globals, and f is a deterministic update
from Rᶜ, so it can be applied before the call or after it. The arguments must not see u, and
the result variable must not interfere with it.
call_footprint for the update of an assignment X <- e to a global variable X outside the
callee's footprint (X.footprint ≤ Rᶜ). X and e come first, for the tactic to supply.
call_assign_global where the value e reads globals the callee cannot touch (EC's swap
of X <- X || f(K) past a call that stays out of X and K): the write commutes for the
globals gl a run of the callee ends in, which keep K (Procedure.satisfies_frame).
A call commutes with an update u that keeps the globals and the arguments, if the result's
write commutes with u whenever a lens K outside the callee's footprint has kept its value
(EC's swap of a call past x <- e, e reading a global the callee cannot touch: the callee's
run keeps K, Procedure.satisfies_frame).
A statement commutes with the identity update (of skip).
A statement commuting with two updates commutes with the one after the other (the update of
a sequence of deterministic statements, Stmt.Det.seq).
A call commutes with the update of a deterministic if (Stmt.Det.ite) if it commutes with
both branches' updates and keeps the condition: what the condition reads is the caller's locals
other than the result, and globals K outside the callee's footprint (EC's swap of a call past
if (…) {X <- …}, X outside the callee).
call_ite for a condition that reads no globals (only the caller's locals other than the
result).
Equivalences #
Fubini for two independent expectations (discrete measures).
Statements with the same expectations are equivalent.
Swap a deterministic statement with one that commutes with its update.
Swap a sampling from a distribution that does not depend on the state with a statement that commutes with every write to the sampled variable.
Swap a sampling with a call whose write-back leaves the distribution unchanged (it reads
only what the call keeps: the caller's locals, say), the call commuting with every write to the
sampled variable. EC's swap of c <$ dout x past a call that does not write x.
Split a pair assignment (x, y) <- e into x <- e.1; y <- e.2, when the second
component does not read x.
State equations #
What the side conditions of Stmt.CommutesWith reduce to, after the reads through writes are
simplified: equations between states written in different orders. Lens.set_outward X moves
the writes through X outwards past writes through lenses disjoint from it, which normalizes
both sides; resets of other names are passed the same way (resetSetter_set_intoLocal).
A write to a local variable moves out past a reset of other names.
resetSetter_set_intoLocal at the spelling a program's local variables carry.
resetSetter_set_intoLocal with the variable name spelled out (as the inliner writes the
variables it makes up): stated for v, the slot's type would be the projection v.type, which
simp cannot match against a concrete type before v is known.
A write to a global moves out past a reset of locals.
A reset of locals leaves the globals alone.
Two resets of locals commute (both write the initial values; simp orders them).
The lens (into the program state) behind a setter: Lens.toSetter X, or an l-value
liftLens X (X itself for a lens into the program state, X.intoGlobal for a global).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Prove the side condition ty (a state equation, or a getter's independence of a write) by
simp: reads through writes (HoareWp.simpReadsDecls), writes through X? pushed outward,
writes to locals pushed out past resets.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Close g by splitting its ifs (up to fuel deep), each case simplified with its
condition's equation added to thms.
Equations
- One or more equations did not get rendered due to their size.
- GaudisCrypt.StmtSwap.discharge.splitClose g thms procs 0 = pure false
Instances For
A proof of ty by the rule lem: its conclusion unified with ty; its premises about
sub-statements (Stmt.CommutesWith t u) by sub, the others by discharge.
Equations
- One or more equations did not get rendered due to their size.
Instances For
∀ a, (procedureDenotation p a).inFootprint ?R for the callee p: M's footprint for the
procedure of a module M (Module.Proc.inFootprint_fvP), or a hypothesis.
Equations
- One or more equations did not get rendered due to their size.
- GaudisCrypt.StmtSwap.calleeFootprint ty = Lean.throwError (Lean.toMessageData "stmt_swap: not a footprint premise:" ++ Lean.toMessageData (Lean.indentExpr ty))
Instances For
A proof that the call s commutes with the update u of the assignment X <- e to the
global variable X, X outside the callee's footprint (Stmt.CommutesWith.call_assign_global;
the disjointness by fp_disjoint, from the restriction hypotheses in context).
Equations
- One or more equations did not get rendered due to their size.
Instances For
The global variables the getter e reads (closed applications of global_vars), as one
lens: their Lens.pair (the disjointness instances synthesized); none if it reads none.
Equations
- One or more equations did not get rendered due to their size.
Instances For
callGlobal with the frame of what the value reads (Stmt.CommutesWith.call_assign_global_frame:
K the globals e reads, outside the callee's footprint).
Equations
- One or more equations did not get rendered due to their size.
Instances For
A global variable that the getter e reads, if any: the first closed application of a
global_var or module_var (a constant with its key theorem x.key_eq), or of a clone's alias
of one (Handle.FRO.m, kept as written), outermost first (the variable applied to all its
arguments).
Equations
- One or more equations did not get rendered due to their size.
Instances For
A proof that the statement s commutes with the update u (Stmt.CommutesWith s u), built
from its syntax; X? is the lens u writes through, if any, and e? the value written, if u
is an assignment's update; d? the deterministic statement whose update u is (detOf?), taken
apart where a call meets it (callDet).
A proof that the call s commutes with the update of the deterministic statement d, by
d's parts: an assignment as commutesOf does, a sequence by Stmt.CommutesWith.comp, a
conditional by Stmt.CommutesWith.call_ite (its condition kept by the call: the globals it reads
outside the callee's footprint) or call_ite_local, skip by id_update.
The sampling x <$ e in s, if s is one.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A proof of (a; b).Equiv (b; a): one of them deterministic and the other commuting with its
update, or one of them a sampling from a constant distribution and the other commuting with
writes to its variable.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Swap the statements at positions p and p + 1 (from 0) of the right-nested sequence s;
the result and the proof of s.Equiv result.
Move the statement at position p by k positions (later for k > 0), one swap at a time.
Equations
- One or more equations did not get rendered due to their size.
Instances For
stmt_swap n k: move the statement at position n (from 1, EC's) by k positions — later for
k > 0, earlier for -k (EC's swap). Each step exchanges two neighbours, one deterministic
and the other commuting with its update, or one a sampling from a constant distribution and the
other commuting with the writes to its variable; the commutation is shown by simp. Any
registered judgment; on a relational one, stmt_swap{i} n k on side i. Positions are EC's
(the resets inlining leaves are not counted, and move with the statement after them).
Equations
- One or more equations did not get rendered due to their size.
Instances For
stmt_swap_raw n k: stmt_swap n k in our positions (from 1, every statement counted,
the resets inlining leaves included). For the older ports, written against our positions; EC-form
proofs use stmt_swap n k (EC's positions).
Equations
- One or more equations did not get rendered due to their size.
Instances For
stmt_swap [a..b] k (EC's swap [a..b] k): move the block of statements a to b by k
positions, in EC's positions (from 1, the resets inlining leaves not counted: each moves with
the statement after it), one of our statements at a time (moveStmt).
Equations
- One or more equations did not get rendered due to their size.
Instances For
EC's statements of side side? as groups of ours (ecGroups).
Equations
- One or more equations did not get rendered due to their size.
Instances For
EC's swap [a..b] k on side side?: see stmt_swap [a..b] k.
Equations
- One or more equations did not get rendered due to their size.
Instances For
EC's swap k (no position): the first statement k positions later, or with -k the
last one k positions earlier (EC's statements: the resets inlining leaves move with the
statement after them). On a relational judgment both sides, or with stmt_swap{i} k side i.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Split the pair assignment at position p into one assignment per component, recursively
for nested pairs (Stmt.split_pair).
(x, y) <- e ↦ x <- e.1; y <- e.2, the second split further if it is a pair.
stmt_split_pair n: split the tuple assignment at position n (from 0) into one assignment
per component (Stmt.split_pair; each component must not read the ones before it). Any
registered judgment; stmt_split_pair{i} n on side i of a relational one.
Equations
- One or more equations did not get rendered due to their size.