paillier integration sort of

This commit is contained in:
jude
2023-03-17 10:42:11 +00:00
parent a6961e1900
commit 2d72cdd87b
11 changed files with 244 additions and 92 deletions

View File

@ -37,3 +37,5 @@ export function mod_inv(a, n) {
return t;
}
window.mod_exp = mod_exp;

View File

@ -1,25 +1,60 @@
import { random2048, generate_prime } from "./random_primes.js";
import { mod_exp } from "./math.js";
export class PaillierPubKey {
constructor(n) {
this.n = n;
// this.g = this.n + 1n;
}
encrypt(m) {
class Cyphertext {
constructor(key, plainText) {
// Compute g^m r^n mod n^2
let r = random2048();
// Resample to avoid modulo bias.
while (r >= this.n) {
while (r >= key.n) {
r = random2048();
}
// Compute g^m by binomial theorem.
let gm = (1n + this.n * m) % this.n ** 2n;
let gm = (1n + key.n * plainText) % key.n ** 2n;
// Compute g^m r^n from crt
return (gm * mod_exp(r, this.n, this.n ** 2n)) % this.n ** 2n;
this.cyphertext = (gm * mod_exp(r, key.n, key.n ** 2n)) % key.n ** 2n;
this.r = r;
this.key = key;
this.plainText = plainText;
this.readOnly = false;
}
update(c) {
this.cyphertext *= c.cyphertext;
this.r *= c.r;
this.plainText += c.plainText;
}
toString() {
return "0x" + this.cyphertext.toString(16);
}
}
export class ReadOnlyCyphertext {
constructor(key, cyphertext) {
this.cyphertext = cyphertext;
this.key = key;
this.readOnly = true;
}
update(c) {
this.cyphertext *= c.cyphertext;
}
}
export class PaillierPubKey {
constructor(n) {
this.n = n;
this.g = this.n + 1n;
}
encrypt(m) {
return new Cyphertext(this, m);
}
toJSON() {

View File

@ -0,0 +1,28 @@
import { random2048 } from "./random_primes.js";
import { mod_exp } from "./math";
class PlaintextVerifier {
constructor(cyphertext, value, pub_key) {
this.proving =
(cyphertext * mod_exp(pub_key.g, value, pub_key.n ** 2)) % pub_key.n ** 2;
this.challenge = random2048();
}
verify(response) {}
}
class PlaintextProver {
constructor(cyphertext, pub_key, priv_key) {
this.value = priv_key.decrypt(cyphertext.text);
this.mixin = random2048();
this.pubKey = pub_key;
}
handleChallenge(challenge) {
return (
(this.mixin * mod_exp(cyphertext.mixin, challenge, this.pubKey.n)) %
this.pubKey.n
);
}
}