Skip to content

Instantly share code, notes, and snippets.

@jc-lab
Last active November 26, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jc-lab/eb1f5a31ebd4f84b2a5f00886e82a5c8 to your computer and use it in GitHub Desktop.
Save jc-lab/eb1f5a31ebd4f84b2a5f00886e82a5c8 to your computer and use it in GitHub Desktop.
paillier-js recovery "r"
function fixedMod(a, b) {
let r = a.mod(b);
if(r.isNegative())
r = b.plus(r);
return r;
}
//simplevariant
decrypt(c) {
//let phi = this._p.subtract(1).multiply(this._q.subtract(1));
let P = L(bigInt(c).modPow(this.lambda, this.publicKey._n2), this.publicKey.n).multiply(this.mu).mod(this.publicKey.n);
let cp = fixedMod(c.multiply(bigInt(1).subtract(this._p.multiply(this.publicKey.n))), this.publicKey._n2);
let bn = this.publicKey.n.modInv(this.lambda);
let r = cp.modPow(bn, this.publicKey.n);
console.log("dec r = ", r.toString(16));
return P;
}
//both
decrypt(c) {
let phi = this.mu.modInv(this.publicKey.n);
let P = L(bigInt(c).modPow(this.lambda, this.publicKey._n2), this.publicKey.n).multiply(this.mu).mod(this.publicKey.n);
let cp = fixedMod(c.multiply(bigInt(1).subtract(this._p.multiply(this.publicKey.n))), this.publicKey._n2);
let bn = this.publicKey.n.modInv(phi);
let r = cp.modPow(bn, this.publicKey.n);
console.log("dec r = ", r.toString(16));
return P;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment