add method

ModulusPoly add(
  1. ModulusPoly other
)

Implementation

ModulusPoly add(ModulusPoly other) {
  if (_field != other._field) {
    throw ArgumentError('ModulusPolys do not have same ModulusGF field');
  }
  if (isZero) {
    return other;
  }
  if (other.isZero) {
    return this;
  }

  List<int> smallerCoefficients = _coefficients;
  List<int> largerCoefficients = other._coefficients;
  if (smallerCoefficients.length > largerCoefficients.length) {
    final temp = smallerCoefficients;
    smallerCoefficients = largerCoefficients;
    largerCoefficients = temp;
  }
  final sumDiff = List.filled(largerCoefficients.length, 0);
  final lengthDiff = largerCoefficients.length - smallerCoefficients.length;
  // Copy high-order terms only found in higher-degree polynomial's coefficients
  List.copyRange(sumDiff, 0, largerCoefficients, 0, lengthDiff);

  for (int i = lengthDiff; i < largerCoefficients.length; i++) {
    sumDiff[i] = _field.add(
      smallerCoefficients[i - lengthDiff],
      largerCoefficients[i],
    );
  }

  return ModulusPoly(_field, sumDiff);
}