decode method
@param received received codewords @param numECCodewords number of those codewords used for EC @param erasures location of erasures @return number of errors @throws ChecksumException if errors cannot be corrected, maybe because of too many errors
Implementation
int decode(List<int> received, int numECCodewords, List<int>? erasures) {
final poly = ModulusPoly(_field, received);
final S = List.filled(numECCodewords, 0);
bool error = false;
for (int i = numECCodewords; i > 0; i--) {
final eval = poly.evaluateAt(_field.exp(i));
S[numECCodewords - i] = eval;
if (eval != 0) {
error = true;
}
}
if (!error) {
return 0;
}
ModulusPoly knownErrors = _field.one;
if (erasures != null) {
for (int erasure in erasures) {
final b = _field.exp(received.length - 1 - erasure);
// Add (1 - bx) term:
final term = ModulusPoly(_field, [_field.subtract(0, b), 1]);
knownErrors = knownErrors.multiplyPoly(term);
}
}
final syndrome = ModulusPoly(_field, S);
//syndrome = syndrome.multiply(knownErrors);
final sigmaOmega = _runEuclideanAlgorithm(
_field.buildMonomial(numECCodewords, 1),
syndrome,
numECCodewords,
);
final sigma = sigmaOmega[0];
final omega = sigmaOmega[1];
//sigma = sigma.multiply(knownErrors);
final errorLocations = _findErrorLocations(sigma);
final errorMagnitudes = _findErrorMagnitudes(omega, sigma, errorLocations);
for (int i = 0; i < errorLocations.length; i++) {
final position = received.length - 1 - _field.log(errorLocations[i]);
if (position < 0) {
throw ChecksumException.getChecksumInstance();
}
received[position] =
_field.subtract(received[position], errorMagnitudes[i]);
}
return errorLocations.length;
}