BchChienSearch constructor

BchChienSearch(
  1. Logic locator,
  2. Logic locatorDegree, {
  3. required BchCode code,
  4. required int correctionCapacity,
  5. String name = 'bch_chien_search',
})

Creates a fixed-capacity Chien search.

Implementation

BchChienSearch(
  Logic locator,
  Logic locatorDegree, {
  required BchCode code,
  required int correctionCapacity,
  super.name = 'bch_chien_search',
}) : super(
        definitionName: 'BchChienSearch_N${code.length}_D$correctionCapacity',
      ) {
  if (correctionCapacity < 1) {
    throw ArgumentError.value(correctionCapacity, 'correctionCapacity');
  }
  final coefficientWidth = code.field.degree;
  final expectedLocatorWidth = coefficientWidth * (correctionCapacity + 1);
  if (locator.width != expectedLocatorWidth) {
    throw PortWidthMismatchException(locator, expectedLocatorWidth);
  }
  if (locatorDegree.width < (correctionCapacity + 1).bitLength) {
    throw PortWidthMismatchException(
      locatorDegree,
      (correctionCapacity + 1).bitLength,
    );
  }

  final localLocator =
      addInput('locator', locator, width: expectedLocatorWidth);
  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 * coefficientWidth + coefficientWidth - 1,
        correctionCapacity * coefficientWidth,
      ),
      field: code.field,
    );
    for (var coefficient = correctionCapacity - 1;
        coefficient >= 0;
        coefficient--) {
      final coefficientValue = GfLogic.of(
        localLocator.slice(
          coefficient * coefficientWidth + coefficientWidth - 1,
          coefficient * coefficientWidth,
        ),
        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);
}