Trait bech32::Checksum

source ·
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§

source

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§

source

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.

source

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.

source

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().

source

const TARGET_RESIDUE: Self::MidstateRepr

The residue, modulo the generator polynomial, that a valid codeword will have.

Provided Methods§

source

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.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl Checksum for Bech32

§

type MidstateRepr = u32

source§

const CODE_LENGTH: usize = 1_023usize

source§

const CHECKSUM_LENGTH: usize = 6usize

source§

const GENERATOR_SH: [u32; 5] = GEN

source§

const TARGET_RESIDUE: u32 = 1u32

source§

impl Checksum for Bech32m

§

type MidstateRepr = u32

source§

const CODE_LENGTH: usize = 1_023usize

source§

const CHECKSUM_LENGTH: usize = 6usize

source§

const GENERATOR_SH: [u32; 5] = GEN

source§

const TARGET_RESIDUE: u32 = 734_539_939u32

source§

impl Checksum for NoChecksum

§

type MidstateRepr = PackedNull

source§

const CODE_LENGTH: usize = 18_446_744_073_709_551_615usize

source§

const CHECKSUM_LENGTH: usize = 0usize

source§

const GENERATOR_SH: [PackedNull; 5] = _

source§

const TARGET_RESIDUE: PackedNull = PackedNull