From d397cf352a1f6afadae31bef5a429d9a71384899 Mon Sep 17 00:00:00 2001 From: jude Date: Sun, 12 Feb 2023 16:51:28 +0000 Subject: [PATCH] needs to be taken mod n for some reason. --- static/js/paillier.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/static/js/paillier.js b/static/js/paillier.js index 367c6da..22b24a9 100644 --- a/static/js/paillier.js +++ b/static/js/paillier.js @@ -8,35 +8,39 @@ class PubKey { encrypt(m) { // Compute g^m r^n mod n^2 - let r = this.n; + let r = random2048(); - while (r >= n) { + while (r >= this.n) { r = random2048(); } // Compute g^m by binomial theorem. - let gm = 1n + this.n * m; - // Compute g^m r^n from fact that g^n = 1 - return fastModularExponentiation(gm * r, this.n, this.n ** 2); + let gm = (1n + this.n * m) % this.n ** 2n; + // Compute g^m r^n from crt + return (gm * fastModularExponentiation(r, this.n, this.n ** 2n)) % this.n ** 2n; } } class PrivKey { - constructor(lambda, mu) { - this.lambda = lambda; - this.mu = mu; + constructor(p, q) { + this.n = p * q; + this.lambda = (p - 1n) * (q - 1n); + this.mu = fastModularExponentiation(this.lambda, this.lambda - 1n, this.n); + } + + decrypt(c) { + return ( + (((fastModularExponentiation(c, this.lambda, this.n ** 2n) - 1n) / this.n) * + (this.mu % this.n)) % + this.n + ); } } document.addEventListener("DOMContentLoaded", () => { - return; - p = generate_prime(); q = generate_prime(); - let n = p * q; - let lambda = (p - 1n) * (q - 1n); - pubKey = new PubKey(p, q); - privKey = new PrivKey(lambda, fastModularExponentiation(lambda, lambda - 1n, n)); + privKey = new PrivKey(p, q); });