Documentation

GaudisCrypt.Logic.TacticMisc

Meta helpers for statement rewriting #

Generic pieces of the statement-rewriting tactics: exposing a statement's head (openStmt), re-associating sequences (flattenSeq), and running a statement rewriter on the statement of a Hoare triple (hoareOnStmt). A rewriter returns its result together with a proof of StmtWithHoles.Equiv (Logic/StmtEquiv.lean). Users: Inline.lean and HoareSplit.lean.

Exposing statements #

The statement heads a traversal stops at. call is one: whnf would step through it to the call' it is defined as, which spreads the callee over several arguments and no longer prints as a call. applyLens and weaken are the inliner's (Inline.lean, which imports this file), and so are named unchecked.

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

    Head reduction that keeps lets: beta, iota and projections only.

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

      Expose a statement position: reduce until a statement head (or a let) is visible, without zeta-reducing and without stepping through assign or call.

      The statement s resets locals (reset_vars S, what inlining a call leaves for the callee's locals): not one of EC's statements, which inlines without them.

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

        Whether each statement along the right spine of s is a reset (isReset), through the lets of the locals.

        EC's statements of a spine (spineResets) as groups of ours: each with the resets just before it (the sizes of the groups, in order).

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

          EC's code gap pos in the statement s, as our gap (the number of our statements before it): pos ≥ 0 after EC's first pos statements, -k after the k-th from the end (-1: after the last); EC's statements are ecGroups (the resets inlining leaves belong to the statement after them, so they are not counted).

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

            Is this skip?

            Equations
            Instances For

              Is this _ <- e, an assignment with no effect?

              Equations
              Instances For

                a.Equiv a.

                Equations
                Instances For

                  flattenSeq #

                  Concatenate two already right-nested sequences, dropping skips and _ <- es: returns the result together with a proof of (a.seq b).Equiv result.

                  Re-associate seq to the right and drop skips and _ <- es, returning the result and a proof of ‹input›.Equiv result. Never fails; ifs are not otherwise touched.

                  Rewriting the statement of a triple #

                  theorem GaudisCrypt.TacticMisc.hoare_seq_det {A B : ProgramState → Prop} {s d : Stmt} {upd : ProgramState → ProgramState} (hd : d.Det upd) (h : hoareStmt A s fun (σ : ProgramState) => B (upd σ)) :

                  The wp rule for trailing deterministic code (Stmt.Det), for Hoare triples.

                  theorem GaudisCrypt.TacticMisc.hoare_seq_sample {α : Type} {A B : ProgramState → Prop} {s : Stmt} {x : Setter α ProgramState} {e : Getter (SubProbability α) ProgramState} (h : hoareStmt A s fun (σ : ProgramState) => ∀ a ∈ (e.get σ).support, B (x.set a σ)) :

                  The wp rule for a trailing sampling, for Hoare triples: every value in the support.

                  On a goal about a statement judgment (hoareStmt A s B, or any registered one): rewrite the statement with f, which returns the new statement s' and a proof of s.Equiv s' (as flattenSeq and HoareSplit.splitSeq do), and continue with the judgment about s'. The conditions are untouched — conv for the statement of a judgment (Judgment.onStmt).

                  Equations
                  Instances For

                    Reads of the state as plain values #

                    What the Hoare rules leave is a goal ∀ σ, … x.get σ …: the state is only read, through lenses (local variables, globals, anything else). generalizeReads replaces each read by a universally quantified value, giving ∀ x, … x …; the same read, wherever it occurs, becomes the same value.

                    Only one direction is proved: the new goal implies the old one (a value for every read, not only the values some state has). So it is always sound, but the new goal can be strictly stronger: when σ is still used elsewhere (it then stays, ∀ σ x, …, with the link between x and σ cut), or when two of the lenses overlap (a variable and a pair containing it, say), since their values are then quantified independently. For distinct variables and no other use of σ, nothing is lost.

                    Wrappers a read is named through: the name comes from their last argument (the inner lens).

                    Equations
                    Instances For

                      Generic combinators a read is not named after.

                      Equations
                      Instances For

                        s as an identifier: only letters, digits, _ and ', not starting with a digit, and v if nothing is left (a made-up name like @.x becomes x).

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

                          The parts of a name for the value a read through the getter g yields: the base name and, for a variable of a module, the module. The base name is the variable's name for a local or global variable slot ((varLens ⟨n, …⟩).intoLocal / .intoGlobal, also spelled localVarLens n T); else, through the wrappers of readNameWrappers, the last component of the lens's head constant or the name of the local it is; else v. The modules are the components before the last of the head constant, the innermost first (RO, F, … for F.RO.m).

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

                            s is the state σ, or its globals or locals.

                            Equations
                            Instances For

                              x.get s for x a lens or getter into ProgramState or VariableAssignment (its type X): a lens read through Lens.toGetter, and a read of the globals at s.globals. none for any other type.

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

                                One step of unfoldReads at e, if a rule applies. Every rule is a definitional unfolding, and fires only on a read of the state σ itself:

                                • (Getter.mk f).get σ ↦ f σ, and a let of the CurrentState (as GaudiExpr[ ] binds it) is substituted;
                                • eval x in the current state ⟨σ⟩ ↦ x.get σ (or x.get σ.globals for a global);
                                • (AsGetter.toG x).get σ ↦ x.get σ;
                                • (x.pair y).get σ ↦ (x.get σ, y.get σ);
                                • (x.chain Lens.fst).get σ ↦ (x.get σ).1, and likewise for Lens.snd.

                                eval/CurrentState (Syntax/ExpressionSyntax.lean) are not imported here, so they are matched by name.

                                Is L a renaming of the locals, by its form: Flatten.trafo, Flatten.emb, Flatten.rename ren, ProgramState.mapLocal m, and chains of them? Its reads keep the globals (globals_chain_get, globals_trafo_get, …, by rfl).

                                s with the renamings of the locals it is read through stripped ((L.get s').globals is s'.globals for such an L), if what is left is the state σ.

                                The globals of σ read through renamings of the locals (what an inlined call leaves, ((Flatten.rename r).chain Flatten.trafo).get σ) as σ.globals, by their form. A definitional rewrite, but one isDefEq would take long to confirm: it compares the renamed state with σ first, field by field, and the locals differ (half a minute on SHA-3's Gcol).

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

                                  foldRenameGlobals on the target of g, for the states σs, without a definitional check (see there).

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

                                    Unfold the reads of σ in e that are not reads of a lens or getter as it is, but of an expression built around them (unfoldReadStep), until none is left: what remains are reads x.get σ (or x.get σ.globals) of the lenses and getters themselves. A definitional rewrite.

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

                                      The reads Getter.get g s in e, s the state σ or its globals or locals, in order of appearance, without syntactic duplicates. Only closed ones (a read under a binder that mentions the bound variable cannot be generalized), and only through a getter that does not itself mention σ.

                                      The binder names inside e, so that a new name does not shadow them.

                                      base, or base1, base2, … if taken.

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

                                        Names for the values of reads, given each read's base name, modules (innermost first) and suffix (the side of a relational judgment, or none): the base name with the suffix (m, m₁). Where that name is taken, in used or by another of the reads, a variable of a module is named with the module (RO_m, RO_m₁), and with the modules further out as far as needed to tell it from the context and from the other reads of that name (F_RO_m, F2_RO_m), so that its name does not depend on a number. A name still taken is numbered (freshIn).

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

                                          The Decidable instances under decide p re-synthesized for p as it stands: simplifying the reads in p (simp_reads) leaves the instance about the old p — equal by unfolding, but not at instances transparency, so that rewriting or generalizing a read through the decide fails ("not type-correct"). Each instance is replaced by the synthesized one when the two are equal by definition (a change).

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

                                            normDecide on the goal (a change).

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

                                              The instance i : Decidable c' re-made for c (c' an older spelling of c): the same instance constant with its arguments read off c (instDecidableAnd, Int.decLe, Classical.propDecidable, …), the Decidable arguments re-made the same way; if i is not of that shape, the synthesized instance of c. (The instance's own structure first: a synthesized instance in place of a classical one is not equal to it by definition.)

                                              The Decidable instance of every decide c and if c then … else … re-made for c as it stands (respellInst), replaced when equal by definition. TacticMisc.normDecide does the synthesized case for decide; this also covers if, and instances that are not synthesized (a classical one, a conjunction with a classical part).

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

                                                normInst under the leading states of a goal ∀ σ₁ … σₙ : ProgramState, P (the instances in P mention the states, so they are re-made with the states in the context).

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

                                                    Strict mode (set_option ec.strict true, Logic/ECReport.lean): a fallback is an error, and a goal a rule leaves about the states whose reads do not generalize gets its instances normalized (normInst) and is tried again.

                                                    Equations
                                                    Instances For

                                                      A region of variables, X.footprint = R (X their Lens.pair): equal on it is equal on X (EC's ={glob C} as ={C.c, C.queries}).

                                                      The variables' lens of a region of variables x₁.footprint ⊔ (x₂.footprint ⊔ …): their Lens.pair (EC's glob C of a concrete module, glob_region).

                                                      EC's glob M of a concrete module given by its code: for a region R (FVP.fvP M), a proof of ∀ a b, (R.touched_getter.get a = R.touched_getter.get b) = (X.get a = X.get b), X the pair of the variables M writes (Logic/GlobWrites.lean sets it: the reading needs the module footprint tactics, which come later than this file).

                                                      EC's glob C of a concrete module, a region of variables x₁.footprint ⊔ (x₂.footprint ⊔ …) (glob_region): its equalities R.touched_getter.get a = R.touched_getter.get b as the variables' (={C.c, C.queries}: touched_getter_eq_iff_of_lens with their Lens.pair, the tuple taken apart), before the reads are generalized (a generalized region would be a value unrelated to the variables). Nothing if no such region occurs.

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

                                                        globTuples on the target and on every hypothesis mentioning a region's touched_getter (each reverted, rewritten, and introduced again under its name).

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

                                                          On a goal ∀ σ : ProgramState, P σ: first unfold the reads of σ through expressions, pairs and the like down to reads of lenses and getters (unfoldReads), then generalize every read g.get σ (or g.get σ.globals) to a universally quantified value (named by readValueNames, against the context, the goal's binders and each other). Reads that agree up to reducible unfolding become one value. If σ is then unused it is dropped, giving ∀ m x …, P'; otherwise it stays in front, ∀ σ m x …, P'. With no read left, only the unfolding is done (and σ dropped if unused). Atomic: if it fails, nothing is assigned (callers fall back to the goal as it was, and a half-done assignment would lose the new goal).

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

                                                            generalizeReads, for the tactic tac: if it fails, the goal stays about the states, and tac says so (ECReport.fallback).

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

                                                              TacticMisc.normDecide on the goal and every hypothesis (the instances under decide about the propositions as they stand).

                                                              Equations
                                                              Instances For

                                                                EC's ={glob C} of a concrete module as its variables' equalities (TacticMisc.globTuples), in the goal and the hypotheses.

                                                                Equations
                                                                Instances For