decodeSingleError method

BchDecodeResult decodeSingleError(
  1. BigInt received
)

Decodes zero or one bit error and reports other patterns as uncorrectable.

Implementation

BchDecodeResult decodeSingleError(BigInt received) {
  final receivedSyndromes = syndromes(received);
  if (receivedSyndromes.every((syndrome) => syndrome.isZero)) {
    return BchDecodeResult(
      codeword: received,
      message: received >> parityBits,
      status: const CodecOracleStatus(),
    );
  }

  for (var position = 0; position < length; position++) {
    final candidate =
        field.x.pow(BigInt.from((firstRoot * position) % fullLength));
    if (receivedSyndromes.first != candidate) {
      continue;
    }
    final expected = [
      for (var index = 0; index < receivedSyndromes.length; index++)
        field.x.pow(
          BigInt.from(((firstRoot + index) * position) % fullLength),
        ),
    ];
    var matches = true;
    for (var index = 0; index < expected.length; index++) {
      if (receivedSyndromes[index] != expected[index]) {
        matches = false;
        break;
      }
    }
    if (matches) {
      final corrected = received ^ (BigInt.one << position);
      return BchDecodeResult(
        codeword: corrected,
        message: corrected >> parityBits,
        status: const CodecOracleStatus(
          errorDetected: true,
          corrected: true,
        ),
      );
    }
  }

  return BchDecodeResult(
    codeword: received,
    message: received >> parityBits,
    status: const CodecOracleStatus(
      errorDetected: true,
      uncorrectable: true,
    ),
  );
}