Documentation

GaudisCrypt.Examples.OTP

One-time pad: perfect secrecy against any adversary #

The adversary picks a message m, sees a ciphertext and guesses a bit. In the real game the ciphertext is m + k for a fresh uniform key k; in the ideal game it is uniform, independent of m. The two games are equivalent for every adversary (real_ideal), so the adversary's guess has the same distribution in both (real_ideal_Pr).

Not an EasyCrypt file; written as one would be:

type msg.                        (* a finite group, written additively *)

module type Adversary = {
  proc choose() : msg
  proc guess(c : msg) : bool
}.

module Real(A : Adversary) = {
  proc main() : bool = {
    var m, k, c, b;
    m <@ A.choose();
    k <$ dmsg;
    c <- m + k;
    b <@ A.guess(c);
    return b;
  }
}.

module Ideal(A : Adversary) = {
  proc main() : bool = {
    var m, c, b;
    m <@ A.choose();
    c <$ dmsg;
    b <@ A.guess(c);
    return b;
  }
}.

section.
declare module A <: Adversary.

equiv real_ideal : Real(A).main ~ Ideal(A).main : ={glob A} ==> ={res, glob A}.
proof.
proc.
call (_ : true); wp.
rnd (fun k => m{1} + k) (fun c => c - m{1}).
call (_ : true).
by auto.
qed.

lemma real_ideal_Pr &m :
  Pr[Real(A).main() @ &m : res] = Pr[Ideal(A).main() @ &m : res].
proof. by byequiv real_ideal. qed.
end section.

No assumption on the adversary: its procedures run on their own locals and globals, and the games' variables that differ (k, c) are local to main.

