From 2682906111718d8fe2dc9fa7f3979dadcb690505 Mon Sep 17 00:00:00 2001 From: Philip Le Riche <7701190+p-leriche@users.noreply.github.com> Date: Tue, 3 Mar 2026 12:23:02 +0000 Subject: [PATCH] Add modPow function to BigIntUtils --- src/core/lib/BigIntUtils.mjs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/core/lib/BigIntUtils.mjs b/src/core/lib/BigIntUtils.mjs index dce23f26..5ddd8786 100644 --- a/src/core/lib/BigIntUtils.mjs +++ b/src/core/lib/BigIntUtils.mjs @@ -12,6 +12,7 @@ import OperationError from "../errors/OperationError.mjs"; * Currently provides: * - parseBigInt * - Extended Euclidean Algorithm + * - Modular Exponentiation * * Additional algorithms may be added as required. */ @@ -51,3 +52,22 @@ export function egcd(a, b) { // oldS and oldT are the Bézout coefficients 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; +} +