Byte codecs from canonical naturals #
A type with a CanonicalNat structure has a fixed-width byte codec: the little-endian bytes
of toNat, bytesFor bound of them, decoded by ofNat?. This is ByteCodec.ofCanonicalNat,
a definition rather than an instance so that a composite type may choose a different codec
(an extension field concatenates its coefficients) while still having a canonical natural.
The same structure gives the total decoder used for challenges: ofBytesModOrder reads any
number of bytes as an integer and reduces it modulo bound. Reading back an exact-width
encoding this way recovers the element (ofBytesModOrder_toBytes); reading more bytes than
the width is what makes the result close to uniform.
ZMod p receives both: instCanonicalNatZMod from CompPoly.Data.Classes.CanonicalNat and the
derived ByteCodec (ZMod p) here, so every spec prime field in CompPoly.Fields serializes.
The fixed-width byte codec of a type with canonical naturals: bytesFor bound little-endian
bytes of toNat, decoded by ofNat?.
Instances For
Read any byte string as a little-endian integer and reduce it modulo bound. Total, and
close to uniform when the string is longer than bytesFor bound.
Instances For
ofBytesModOrder on a ByteArray of any size.
Instances For
Reading back an exact-width little-endian encoding modulo the order recovers the element.
Reading back the ofCanonicalNat encoding modulo the order recovers the element.
Round trips through the class interfaces #
For a type whose codec is ByteCodec.ofCanonicalNat, the total decoder inverts the serializer
at the codec's width. Together with ByteCodec.deserialize_serialize (the partial decoder) and
the Serialize.IsInjective instances, this is the full round-trip story a protocol relies on.
The total decoder inverts the serializer at the codec's own width, whenever the codec is the one derived from canonical naturals.
ZMod p: the total decoder inverts the serializer.