ReedSolomonChienSearch constructor

ReedSolomonChienSearch(
  1. Logic locator,
  2. Logic locatorDegree, {
  3. required ReedSolomonCode code,
  4. required int correctionCapacity,
  5. String name = 'reed_solomon_chien_search',
})

Creates a fixed-capacity Reed--Solomon Chien search.

Implementation

ReedSolomonChienSearch(
  Logic locator,
  Logic locatorDegree, {
  required ReedSolomonCode code,
  required int correctionCapacity,
  super.name = 'reed_solomon_chien_search',
}) : super(
        definitionName:
            'ReedSolomonChienSearch_N${code.length}_T$correctionCapacity',
      ) {
  if (correctionCapacity < 1) {
    throw ArgumentError.value(correctionCapacity, 'correctionCapacity');
  }
  final width = code.field.degree;
  final locatorWidth = width * (correctionCapacity + 1);
  if (locator.width != locatorWidth) {
    throw PortWidthMismatchException(locator, locatorWidth);
  }
  if (locatorDegree.width < (correctionCapacity + 1).bitLength) {
    throw PortWidthMismatchException(
      locatorDegree,
      (correctionCapacity + 1).bitLength,
    );
  }
  final localLocator = addInput('locator', locator, width: locatorWidth);
  final localDegree = addInput(
    'locatorDegree',
    locatorDegree,
    width: locatorDegree.width,
  );
  Logic mask = Const(0, width: code.length);
  Logic count = Const(0, width: locatorDegree.width);

  for (var position = 0; position < code.length; position++) {
    final inverseRoot = GfLogic.constant(
      code.field.x.pow(
        BigInt.from((code.fullLength - position) % code.fullLength),
      ),
    );
    var value = GfLogic.of(
      localLocator.slice(
        correctionCapacity * width + width - 1,
        correctionCapacity * width,
      ),
      field: code.field,
    );
    for (var coefficient = correctionCapacity - 1;
        coefficient >= 0;
        coefficient--) {
      final coefficientValue = GfLogic.of(
        localLocator.slice(
          coefficient * width + width - 1,
          coefficient * width,
        ),
        field: code.field,
      );
      value = value * inverseRoot + coefficientValue;
    }
    final isRoot = value.eq(GfLogic.constant(code.field.zero));
    mask = mask |
        mux(
          isRoot,
          Logic.from(Const(BigInt.one << position, width: code.length)),
          Logic.from(Const(0, width: code.length)),
        );
    count = mux(
      isRoot,
      count + Const(1, width: count.width),
      count,
    );
  }

  addOutput('errorMask', width: code.length) <= mask;
  addOutput('errorCount', width: count.width) <= count;
  final rootsMatch = count.eq(localDegree);
  addOutput('valid') <= rootsMatch & localDegree.neq(0);
  addOutput('uncorrectable') <= ~rootsMatch | localDegree.eq(0);
}