diff --git a/README.md b/README.md
index 5337964..c4bb6c4 100644
--- a/README.md
+++ b/README.md
@@ -107,8 +107,8 @@ iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)
A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
@@ -235,16 +235,16 @@ Modular inverse.
-## modPow(a, b, n) ⇒ bigint
-Modular exponentiation a**b mod n
+## modPow(b, e, n) ⇒ bigint
+Modular exponentiation b**e mod n
**Kind**: global function
-**Returns**: bigint
- a**b mod n
+**Returns**: bigint
- b**e mod n
| Param | Type | Description |
| --- | --- | --- |
-| a | number
\| bigint
| base |
-| b | number
\| bigint
| exponent |
+| b | number
\| bigint
| base |
+| e | number
\| bigint
| exponent |
| n | number
\| bigint
| modulo |
diff --git a/dist/bigint-crypto-utils-latest.browser.js b/dist/bigint-crypto-utils-latest.browser.js
index e239b70..388d584 100644
--- a/dist/bigint-crypto-utils-latest.browser.js
+++ b/dist/bigint-crypto-utils-latest.browser.js
@@ -131,7 +131,7 @@ var bigintCryptoUtils = (function (exports) {
}
{ // browser
return new Promise((resolve, reject) => {
- let worker = new Worker(_isProbablyPrimeWorkerUrl());
+ const worker = new Worker(_isProbablyPrimeWorkerUrl());
worker.onmessage = (event) => {
worker.terminate();
@@ -187,35 +187,36 @@ var bigintCryptoUtils = (function (exports) {
}
/**
- * Modular exponentiation a**b mod n
- * @param {number|bigint} a base
- * @param {number|bigint} b exponent
+ * Modular exponentiation b**e mod n
+ * @param {number|bigint} b base
+ * @param {number|bigint} e exponent
* @param {number|bigint} n modulo
*
- * @returns {bigint} a**b mod n
+ * @returns {bigint} b**e mod n
*/
- function modPow(a, b, n) {
- // See Knuth, volume 2, section 4.6.3.
+ function modPow(b, e, n) {
n = BigInt(n);
if (n === _ZERO)
return NaN;
+ else if (n === _ONE)
+ return _ZERO;
- a = toZn(a, n);
- b = BigInt(b);
- if (b < _ZERO) {
- return modInv(modPow(a, abs(b), n), n);
+ b = toZn(b, n);
+
+ e = BigInt(e);
+ if (e < _ZERO) {
+ return modInv(modPow(b, abs(e), n), n);
}
- let result = _ONE;
- let x = a;
- while (b > 0) {
- var leastSignificantBit = b & _ONE;
- b = b / _TWO;
- if (leastSignificantBit === _ONE) {
- result = (result * x) % n;
+
+ let r = _ONE;
+ while (e > 0) {
+ if ((e % _TWO) === _ONE) {
+ r = (r * b) % 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) {
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);
}
diff --git a/dist/bigint-crypto-utils-latest.browser.min.js b/dist/bigint-crypto-utils-latest.browser.min.js
index 5baa083..e6feedb 100644
--- a/dist/bigint-crypto-utils-latest.browser.min.js
+++ b/dist/bigint-crypto-utils-latest.browser.min.js
@@ -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<{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 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< {${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;aa)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;ae(a.data,b),d.push(b)}for(let e=0;ea)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<{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 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< {${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;aa)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;ae(a.data,b),d.push(b)}for(let e=0;ea)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}({});
diff --git a/dist/bigint-crypto-utils-latest.browser.mod.js b/dist/bigint-crypto-utils-latest.browser.mod.js
index c4f0ca4..9726f72 100644
--- a/dist/bigint-crypto-utils-latest.browser.mod.js
+++ b/dist/bigint-crypto-utils-latest.browser.mod.js
@@ -128,7 +128,7 @@ async function isProbablyPrime(w, iterations = 16) {
}
{ // browser
return new Promise((resolve, reject) => {
- let worker = new Worker(_isProbablyPrimeWorkerUrl());
+ const worker = new Worker(_isProbablyPrimeWorkerUrl());
worker.onmessage = (event) => {
worker.terminate();
@@ -184,35 +184,36 @@ function modInv(a, n) {
}
/**
- * Modular exponentiation a**b mod n
- * @param {number|bigint} a base
- * @param {number|bigint} b exponent
+ * Modular exponentiation b**e mod n
+ * @param {number|bigint} b base
+ * @param {number|bigint} e exponent
* @param {number|bigint} n modulo
*
- * @returns {bigint} a**b mod n
+ * @returns {bigint} b**e mod n
*/
-function modPow(a, b, n) {
- // See Knuth, volume 2, section 4.6.3.
+function modPow(b, e, n) {
n = BigInt(n);
if (n === _ZERO)
return NaN;
+ else if (n === _ONE)
+ return _ZERO;
- a = toZn(a, n);
- b = BigInt(b);
- if (b < _ZERO) {
- return modInv(modPow(a, abs(b), n), n);
+ b = toZn(b, n);
+
+ e = BigInt(e);
+ if (e < _ZERO) {
+ return modInv(modPow(b, abs(e), n), n);
}
- let result = _ONE;
- let x = a;
- while (b > 0) {
- var leastSignificantBit = b & _ONE;
- b = b / _TWO;
- if (leastSignificantBit === _ONE) {
- result = (result * x) % n;
+
+ let r = _ONE;
+ while (e > 0) {
+ if ((e % _TWO) === _ONE) {
+ r = (r * b) % 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) {
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);
}
diff --git a/dist/bigint-crypto-utils-latest.browser.mod.min.js b/dist/bigint-crypto-utils-latest.browser.mod.min.js
index 0255a93..2d77dc5 100644
--- a/dist/bigint-crypto-utils-latest.browser.mod.min.js
+++ b/dist/bigint-crypto-utils-latest.browser.mod.min.js
@@ -1 +1 @@
-const _ZERO=BigInt(0),_ONE=BigInt(1),_TWO=BigInt(2);function abs(b){return b=BigInt(b),b>=_ZERO?b:-b}function bitLength(b){if(b=BigInt(b),b===_ONE)return 1;let c=1;do c++;while((b>>=_ONE)>_ONE);return c}function eGcd(c,d){if(c=BigInt(c),d=BigInt(d),c<=_ZERO|d<=_ZERO)return NaN;let e=_ZERO,f=_ONE,g=_ONE,h=_ZERO;for(;c!==_ZERO;){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 gcd(c,d){if(c=abs(c),d=abs(d),c===_ZERO)return d;if(d===_ZERO)return c;let e=_ZERO;for(;!((c|d)&_ONE);)c>>=_ONE,d>>=_ONE,e++;for(;!(c&_ONE);)c>>=_ONE;do{for(;!(d&_ONE);)d>>=_ONE;if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<{let e=new Worker(_isProbablyPrimeWorkerUrl());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function lcm(c,d){return c=BigInt(c),d=BigInt(d),c===_ZERO&&d===_ZERO?_ZERO:abs(c*d)/gcd(c,d)}function modInv(b,a){if(b==_ZERO|a<=_ZERO)return NaN;let c=eGcd(toZn(b,a),a);return c.b===_ONE?toZn(c.x,a):NaN}function modPow(c,d,e){if(e=BigInt(e),e===_ZERO)return NaN;if(c=toZn(c,e),d=BigInt(d),d<_ZERO)return modInv(modPow(c,abs(d),e),e);let f=_ONE,g=c;for(;0a)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;ae(a.data,b),d.push(b)}for(let e=0;e min");const c=a-b;let d,e=bitLength(c);do{let a=randBits(e);d=fromBuffer(a)}while(d>c);return d+b}function randBits(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=randBytesSync(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 randBytes(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)})}function randBytesSync(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 toZn(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function fromBuffer(a){let b=_ZERO;for(let c of a.values()){let a=BigInt(c);b=(b< {${a}})()`;var b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function _isProbablyPrime(c,b=16){if(c===_TWO)return!0;if((c&_ONE)===_ZERO||c===_ONE)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=_ZERO?b:-b}function bitLength(b){if(b=BigInt(b),b===_ONE)return 1;let c=1;do c++;while((b>>=_ONE)>_ONE);return c}function eGcd(c,d){if(c=BigInt(c),d=BigInt(d),c<=_ZERO|d<=_ZERO)return NaN;let e=_ZERO,f=_ONE,g=_ONE,h=_ZERO;for(;c!==_ZERO;){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 gcd(c,d){if(c=abs(c),d=abs(d),c===_ZERO)return d;if(d===_ZERO)return c;let e=_ZERO;for(;!((c|d)&_ONE);)c>>=_ONE,d>>=_ONE,e++;for(;!(c&_ONE);)c>>=_ONE;do{for(;!(d&_ONE);)d>>=_ONE;if(c>d){let a=c;c=d,d=a}d-=c}while(d);return c<{const e=new Worker(_isProbablyPrimeWorkerUrl());e.onmessage=a=>{e.terminate(),c(a.data.isPrime)},e.onmessageerror=a=>{d(a)},e.postMessage({rnd:a,iterations:b,id:0})})}function lcm(c,d){return c=BigInt(c),d=BigInt(d),c===_ZERO&&d===_ZERO?_ZERO:abs(c*d)/gcd(c,d)}function modInv(b,a){if(b==_ZERO|a<=_ZERO)return NaN;let c=eGcd(toZn(b,a),a);return c.b===_ONE?toZn(c.x,a):NaN}function modPow(a,c,d){if(d=BigInt(d),d===_ZERO)return NaN;if(d===_ONE)return _ZERO;if(a=toZn(a,d),c=BigInt(c),c<_ZERO)return modInv(modPow(a,abs(c),d),d);let f=_ONE;for(;0a)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;ae(a.data,b),d.push(b)}for(let e=0;e min");const c=a-b;let d,e=bitLength(c);do{let a=randBits(e);d=fromBuffer(a)}while(d>c);return d+b}function randBits(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=randBytesSync(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 randBytes(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)})}function randBytesSync(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 toZn(b,c){return(c=BigInt(c),0>=c)?NaN:(b=BigInt(b)%c,0>b?b+c:b)}function fromBuffer(a){let b=_ZERO;for(let c of a.values()){let a=BigInt(c);b=(b< {${a}})()`;const b=new Blob([a],{type:"text/javascript"});return window.URL.createObjectURL(b)}function _isProbablyPrime(c,b=16){if(c===_TWO)return!0;if((c&_ONE)===_ZERO||c===_ONE)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 {
- let worker = new Worker(__filename);
+ const worker = new Worker(__filename);
worker.on('message', (data) => {
worker.terminate();
@@ -194,35 +194,36 @@ function modInv(a, n) {
}
/**
- * Modular exponentiation a**b mod n
- * @param {number|bigint} a base
- * @param {number|bigint} b exponent
+ * Modular exponentiation b**e mod n
+ * @param {number|bigint} b base
+ * @param {number|bigint} e exponent
* @param {number|bigint} n modulo
*
- * @returns {bigint} a**b mod n
+ * @returns {bigint} b**e mod n
*/
-function modPow(a, b, n) {
- // See Knuth, volume 2, section 4.6.3.
+function modPow(b, e, n) {
n = BigInt(n);
if (n === _ZERO)
return NaN;
+ else if (n === _ONE)
+ return _ZERO;
- a = toZn(a, n);
- b = BigInt(b);
- if (b < _ZERO) {
- return modInv(modPow(a, abs(b), n), n);
+ b = toZn(b, n);
+
+ e = BigInt(e);
+ if (e < _ZERO) {
+ return modInv(modPow(b, abs(e), n), n);
}
- let result = _ONE;
- let x = a;
- while (b > 0) {
- var leastSignificantBit = b & _ONE;
- b = b / _TWO;
- if (leastSignificantBit === _ONE) {
- result = (result * x) % n;
+
+ let r = _ONE;
+ while (e > 0) {
+ if ((e % _TWO) === _ONE) {
+ r = (r * b) % n;
}
- x = (x * x) % n;
+ e = e / _TWO;
+ b = b**_TWO % n;
}
- return result;
+ return r;
}
/**
diff --git a/src/main.js b/src/main.js
index 0708af7..91b9764 100644
--- a/src/main.js
+++ b/src/main.js
@@ -132,7 +132,7 @@ export async function isProbablyPrime(w, iterations = 16) {
if (_useWorkers) {
const { Worker } = require('worker_threads');
return new Promise((resolve, reject) => {
- let worker = new Worker(__filename);
+ const worker = new Worker(__filename);
worker.on('message', (data) => {
worker.terminate();
@@ -155,7 +155,7 @@ export async function isProbablyPrime(w, iterations = 16) {
}
} else { // browser
return new Promise((resolve, reject) => {
- let worker = new Worker(_isProbablyPrimeWorkerUrl());
+ const worker = new Worker(_isProbablyPrimeWorkerUrl());
worker.onmessage = (event) => {
worker.terminate();
@@ -211,35 +211,37 @@ export function modInv(a, n) {
}
/**
- * Modular exponentiation a**b mod n
- * @param {number|bigint} a base
- * @param {number|bigint} b exponent
+ * Modular exponentiation b**e mod n. Currently using the right-to-left binary method
+ *
+ * @param {number|bigint} b base
+ * @param {number|bigint} e exponent
* @param {number|bigint} n modulo
*
- * @returns {bigint} a**b mod n
+ * @returns {bigint} b**e mod n
*/
-export function modPow(a, b, n) {
- // See Knuth, volume 2, section 4.6.3.
+export function modPow(b, e, n) {
n = BigInt(n);
if (n === _ZERO)
return NaN;
+ else if (n === _ONE)
+ return _ZERO;
- a = toZn(a, n);
- b = BigInt(b);
- if (b < _ZERO) {
- return modInv(modPow(a, abs(b), n), n);
+ b = toZn(b, n);
+
+ e = BigInt(e);
+ if (e < _ZERO) {
+ return modInv(modPow(b, abs(e), n), n);
}
- let result = _ONE;
- let x = a;
- while (b > 0) {
- var leastSignificantBit = b & _ONE;
- b = b / _TWO;
- if (leastSignificantBit === _ONE) {
- result = (result * x) % n;
+
+ let r = _ONE;
+ while (e > 0) {
+ if ((e % _TWO) === _ONE) {
+ r = (r * b) % 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) {
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);
}
diff --git a/test/abs.js b/test/abs.js
index eec1ad3..aed2ff7 100644
--- a/test/abs.js
+++ b/test/abs.js
@@ -26,10 +26,9 @@ const inputs = [
describe('abs', function () {
for (const input of inputs) {
- let ret;
describe(`abs(${input.value})`, 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);
});
});
diff --git a/test/bitLength.js b/test/bitLength.js
index 4edde04..20b6055 100644
--- a/test/bitLength.js
+++ b/test/bitLength.js
@@ -23,10 +23,9 @@ const inputs = [
describe('bitLength', function () {
for (const input of inputs) {
- let ret;
describe(`bitLength(${input.value})`, 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);
});
});
diff --git a/test/browser/tests.js b/test/browser/tests.js
index ba5f973..e5ab0aa 100644
--- a/test/browser/tests.js
+++ b/test/browser/tests.js
@@ -24,10 +24,9 @@ const inputs = [
describe('abs', function () {
for (const input of inputs) {
- let ret;
describe(`abs(${input.value})`, 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);
});
});
@@ -57,10 +56,9 @@ const inputs$1 = [
describe('bitLength', function () {
for (const input of inputs$1) {
- let ret;
describe(`bitLength(${input.value})`, 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);
});
});
@@ -117,10 +115,9 @@ const inputs$2 = [
describe('gcd', function () {
for (const input of inputs$2) {
- let ret;
describe(`gcd(${input.a}, ${input.b})`, 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);
});
});
@@ -229,10 +226,9 @@ const inputs$3 = [
describe('lcm', function () {
for (const input of inputs$3) {
- let ret;
describe(`lcm(${input.a}, ${input.b})`, 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);
});
});
@@ -268,11 +264,10 @@ const inputs$4 = [
];
describe('modInv', function () {
- let ret;
for (const input of inputs$4) {
describe(`modInv(${input.a}, ${input.n})`, 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.expect(String(ret)).to.be.equal(String(input.modInv));
});
@@ -314,14 +309,26 @@ const inputs$5 = [
describe('modPow', function () {
for (const input of inputs$5) {
- let ret;
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, 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);
});
});
}
+ 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
@@ -342,8 +349,8 @@ describe('prime', function () {
for (const bitLength of bitLengths) {
describe(`prime(${bitLength})`, function () {
it(`should return a random ${bitLength}-bits probable prime`, async function () {
- let prime = await bigintCryptoUtils.prime(bitLength);
- let primeBitLength = bigintCryptoUtils.bitLength(prime);
+ const prime = await bigintCryptoUtils.prime(bitLength);
+ const primeBitLength = bigintCryptoUtils.bitLength(prime);
chai.expect(primeBitLength).to.equal(bitLength);
});
});
@@ -375,10 +382,9 @@ const inputs$6 = [
describe('toZn', function () {
for (const input of inputs$6) {
- let ret;
describe(`toZn(${input.a}, ${input.n})`, 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);
});
});
diff --git a/test/gcd.js b/test/gcd.js
index 49d1fb7..4372004 100644
--- a/test/gcd.js
+++ b/test/gcd.js
@@ -50,10 +50,9 @@ const inputs = [
describe('gcd', function () {
for (const input of inputs) {
- let ret;
describe(`gcd(${input.a}, ${input.b})`, 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);
});
});
diff --git a/test/lcm.js b/test/lcm.js
index 84f15d9..c13b292 100644
--- a/test/lcm.js
+++ b/test/lcm.js
@@ -35,10 +35,9 @@ const inputs = [
describe('lcm', function () {
for (const input of inputs) {
- let ret;
describe(`lcm(${input.a}, ${input.b})`, 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);
});
});
diff --git a/test/modInv.js b/test/modInv.js
index 11a6bcd..1bcf724 100644
--- a/test/modInv.js
+++ b/test/modInv.js
@@ -29,11 +29,10 @@ const inputs = [
];
describe('modInv', function () {
- let ret;
for (const input of inputs) {
describe(`modInv(${input.a}, ${input.n})`, 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.expect(String(ret)).to.be.equal(String(input.modInv));
});
diff --git a/test/modPow.js b/test/modPow.js
index 8b5d92f..8b28c00 100644
--- a/test/modPow.js
+++ b/test/modPow.js
@@ -34,12 +34,24 @@ const inputs = [
describe('modPow', function () {
for (const input of inputs) {
- let ret;
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, 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);
});
});
}
+ 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;
+ });
+ });
});
\ No newline at end of file
diff --git a/test/prime.js b/test/prime.js
index 0becbe3..94b8bb9 100644
--- a/test/prime.js
+++ b/test/prime.js
@@ -18,8 +18,8 @@ describe('prime', function () {
for (const bitLength of bitLengths) {
describe(`prime(${bitLength})`, function () {
it(`should return a random ${bitLength}-bits probable prime`, async function () {
- let prime = await bigintCryptoUtils.prime(bitLength);
- let primeBitLength = bigintCryptoUtils.bitLength(prime);
+ const prime = await bigintCryptoUtils.prime(bitLength);
+ const primeBitLength = bigintCryptoUtils.bitLength(prime);
chai.expect(primeBitLength).to.equal(bitLength);
});
});
diff --git a/test/toZn.js b/test/toZn.js
index cf5234c..58039b1 100644
--- a/test/toZn.js
+++ b/test/toZn.js
@@ -25,10 +25,9 @@ const inputs = [
describe('toZn', function () {
for (const input of inputs) {
- let ret;
describe(`toZn(${input.a}, ${input.n})`, 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);
});
});