Skip to main content

decode_with_correction

Function decode_with_correction 

Source
pub fn decode_with_correction(
    s: &str,
) -> Result<(Tag, Payload, Vec<CorrectionDetail>)>
Expand description

BCH-error-correcting decode for a single ms1 string.

Per plan §1 Q1 lock — full-decode semantics: this is the single entry point that callers needing both “did anything get repaired?” AND “the fully-decoded (Tag, Payload)” should use.

Algorithm:

  1. Parse the input as ms1 (ms1 HRP + codex32 data-part) into a 5-bit symbol vector.
  2. Compute the BCH polymod residue (hrp_expand("ms") || data_with_checksum) XOR’d against crate::bch::MS_REGULAR_CONST.
  3. Residue == 0 ⇒ clean codeword; pass through to the existing decode entry point unchanged.
  4. Residue != 0 ⇒ invoke crate::bch_decode::decode_regular_errors. If None, return Err(Error::TooManyErrors { bound: 8 }) per plan §2.B.4 D29 error-mapping table.
  5. Apply corrections to the symbol vector, re-verify via polymod (a defensive catch for pathological 5+-error patterns that fool BM into returning a degree-≤4 locator with 4 valid roots), and record one CorrectionDetail per repaired character.
  6. Re-encode the corrected symbol vector as an ms1 string and forward it to the existing decode entry point.

Per Q1 lock + D29 error-mapping table, any §4-rule error from the full decode (orphan variants like ThresholdNotZero, ReservedTagNotEmittedInV01, etc.) surfaces directly; toolkit-side repair_via_ms_codec (B.7) absorbs these into RepairError::PostCorrectionDecodeFailed.

Returns (Tag, Payload, Vec<CorrectionDetail>) on success. The correction-detail vector is in ascending position order; an empty vector means the input was already a valid codeword.