Documentation

GaudisCrypt.Logic.StmtSwap

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:

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
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).

    theorem GaudisCrypt.Stmt.CommutesWith.seq {u : ProgramState → ProgramState} {s₁ s₂ : Stmt} (h₁ : s₁.CommutesWith u) (h₂ : s₂.CommutesWith u) :
    CommutesWith (GaudiProg[ stmt s₁; stmt s₂; ]) u
    theorem GaudisCrypt.Stmt.CommutesWith.sample {u : ProgramState → ProgramState} {α : Type} {y : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} (he : ∀ (σ : ProgramState), e.get (u σ) = e.get σ) (hy : ∀ (v : α) (σ : ProgramState), y.set v (u σ) = u (y.set v σ)) :
    CommutesWith (GaudiProg[ y <$ §e; ]) u
    theorem GaudisCrypt.Stmt.CommutesWith.assign {u : ProgramState → ProgramState} {α : Type} {y : Setter α ProgramState} {e : Getter α ProgramState} (h : ∀ (σ : ProgramState), y.set (e.get (u σ)) (u σ) = u (y.set (e.get σ) σ)) :
    CommutesWith (GaudiProg[ y <- §e; ]) u
    theorem GaudisCrypt.Stmt.CommutesWith.ite {u : ProgramState → ProgramState} {c : Getter Bool ProgramState} {t e : Stmt} (hc : ∀ (σ : ProgramState), c.get (u σ) = c.get σ) (ht : t.CommutesWith u) (he : e.CommutesWith u) :
    CommutesWith (GaudiProg[ if (§c) { stmt t; } else { stmt e; } ]) u
    theorem GaudisCrypt.Stmt.CommutesWith.while {u : ProgramState → ProgramState} {c : Getter Bool ProgramState} {b : Stmt} (hc : ∀ (σ : ProgramState), c.get (u σ) = c.get σ) (hb : b.CommutesWith u) :
    CommutesWith (GaudiProg[ while (§c) { stmt b; } ]) u

    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).

    theorem GaudisCrypt.Stmt.CommutesWith.call {u : ProgramState → ProgramState} {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} (ha : ∀ (σ : ProgramState), args.get (u σ) = args.get σ) (hg : ∀ (σ : ProgramState), (u σ).globals = σ.globals) (hy : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), y.set v (ProgramState.globalL.set gl (u σ)) = u (y.set v (ProgramState.globalL.set gl σ))) :
    GaudiProg[ y <- call p (§args); ].CommutesWith u
    theorem GaudisCrypt.Stmt.CommutesWith.call_footprint {u : ProgramState → ProgramState} {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {R : Footprint VariableAssignment} (hp : ∀ (a : sig.ParamType), (procedureDenotation p a).inFootprint R) {f : ProgramState → VariableAssignment → VariableAssignment} (hf : ∀ (σ : ProgramState), (fun (g : VariableAssignment) => pure (f σ g)) ∈ Rᶜ.updates) (hu : ∀ (σ : ProgramState), u σ = ProgramState.globalL.set (f σ σ.globals) σ) (ha : ∀ (σ : ProgramState), args.get (u σ) = args.get σ) (hy : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), y.set v (ProgramState.globalL.set (f σ gl) σ) = u (y.set v (ProgramState.globalL.set gl σ))) :
    GaudiProg[ y <- call p (§args); ].CommutesWith u

    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.

    theorem GaudisCrypt.Stmt.CommutesWith.call_assign_global {α : Type} (X : Lens α VariableAssignment) (e : Getter α ProgramState) {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {R : Footprint VariableAssignment} {u : ProgramState → ProgramState} (hp : ∀ (a : sig.ParamType), (procedureDenotation p a).inFootprint R) (hX : X.footprint ≤ Rᶜ) (hu : ∀ (σ : ProgramState), u σ = ProgramState.globalL.set (X.set (e.get σ) σ.globals) σ) (ha : ∀ (σ : ProgramState), args.get (u σ) = args.get σ) (hy : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), y.set v (ProgramState.globalL.set (X.set (e.get σ) gl) σ) = u (y.set v (ProgramState.globalL.set gl σ))) :
    GaudiProg[ y <- call p (§args); ].CommutesWith u

    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.

    theorem GaudisCrypt.Stmt.CommutesWith.call_assign_global_frame {α : Type} (X : Lens α VariableAssignment) (e : Getter α ProgramState) {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {R : Footprint VariableAssignment} {u : ProgramState → ProgramState} (hp : ∀ (a : sig.ParamType), (procedureDenotation p a).inFootprint R) (hX : X.footprint ≤ Rᶜ) {β : Type} (K : Lens β VariableAssignment) (hK : K.footprint ≤ Rᶜ) (hu : ∀ (σ : ProgramState), u σ = ProgramState.globalL.set (X.set (e.get σ) σ.globals) σ) (ha : ∀ (σ : ProgramState), args.get (u σ) = args.get σ) (hy : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), K.get gl = K.get σ.globals → y.set v (ProgramState.globalL.set (X.set (e.get σ) gl) σ) = u (y.set v (ProgramState.globalL.set gl σ))) :
    GaudiProg[ y <- call p (§args); ].CommutesWith u

    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).

    theorem GaudisCrypt.Stmt.CommutesWith.call_frame {u : ProgramState → ProgramState} {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {R : Footprint VariableAssignment} (hp : ∀ (a : sig.ParamType), (procedureDenotation p a).inFootprint R) {β : Type} (K : Lens β VariableAssignment) (hK : K.footprint ≤ Rᶜ) (hg : ∀ (σ : ProgramState), (u σ).globals = σ.globals) (ha : ∀ (σ : ProgramState), args.get (u σ) = args.get σ) (hy : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), K.get gl = K.get σ.globals → y.set v (ProgramState.globalL.set gl (u σ)) = u (y.set v (ProgramState.globalL.set gl σ))) :
    GaudiProg[ y <- call p (§args); ].CommutesWith u

    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).

    theorem GaudisCrypt.Stmt.CommutesWith.comp {s : Stmt} {u₁ u₂ : ProgramState → ProgramState} (h₁ : s.CommutesWith u₁) (h₂ : s.CommutesWith u₂) :
    s.CommutesWith fun (σ : ProgramState) => u₂ (u₁ σ)

    A statement commuting with two updates commutes with the one after the other (the update of a sequence of deterministic statements, Stmt.Det.seq).

    theorem GaudisCrypt.Stmt.CommutesWith.call_ite {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {R : Footprint VariableAssignment} (hp : ∀ (a : sig.ParamType), (procedureDenotation p a).inFootprint R) {β : Type} (K : Lens β VariableAssignment) (hK : K.footprint ≤ Rᶜ) {c : Getter Bool ProgramState} {u₁ u₂ : ProgramState → ProgramState} (h₁ : GaudiProg[ y <- call p (§args); ].CommutesWith u₁) (h₂ : GaudiProg[ y <- call p (§args); ].CommutesWith u₂) (hc : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), K.get gl = K.get σ.globals → c.get (y.set v (ProgramState.globalL.set gl σ)) = c.get σ) :
    GaudiProg[ y <- call p (§args); ].CommutesWith fun (σ : ProgramState) => if c.get σ = true then u₁ σ else u₂ σ

    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).

    theorem GaudisCrypt.Stmt.CommutesWith.call_ite_local {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} {c : Getter Bool ProgramState} {u₁ u₂ : ProgramState → ProgramState} (h₁ : GaudiProg[ y <- call p (§args); ].CommutesWith u₁) (h₂ : GaudiProg[ y <- call p (§args); ].CommutesWith u₂) (hc : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), c.get (y.set v (ProgramState.globalL.set gl σ)) = c.get σ) :
    GaudiProg[ y <- call p (§args); ].CommutesWith fun (σ : ProgramState) => if c.get σ = true then u₁ σ else u₂ σ

    call_ite for a condition that reads no globals (only the caller's locals other than the result).

    Equivalences #

    theorem GaudisCrypt.SubProbability.expected_comm {A : Type u_1} {B : Type u_2} (μ : SubProbability A) (ν : SubProbability B) (G : A → B → ENNReal) :
    (μ.expected fun (a : A) => ν.expected (G a)) = ν.expected fun (b : B) => μ.expected fun (a : A) => G a b

    Fubini for two independent expectations (discrete measures).

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

    Statements with the same expectations are equivalent.

    theorem GaudisCrypt.Stmt.swap_det {d s : Stmt} {u : ProgramState → ProgramState} (hd : d.Det u) (hs : s.CommutesWith u) :
    GaudiProg[ stmt d; stmt s; ].Equiv (GaudiProg[ stmt s; stmt d; ])

    Swap a deterministic statement with one that commutes with its update.

    theorem GaudisCrypt.Stmt.swap_sample {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} {s : Stmt} (he : ∀ (σ τ : ProgramState), e.get σ = e.get τ) (hs : ∀ (a : α), s.CommutesWith (x.set a)) :
    GaudiProg[ x <$ §e; stmt s; ].Equiv (GaudiProg[ stmt s; x <$ §e; ])

    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.

    theorem GaudisCrypt.Stmt.swap_sample_call {α : Type} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} {sig : ProcedureSignature} {y : Setter sig.ret ProgramState} {p : Procedure sig} {args : Getter sig.ParamType ProgramState} (he : ∀ (v : sig.ret) (gl : VariableAssignment) (σ : ProgramState), e.get (y.set v (ProgramState.globalL.set gl σ)) = e.get σ) (hs : ∀ (a : α), GaudiProg[ y <- call p (§args); ].CommutesWith (x.set a)) :
    GaudiProg[ x <$ §e; y <- call p (§args); ].Equiv (GaudiProg[ y <- call p (§args); x <$ §e; ])

    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.

    theorem GaudisCrypt.Stmt.split_pair {α β : Type} {x : Lens α ProgramState} {y : Lens β ProgramState} [hxy : x.Disjoint y] {e : Getter (α × β) ProgramState} (h2 : ∀ (v : α) (σ : ProgramState), (e.get (x.set v σ)).2 = (e.get σ).2) :
    GaudiProg[ x, y <- §e; ].Equiv (GaudiProg[ x <- (e.get CurrentState.state).1; y <- (e.get CurrentState.state).2; ])

    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).

    theorem GaudisCrypt.Lens.set_outward {α : Type u_1} {m : Type u_2} (X : Lens α m) {β : Type u_3} (Y : Lens β m) [hd : Y.Disjoint X] (w : β) (v : α) (s : m) :
    Y.set w (X.set v s) = X.set v (Y.set w s)

    A write through X moves out past a write through a disjoint lens.

    theorem GaudisCrypt.resetSetter_set_intoLocal (S : Set String) (v : VariableName) (hv : v.name ∉ S) (u : Unit) (a : v.type) (σ : ProgramState) :

    A write to a local variable moves out past a reset of other names.

    theorem GaudisCrypt.resetSetter_set_localVarLens (S : Set String) (n : String) (T : Type) [Nonempty T] {key : ℕ} (kc : key = VariableName.encode n) (hn : n ∉ S) (u : Unit) (a : T) (σ : ProgramState) :
    (resetSetter S).set u ((localVarLens n T).set a σ) = (localVarLens n T).set a ((resetSetter S).set u σ)

    resetSetter_set_intoLocal at the spelling a program's local variables carry.

    theorem GaudisCrypt.resetSetter_set_varLens (S : Set String) (n : String) (T : Type) (i : Nonempty T) (key : ℕ) (kc : key = VariableName.encode n) (hn : n ∉ S) (u : Unit) (a : T) (σ : ProgramState) :
    (resetSetter S).set u ((varLens { name := n, type := T, nonempty := i, key := key, keyCorrect := kc }).intoLocal.set a σ) = (varLens { name := n, type := T, nonempty := i, key := key, keyCorrect := kc }).intoLocal.set a ((resetSetter S).set u σ)

    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.

    theorem GaudisCrypt.resetSetter_set_intoGlobal (S : Set String) {a : Type u_1} (X : Lens a VariableAssignment) (u : Unit) (v : a) (σ : ProgramState) :
    (resetSetter S).set u (X.intoGlobal.set v σ) = X.intoGlobal.set v ((resetSetter S).set u σ)

    A write to a global moves out past a reset of locals.

    A reset of locals leaves the globals alone.

    theorem GaudisCrypt.resetSetter_comm (S T : Set String) (u v : Unit) (σ : ProgramState) :
    (resetSetter S).set u ((resetSetter T).set v σ) = (resetSetter T).set v ((resetSetter S).set u σ)

    Two resets of locals commute (both write the initial values; simp orders them).

    instance GaudisCrypt.Lens.Disjoint.pair_left {a : Type u_1} {b : Type u_2} {c : Type u_3} {m : Type u_4} {x : Lens a m} {y : Lens b m} {z : Lens c m} [x.Disjoint y] [hxz : x.Disjoint z] [hyz : y.Disjoint z] :
    (x.pair y).Disjoint z

    A pair of variables is disjoint from what both components are disjoint from.

    instance GaudisCrypt.Lens.Disjoint.pair_right {a : Type u_1} {b : Type u_2} {c : Type u_3} {m : Type u_4} {x : Lens a m} {y : Lens b m} {z : Lens c m} [x.Disjoint y] [hzx : z.Disjoint x] [hzy : z.Disjoint y] :
    z.Disjoint (x.pair y)

    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
        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
            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.
                                        Instances For