This commit is contained in:
2023-04-07 18:59:33 +01:00
parent cc2e1a618e
commit 5557a8bff6
4 changed files with 45 additions and 60 deletions

View File

@ -13,16 +13,17 @@ class Cyphertext {
}
// Compute g^m by binomial theorem.
let gm = (1n + key.n * plainText) % key.n ** 2n;
let gm = (1n + key.n * plainText) % key.n2;
// Compute g^m r^n from crt.
this.cyphertext = (gm * mod_exp(r, key.n, key.n ** 2n)) % key.n ** 2n;
this.cyphertext = (gm * mod_exp(r, key.n, key.n2)) % key.n2;
// Force into range.
while (this.cyphertext < 0n) {
this.cyphertext += key.n ** 2n;
this.cyphertext += key.n2;
}
console.log(performance.now());
this.r = r;
this.pubKey = key;
this.plainText = plainText;
@ -130,18 +131,14 @@ class ProofSessionVerifier {
if (gcd(this.a, this.cipherText.pubKey.n) !== 1n) return -3;
// check exp
return mod_exp(
proof,
this.cipherText.pubKey.n,
this.cipherText.pubKey.n ** 2n
) ===
return mod_exp(proof, this.cipherText.pubKey.n, this.cipherText.pubKey.n2) ===
(this.a *
mod_exp(
this.cipherText.cyphertext,
this.challenge,
this.cipherText.pubKey.n ** 2n
this.cipherText.pubKey.n2
)) %
this.cipherText.pubKey.n ** 2n
this.cipherText.pubKey.n2
? 1
: -4;
}
@ -152,6 +149,7 @@ window.ReadOnlyCyphertext = ReadOnlyCyphertext;
export class PaillierPubKey {
constructor(n) {
this.n = n;
this.n2 = this.n ** 2n;
this.g = this.n + 1n;
}
@ -173,14 +171,14 @@ export class PaillierPubKey {
class PaillierPrivKey {
constructor(p, q) {
this.n = p * q;
// precompute square of n
this.n2 = this.n ** 2n;
this.lambda = (p - 1n) * (q - 1n);
this.mu = mod_exp(this.lambda, this.lambda - 1n, this.n);
}
decrypt(c) {
return (
(((mod_exp(c, this.lambda, this.n ** 2n) - 1n) / this.n) * this.mu) % this.n
);
return (((mod_exp(c, this.lambda, this.n2) - 1n) / this.n) * this.mu) % this.n;
}
}