paillier integration sort of
This commit is contained in:
@ -37,3 +37,5 @@ export function mod_inv(a, n) {
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
window.mod_exp = mod_exp;
|
||||
|
@ -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() {
|
||||
|
28
static/js/modules/crypto/paillier_proof.js
Normal file
28
static/js/modules/crypto/paillier_proof.js
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user