From 9e184e8aa637744883deb404e899c0c5a2d343f0 Mon Sep 17 00:00:00 2001 From: jude Date: Mon, 27 Feb 2023 20:08:50 +0000 Subject: [PATCH] Replace FME with addition chaining from Bruce Schneier --- static/js/random_primes.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/static/js/random_primes.js b/static/js/random_primes.js index 7abf1bc..0e8a2ff 100644 --- a/static/js/random_primes.js +++ b/static/js/random_primes.js @@ -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; - b >>= 1n; - - if (leastSignificantBit === 1n) { - result = result * x; - result = result % n; + if (b % 2n === 1n) { + res = (res * a) % n; } - x = x * x; - x = x % n; + b >>= 1n; + a = (a * a) % n; } - return result; -}; + + return res; +} const SMALL_PRIMES = [ 2n,