correct modular inverse

This commit is contained in:
jude 2023-03-04 10:50:49 +00:00
parent ed171bf77f
commit ab629a78b4
3 changed files with 13 additions and 9 deletions

View File

@ -21,10 +21,14 @@ export function mod_inv(a, n) {
while (new_r !== 0n) { while (new_r !== 0n) {
let quotient = r / new_r; let quotient = r / new_r;
let t_temp = t;
t = new_t; t = new_t;
new_t = t - quotient * new_t; new_t = t_temp - quotient * new_t;
let r_temp = r;
r = new_r; r = new_r;
new_r = r - quotient * new_r; new_r = r_temp - quotient * new_r;
} }
if (t < 0) { if (t < 0) {

View File

@ -6,10 +6,10 @@ let p, q, pubKey, privKey;
class PubKey { class PubKey {
constructor(p, q) { constructor(p, q) {
this.n = p * q; this.n = p * q;
this.e = 65537; this.e = 65537n;
} }
decrypt(m) { encrypt(m) {
return mod_exp(m, this.e, this.n); return mod_exp(m, this.e, this.n);
} }
} }
@ -17,10 +17,10 @@ class PubKey {
class PrivKey { class PrivKey {
constructor(p, q) { constructor(p, q) {
this.n = 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); return mod_exp(c, this.d, this.n);
} }
} }

View File

@ -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 { Random } from "./random.js";
import { Barrier } from "./barrier.js"; import { Barrier } from "./barrier.js";
import { Packet } from "./packet.js"; import { Packet } from "./packet.js";
@ -10,8 +10,8 @@ export const game = new Game();
export let socket; export let socket;
let random; let random;
let barrier; let barrier;
const paillier = generate_keypair(); window.paillier = generate_keypair();
const rsa = generate_rsa_keypair(); window.rsa = generate_rsa_keypair();
// Not totally reliable but better than nothing. // Not totally reliable but better than nothing.
window.addEventListener("beforeunload", () => { window.addEventListener("beforeunload", () => {