Refactor modPow to BigIntUtils, update to use shared function

This commit is contained in:
Philip Le Riche 2026-02-28 10:52:21 +00:00
parent 21ba3e4b31
commit 9dce060090

View File

@ -6,31 +6,10 @@
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
import { parseBigInt } from "../lib/BigIntUtils.mjs"; import { parseBigInt, modPow } from "../lib/BigIntUtils.mjs";
/* ---------- helper functions ---------- */
/**
* modPow helper operation
*/
function modPow(base, exponent, modulus) {
let result = 1n;
base %= modulus;
while (exponent > 0n) {
if (exponent & 1n) {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exponent >>= 1n;
}
return result;
}
/* ---------- operation class ---------- */ /* ---------- operation class ---------- */
/** /**
* Modular Exponentiation operation * Modular Exponentiation operation
*/ */