The theory's type: the messages (and keys and ciphertexts), a finite group.

    Instances
      @[reducible, inline]
      noncomputable abbrev GaudisCrypt.Examples.OTP.dmsg {msg : Type} [Fintype msg] [Inhabited msg] :

      The uniform distribution on messages.

      Equations
      Instances For
        def GaudisCrypt.Examples.OTP.Adversary.typeRep {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

        The ModuleTypeRep underlying Adversary, the module type declared by moduletype Adversary: the right-nested product of its 2 field types, in declaration order.

        Adversary is by definition Module Adversary.typeRep.

        Equations
        Instances For
          noncomputable def GaudisCrypt.Examples.OTP.Adversary.structure {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

          The record of a module's fields: Adversary.structure m is Adversary.Structure.mk applied to Adversary.choose, Adversary.guess of m.

          Inverse of Adversary.mk (Adversary.mk_destruct, Adversary.destruct_mk).

          Equations
          Instances For
            noncomputable def GaudisCrypt.Examples.OTP.Adversary.guess {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

            The field guess of a module of type Adversary: the projection onto component 1 of the right-nested tuple Adversary.typeRep, as a chain of Module.fst'/Module.snd'. Tagged @[module_accessor].

            Adversary.guess.mk_simp reads this field back off a module built by Adversary.mk; Adversary.guess.utilities has the accessor as a module, and the lemmas about it.

            Its parameters are named c, in that order (recorded in @[gaudiProcParamNames], a ProcedureSignature holding only their types).

            Equations
            Instances For
              def GaudisCrypt.Examples.OTP.Adversary {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

              The module type declared by moduletype Adversary, with fields choose, guess: the type Module Adversary.typeRep of modules that are a right-nested tuple of the field types.

              Build one from a record of its fields with Adversary.mk, read a single field off one with the accessors Adversary.choose, Adversary.guess, and get the whole record back with Adversary.structure.

              Equations
              Instances For
                @[reducible]
                noncomputable def GaudisCrypt.Examples.OTP.Adversary.mk {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                Builds a module of type Adversary from the record Adversary.Structure of its fields, as a right-nested Module.pair'. @[reducible].

                Inverse of Adversary.structure (Adversary.mk_destruct, Adversary.destruct_mk); a single field of a module built this way is read back by the @[simp] lemmas Adversary.choose.mk_simp, Adversary.guess.mk_simp.

                Equations
                Instances For
                  @[simp]
                  theorem GaudisCrypt.Examples.OTP.Adversary.mk_destruct {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (theRecord✝ : Structure) :
                  (mk theRecord✝).structure = theRecord✝

                  @[simp]: Adversary.structure (Adversary.mk r) = r — destructing a module built from a record gives that record back.

                  @[implicit_reducible]
                  instance GaudisCrypt.Examples.OTP.instIsModuleAdversary {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                  The IsModule Adversary instance: Adversary is a module type, with representation Adversary.typeRep.

                  It is what lets the name Adversary stand where a module type is expected — in Module.Arr/Module.Prod (→ₘ/×ₘ), say — which the underlying Module Adversary.typeRep would do on its own but Adversary, a plain (non-reducible) def, would not.

                  Equations
                  noncomputable def GaudisCrypt.Examples.OTP.Adversary.choose.utilities {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                  What is derivable about the accessor Adversary.choose, bundled into one ModuleTypeUtilities (one name per field in the namespace, rather than one per fact):

                  • Adversary.choose.utilities.accessorModule : Adversary →ₘ … — the accessor as a module, a projection being a module morphism;
                  • Adversary.choose.utilities.apply_simp — Module.app Adversary.choose.utilities.accessorModule m = Adversary.choose m: applying that module is the accessor;
                  • Adversary.choose.utilities.expression_eq — the same at the level of expressions: (Adversary.choose m).expression = (Adversary.choose.utilities.proj m.expression).reduce.
                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    noncomputable def GaudisCrypt.Examples.OTP.Adversary.choose {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                    The field choose of a module of type Adversary: the projection onto component 0 of the right-nested tuple Adversary.typeRep, as a chain of Module.fst'/Module.snd'. Tagged @[module_accessor].

                    Adversary.choose.mk_simp reads this field back off a module built by Adversary.mk; Adversary.choose.utilities has the accessor as a module, and the lemmas about it.

                    It takes no parameters (recorded in @[gaudiProcParamNames]).

                    Equations
                    Instances For
                      noncomputable def GaudisCrypt.Examples.OTP.Adversary.guess.utilities {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                      What is derivable about the accessor Adversary.guess, bundled into one ModuleTypeUtilities (one name per field in the namespace, rather than one per fact):

                      • Adversary.guess.utilities.accessorModule : Adversary →ₘ … — the accessor as a module, a projection being a module morphism;
                      • Adversary.guess.utilities.apply_simp — Module.app Adversary.guess.utilities.accessorModule m = Adversary.guess m: applying that module is the accessor;
                      • Adversary.guess.utilities.expression_eq — the same at the level of expressions: (Adversary.guess m).expression = (Adversary.guess.utilities.proj m.expression).reduce.
                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        structure GaudisCrypt.Examples.OTP.Adversary.Structure {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                        The record of the fields of a module of type Adversary: the fields choose, guess, one per moduletype field, each of that field's own module type.

                        Adversary.mk turns such a record into a Adversary, Adversary.structure turns a Adversary back into one.

                        Instances For
                          @[simp]
                          theorem GaudisCrypt.Examples.OTP.Adversary.guess.mk_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (theRecord✝ : Structure) :
                          (mk theRecord✝).guess = theRecord✝.guess

                          @[simp]: reading the field guess back off a module built from a record — Adversary.guess (Adversary.mk r) = r.guess.

                          It is what carries the right-hand side of an X.apply_simp (which is an Adversary.mk) on to the field the caller actually asked for, so that simp chains X.apply_simp into X.guess.apply_simp.

                          @[simp]
                          theorem GaudisCrypt.Examples.OTP.Adversary.destruct_mk {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (theModule✝ : Adversary) :
                          mk theModule✝.structure = theModule✝

                          @[simp]: Adversary.mk (Adversary.structure m) = m — rebuilding a module from its own fields gives that module back.

                          @[simp]
                          theorem GaudisCrypt.Examples.OTP.Adversary.choose.mk_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (theRecord✝ : Structure) :
                          (mk theRecord✝).choose = theRecord✝.choose

                          @[simp]: reading the field choose back off a module built from a record — Adversary.choose (Adversary.mk r) = r.choose.

                          It is what carries the right-hand side of an X.apply_simp (which is an Adversary.mk) on to the field the caller actually asked for, so that simp chains X.apply_simp into X.choose.apply_simp.

                          @[simp]
                          theorem GaudisCrypt.Examples.OTP.Real.main.apply_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (A : Adversary) :

                          @[simp]: applying Real.main to the parameters proc main uses (A) is Module.proc (Real.main.procedure.instantiate …) — the procedure with its holes filled by the callees they were made from, each written as it was in the body (with the arguments in place of the module parameters) and turned into a Procedure by Module.Proc.procedure.

                          noncomputable def GaudisCrypt.Examples.OTP.Real.main.procedure {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                          The body of proc main of module Real, as a procedure of its own: the 2 calls it makes to module parameters (A) are its holes, in the order the body makes them.

                          A hole stands for a call whose callee is not known here — a module parameter is supplied only when the module is applied — so the body is a ProcedureWithHoles, to be instantiated (see Real.main.procedure.apply_simp) rather than run. Use Real.main for this procedure as a module.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            noncomputable def GaudisCrypt.Examples.OTP.Real {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                            The module declared by module Real, abstracted over its single parameter A, which it takes as one right-nested tuple: Module.Arr (T₁ ×ₘ (… ×ₘ Tₙ)) M, with M the anonymous product of the procedures' own types, the declaration naming no module type.

                            Applied to such a tuple it is the record of its procedures Real.main, each applied to the parameters that procedure itself uses — which is what the @[simp] lemma Real.apply_simp says.

                            Equations
                            Instances For
                              noncomputable def GaudisCrypt.Examples.OTP.Real.main {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                              proc main of module Real as a module, abstracted over the module parameters it uses (A, in declaration order): Module.Arr … (Module.Proc (procsig …)).

                              Apply it with the @[simp] lemma Real.main.apply_simp, which fills the holes of Real.main.procedure with the callees the body called.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                theorem GaudisCrypt.Examples.OTP.Real.main.procedure.apply_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (holeArgs✝ : (HoleSigs.cons (procsig () → msg) (HoleSigs.cons (procsig (msg) → Bool) HoleSigs.empty)).Instantiation) :
                                procedure.instantiate holeArgs✝ = proc () : Bool { var m : msg, k : msg, c : msg, b : Bool; m <- call (holeArgs✝.lookup HoleIndex.zero) (); k <$ dmsg; c <- §m + §k; b <- call (holeArgs✝.lookup HoleIndex.zero.succ) (§c); return §b }

                                Instantiating the holes of Real.main.procedure with a tuple holeArgs of procedures: the body of proc main as written, with each hole call replaced by the matching holeArgs.lookup (HoleIndex.zero being the first hole the body declared). True by rfl.

                                Deliberately not @[simp], unlike the other apply_simp lemmas: its right-hand side is the whole body written out, so letting it fire on its own would turn any goal mentioning an instantiated procedure into that body. Inlining a procedure is a step to ask for by name (simp [Real.main.procedure.apply_simp]).

                                @[simp]
                                theorem GaudisCrypt.Examples.OTP.Real.apply_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (A : Adversary) :
                                Real(A).main = Module.app main A

                                @[simp]: applying Real to a tuple of its single parameter A — the record of its procedures Real.main, each applied to the parameters it uses.

                                This is the lemma that unfolds a module use: simp rewrites with it, then with the field lemmas of the module type (N.f.mk_simp) to get at the one procedure the goal is about, and then with Real.main.apply_simp to fill that procedure's holes.

                                theorem GaudisCrypt.Examples.OTP.Ideal.main.procedure.apply_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (holeArgs✝ : (HoleSigs.cons (procsig () → msg) (HoleSigs.cons (procsig (msg) → Bool) HoleSigs.empty)).Instantiation) :
                                procedure.instantiate holeArgs✝ = proc () : Bool { var m : msg, c : msg, b : Bool; m <- call (holeArgs✝.lookup HoleIndex.zero) (); c <$ dmsg; b <- call (holeArgs✝.lookup HoleIndex.zero.succ) (§c); return §b }

                                Instantiating the holes of Ideal.main.procedure with a tuple holeArgs of procedures: the body of proc main as written, with each hole call replaced by the matching holeArgs.lookup (HoleIndex.zero being the first hole the body declared). True by rfl.

                                Deliberately not @[simp], unlike the other apply_simp lemmas: its right-hand side is the whole body written out, so letting it fire on its own would turn any goal mentioning an instantiated procedure into that body. Inlining a procedure is a step to ask for by name (simp [Ideal.main.procedure.apply_simp]).

                                noncomputable def GaudisCrypt.Examples.OTP.Ideal {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                                The module declared by module Ideal, abstracted over its single parameter A, which it takes as one right-nested tuple: Module.Arr (T₁ ×ₘ (… ×ₘ Tₙ)) M, with M the anonymous product of the procedures' own types, the declaration naming no module type.

                                Applied to such a tuple it is the record of its procedures Ideal.main, each applied to the parameters that procedure itself uses — which is what the @[simp] lemma Ideal.apply_simp says.

                                Equations
                                Instances For
                                  noncomputable def GaudisCrypt.Examples.OTP.Ideal.main.procedure {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                                  The body of proc main of module Ideal, as a procedure of its own: the 2 calls it makes to module parameters (A) are its holes, in the order the body makes them.

                                  A hole stands for a call whose callee is not known here — a module parameter is supplied only when the module is applied — so the body is a ProcedureWithHoles, to be instantiated (see Ideal.main.procedure.apply_simp) rather than run. Use Ideal.main for this procedure as a module.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    noncomputable def GaudisCrypt.Examples.OTP.Ideal.main {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] :

                                    proc main of module Ideal as a module, abstracted over the module parameters it uses (A, in declaration order): Module.Arr … (Module.Proc (procsig …)).

                                    Apply it with the @[simp] lemma Ideal.main.apply_simp, which fills the holes of Ideal.main.procedure with the callees the body called.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[simp]
                                      theorem GaudisCrypt.Examples.OTP.Ideal.main.apply_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (A : Adversary) :

                                      @[simp]: applying Ideal.main to the parameters proc main uses (A) is Module.proc (Ideal.main.procedure.instantiate …) — the procedure with its holes filled by the callees they were made from, each written as it was in the body (with the arguments in place of the module parameters) and turned into a Procedure by Module.Proc.procedure.

                                      @[simp]
                                      theorem GaudisCrypt.Examples.OTP.Ideal.apply_simp {msg : Type} [_gaudiParam0 : AddCommGroup msg] [_gaudiParam1 : Fintype msg] [_gaudiParam2 : Inhabited msg] [_gaudiParam3 : Params msg] (A : Adversary) :

                                      @[simp]: applying Ideal to a tuple of its single parameter A — the record of its procedures Ideal.main, each applied to the parameters it uses.

                                      This is the lemma that unfolds a module use: simp rewrites with it, then with the field lemmas of the module type (N.f.mk_simp) to get at the one procedure the goal is about, and then with Ideal.main.apply_simp to fill that procedure's holes.

                                      theorem GaudisCrypt.Examples.OTP.real_ideal {msg : Type} [AddCommGroup msg] [Fintype msg] [Inhabited msg] [Params msg] (A : Adversary) [Module.Declared A ⊥] :
                                      equiv[ Real(A).main ~ Ideal(A).main : ={(FVP.fvP A).touched_getter} ==> ={res, (FVP.fvP A).touched_getter} ]

                                      Perfect secrecy: the real and the ideal game are equivalent, for every adversary.

                                      theorem GaudisCrypt.Examples.OTP.real_ideal_Pr {msg : Type} [AddCommGroup msg] [Fintype msg] [Inhabited msg] [Params msg] (A : Adversary) [Module.Declared A ⊥] (m : VariableAssignment) :
                                      Pr[ Real(A).main () @ &m : §res = true ] = Pr[ Ideal(A).main () @ &m : §res = true ]

                                      Perfect secrecy, as probabilities: the guess has the same distribution in the two games.