Add modPow function to BigIntUtils

This commit is contained in:
Philip Le Riche 2026-03-03 12:23:02 +00:00
parent 3f8f52281e
commit 2682906111

View File

@ -12,6 +12,7 @@ import OperationError from "../errors/OperationError.mjs";
* Currently provides: * Currently provides:
* - parseBigInt * - parseBigInt
* - Extended Euclidean Algorithm * - Extended Euclidean Algorithm
* - Modular Exponentiation
* *
* Additional algorithms may be added as required. * Additional algorithms may be added as required.
*/ */
@ -51,3 +52,22 @@ export function egcd(a, b) {
// oldS and oldT are the Bézout coefficients // oldS and oldT are the Bézout coefficients
return [oldR, oldS, oldT]; return [oldR, oldS, oldT];
} }
/**
* Modular exponentiation
*/
export 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;
}