bdk_chain::bitcoin::bech32::primitives::checksum

Trait Checksum

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 Constants§

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.

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.

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

const TARGET_RESIDUE: Self::MidstateRepr

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

Required Associated Types§

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.

Provided Methods§

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

§

impl Checksum for Bech32

§

const CODE_LENGTH: usize = 1_023usize

§

const CHECKSUM_LENGTH: usize = 6usize

§

const GENERATOR_SH: [u32; 5] = GEN

§

const TARGET_RESIDUE: u32 = 1u32

§

type MidstateRepr = u32

§

impl Checksum for Bech32m

§

const CODE_LENGTH: usize = 1_023usize

§

const CHECKSUM_LENGTH: usize = 6usize

§

const GENERATOR_SH: [u32; 5] = GEN

§

const TARGET_RESIDUE: u32 = 734_539_939u32

§

type MidstateRepr = u32

§

impl Checksum for NoChecksum

§

const CODE_LENGTH: usize = 18_446_744_073_709_551_615usize

§

const CHECKSUM_LENGTH: usize = 0usize

§

const GENERATOR_SH: [PackedNull; 5] = _

§

const TARGET_RESIDUE: PackedNull = PackedNull

§

type MidstateRepr = PackedNull