Riskless/static/js/paillier.js

43 lines
885 B
JavaScript
Raw Normal View History

2023-02-08 17:55:45 +00:00
let p, q, pubKey, privKey;
class PubKey {
2023-02-11 14:59:24 +00:00
constructor(p, q) {
this.n = p * q;
this.g = this.n + 1n;
}
encrypt(m) {
// Compute g^m r^n mod n^2
let r = this.n;
while (r >= n) {
r = random2048();
}
// Compute g^m by binomial theorem.
let gm = 1n + this.n * m;
// Compute g^m r^n from fact that g^n = 1
return fastModularExponentiation(gm * r, this.n, this.n ** 2);
2023-02-08 17:55:45 +00:00
}
}
class PrivKey {
constructor(lambda, mu) {
this.lambda = lambda;
this.mu = mu;
2023-02-08 15:52:02 +00:00
}
2023-02-08 17:55:45 +00:00
}
document.addEventListener("DOMContentLoaded", () => {
return;
p = generate_prime();
q = generate_prime();
let n = p * q;
let lambda = (p - 1n) * (q - 1n);
2023-02-11 14:59:24 +00:00
pubKey = new PubKey(p, q);
2023-02-08 17:55:45 +00:00
privKey = new PrivKey(lambda, fastModularExponentiation(lambda, lambda - 1n, n));
2023-02-08 15:52:02 +00:00
});