From 19dd8b6491f49b88d15dbd49545f953066f38f52 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Hern=C3=A1ndez=20Serrano?=
Date: Wed, 9 Oct 2019 14:17:03 +0200
Subject: [PATCH] added sync version of prime
---
README.md | 7 ++++---
dist/bigint-crypto-utils-latest.browser.js | 15 +++++++++++++--
dist/bigint-crypto-utils-latest.browser.min.js | 2 +-
dist/bigint-crypto-utils-latest.browser.mod.js | 15 +++++++++++++--
.../bigint-crypto-utils-latest.browser.mod.min.js | 2 +-
dist/bigint-crypto-utils-latest.node.js | 9 ++++++---
src/main.js | 9 ++++++---
test/browser/tests.js | 7 +++++++
test/prime.js | 7 +++++++
9 files changed, 58 insertions(+), 15 deletions(-)
diff --git a/README.md b/README.md
index 6c92f55..0ea9d02 100644
--- a/README.md
+++ b/README.md
@@ -116,7 +116,7 @@ iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)
A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
The browser version uses web workers to parallelise prime look up. Therefore, it does not lock the UI
main process, and it can be much faster (if several cores or cpu are available).
@@ -281,7 +281,7 @@ Modular exponentiation b**e mod n. Currently using the right-to-left binary meth
-## prime(bitLength, iterations) ⇒ Promise
+## prime(bitLength, iterations, sync) ⇒ Promise
\| bigint
A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
The browser version uses web workers to parallelise prime look up. Therefore, it does not lock the UI
main process, and it can be much faster (if several cores or cpu are available).
@@ -289,12 +289,13 @@ The node version can also use worker_threads if they are available (enabled by d
and can be enabled at runtime executing node --experimental-worker with node >=10.5.0).
**Kind**: global function
-**Returns**: Promise
- A promise that resolves to a bigint probable prime of bitLength bits
+**Returns**: Promise
\| bigint
- A promise that resolves to a bigint probable prime of bitLength bits or a bigint if called in synchronous mode.
| Param | Type | Description |
| --- | --- | --- |
| bitLength | number
| The required bit length for the generated prime |
| iterations | number
| The number of iterations for the Miller-Rabin Probabilistic Primality Test |
+| sync | boolean
| NOT RECOMMENDED. Invoke the function synchronously. It won't use workers so it'll be slower and may freeze thw window in browser's javascript. |
diff --git a/dist/bigint-crypto-utils-latest.browser.js b/dist/bigint-crypto-utils-latest.browser.js
index 82ef8b1..7afc65a 100644
--- a/dist/bigint-crypto-utils-latest.browser.js
+++ b/dist/bigint-crypto-utils-latest.browser.js
@@ -257,12 +257,23 @@ var bigintCryptoUtils = (function (exports) {
*
* @param {number} bitLength The required bit length for the generated prime
* @param {number} iterations The number of iterations for the Miller-Rabin Probabilistic Primality Test
+ * @param {boolean} sync NOT RECOMMENDED. Invoke the function synchronously. It won't use workers so it'll be slower and may freeze thw window in browser's javascript.
*
- * @returns {Promise} A promise that resolves to a bigint probable prime of bitLength bits
+ * @returns {Promise|bigint} A promise that resolves to a bigint probable prime of bitLength bits or a bigint if called in synchronous mode.
*/
- function prime(bitLength, iterations = 16) {
+ function prime(bitLength, iterations = 16, sync = false) {
if (bitLength < 1)
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
+
+ if ( sync) {
+ let rnd = _ZERO;
+ do {
+ rnd = fromBuffer(randBytesSync(bitLength / 8, true));
+ } while (!_isProbablyPrime(rnd, iterations));
+ if(sync)
+ return rnd;
+ return new Promise((resolve) => { resolve(rnd); });
+ }
return new Promise((resolve) => {
let workerList = [];
const _onmessage = (msg, newWorker) => {
diff --git a/dist/bigint-crypto-utils-latest.browser.min.js b/dist/bigint-crypto-utils-latest.browser.min.js
index fbf81af..42d6ff2 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>=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;a=d?c:d},a.min=function(c,d){return c=BigInt(c),d=BigInt(d),c>=d?d:c},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;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;a=d?c:d},a.min=function(c,d){return c=BigInt(c),d=BigInt(d),c>=d?d:c},a.modInv=h,a.modPow=i,a.prime=function(a,b=16,c=!1){if(1>a)throw new RangeError(`bitLength MUST be > 0 and it is ${a}`);if(c){let d=s;do d=n(l(a/8,!0));while(!q(d,b));return c?d:new Promise(a=>{a(d)})}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 68fa974..ff5e34c 100644
--- a/dist/bigint-crypto-utils-latest.browser.mod.js
+++ b/dist/bigint-crypto-utils-latest.browser.mod.js
@@ -254,12 +254,23 @@ function modPow(b, e, n) {
*
* @param {number} bitLength The required bit length for the generated prime
* @param {number} iterations The number of iterations for the Miller-Rabin Probabilistic Primality Test
+ * @param {boolean} sync NOT RECOMMENDED. Invoke the function synchronously. It won't use workers so it'll be slower and may freeze thw window in browser's javascript.
*
- * @returns {Promise} A promise that resolves to a bigint probable prime of bitLength bits
+ * @returns {Promise|bigint} A promise that resolves to a bigint probable prime of bitLength bits or a bigint if called in synchronous mode.
*/
-function prime(bitLength, iterations = 16) {
+function prime(bitLength, iterations = 16, sync = false) {
if (bitLength < 1)
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
+
+ if ( sync) {
+ let rnd = _ZERO;
+ do {
+ rnd = fromBuffer(randBytesSync(bitLength / 8, true));
+ } while (!_isProbablyPrime(rnd, iterations));
+ if(sync)
+ return rnd;
+ return new Promise((resolve) => { resolve(rnd); });
+ }
return new Promise((resolve) => {
let workerList = [];
const _onmessage = (msg, newWorker) => {
diff --git a/dist/bigint-crypto-utils-latest.browser.mod.min.js b/dist/bigint-crypto-utils-latest.browser.mod.min.js
index 3de246e..3bb4db5 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<{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 max(c,d){return c=BigInt(c),d=BigInt(d),c>=d?c:d}function min(c,d){return c=BigInt(c),d=BigInt(d),c>=d?d:c}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=_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 max(c,d){return c=BigInt(c),d=BigInt(d),c>=d?c:d}function min(c,d){return c=BigInt(c),d=BigInt(d),c>=d?d:c}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}`);if(c){let d=_ZERO;do d=fromBuffer(randBytesSync(a/8,!0));while(!_isProbablyPrime(d,b));return c?d:new Promise(a=>{a(d)})}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 0 and it is ${bitLength}`);
- if ( !_useWorkers) {
+ if (( !_useWorkers) || sync) {
let rnd = _ZERO;
do {
rnd = fromBuffer(randBytesSync(bitLength / 8, true));
} while (!_isProbablyPrime(rnd, iterations));
+ if(sync)
+ return rnd;
return new Promise((resolve) => { resolve(rnd); });
}
return new Promise((resolve) => {
diff --git a/src/main.js b/src/main.js
index 5689359..86226c2 100644
--- a/src/main.js
+++ b/src/main.js
@@ -281,18 +281,21 @@ export function modPow(b, e, n) {
*
* @param {number} bitLength The required bit length for the generated prime
* @param {number} iterations The number of iterations for the Miller-Rabin Probabilistic Primality Test
+ * @param {boolean} sync NOT RECOMMENDED. Invoke the function synchronously. It won't use workers so it'll be slower and may freeze thw window in browser's javascript.
*
- * @returns {Promise} A promise that resolves to a bigint probable prime of bitLength bits
+ * @returns {Promise|bigint} A promise that resolves to a bigint probable prime of bitLength bits or a bigint if called in synchronous mode.
*/
-export function prime(bitLength, iterations = 16) {
+export function prime(bitLength, iterations = 16, sync = false) {
if (bitLength < 1)
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
- if (!process.browser && !_useWorkers) {
+ if ((!process.browser && !_useWorkers) || sync) {
let rnd = _ZERO;
do {
rnd = fromBuffer(randBytesSync(bitLength / 8, true));
} while (!_isProbablyPrime(rnd, iterations));
+ if(sync)
+ return rnd;
return new Promise((resolve) => { resolve(rnd); });
}
return new Promise((resolve) => {
diff --git a/test/browser/tests.js b/test/browser/tests.js
index 0b17b84..d818531 100644
--- a/test/browser/tests.js
+++ b/test/browser/tests.js
@@ -537,6 +537,13 @@ describe('prime', function () {
});
});
}
+ describe('Testing sync (NOT-RECOMMENDED) version of prime', function() {
+ it('should return a random 1024-bits probable prime', function () {
+ const prime = bigintCryptoUtils.prime(1024, 16, true);
+ const primeBitLength = bigintCryptoUtils.bitLength(prime);
+ chai.expect(primeBitLength).to.equal(1024);
+ });
+ });
});
// For the browser test builder to work you MUST import them module in a variable that
diff --git a/test/prime.js b/test/prime.js
index 94b8bb9..a15a924 100644
--- a/test/prime.js
+++ b/test/prime.js
@@ -24,4 +24,11 @@ describe('prime', function () {
});
});
}
+ describe('Testing sync (NOT-RECOMMENDED) version of prime', function() {
+ it('should return a random 1024-bits probable prime', function () {
+ const prime = bigintCryptoUtils.prime(1024, 16, true);
+ const primeBitLength = bigintCryptoUtils.bitLength(prime);
+ chai.expect(primeBitLength).to.equal(1024);
+ });
+ });
});
\ No newline at end of file