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§
- Correction
Result - Result of a successful BCH decode + correct attempt.
- Decoded
String - Result of a successful mk1 string decode at the BCH layer.
Enums§
- BchCode
- Which BCH code variant a string uses.
- Case
Status - 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.