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.
Instances
Decode a natural as an element, failing when it is not a canonical natural.
Instances For
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.
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
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
ZMod p with the residue in [0, p) as canonical natural.