ReedSolomonBerlekampMassey constructor
- Logic syndromes, {
- required BinaryExtensionField field,
- required int correctionCapacity,
- String name = 'reed_solomon_berlekamp_massey',
Creates a bounded finite-field Berlekamp--Massey locator.
Implementation
ReedSolomonBerlekampMassey(
Logic syndromes, {
required BinaryExtensionField field,
required int correctionCapacity,
super.name = 'reed_solomon_berlekamp_massey',
}) : super(
definitionName: 'ReedSolomonBerlekampMassey_D${field.degree}_T'
'$correctionCapacity',
) {
if (correctionCapacity < 1) {
throw ArgumentError.value(correctionCapacity, 'correctionCapacity');
}
if (syndromes.width % field.degree != 0) {
throw PortWidthMismatchException(
syndromes,
syndromes.width,
);
}
final syndromeCount = syndromes.width ~/ field.degree;
if (syndromeCount < correctionCapacity * 2) {
throw ArgumentError.value(
syndromeCount,
'syndromes',
'At least twice the correction capacity is required.',
);
}
final localSyndromes =
addInput('syndromes', syndromes, width: syndromes.width);
GfLogic syndrome(int index) => GfLogic.of(
localSyndromes.slice(
(index + 1) * field.degree - 1,
index * field.degree,
),
field: field,
);
if (correctionCapacity == 2 && syndromeCount == 4) {
final s1 = syndrome(0);
final s2 = syndrome(1);
final s3 = syndrome(2);
final s4 = syndrome(3);
final denominator = s1 * s3 + s2 * s2;
final inverse = GfInverse.typed(denominator);
final sigmaOne = (s1 * s4 + s2 * s3) * inverse.gfResult;
final sigmaTwo = (s2 * s4 + s3 * s3) * inverse.gfResult;
final nonzero = denominator.neq(GfLogic.constant(field.zero));
final packed = Const(1, width: field.degree * 3) |
(sigmaOne.zeroExtend(field.degree * 3) << field.degree) |
(sigmaTwo.zeroExtend(field.degree * 3) << (2 * field.degree));
addOutput('locator', width: field.degree * 3) <=
mux(nonzero, packed, Const(0, width: field.degree * 3));
addOutput('locatorDegree', width: 3) <=
mux(
nonzero,
Logic.from(Const(2, width: 3)),
Logic.from(Const(0, width: 3)),
);
addOutput('valid') <= nonzero;
return;
}
var coefficients = <GfLogic>[
GfLogic.constant(field.one),
for (var index = 0; index < correctionCapacity; index++)
GfLogic.constant(field.zero),
];
var previous = List<GfLogic>.from(coefficients);
// The shift counter spans the entire supplied syndrome sequence.
final stateWidth = (syndromeCount + 1).bitLength;
Logic degree = Const(0, width: stateWidth);
Logic shift = Const(1, width: stateWidth);
var scale = GfLogic.constant(field.one);
for (var index = 0; index < syndromeCount; index++) {
var discrepancy = syndrome(index);
for (var coefficient = 1;
coefficient <= correctionCapacity && coefficient <= index;
coefficient++) {
final term = coefficients[coefficient] * syndrome(index - coefficient);
discrepancy += GfLogic.of(
mux(
degree.gte(Const(coefficient, width: degree.width)),
term,
GfLogic.constant(field.zero),
),
field: field,
);
}
final inverse = GfInverse.typed(scale);
final factor = discrepancy * inverse.gfResult;
final nonzero = discrepancy.neq(GfLogic.constant(field.zero));
final update =
nonzero & (degree + degree).lte(Const(index, width: degree.width));
final nextCoefficients = <GfLogic>[];
for (var coefficient = 0;
coefficient <= correctionCapacity;
coefficient++) {
Logic shifted = GfLogic.constant(field.zero);
for (var amount = 1; amount <= syndromeCount + 1; amount++) {
final source = coefficient >= amount
? previous[coefficient - amount]
: GfLogic.constant(field.zero);
shifted = GfLogic.of(
mux(
shift.eq(Const(amount, width: shift.width)),
source,
shifted,
),
field: field,
);
}
final updated = coefficients[coefficient] +
GfLogic.of(
factor * GfLogic.of(shifted, field: field),
field: field,
);
nextCoefficients.add(
GfLogic.of(
mux(nonzero, updated, coefficients[coefficient]),
field: field,
),
);
}
final nextDegree = mux(
update,
Const(index + 1, width: degree.width) - degree,
degree,
);
final nextShift = mux(
nonzero,
mux(
update,
Const(1, width: shift.width),
shift + Const(1, width: shift.width),
),
shift + Const(1, width: shift.width),
);
final nextPrevious = [
for (var coefficient = 0;
coefficient <= correctionCapacity;
coefficient++)
GfLogic.of(
mux(update, coefficients[coefficient], previous[coefficient]),
field: field,
),
];
coefficients = nextCoefficients;
previous = nextPrevious;
degree = nextDegree;
shift = nextShift;
scale = GfLogic.of(
mux(update, discrepancy, scale),
field: field,
);
}
Logic packed = Const(0, width: field.degree * (correctionCapacity + 1));
for (var coefficient = 0;
coefficient <= correctionCapacity;
coefficient++) {
packed = packed |
coefficients[coefficient].zeroExtend(packed.width) <<
(coefficient * field.degree);
}
addOutput('locator', width: packed.width) <= packed;
addOutput('locatorDegree', width: degree.width) <= degree;
addOutput('valid') <=
degree.lte(Const(correctionCapacity, width: degree.width));
}