Skip to main content

Module bch_decode

Module bch_decode 

Source
Expand description

Syndrome-based BCH decoder for the MS regular code.

Forked from mk-codec v0.3.1 (crates/mk-codec/src/string_layer/bch_decode.rs) at v0.2.0 per plan §1 D22 + §2.B.2. The algorithm is constant-agnostic — the caller XORs the polymod residue against the per-HRP target constant (crate::bch::MS_REGULAR_CONST) before invoking decode_regular_errors. Mirrors md-codec’s bch_decode.rs structure verbatim (only the pub caller-side constant differs). The fork copy is expected to be retired once the mc-codex32 shared-crate extraction lands.

ms1 is single-chunk only per codex32 spec (one BIP-39 entropy → one ms1 string) — no chunk-index parameter is threaded through the decoder API. mk-codec’s long-code path is also dropped: ms1 strings are all regular-form per consts::VALID_STR_LENGTHS.

§Position indexing

The polymod consumes symbols in the order hrp_expand(hrp) || data || checksum. If n is the total number of symbols fed, then symbol i (in feed order) is the coefficient of x^{n-1-i} in the input polynomial. Errors are constrained to the data_with_checksum segment (the HRP prefix is fixed-and-known). For data_with_checksum.len() = L (L ≤ 93 regular), an error at index k of data_with_checksum lies at polynomial degree d = L - 1 - k. The Chien search returns degrees d and we translate to indices via k = (L - 1) - d.

§Local constants (Q3 lock, plan §2.B.2)

POLYMOD_INIT / REGULAR_SHIFT / REGULAR_MASK from bch.rs:39-41 stay bare-private in bch.rs. This module does NOT need to re-declare them — the polymod is run by the caller via crate::bch::polymod_run, which already references the bare-private originals internally. Mirrors md-codec’s B.2 finding (Phase B.0 (f) corrected the original plan assumption): these three values are not referenced by bch_decode itself, so no re-declaration is required.

Functions§

decode_regular_errors
Decode a regular-code BCH error pattern. Inputs: