Documentation

CompPoly.Data.Classes.CanonicalNat

Canonical naturals #

CanonicalNat F says that every element of F has a canonical natural number below a fixed bound, and that every natural reduces to an element by ofNat, with ofNat acting as reduction modulo bound. The laws make toNat a bijection onto Fin bound, so bound is the cardinality of F and ofNat is exactly "the element whose canonical natural is n % bound".

For a prime field the canonical natural is the residue in [0, p); for a binary field it is the bit pattern of the declared basis; for an extension of degree d over a base of size q it is the base-q expansion of the coefficients.

Serialization is derived from this class rather than stated with it. The fixed-width byte codec of a scalar field is the little-endian bytes of toNat (ByteCodec.ofCanonicalNat in CompPoly.Data.Bytes.CanonicalNat), and sampling a field element from a longer byte string is ofNat of the string's integer, which is uniform up to a bound proved in Nat terms. Two carriers of the same field agree on toNat, so they agree on every derived encoding.

F has a canonical natural below bound for every element, and every natural reduces to an element of F modulo bound. The laws make toNat a bijection F ≃ Fin bound.

  • bound :

    The exclusive upper bound on canonical naturals; equal to the cardinality of F.

  • toNat : F

    The canonical natural of an element.

  • ofNat : F

    The element with canonical natural n % bound.

  • toNat_lt (x : F) : toNat x < bound F
  • ofNat_toNat (x : F) : ofNat (toNat x) = x
  • toNat_ofNat_of_lt {n : } (h : n < bound F) : toNat (ofNat n) = n
  • ofNat_mod (n : ) : ofNat (n % bound F) = ofNat n
Instances
    @[simp]
    theorem CompPoly.CanonicalNat.toNat_inj {F : Type u} [CanonicalNat F] {x y : F} :
    toNat x = toNat y x = y
    @[simp]

    Decode a natural as an element, failing when it is not a canonical natural.

    Instances For
      @[simp]

      The canonical natural as an equivalence with Fin (bound F).

      Instances For

        The bound is the cardinality of F.

        Constructors #

        Two ways a concrete type acquires the structure. A fast carrier of a prime field supplies its own toNat together with the conversion from ZMod p; a binary field supplies its bit pattern. Both are @[instance_reducible] definitions so an instance can be CanonicalNat.ofToField or CanonicalNat.ofBitVec directly.

        @[instance_reducible]
        def CompPoly.CanonicalNat.ofToField {F : Type u} {p : } [NeZero p] (toNat : F) (ofField : ZMod pF) (toNat_lt : ∀ (x : F), toNat x < p) (ofField_cast_toNat : ∀ (x : F), ofField (toNat x) = x) (toNat_ofField : ∀ (n : ZMod p), toNat (ofField n) = n.val) :

        The canonical-natural structure of a carrier of ZMod p: the carrier's own toNat, with ofField as the way back. The hypothesis toNat_ofField is the carrier-agreement law, so every encoding derived from this structure agrees with the one on ZMod p.

        Instances For
          @[instance_reducible]
          def CompPoly.CanonicalNat.ofBitVec {F : Type u} {k : } (toBitVec : FBitVec k) (ofBitVec : BitVec kF) (ofBitVec_toBitVec : ∀ (x : F), ofBitVec (toBitVec x) = x) (toBitVec_ofBitVec : ∀ (b : BitVec k), toBitVec (ofBitVec b) = b) :

          The canonical-natural structure of a type presented as k-bit patterns: the bit pattern read as an integer, with bound = 2 ^ k.

          Instances For
            @[instance_reducible]

            ZMod p with the residue in [0, p) as canonical natural.

            @[simp]
            @[simp]
            theorem CompPoly.CanonicalNat.toNat_zmod {p : } [NeZero p] (x : ZMod p) :
            toNat x = x.val
            @[simp]
            theorem CompPoly.CanonicalNat.ofNat_zmod {p : } [NeZero p] (n : ) :
            ofNat n = n