pub trait Checksum {
type MidstateRepr: PackedFe32;
const CODE_LENGTH: usize;
const CHECKSUM_LENGTH: usize;
const GENERATOR_SH: [Self::MidstateRepr; 5];
const TARGET_RESIDUE: Self::MidstateRepr;
// Provided method
fn sanity_check() { ... }
}
Expand description
Trait defining a particular checksum.
For users, this can be treated as a marker trait; none of the associated data are end-user relevant.
Required Associated Types§
sourcetype MidstateRepr: PackedFe32
type MidstateRepr: PackedFe32
An unsigned integer type capable of holding a packed version of the generator polynomial (without its leading 1) and target residue (which will have the same width).
Generally, this is the number of characters in the checksum times 5. So e.g. for bech32, which has a 6-character checksum, we need 30 bits, so we can use u32 here.
The smallest type possible should be used, for efficiency reasons, but the only operations we do on these types are bitwise xor and shifts, so it should be pretty efficient no matter what.
Required Associated Constants§
sourceconst CODE_LENGTH: usize
const CODE_LENGTH: usize
The length of the code.
The length of the code is how long a coded message can be (including the checksum!) for the code to retain its error-correcting properties.
sourceconst CHECKSUM_LENGTH: usize
const CHECKSUM_LENGTH: usize
The number of characters in the checksum.
Alternately, the degree of the generator polynomial. This is not the same
as Self::CODE_LENGTH
.
sourceconst GENERATOR_SH: [Self::MidstateRepr; 5]
const GENERATOR_SH: [Self::MidstateRepr; 5]
The coefficients of the generator polynomial, except the leading monic term, in “big-endian” (highest-degree coefficients get leftmost bits) order, along with the 4 shifts of the generator.
The shifts are literally the generator polynomial left-shifted (i.e. multiplied by the appropriate power of 2) in the field. That is, the 5 entries in this array are the generator times { P, Z, Y, G, S } in that order.
These cannot be usefully pre-computed because of Rust’s limited constfn support
as of 1.67, so they must be specified manually for each checksum. To check the
values for consistency, run Self::sanity_check()
.
sourceconst TARGET_RESIDUE: Self::MidstateRepr
const TARGET_RESIDUE: Self::MidstateRepr
The residue, modulo the generator polynomial, that a valid codeword will have.
Provided Methods§
sourcefn sanity_check()
fn sanity_check()
Sanity checks that the various constants of the trait are set in a way that they are consistent with each other.
This function never needs to be called by users, but anyone defining a checksum should add a unit test to their codebase which calls this.