diff --git a/static/js/modules/crypto/paillier.js b/static/js/modules/crypto/paillier.js index 07aa1c8..7da01cb 100644 --- a/static/js/modules/crypto/paillier.js +++ b/static/js/modules/crypto/paillier.js @@ -42,7 +42,7 @@ class Ciphertext { let gm = (1n + key.n * plainText) % key.n2; // 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. while (this.cipherText < 0n) { @@ -50,7 +50,7 @@ class Ciphertext { } this.mode = JURIK; - this.r = mod_exp(key.h, r, key.n); + this.r = key.h_exp(r); } else { // Use the standard form // Compute g^m by binomial theorem. @@ -282,6 +282,44 @@ export class PaillierPubKey { this.n2 = this.n ** 2n; 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) { diff --git a/static/js/modules/crypto/random_primes.js b/static/js/modules/crypto/random_primes.js index 93628b5..5da1b5f 100644 --- a/static/js/modules/crypto/random_primes.js +++ b/static/js/modules/crypto/random_primes.js @@ -19,6 +19,8 @@ export function cryptoRandom(bits) { return intRepr; } +window.cryptoRandom = cryptoRandom; + /** * Generate random integer of length N bits. * diff --git a/whitepaper/Dissertation.bib b/whitepaper/Dissertation.bib index ef9df9a..b0359df 100644 --- a/whitepaper/Dissertation.bib +++ b/whitepaper/Dissertation.bib @@ -256,7 +256,7 @@ doi={10.1109/SP.2014.36}} @misc{msgpack, author = {msgpack}, - title = {MessagePack: Spec}, + title = {{MessagePack}: Spec}, year = {2021}, publisher = {GitHub}, journal = {GitHub repository}, @@ -330,10 +330,10 @@ doi={10.1109/SP.2014.36}} } @misc{ - projectgemini, + projectgemini, title={{Project Gemini}: Speculative specification}, - url={gemini://gemini.circumlunar.space/docs/specification.gmi}, - journal={Project gemini}, + url={gemini://gemini.circumlunar.space/docs/specification.gmi}, + journal={Project gemini}, author={Solderpunk}, year={2022} } diff --git a/whitepaper/Dissertation.pdf b/whitepaper/Dissertation.pdf index f7f8268..7ddbdd4 100644 Binary files a/whitepaper/Dissertation.pdf and b/whitepaper/Dissertation.pdf differ