Ensure ciphertexts are cloned to avoid mutating actual object.

This commit is contained in:
2023-04-09 18:36:58 +01:00
parent 5557a8bff6
commit 848c56ff84
6 changed files with 133 additions and 126 deletions

View File

@ -23,7 +23,6 @@ class Cyphertext {
this.cyphertext += key.n2;
}
console.log(performance.now());
this.r = r;
this.pubKey = key;
this.plainText = plainText;
@ -32,16 +31,13 @@ class Cyphertext {
}
update(c) {
this.cyphertext = (this.cyphertext * c.cyphertext) % this.pubKey.n ** 2n;
this.r = (this.r * c.r) % this.pubKey.n ** 2n;
this.cyphertext = (this.cyphertext * c.cyphertext) % this.pubKey.n2;
this.r = (this.r * c.r) % this.pubKey.n2;
this.plainText += c.plainText;
// Force into range
while (this.cyphertext < 0n) {
this.cyphertext += this.pubKey.n ** 2n;
}
while (this.r < 0n) {
this.r += this.pubKey.n ** 2n;
this.cyphertext += this.pubKey.n2;
}
}
@ -69,11 +65,11 @@ class ProofSessionProver {
}
get a() {
return mod_exp(this.rp, this.cipherText.pubKey.n, this.cipherText.pubKey.n ** 2n);
return mod_exp(this.rp, this.cipherText.pubKey.n, this.cipherText.pubKey.n2);
}
noise() {
return mod_exp(this.rp, this.cipherText.pubKey.n, this.cipherText.pubKey.n ** 2n);
return mod_exp(this.rp, this.cipherText.pubKey.n, this.cipherText.pubKey.n2);
}
prove(challenge) {
@ -102,26 +98,33 @@ export class ReadOnlyCyphertext {
}
update(c) {
this.cyphertext = (this.cyphertext * c.cyphertext) % this.pubKey.n ** 2n;
this.cyphertext = (this.cyphertext * c.cyphertext) % this.pubKey.n2;
// Force into range
while (this.cyphertext < 0n) {
this.cyphertext += this.pubKey.n ** 2n;
this.cyphertext += this.pubKey.n2;
}
}
prove(plainText, a) {
return new ProofSessionVerifier(this, plainText, a);
}
clone() {
return new ReadOnlyCyphertext(this.pubKey, this.cyphertext);
}
}
class ProofSessionVerifier {
constructor(cipherText, plainText, a) {
this.cipherText = cipherText;
// Clone, otherwise the update below will mutate the original value
this.cipherText = cipherText.clone();
this.cipherText.update(this.cipherText.pubKey.encrypt(-1n * plainText, 1n));
// Shift the challenge down by 1 to ensure it is smaller than either prime factor.
this.challenge = cryptoRandom(2048) << 1n;
this.a = a;
this.plainText = plainText;
}
verify(proof) {
@ -204,3 +207,5 @@ export function generate_keypair() {
return { pubKey, privKey };
}
// p = a.prove(); v = p.asVerifier(); v.verify(p.prove(v.challenge));