Replace FME with addition chaining from Bruce Schneier

This commit is contained in:
jude 2023-02-27 20:08:50 +00:00
parent 62204634e1
commit 9e184e8aa6

View File

@ -86,26 +86,20 @@ function generate_prime() {
}
}
// gist.github.com/krzkaczor/0bdba0ee9555659ae5fe
var fastModularExponentiation = function (a, b, n) {
a = a % n;
var result = 1n;
var x = a;
function fastModularExponentiation(a, b, n) {
let res = 1n;
while (b > 0n) {
var leastSignificantBit = b % 2n;
if (b % 2n === 1n) {
res = (res * a) % n;
}
b >>= 1n;
if (leastSignificantBit === 1n) {
result = result * x;
result = result % n;
a = (a * a) % n;
}
x = x * x;
x = x % n;
return res;
}
return result;
};
const SMALL_PRIMES = [
2n,