Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created May 2, 2017 01:39
Show Gist options
  • Save AndyNovo/715b2f71827227b64dd9b9a0d9dc77b4 to your computer and use it in GitHub Desktop.
Save AndyNovo/715b2f71827227b64dd9b9a0d9dc77b4 to your computer and use it in GitHub Desktop.
function expmod( base, exp, mod ){
if (exp === 0) return 1;
if (exp % 2 === 0){
return Math.pow( expmod( base, (exp / 2), mod), 2) % mod;
}
else {
return (base * expmod( base, (exp - 1), mod)) % mod;
}
}
var p = 21755381;
var g = 2;
var a = Math.floor((p-2)*Math.random());
var b = Math.floor((p-2)*Math.random());
//var a = 20518605; // value I stored from last time
var A = expmod(g, a, p);
var B = expmod(g, b, p);
console.log(a, " Alice private key");
console.log(A, " Alice public key");
console.log(b, " Bob private key");
console.log(B, " Bob public key");
var alice_calculates = expmod(B, a, p);
var bob_calculates = expmod(A, b, p);
console.log(alice_calculates, bob_calculates);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment