diff --git a/static/js/modules/crypto/math.js b/static/js/modules/crypto/math.js index 5a34866..5f33303 100644 --- a/static/js/modules/crypto/math.js +++ b/static/js/modules/crypto/math.js @@ -21,10 +21,14 @@ export function mod_inv(a, n) { while (new_r !== 0n) { let quotient = r / new_r; + + let t_temp = t; t = new_t; - new_t = t - quotient * new_t; + new_t = t_temp - quotient * new_t; + + let r_temp = r; r = new_r; - new_r = r - quotient * new_r; + new_r = r_temp - quotient * new_r; } if (t < 0) { diff --git a/static/js/modules/crypto/rsa.js b/static/js/modules/crypto/rsa.js index 2a6f124..5828d17 100644 --- a/static/js/modules/crypto/rsa.js +++ b/static/js/modules/crypto/rsa.js @@ -6,10 +6,10 @@ let p, q, pubKey, privKey; class PubKey { constructor(p, q) { this.n = p * q; - this.e = 65537; + this.e = 65537n; } - decrypt(m) { + encrypt(m) { return mod_exp(m, this.e, this.n); } } @@ -17,10 +17,10 @@ class PubKey { class PrivKey { constructor(p, q) { this.n = p * q; - this.d = mod_inv(65537, (q - 1) * (p - 1)); + this.d = mod_inv(65537n, (q - 1n) * (p - 1n)); } - encrypt(c) { + decrypt(c) { return mod_exp(c, this.d, this.n); } } diff --git a/static/js/modules/interface/main.js b/static/js/modules/interface/main.js index 96557c0..62d092c 100644 --- a/static/js/modules/interface/main.js +++ b/static/js/modules/interface/main.js @@ -1,4 +1,4 @@ -import { generate_keypair } from "../crypto/main.js"; +import { generate_keypair, generate_rsa_keypair } from "../crypto/main.js"; import { Random } from "./random.js"; import { Barrier } from "./barrier.js"; import { Packet } from "./packet.js"; @@ -10,8 +10,8 @@ export const game = new Game(); export let socket; let random; let barrier; -const paillier = generate_keypair(); -const rsa = generate_rsa_keypair(); +window.paillier = generate_keypair(); +window.rsa = generate_rsa_keypair(); // Not totally reliable but better than nothing. window.addEventListener("beforeunload", () => {