This commit is contained in:
jude 2023-04-17 16:32:35 +01:00
parent 659fcc389a
commit c87f88b287
4 changed files with 46 additions and 6 deletions

View File

@ -42,7 +42,7 @@ class Ciphertext {
let gm = (1n + key.n * plainText) % key.n2; let gm = (1n + key.n * plainText) % key.n2;
// Compute g^m h^r. // Compute g^m h^r.
this.cipherText = (gm * mod_exp(key.hn, r, key.n2)) % key.n2; this.cipherText = (gm * key.hn_exp(r)) % key.n2;
// Force into range. // Force into range.
while (this.cipherText < 0n) { while (this.cipherText < 0n) {
@ -50,7 +50,7 @@ class Ciphertext {
} }
this.mode = JURIK; this.mode = JURIK;
this.r = mod_exp(key.h, r, key.n); this.r = key.h_exp(r);
} else { } else {
// Use the standard form // Use the standard form
// Compute g^m by binomial theorem. // Compute g^m by binomial theorem.
@ -282,6 +282,44 @@ export class PaillierPubKey {
this.n2 = this.n ** 2n; this.n2 = this.n ** 2n;
this.hn = mod_exp(this.h, this.n, this.n2); this.hn = mod_exp(this.h, this.n, this.n2);
this._h_cache = [];
this._hn_cache = [];
for (let i = 0n; i < BigInt(KEY_SIZE); i++) {
this._h_cache.push(mod_exp(this.h, 2n ** i, this.n));
this._hn_cache.push(mod_exp(this.h, 2n ** i, this.n2));
}
}
h_exp(b) {
let ctr = 1n;
let i = 0;
while (b !== 0n) {
if (b % 2n === 1n) {
ctr *= this._h_cache[i];
ctr %= this.n;
}
i++;
b >>= 1n;
}
return ctr;
}
hn_exp(b) {
let ctr = 1n;
let i = 0;
while (b !== 0n) {
if (b % 2n === 1n) {
ctr *= this._hn_cache[i];
ctr %= this.n2;
}
i++;
b >>= 1n;
}
return ctr;
} }
encrypt(m, r) { encrypt(m, r) {

View File

@ -19,6 +19,8 @@ export function cryptoRandom(bits) {
return intRepr; return intRepr;
} }
window.cryptoRandom = cryptoRandom;
/** /**
* Generate random integer of length N bits. * Generate random integer of length N bits.
* *

View File

@ -256,7 +256,7 @@ doi={10.1109/SP.2014.36}}
@misc{msgpack, @misc{msgpack,
author = {msgpack}, author = {msgpack},
title = {MessagePack: Spec}, title = {{MessagePack}: Spec},
year = {2021}, year = {2021},
publisher = {GitHub}, publisher = {GitHub},
journal = {GitHub repository}, journal = {GitHub repository},

Binary file not shown.