modPow method

BinaryPolynomial modPow(
  1. BigInt exponent,
  2. BinaryPolynomial modulus
)

Raises this polynomial to exponent modulo modulus.

Implementation

BinaryPolynomial modPow(BigInt exponent, BinaryPolynomial modulus) {
  if (exponent.isNegative) {
    throw RohdHclException(
        'Binary polynomial exponents must be non-negative.');
  }
  if (modulus.isZero) {
    throw RohdHclException('Binary polynomial modulus must be non-zero.');
  }

  var result = one % modulus;
  var factor = this % modulus;
  var remainingExponent = exponent;
  while (remainingExponent != BigInt.zero) {
    if (remainingExponent.isOdd) {
      result = (result * factor) % modulus;
    }
    remainingExponent >>= 1;
    if (remainingExponent != BigInt.zero) {
      factor = (factor * factor) % modulus;
    }
  }
  return result;
}