This commit is contained in:
jude
2023-03-13 14:52:14 +00:00
parent 4a55f5c11f
commit a6961e1900
9 changed files with 146 additions and 35 deletions

View File

@ -1,11 +1,9 @@
import { random2048, generate_prime } from "./random_primes.js";
import { mod_exp } from "./math.js";
let p, q, pubKey, privKey;
class PubKey {
constructor(p, q) {
this.n = p * q;
export class PaillierPubKey {
constructor(n) {
this.n = n;
// this.g = this.n + 1n;
}
@ -23,9 +21,19 @@ class PubKey {
// Compute g^m r^n from crt
return (gm * mod_exp(r, this.n, this.n ** 2n)) % this.n ** 2n;
}
toJSON() {
return {
n: "0x" + this.n.toString(16),
};
}
static fromJSON(data) {
return new PaillierPubKey(BigInt(data.n));
}
}
class PrivKey {
class PaillierPrivKey {
constructor(p, q) {
this.n = p * q;
this.lambda = (p - 1n) * (q - 1n);
@ -40,6 +48,8 @@ class PrivKey {
}
export function generate_keypair() {
let p, q, pubKey, privKey;
if (window.sessionStorage.getItem("p") === null) {
p = generate_prime();
window.sessionStorage.setItem("p", p);
@ -54,8 +64,8 @@ export function generate_keypair() {
q = BigInt(window.sessionStorage.getItem("q"));
}
pubKey = new PubKey(p, q);
privKey = new PrivKey(p, q);
pubKey = new PaillierPubKey(p * q);
privKey = new PaillierPrivKey(p, q);
return { pubKey, privKey };
}

View File

@ -1,8 +1,6 @@
import { generate_prime } from "./random_primes.js";
import { mod_exp, mod_inv } from "./math.js";
let p, q, pubKey, privKey;
export class RsaPubKey {
constructor(n, e) {
this.n = n;
@ -37,6 +35,8 @@ class RsaPrivKey {
}
export function generate_rsa_keypair() {
let p, q, pubKey, privKey;
if (window.sessionStorage.getItem("rsa_p") === null) {
p = generate_prime();
window.sessionStorage.setItem("rsa_p", p);