Skip to main content

Module bch

Module bch 

Source
Expand description

BCH primitives for the mk1 string layer: bech32 alphabet conversion and syndrome-based error correction.

Forked from md-codec v0.4.x (crates/md-codec/src/encoding.rs) at the start of the mk1 v0.1 implementation per design/DECISIONS.md D-13. The BCH polynomials and field arithmetic are shared with the sibling md1 format (both reuse BIP 93’s BCH(93,80,8) regular code and BCH(108,93,8) long code); the only mk1-specific knobs are the HRP ("mk") and the NUMS-derived target residues (crate::consts::MK_REGULAR_CONST / crate::consts::MK_LONG_CONST).

Unlike md-codec’s encoding module, this file does not expose a top-level encode_string / decode_string: mk1’s string-layer header lives at the 5-bit symbol layer (per closure Q-5 — 2 symbols for SingleString, 8 symbols for Chunked) rather than the byte-aligned layer md1 uses. The mk1 string_layer/mod.rs builds string-level encode/decode on top of the BCH primitives here.

Structs§

CorrectionResult
Result of a successful BCH decode + correct attempt.
DecodedString
Result of a successful mk1 string decode at the BCH layer.

Enums§

BchCode
Which BCH code variant a string uses.
CaseStatus
Result of a case check.

Constants§

ALPHABET
The bech32 32-character alphabet, in 5-bit-value order.
GEN_LONG
BCH polymod constants for the long checksum (BCH(108,93,8)).
GEN_REGULAR
BCH polymod constants for the regular checksum (BCH(93,80,8)).
LONG_MASK
Mask preserving the low 70 bits of a 75-bit long-code residue.
LONG_SHIFT
Right-shift amount to extract the top 5 bits from a 75-bit long-code residue.
POLYMOD_INIT
Initial residue value for both the regular and long polymod algorithms (BIP 93).
REGULAR_MASK
Mask preserving the low 60 bits of a 65-bit regular-code residue.
REGULAR_SHIFT
Right-shift amount to extract the top 5 bits from a 65-bit regular-code residue.
SEPARATOR
The bech32 separator character between HRP and data-part (BIP 173 §3).

Functions§

bch_code_for_length
Determine the BchCode variant from a total data-part length.
bch_correct_long
Long-code analog of bch_correct_regular.
bch_correct_regular
Attempt to correct a regular-code BCH-checksummed string with up to four substitutions, the full t = 4 capacity of the BCH(93, 80, 8) code.
bch_create_checksum_long
Compute the 15-character BCH checksum for the long code.
bch_create_checksum_regular
Compute the 13-character BCH checksum for the regular code over the HRP-expanded preamble plus the data part.
bch_verify_long
Verify a long-code BCH checksum.
bch_verify_regular
Verify a regular-code BCH checksum.
bytes_to_5bit
Convert a sequence of 8-bit bytes to a sequence of 5-bit values (padded with zero bits at the end if the bit count is not a multiple of 5).
case_check
Check whether a string is all-lowercase, all-uppercase, or mixed.
decode_string
Decode an mk1 string, validating HRP, case, length, and checksum.
encode_5bit_to_string
Encode a 5-bit-symbol data stream as a complete mk1 string.
five_bit_to_bytes
Convert a sequence of 5-bit values back to 8-bit bytes.
hrp_expand
BIP 173-style HRP-expansion: produces the 5-bit-symbol prelude that gets prepended to the data part before running the BCH polymod.