Skip to content

Instantly share code, notes, and snippets.

@leoschur
Created February 7, 2021 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoschur/efa31f435eed3b5bfed6f0fd9f08af7d to your computer and use it in GitHub Desktop.
Save leoschur/efa31f435eed3b5bfed6f0fd9f08af7d to your computer and use it in GitHub Desktop.
square and multiply algorithm
function sqm(base, exponent, mod) {
let ze = 1; let a = base;
while (exponent) {
if (exponent & 1) ze = (ze * a) % mod;
a = (a * a) % mod;
exponent = exponent >> 1;
// console.log("Ze: " + ze + " A: " + a);
}
return ze;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment