Slight modification of modPow(). Some non-important fixes
This commit is contained in:
parent
02fbaaac83
commit
9cdf125c6f
14
README.md
14
README.md
|
@ -107,8 +107,8 @@ iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)</p>
|
||||||
<dt><a href="#modInv">modInv(a, n)</a> ⇒ <code>bigint</code></dt>
|
<dt><a href="#modInv">modInv(a, n)</a> ⇒ <code>bigint</code></dt>
|
||||||
<dd><p>Modular inverse.</p>
|
<dd><p>Modular inverse.</p>
|
||||||
</dd>
|
</dd>
|
||||||
<dt><a href="#modPow">modPow(a, b, n)</a> ⇒ <code>bigint</code></dt>
|
<dt><a href="#modPow">modPow(b, e, n)</a> ⇒ <code>bigint</code></dt>
|
||||||
<dd><p>Modular exponentiation a**b mod n</p>
|
<dd><p>Modular exponentiation b**e mod n</p>
|
||||||
</dd>
|
</dd>
|
||||||
<dt><a href="#prime">prime(bitLength, iterations)</a> ⇒ <code>Promise</code></dt>
|
<dt><a href="#prime">prime(bitLength, iterations)</a> ⇒ <code>Promise</code></dt>
|
||||||
<dd><p>A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
|
<dd><p>A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
|
||||||
|
@ -235,16 +235,16 @@ Modular inverse.
|
||||||
|
|
||||||
<a name="modPow"></a>
|
<a name="modPow"></a>
|
||||||
|
|
||||||
## modPow(a, b, n) ⇒ <code>bigint</code>
|
## modPow(b, e, n) ⇒ <code>bigint</code>
|
||||||
Modular exponentiation a**b mod n
|
Modular exponentiation b**e mod n
|
||||||
|
|
||||||
**Kind**: global function
|
**Kind**: global function
|
||||||
**Returns**: <code>bigint</code> - a**b mod n
|
**Returns**: <code>bigint</code> - b**e mod n
|
||||||
|
|
||||||
| Param | Type | Description |
|
| Param | Type | Description |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| a | <code>number</code> \| <code>bigint</code> | base |
|
| b | <code>number</code> \| <code>bigint</code> | base |
|
||||||
| b | <code>number</code> \| <code>bigint</code> | exponent |
|
| e | <code>number</code> \| <code>bigint</code> | exponent |
|
||||||
| n | <code>number</code> \| <code>bigint</code> | modulo |
|
| n | <code>number</code> \| <code>bigint</code> | modulo |
|
||||||
|
|
||||||
<a name="prime"></a>
|
<a name="prime"></a>
|
||||||
|
|
|
@ -131,7 +131,7 @@ var bigintCryptoUtils = (function (exports) {
|
||||||
}
|
}
|
||||||
{ // browser
|
{ // browser
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let worker = new Worker(_isProbablyPrimeWorkerUrl());
|
const worker = new Worker(_isProbablyPrimeWorkerUrl());
|
||||||
|
|
||||||
worker.onmessage = (event) => {
|
worker.onmessage = (event) => {
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
@ -187,35 +187,36 @@ var bigintCryptoUtils = (function (exports) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modular exponentiation a**b mod n
|
* Modular exponentiation b**e mod n
|
||||||
* @param {number|bigint} a base
|
* @param {number|bigint} b base
|
||||||
* @param {number|bigint} b exponent
|
* @param {number|bigint} e exponent
|
||||||
* @param {number|bigint} n modulo
|
* @param {number|bigint} n modulo
|
||||||
*
|
*
|
||||||
* @returns {bigint} a**b mod n
|
* @returns {bigint} b**e mod n
|
||||||
*/
|
*/
|
||||||
function modPow(a, b, n) {
|
function modPow(b, e, n) {
|
||||||
// See Knuth, volume 2, section 4.6.3.
|
|
||||||
n = BigInt(n);
|
n = BigInt(n);
|
||||||
if (n === _ZERO)
|
if (n === _ZERO)
|
||||||
return NaN;
|
return NaN;
|
||||||
|
else if (n === _ONE)
|
||||||
|
return _ZERO;
|
||||||
|
|
||||||
a = toZn(a, n);
|
b = toZn(b, n);
|
||||||
b = BigInt(b);
|
|
||||||
if (b < _ZERO) {
|
e = BigInt(e);
|
||||||
return modInv(modPow(a, abs(b), n), n);
|
if (e < _ZERO) {
|
||||||
|
return modInv(modPow(b, abs(e), n), n);
|
||||||
}
|
}
|
||||||
let result = _ONE;
|
|
||||||
let x = a;
|
let r = _ONE;
|
||||||
while (b > 0) {
|
while (e > 0) {
|
||||||
var leastSignificantBit = b & _ONE;
|
if ((e % _TWO) === _ONE) {
|
||||||
b = b / _TWO;
|
r = (r * b) % n;
|
||||||
if (leastSignificantBit === _ONE) {
|
|
||||||
result = (result * x) % n;
|
|
||||||
}
|
}
|
||||||
x = (x * x) % n;
|
e = e / _TWO;
|
||||||
|
b = b**_TWO % n;
|
||||||
}
|
}
|
||||||
return result;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -419,7 +420,7 @@ var bigintCryptoUtils = (function (exports) {
|
||||||
|
|
||||||
function _workerUrl(workerCode) {
|
function _workerUrl(workerCode) {
|
||||||
workerCode = `(() => {${workerCode}})()`; // encapsulate IIFE
|
workerCode = `(() => {${workerCode}})()`; // encapsulate IIFE
|
||||||
var _blob = new Blob([workerCode], { type: 'text/javascript' });
|
const _blob = new Blob([workerCode], { type: 'text/javascript' });
|
||||||
return window.URL.createObjectURL(_blob);
|
return window.URL.createObjectURL(_blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
var bigintCryptoUtils=function(a){'use strict';function c(b){return b=BigInt(b),b>=r?b:-b}function d(b){if(b=BigInt(b),b===s)return 1;let c=1;do c++;while((b>>=s)>s);return c}function e(c,d){if(c=BigInt(c),d=BigInt(d),c<=r|d<=r)return NaN;let e=r,f=s,g=s,h=r;for(;c!==r;){let a=d/c,b=d%c,i=e-g*a,j=f-h*a;d=c,c=b,e=g,f=h,g=i,h=j}return{b:d,x:e,y:f}}function f(d,e){if(d=c(d),e=c(e),d===r)return e;if(e===r)return d;let f=r;for(;!((d|e)&s);)d>>=s,e>>=s,f++;for(;!(d&s);)d>>=s;do{for(;!(e&s);)e>>=s;if(d>e){let a=d;d=e,e=a}e-=d}while(e);return d<<f}async function g(a,b=16){return"number"==typeof a&&(a=BigInt(a)),new Promise((c,d)=>{let e=new Worker(o());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function h(b,a){if(b==r|a<=r)return NaN;let c=e(m(b,a),a);return c.b===s?m(c.x,a):NaN}function i(d,e,f){if(f=BigInt(f),f===r)return NaN;if(d=m(d,f),e=BigInt(e),e<r)return h(i(d,c(e),f),f);let g=s,j=d;for(;0<e;){var k=e&s;e/=t,k===s&&(g=g*j%f),j=j*j%f}return g}function j(a,b=s){if(a<=b)throw new Error("max must be > min");const c=a-b;let e,f=d(c);do{let a=k(f);e=n(a)}while(e>c);return e+b}function k(a,b=!1){var c=Math.ceil;if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);const d=c(a/8);let e=l(d,!1);if(e[0]&=2**(a%8)-1,b){let b=a%8?2**(a%8-1):128;e[0]|=b}return e}function l(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),c}function m(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function n(a){let b=r;for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function o(){let a=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1);const _TWO = BigInt(2);const eGcd = ${e.toString()};const modInv = ${h.toString()};const modPow = ${i.toString()};const toZn = ${m.toString()};const randBits = ${k.toString()};const randBytesSync = ${l.toString()};const randBetween = ${j.toString()};const isProbablyPrime = ${q.toString()};${d.toString()}${n.toString()}`;return a+=`onmessage = ${async function(a){const b=await g(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd,id:a.data.id})}.toString()};`,p(a)}function p(a){a=`(() => {${a}})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function q(c,b=16){if(c===t)return!0;if((c&s)===r||c===s)return!1;const e=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597];for(let a=0;a<e.length&&BigInt(e[a])<=c;a++){const b=BigInt(e[a]);if(c===b)return!0;if(c%b===r)return!1}let f=r,g=c-s;for(;g%t===r;)g/=t,++f;let h=(c-s)/t**f;loop:do{let a=j(c-s,t),b=i(a,h,c);if(b===s||b===c-s)continue;for(let a=1;a<f;a++){if(b=i(b,t,c),b===c-s)continue loop;if(b===s)break}return!1}while(--b);return!0}const r=BigInt(0),s=BigInt(1),t=BigInt(2);return a.abs=c,a.bitLength=d,a.eGcd=e,a.gcd=f,a.isProbablyPrime=g,a.lcm=function(d,e){return d=BigInt(d),e=BigInt(e),d===r&&e===r?r:c(d*e)/f(d,e)},a.modInv=h,a.modPow=i,a.prime=function(a,b=16){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);return new Promise(c=>{let d=[];const e=(e,f)=>{if(e.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.value)}else{let c=k(a,!0),d=n(c);try{f.postMessage({rnd:d,iterations:b,id:e.id})}catch(a){}}};{let a=o();for(let b,c=0;c<self.navigator.hardwareConcurrency;c++)b=new Worker(a),b.onmessage=a=>e(a.data,b),d.push(b)}for(let e=0;e<d.length;e++){let c=k(a,!0),f=n(c);d[e].postMessage({rnd:f,iterations:b,id:e})}})},a.randBetween=j,a.randBits=k,a.randBytes=function(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return new Promise(function(d){c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),d(c)})},a.randBytesSync=l,a.toZn=m,a}({});
|
var bigintCryptoUtils=function(a){'use strict';function c(b){return b=BigInt(b),b>=s?b:-b}function d(b){if(b=BigInt(b),b===t)return 1;let c=1;do c++;while((b>>=t)>t);return c}function e(c,d){if(c=BigInt(c),d=BigInt(d),c<=s|d<=s)return NaN;let e=s,f=t,g=t,h=s;for(;c!==s;){let a=d/c,b=d%c,i=e-g*a,j=f-h*a;d=c,c=b,e=g,f=h,g=i,h=j}return{b:d,x:e,y:f}}function f(d,e){if(d=c(d),e=c(e),d===s)return e;if(e===s)return d;let f=s;for(;!((d|e)&t);)d>>=t,e>>=t,f++;for(;!(d&t);)d>>=t;do{for(;!(e&t);)e>>=t;if(d>e){let a=d;d=e,e=a}e-=d}while(e);return d<<f}async function g(a,b=16){return"number"==typeof a&&(a=BigInt(a)),new Promise((c,d)=>{const e=new Worker(o());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function h(b,a){if(b==s|a<=s)return NaN;let c=e(m(b,a),a);return c.b===t?m(c.x,a):NaN}function i(a,d,f){if(f=BigInt(f),f===s)return NaN;if(f===t)return s;if(a=m(a,f),d=BigInt(d),d<s)return h(i(a,c(d),f),f);let g=t;for(;0<d;)d%u===t&&(g=g*a%f),d/=u,a=a**u%f;return g}function j(a,b=t){if(a<=b)throw new Error("max must be > min");const c=a-b;let e,f=d(c);do{let a=k(f);e=n(a)}while(e>c);return e+b}function k(a,b=!1){var c=Math.ceil;if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);const d=c(a/8);let e=l(d,!1);if(e[0]&=2**(a%8)-1,b){let b=a%8?2**(a%8-1):128;e[0]|=b}return e}function l(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),c}function m(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function n(a){let b=s;for(let c of a.values()){let a=BigInt(c);b=(b<<BigInt(8))+a}return b}function o(){let a=`'use strict';const _ZERO = BigInt(0);const _ONE = BigInt(1);const _TWO = BigInt(2);const eGcd = ${e.toString()};const modInv = ${h.toString()};const modPow = ${i.toString()};const toZn = ${m.toString()};const randBits = ${k.toString()};const randBytesSync = ${l.toString()};const randBetween = ${j.toString()};const isProbablyPrime = ${q.toString()};${d.toString()}${n.toString()}`;return a+=`onmessage = ${async function(a){const b=await g(a.data.rnd,a.data.iterations);postMessage({isPrime:b,value:a.data.rnd,id:a.data.id})}.toString()};`,p(a)}function p(a){a=`(() => {${a}})()`;const b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function q(c,b=16){if(c===u)return!0;if((c&t)===s||c===t)return!1;const e=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597];for(let a=0;a<e.length&&BigInt(e[a])<=c;a++){const b=BigInt(e[a]);if(c===b)return!0;if(c%b===s)return!1}let f=s,g=c-t;for(;g%u===s;)g/=u,++f;let h=(c-t)/u**f;loop:do{let a=j(c-t,u),b=i(a,h,c);if(b===t||b===c-t)continue;for(let a=1;a<f;a++){if(b=i(b,u,c),b===c-t)continue loop;if(b===t)break}return!1}while(--b);return!0}const s=BigInt(0),t=BigInt(1),u=BigInt(2);return a.abs=c,a.bitLength=d,a.eGcd=e,a.gcd=f,a.isProbablyPrime=g,a.lcm=function(d,e){return d=BigInt(d),e=BigInt(e),d===s&&e===s?s:c(d*e)/f(d,e)},a.modInv=h,a.modPow=i,a.prime=function(a,b=16){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);return new Promise(c=>{let d=[];const e=(e,f)=>{if(e.isPrime){for(let a=0;a<d.length;a++)d[a].terminate();for(;d.length;)d.pop();c(e.value)}else{let c=k(a,!0),d=n(c);try{f.postMessage({rnd:d,iterations:b,id:e.id})}catch(a){}}};{let a=o();for(let b,c=0;c<self.navigator.hardwareConcurrency;c++)b=new Worker(a),b.onmessage=a=>e(a.data,b),d.push(b)}for(let e=0;e<d.length;e++){let c=k(a,!0),f=n(c);d[e].postMessage({rnd:f,iterations:b,id:e})}})},a.randBetween=j,a.randBits=k,a.randBytes=function(a,b=!1){if(1>a)throw new RangeError(`byteLength MUST be > 0 and it is ${a}`);let c;return new Promise(function(d){c=new Uint8Array(a),self.crypto.getRandomValues(c),b&&(c[0]|=128),d(c)})},a.randBytesSync=l,a.toZn=m,a}({});
|
||||||
|
|
|
@ -128,7 +128,7 @@ async function isProbablyPrime(w, iterations = 16) {
|
||||||
}
|
}
|
||||||
{ // browser
|
{ // browser
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let worker = new Worker(_isProbablyPrimeWorkerUrl());
|
const worker = new Worker(_isProbablyPrimeWorkerUrl());
|
||||||
|
|
||||||
worker.onmessage = (event) => {
|
worker.onmessage = (event) => {
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
@ -184,35 +184,36 @@ function modInv(a, n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modular exponentiation a**b mod n
|
* Modular exponentiation b**e mod n
|
||||||
* @param {number|bigint} a base
|
* @param {number|bigint} b base
|
||||||
* @param {number|bigint} b exponent
|
* @param {number|bigint} e exponent
|
||||||
* @param {number|bigint} n modulo
|
* @param {number|bigint} n modulo
|
||||||
*
|
*
|
||||||
* @returns {bigint} a**b mod n
|
* @returns {bigint} b**e mod n
|
||||||
*/
|
*/
|
||||||
function modPow(a, b, n) {
|
function modPow(b, e, n) {
|
||||||
// See Knuth, volume 2, section 4.6.3.
|
|
||||||
n = BigInt(n);
|
n = BigInt(n);
|
||||||
if (n === _ZERO)
|
if (n === _ZERO)
|
||||||
return NaN;
|
return NaN;
|
||||||
|
else if (n === _ONE)
|
||||||
|
return _ZERO;
|
||||||
|
|
||||||
a = toZn(a, n);
|
b = toZn(b, n);
|
||||||
b = BigInt(b);
|
|
||||||
if (b < _ZERO) {
|
e = BigInt(e);
|
||||||
return modInv(modPow(a, abs(b), n), n);
|
if (e < _ZERO) {
|
||||||
|
return modInv(modPow(b, abs(e), n), n);
|
||||||
}
|
}
|
||||||
let result = _ONE;
|
|
||||||
let x = a;
|
let r = _ONE;
|
||||||
while (b > 0) {
|
while (e > 0) {
|
||||||
var leastSignificantBit = b & _ONE;
|
if ((e % _TWO) === _ONE) {
|
||||||
b = b / _TWO;
|
r = (r * b) % n;
|
||||||
if (leastSignificantBit === _ONE) {
|
|
||||||
result = (result * x) % n;
|
|
||||||
}
|
}
|
||||||
x = (x * x) % n;
|
e = e / _TWO;
|
||||||
|
b = b**_TWO % n;
|
||||||
}
|
}
|
||||||
return result;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -416,7 +417,7 @@ function _isProbablyPrimeWorkerUrl() {
|
||||||
|
|
||||||
function _workerUrl(workerCode) {
|
function _workerUrl(workerCode) {
|
||||||
workerCode = `(() => {${workerCode}})()`; // encapsulate IIFE
|
workerCode = `(() => {${workerCode}})()`; // encapsulate IIFE
|
||||||
var _blob = new Blob([workerCode], { type: 'text/javascript' });
|
const _blob = new Blob([workerCode], { type: 'text/javascript' });
|
||||||
return window.URL.createObjectURL(_blob);
|
return window.URL.createObjectURL(_blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -134,7 +134,7 @@ async function isProbablyPrime(w, iterations = 16) {
|
||||||
if (_useWorkers) {
|
if (_useWorkers) {
|
||||||
const { Worker } = require('worker_threads');
|
const { Worker } = require('worker_threads');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let worker = new Worker(__filename);
|
const worker = new Worker(__filename);
|
||||||
|
|
||||||
worker.on('message', (data) => {
|
worker.on('message', (data) => {
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
@ -194,35 +194,36 @@ function modInv(a, n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modular exponentiation a**b mod n
|
* Modular exponentiation b**e mod n
|
||||||
* @param {number|bigint} a base
|
* @param {number|bigint} b base
|
||||||
* @param {number|bigint} b exponent
|
* @param {number|bigint} e exponent
|
||||||
* @param {number|bigint} n modulo
|
* @param {number|bigint} n modulo
|
||||||
*
|
*
|
||||||
* @returns {bigint} a**b mod n
|
* @returns {bigint} b**e mod n
|
||||||
*/
|
*/
|
||||||
function modPow(a, b, n) {
|
function modPow(b, e, n) {
|
||||||
// See Knuth, volume 2, section 4.6.3.
|
|
||||||
n = BigInt(n);
|
n = BigInt(n);
|
||||||
if (n === _ZERO)
|
if (n === _ZERO)
|
||||||
return NaN;
|
return NaN;
|
||||||
|
else if (n === _ONE)
|
||||||
|
return _ZERO;
|
||||||
|
|
||||||
a = toZn(a, n);
|
b = toZn(b, n);
|
||||||
b = BigInt(b);
|
|
||||||
if (b < _ZERO) {
|
e = BigInt(e);
|
||||||
return modInv(modPow(a, abs(b), n), n);
|
if (e < _ZERO) {
|
||||||
|
return modInv(modPow(b, abs(e), n), n);
|
||||||
}
|
}
|
||||||
let result = _ONE;
|
|
||||||
let x = a;
|
let r = _ONE;
|
||||||
while (b > 0) {
|
while (e > 0) {
|
||||||
var leastSignificantBit = b & _ONE;
|
if ((e % _TWO) === _ONE) {
|
||||||
b = b / _TWO;
|
r = (r * b) % n;
|
||||||
if (leastSignificantBit === _ONE) {
|
|
||||||
result = (result * x) % n;
|
|
||||||
}
|
}
|
||||||
x = (x * x) % n;
|
e = e / _TWO;
|
||||||
|
b = b**_TWO % n;
|
||||||
}
|
}
|
||||||
return result;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
46
src/main.js
46
src/main.js
|
@ -132,7 +132,7 @@ export async function isProbablyPrime(w, iterations = 16) {
|
||||||
if (_useWorkers) {
|
if (_useWorkers) {
|
||||||
const { Worker } = require('worker_threads');
|
const { Worker } = require('worker_threads');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let worker = new Worker(__filename);
|
const worker = new Worker(__filename);
|
||||||
|
|
||||||
worker.on('message', (data) => {
|
worker.on('message', (data) => {
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
@ -155,7 +155,7 @@ export async function isProbablyPrime(w, iterations = 16) {
|
||||||
}
|
}
|
||||||
} else { // browser
|
} else { // browser
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let worker = new Worker(_isProbablyPrimeWorkerUrl());
|
const worker = new Worker(_isProbablyPrimeWorkerUrl());
|
||||||
|
|
||||||
worker.onmessage = (event) => {
|
worker.onmessage = (event) => {
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
@ -211,35 +211,37 @@ export function modInv(a, n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modular exponentiation a**b mod n
|
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
||||||
* @param {number|bigint} a base
|
*
|
||||||
* @param {number|bigint} b exponent
|
* @param {number|bigint} b base
|
||||||
|
* @param {number|bigint} e exponent
|
||||||
* @param {number|bigint} n modulo
|
* @param {number|bigint} n modulo
|
||||||
*
|
*
|
||||||
* @returns {bigint} a**b mod n
|
* @returns {bigint} b**e mod n
|
||||||
*/
|
*/
|
||||||
export function modPow(a, b, n) {
|
export function modPow(b, e, n) {
|
||||||
// See Knuth, volume 2, section 4.6.3.
|
|
||||||
n = BigInt(n);
|
n = BigInt(n);
|
||||||
if (n === _ZERO)
|
if (n === _ZERO)
|
||||||
return NaN;
|
return NaN;
|
||||||
|
else if (n === _ONE)
|
||||||
|
return _ZERO;
|
||||||
|
|
||||||
a = toZn(a, n);
|
b = toZn(b, n);
|
||||||
b = BigInt(b);
|
|
||||||
if (b < _ZERO) {
|
e = BigInt(e);
|
||||||
return modInv(modPow(a, abs(b), n), n);
|
if (e < _ZERO) {
|
||||||
|
return modInv(modPow(b, abs(e), n), n);
|
||||||
}
|
}
|
||||||
let result = _ONE;
|
|
||||||
let x = a;
|
let r = _ONE;
|
||||||
while (b > 0) {
|
while (e > 0) {
|
||||||
var leastSignificantBit = b & _ONE;
|
if ((e % _TWO) === _ONE) {
|
||||||
b = b / _TWO;
|
r = (r * b) % n;
|
||||||
if (leastSignificantBit === _ONE) {
|
|
||||||
result = (result * x) % n;
|
|
||||||
}
|
}
|
||||||
x = (x * x) % n;
|
e = e / _TWO;
|
||||||
|
b = b**_TWO % n;
|
||||||
}
|
}
|
||||||
return result;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -472,7 +474,7 @@ function _isProbablyPrimeWorkerUrl() {
|
||||||
|
|
||||||
function _workerUrl(workerCode) {
|
function _workerUrl(workerCode) {
|
||||||
workerCode = `(() => {${workerCode}})()`; // encapsulate IIFE
|
workerCode = `(() => {${workerCode}})()`; // encapsulate IIFE
|
||||||
var _blob = new Blob([workerCode], { type: 'text/javascript' });
|
const _blob = new Blob([workerCode], { type: 'text/javascript' });
|
||||||
return window.URL.createObjectURL(_blob);
|
return window.URL.createObjectURL(_blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,10 +26,9 @@ const inputs = [
|
||||||
|
|
||||||
describe('abs', function () {
|
describe('abs', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`abs(${input.value})`, function () {
|
describe(`abs(${input.value})`, function () {
|
||||||
it(`should return ${input.abs}`, function () {
|
it(`should return ${input.abs}`, function () {
|
||||||
ret = bigintCryptoUtils.abs(input.value);
|
const ret = bigintCryptoUtils.abs(input.value);
|
||||||
chai.expect(ret).to.equal(input.abs);
|
chai.expect(ret).to.equal(input.abs);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,10 +23,9 @@ const inputs = [
|
||||||
|
|
||||||
describe('bitLength', function () {
|
describe('bitLength', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`bitLength(${input.value})`, function () {
|
describe(`bitLength(${input.value})`, function () {
|
||||||
it(`should return ${input.bitLength}`, function () {
|
it(`should return ${input.bitLength}`, function () {
|
||||||
ret = bigintCryptoUtils.bitLength(input.value);
|
const ret = bigintCryptoUtils.bitLength(input.value);
|
||||||
chai.expect(ret).to.equal(input.bitLength);
|
chai.expect(ret).to.equal(input.bitLength);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -24,10 +24,9 @@ const inputs = [
|
||||||
|
|
||||||
describe('abs', function () {
|
describe('abs', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`abs(${input.value})`, function () {
|
describe(`abs(${input.value})`, function () {
|
||||||
it(`should return ${input.abs}`, function () {
|
it(`should return ${input.abs}`, function () {
|
||||||
ret = bigintCryptoUtils.abs(input.value);
|
const ret = bigintCryptoUtils.abs(input.value);
|
||||||
chai.expect(ret).to.equal(input.abs);
|
chai.expect(ret).to.equal(input.abs);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -57,10 +56,9 @@ const inputs$1 = [
|
||||||
|
|
||||||
describe('bitLength', function () {
|
describe('bitLength', function () {
|
||||||
for (const input of inputs$1) {
|
for (const input of inputs$1) {
|
||||||
let ret;
|
|
||||||
describe(`bitLength(${input.value})`, function () {
|
describe(`bitLength(${input.value})`, function () {
|
||||||
it(`should return ${input.bitLength}`, function () {
|
it(`should return ${input.bitLength}`, function () {
|
||||||
ret = bigintCryptoUtils.bitLength(input.value);
|
const ret = bigintCryptoUtils.bitLength(input.value);
|
||||||
chai.expect(ret).to.equal(input.bitLength);
|
chai.expect(ret).to.equal(input.bitLength);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -117,10 +115,9 @@ const inputs$2 = [
|
||||||
|
|
||||||
describe('gcd', function () {
|
describe('gcd', function () {
|
||||||
for (const input of inputs$2) {
|
for (const input of inputs$2) {
|
||||||
let ret;
|
|
||||||
describe(`gcd(${input.a}, ${input.b})`, function () {
|
describe(`gcd(${input.a}, ${input.b})`, function () {
|
||||||
it(`should return ${input.gcd}`, function () {
|
it(`should return ${input.gcd}`, function () {
|
||||||
ret = bigintCryptoUtils.gcd(input.a, input.b);
|
const ret = bigintCryptoUtils.gcd(input.a, input.b);
|
||||||
chai.expect(ret).to.equal(input.gcd);
|
chai.expect(ret).to.equal(input.gcd);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -229,10 +226,9 @@ const inputs$3 = [
|
||||||
|
|
||||||
describe('lcm', function () {
|
describe('lcm', function () {
|
||||||
for (const input of inputs$3) {
|
for (const input of inputs$3) {
|
||||||
let ret;
|
|
||||||
describe(`lcm(${input.a}, ${input.b})`, function () {
|
describe(`lcm(${input.a}, ${input.b})`, function () {
|
||||||
it(`should return ${input.lcm}`, function () {
|
it(`should return ${input.lcm}`, function () {
|
||||||
ret = bigintCryptoUtils.lcm(input.a, input.b);
|
const ret = bigintCryptoUtils.lcm(input.a, input.b);
|
||||||
chai.expect(ret).to.equal(input.lcm);
|
chai.expect(ret).to.equal(input.lcm);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -268,11 +264,10 @@ const inputs$4 = [
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('modInv', function () {
|
describe('modInv', function () {
|
||||||
let ret;
|
|
||||||
for (const input of inputs$4) {
|
for (const input of inputs$4) {
|
||||||
describe(`modInv(${input.a}, ${input.n})`, function () {
|
describe(`modInv(${input.a}, ${input.n})`, function () {
|
||||||
it(`should return ${input.modInv}`, function () {
|
it(`should return ${input.modInv}`, function () {
|
||||||
ret = bigintCryptoUtils.modInv(input.a, input.n);
|
const ret = bigintCryptoUtils.modInv(input.a, input.n);
|
||||||
// chai.assert( String(ret) === String(input.modInv) );
|
// chai.assert( String(ret) === String(input.modInv) );
|
||||||
chai.expect(String(ret)).to.be.equal(String(input.modInv));
|
chai.expect(String(ret)).to.be.equal(String(input.modInv));
|
||||||
});
|
});
|
||||||
|
@ -314,14 +309,26 @@ const inputs$5 = [
|
||||||
|
|
||||||
describe('modPow', function () {
|
describe('modPow', function () {
|
||||||
for (const input of inputs$5) {
|
for (const input of inputs$5) {
|
||||||
let ret;
|
|
||||||
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
|
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
|
||||||
it(`should return ${input.modPow}`, function () {
|
it(`should return ${input.modPow}`, function () {
|
||||||
ret = bigintCryptoUtils.modPow(input.a, input.b, input.n);
|
const ret = bigintCryptoUtils.modPow(input.a, input.b, input.n);
|
||||||
chai.expect(ret).to.equal(input.modPow);
|
chai.expect(ret).to.equal(input.modPow);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
describe('Time profiling', function () {
|
||||||
|
let iterations = 3000;
|
||||||
|
it(`just testing ${iterations} iterations of a big modular exponentiation (1024 bits)`, function () {
|
||||||
|
const p = BigInt('103920301461718841589267304263845359224454055603847417021399996422142529929535423886894599506329362009085557636432288745748144369296043048325513558512136442971686130986388589421125262751724362880217790112013162815676017250234401214198365302142787009943498370856167174244675719638815809347261773472114842038647');
|
||||||
|
const b = BigInt('313632271690673451924314047671460131678794095260951233878123501752357966284491455239133687519908410656818506813151659324961829045286402303082891913186909806785080978448037486178337722667190743610785429936585699831407575170854873682955317589189564880931807976657385223632835801016017549762825562427694700595');
|
||||||
|
const e = BigInt('452149997592306202232720864363485824701879487303880767747217308770351197801836846325633986474037061753983278534192061455638289551714281047915315943771002615269860312318606105460307037327329178890486613832051027105330475852552183444938408408863970975090778239473049899109989825645608770309107015209564444316');
|
||||||
|
while (iterations > 0) {
|
||||||
|
bigintCryptoUtils.modPow(b, e, p);
|
||||||
|
iterations--;
|
||||||
|
}
|
||||||
|
chai.expect(true).to.be.true;
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// For the browser test builder to work you MUST import them module in a variable that
|
// For the browser test builder to work you MUST import them module in a variable that
|
||||||
|
@ -342,8 +349,8 @@ describe('prime', function () {
|
||||||
for (const bitLength of bitLengths) {
|
for (const bitLength of bitLengths) {
|
||||||
describe(`prime(${bitLength})`, function () {
|
describe(`prime(${bitLength})`, function () {
|
||||||
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
||||||
let prime = await bigintCryptoUtils.prime(bitLength);
|
const prime = await bigintCryptoUtils.prime(bitLength);
|
||||||
let primeBitLength = bigintCryptoUtils.bitLength(prime);
|
const primeBitLength = bigintCryptoUtils.bitLength(prime);
|
||||||
chai.expect(primeBitLength).to.equal(bitLength);
|
chai.expect(primeBitLength).to.equal(bitLength);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -375,10 +382,9 @@ const inputs$6 = [
|
||||||
|
|
||||||
describe('toZn', function () {
|
describe('toZn', function () {
|
||||||
for (const input of inputs$6) {
|
for (const input of inputs$6) {
|
||||||
let ret;
|
|
||||||
describe(`toZn(${input.a}, ${input.n})`, function () {
|
describe(`toZn(${input.a}, ${input.n})`, function () {
|
||||||
it(`should return ${input.toZn}`, function () {
|
it(`should return ${input.toZn}`, function () {
|
||||||
ret = bigintCryptoUtils.toZn(input.a, input.n);
|
const ret = bigintCryptoUtils.toZn(input.a, input.n);
|
||||||
chai.expect(ret).to.equal(input.toZn);
|
chai.expect(ret).to.equal(input.toZn);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -50,10 +50,9 @@ const inputs = [
|
||||||
|
|
||||||
describe('gcd', function () {
|
describe('gcd', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`gcd(${input.a}, ${input.b})`, function () {
|
describe(`gcd(${input.a}, ${input.b})`, function () {
|
||||||
it(`should return ${input.gcd}`, function () {
|
it(`should return ${input.gcd}`, function () {
|
||||||
ret = bigintCryptoUtils.gcd(input.a, input.b);
|
const ret = bigintCryptoUtils.gcd(input.a, input.b);
|
||||||
chai.expect(ret).to.equal(input.gcd);
|
chai.expect(ret).to.equal(input.gcd);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -35,10 +35,9 @@ const inputs = [
|
||||||
|
|
||||||
describe('lcm', function () {
|
describe('lcm', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`lcm(${input.a}, ${input.b})`, function () {
|
describe(`lcm(${input.a}, ${input.b})`, function () {
|
||||||
it(`should return ${input.lcm}`, function () {
|
it(`should return ${input.lcm}`, function () {
|
||||||
ret = bigintCryptoUtils.lcm(input.a, input.b);
|
const ret = bigintCryptoUtils.lcm(input.a, input.b);
|
||||||
chai.expect(ret).to.equal(input.lcm);
|
chai.expect(ret).to.equal(input.lcm);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -29,11 +29,10 @@ const inputs = [
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('modInv', function () {
|
describe('modInv', function () {
|
||||||
let ret;
|
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
describe(`modInv(${input.a}, ${input.n})`, function () {
|
describe(`modInv(${input.a}, ${input.n})`, function () {
|
||||||
it(`should return ${input.modInv}`, function () {
|
it(`should return ${input.modInv}`, function () {
|
||||||
ret = bigintCryptoUtils.modInv(input.a, input.n);
|
const ret = bigintCryptoUtils.modInv(input.a, input.n);
|
||||||
// chai.assert( String(ret) === String(input.modInv) );
|
// chai.assert( String(ret) === String(input.modInv) );
|
||||||
chai.expect(String(ret)).to.be.equal(String(input.modInv));
|
chai.expect(String(ret)).to.be.equal(String(input.modInv));
|
||||||
});
|
});
|
||||||
|
|
|
@ -34,12 +34,24 @@ const inputs = [
|
||||||
|
|
||||||
describe('modPow', function () {
|
describe('modPow', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
|
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
|
||||||
it(`should return ${input.modPow}`, function () {
|
it(`should return ${input.modPow}`, function () {
|
||||||
ret = bigintCryptoUtils.modPow(input.a, input.b, input.n);
|
const ret = bigintCryptoUtils.modPow(input.a, input.b, input.n);
|
||||||
chai.expect(ret).to.equal(input.modPow);
|
chai.expect(ret).to.equal(input.modPow);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
describe('Time profiling', function () {
|
||||||
|
let iterations = 3000;
|
||||||
|
it(`just testing ${iterations} iterations of a big modular exponentiation (1024 bits)`, function () {
|
||||||
|
const p = BigInt('103920301461718841589267304263845359224454055603847417021399996422142529929535423886894599506329362009085557636432288745748144369296043048325513558512136442971686130986388589421125262751724362880217790112013162815676017250234401214198365302142787009943498370856167174244675719638815809347261773472114842038647');
|
||||||
|
const b = BigInt('313632271690673451924314047671460131678794095260951233878123501752357966284491455239133687519908410656818506813151659324961829045286402303082891913186909806785080978448037486178337722667190743610785429936585699831407575170854873682955317589189564880931807976657385223632835801016017549762825562427694700595');
|
||||||
|
const e = BigInt('452149997592306202232720864363485824701879487303880767747217308770351197801836846325633986474037061753983278534192061455638289551714281047915315943771002615269860312318606105460307037327329178890486613832051027105330475852552183444938408408863970975090778239473049899109989825645608770309107015209564444316');
|
||||||
|
while (iterations > 0) {
|
||||||
|
bigintCryptoUtils.modPow(b, e, p);
|
||||||
|
iterations--;
|
||||||
|
}
|
||||||
|
chai.expect(true).to.be.true;
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -18,8 +18,8 @@ describe('prime', function () {
|
||||||
for (const bitLength of bitLengths) {
|
for (const bitLength of bitLengths) {
|
||||||
describe(`prime(${bitLength})`, function () {
|
describe(`prime(${bitLength})`, function () {
|
||||||
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
it(`should return a random ${bitLength}-bits probable prime`, async function () {
|
||||||
let prime = await bigintCryptoUtils.prime(bitLength);
|
const prime = await bigintCryptoUtils.prime(bitLength);
|
||||||
let primeBitLength = bigintCryptoUtils.bitLength(prime);
|
const primeBitLength = bigintCryptoUtils.bitLength(prime);
|
||||||
chai.expect(primeBitLength).to.equal(bitLength);
|
chai.expect(primeBitLength).to.equal(bitLength);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -25,10 +25,9 @@ const inputs = [
|
||||||
|
|
||||||
describe('toZn', function () {
|
describe('toZn', function () {
|
||||||
for (const input of inputs) {
|
for (const input of inputs) {
|
||||||
let ret;
|
|
||||||
describe(`toZn(${input.a}, ${input.n})`, function () {
|
describe(`toZn(${input.a}, ${input.n})`, function () {
|
||||||
it(`should return ${input.toZn}`, function () {
|
it(`should return ${input.toZn}`, function () {
|
||||||
ret = bigintCryptoUtils.toZn(input.a, input.n);
|
const ret = bigintCryptoUtils.toZn(input.a, input.n);
|
||||||
chai.expect(ret).to.equal(input.toZn);
|
chai.expect(ret).to.equal(input.toZn);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue