Documentation

QedShare.Field.Tower

GF(2¹²⁸) as a tower of quadratic extensions over GF(2⁸) #

GF(2¹⁶)  = GF(2⁸)[y₁]  / (y₁² + y₁ + c₁)
GF(2³²)  = GF(2¹⁶)[y₂] / (y₂² + y₂ + c₂)
GF(2⁶⁴)  = GF(2³²)[y₃] / (y₃² + y₃ + c₃)
GF(2¹²⁸) = GF(2⁶⁴)[y₄] / (y₄² + y₄ + c₄)

The AMD tag uses this field to detect forged shares (README: Forged shares). Each step is the same construction, so there is one generic type, Quad K: an element lo + hi·y stored as the pair (lo, hi). An element of GF(2¹²⁸) is 16 bytes, and those bytes are its coordinates over GF(2⁸). That matters for the AMD proof: multiplying every byte by the same a ∈ GF(2⁸) (which is what a forger can do to the rebuilt secret) is multiplication by a in the field.

The constants are c₁ = 0x20 and cₙ = 0x20 · y₁ ⋯ yₙ₋₁: in each case the top coordinate is 0x20 and the rest are zero. Each has trace 1 over GF(2), which is what makes y² + y + c irreducible, so each step is a field. That proof lives in proofs/ (it needs Mathlib's finite field theory); here we define the arithmetic and trace.

Only + and × are needed at runtime, both built from the constant-time GF(2⁸) multiply: no branches, no tables, no inverse.

The constant c of the quadratic step on top of K: the next field is K[y] / (y² + y + c).

  • c : K

    The constant term of the defining polynomial y² + y + c.

Instances

    Fixed-width byte coordinates of a tower field: GF(2⁸) is 1 byte, each step doubles it. scale a multiplies every byte by a ∈ GF(2⁸).

    Instances
      structure QedShare.Quad (K : Type) :

      An element lo + hi·y of the quadratic extension K[y] / (y² + y + c).

      • lo : K

        The constant coordinate.

      • hi : K

        The coordinate of y.

      Instances For
        def QedShare.instDecidableEqQuad.decEq {K✝ : Type} [DecidableEq K✝] (x✝ x✝¹ : Quad K✝) :
        Decidable (x✝ = x✝¹)
        Equations
        Instances For
          @[instance_reducible]
          instance QedShare.instReprQuad {K✝ : Type} [Repr K✝] :
          Repr (Quad K✝)
          Equations
          def QedShare.instReprQuad.repr {K✝ : Type} [Repr K✝] :
          Quad K✝ → Nat → Std.Format
          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            @[instance_reducible]
            instance QedShare.instInhabitedQuad {a✝ : Type} [Inhabited a✝] :
            Equations
            theorem QedShare.Quad.ext {K : Type} {a b : Quad K} (h1 : a.lo = b.lo) (h2 : a.hi = b.hi) :
            a = b

            Two elements are equal when both coordinates are.

            theorem QedShare.Quad.ext_iff {K : Type} {a b : Quad K} :
            a = b ↔ a.lo = b.lo ∧ a.hi = b.hi
            @[instance_reducible]
            instance QedShare.Quad.instZero {K : Type} [Zero K] :
            Equations
            @[instance_reducible]
            instance QedShare.Quad.instOneOfZero {K : Type} [Zero K] [One K] :
            One (Quad K)
            Equations
            def QedShare.Quad.add {K : Type} [Add K] (a b : Quad K) :

            Addition is coordinate-wise (XOR of all bytes).

            Equations
            Instances For
              @[instance_reducible]
              instance QedShare.Quad.instAdd {K : Type} [Add K] :
              Add (Quad K)
              Equations
              def QedShare.Quad.mul {K : Type} [Add K] [Mul K] [TowerStep K] (a b : Quad K) :

              Multiplication in K[y] / (y² + y + c), with three multiplications in K (Karatsuba) plus one by c: (a₀ + a₁y)(b₀ + b₁y) = a₀b₀ + a₁b₁c + (a₀b₁ + a₁b₀ + a₁b₁) y, using y² = y + c and a₀b₁ + a₁b₀ + a₁b₁ = (a₀ + a₁)(b₀ + b₁) + a₀b₀ in characteristic 2.

              Equations
              Instances For
                @[simp]
                theorem QedShare.Quad.add_lo {K : Type} [Add K] (a b : Quad K) :
                (a + b).lo = a.lo + b.lo

                The coordinates of a sum.

                @[simp]
                theorem QedShare.Quad.add_hi {K : Type} [Add K] (a b : Quad K) :
                (a + b).hi = a.hi + b.hi

                The coordinates of a sum.

                theorem QedShare.Quad.mul_lo {K : Type} [Add K] [Mul K] [TowerStep K] (a b : Quad K) :
                (a * b).lo = a.lo * b.lo + a.hi * b.hi * TowerStep.c

                The coordinates of a product.

                theorem QedShare.Quad.mul_hi {K : Type} [Add K] [Mul K] [TowerStep K] (a b : Quad K) :
                (a * b).hi = (a.lo + a.hi) * (b.lo + b.hi) + a.lo * b.lo

                The coordinates of a product.

                @[simp]
                theorem QedShare.Quad.zero_lo {K : Type} [Zero K] :
                lo 0 = 0

                The coordinates of 0.

                @[simp]
                theorem QedShare.Quad.zero_hi {K : Type} [Zero K] :
                hi 0 = 0

                The coordinates of 0.

                @[simp]
                theorem QedShare.Quad.one_lo {K : Type} [Zero K] [One K] :
                lo 1 = 1

                The coordinates of 1.

                @[simp]
                theorem QedShare.Quad.one_hi {K : Type} [Zero K] [One K] :
                hi 1 = 0

                The coordinates of 1.

                @[instance_reducible]

                Bytes of lo, then bytes of hi.

                Equations
                • One or more equations did not get rendered due to their size.
                @[instance_reducible]

                GF(2⁸) is one byte.

                Equations
                • One or more equations did not get rendered due to their size.
                @[reducible, inline]

                GF(2¹⁶) = GF(2⁸)[y₁] / (y₁² + y₁ + c₁).

                Equations
                Instances For
                  @[reducible, inline]

                  GF(2³²) = GF(2¹⁶)[y₂] / (y₂² + y₂ + c₂).

                  Equations
                  Instances For
                    @[reducible, inline]

                    GF(2⁶⁴) = GF(2³²)[y₃] / (y₃² + y₃ + c₃).

                    Equations
                    Instances For
                      @[reducible, inline]

                      GF(2¹²⁸) = GF(2⁶⁴)[y₄] / (y₄² + y₄ + c₄): 16 bytes.

                      Equations
                      Instances For
                        @[instance_reducible]

                        c₁ = 0x20.

                        Equations
                        @[instance_reducible]

                        c₂ = 0x20 · y₁.

                        Equations
                        @[instance_reducible]

                        c₃ = 0x20 · y₁ · y₂.

                        Equations
                        @[instance_reducible]

                        c₄ = 0x20 · y₁ · y₂ · y₃.

                        Equations

                        An element of GF(2¹²⁸) is 16 bytes.

                        a ∈ GF(2⁸) as an element of GF(2¹²⁸): a in the lowest coordinate, zeros elsewhere. Share indices become GF(2¹²⁸) points this way for the share tokens (Encoding/Token.lean).

                        Equations
                        Instances For

                          Trace #

                          trace m x = x + x² + x⁴ + ⋯ + x^(2^(m-1)). For a field with 2^m elements this is the trace over GF(2). y² + y + c has a root in that field only if trace m c = 0 (proved in proofs/), so trace m c = 1 for each step's constant makes each step a field.

                          def QedShare.trace {K : Type} [Zero K] [Add K] [Mul K] (m : Nat) (x : K) :
                          K

                          x + x² + x⁴ + ⋯ (m terms).

                          Equations
                          Instances For
                            def QedShare.trace.go {K : Type} [Zero K] [Add K] [Mul K] :
                            Nat → K → K × K

                            Returns (x + x² + ⋯ + x^(2^(n-1)), x^(2^n)).

                            Equations
                            Instances For