added sync version of prime
This commit is contained in:
parent
dd8599fe08
commit
19dd8b6491
|
@ -116,7 +116,7 @@ iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)</p>
|
||||||
<dt><a href="#modPow">modPow(b, e, n)</a> ⇒ <code>bigint</code></dt>
|
<dt><a href="#modPow">modPow(b, e, n)</a> ⇒ <code>bigint</code></dt>
|
||||||
<dd><p>Modular exponentiation b**e mod n. Currently using the right-to-left binary method</p>
|
<dd><p>Modular exponentiation b**e mod n. Currently using the right-to-left binary method</p>
|
||||||
</dd>
|
</dd>
|
||||||
<dt><a href="#prime">prime(bitLength, iterations)</a> ⇒ <code>Promise</code></dt>
|
<dt><a href="#prime">prime(bitLength, iterations, sync)</a> ⇒ <code>Promise</code> | <code>bigint</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.
|
||||||
The browser version uses web workers to parallelise prime look up. Therefore, it does not lock the UI
|
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).
|
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
|
||||||
|
|
||||||
<a name="prime"></a>
|
<a name="prime"></a>
|
||||||
|
|
||||||
## prime(bitLength, iterations) ⇒ <code>Promise</code>
|
## prime(bitLength, iterations, sync) ⇒ <code>Promise</code> \| <code>bigint</code>
|
||||||
A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
|
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
|
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).
|
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).
|
and can be enabled at runtime executing node --experimental-worker with node >=10.5.0).
|
||||||
|
|
||||||
**Kind**: global function
|
**Kind**: global function
|
||||||
**Returns**: <code>Promise</code> - A promise that resolves to a bigint probable prime of bitLength bits
|
**Returns**: <code>Promise</code> \| <code>bigint</code> - A promise that resolves to a bigint probable prime of bitLength bits or a bigint if called in synchronous mode.
|
||||||
|
|
||||||
| Param | Type | Description |
|
| Param | Type | Description |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| bitLength | <code>number</code> | The required bit length for the generated prime |
|
| bitLength | <code>number</code> | The required bit length for the generated prime |
|
||||||
| iterations | <code>number</code> | The number of iterations for the Miller-Rabin Probabilistic Primality Test |
|
| iterations | <code>number</code> | The number of iterations for the Miller-Rabin Probabilistic Primality Test |
|
||||||
|
| sync | <code>boolean</code> | 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. |
|
||||||
|
|
||||||
<a name="randBetween"></a>
|
<a name="randBetween"></a>
|
||||||
|
|
||||||
|
|
|
@ -257,12 +257,23 @@ var bigintCryptoUtils = (function (exports) {
|
||||||
*
|
*
|
||||||
* @param {number} bitLength The required bit length for the generated prime
|
* @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 {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)
|
if (bitLength < 1)
|
||||||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
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) => {
|
return new Promise((resolve) => {
|
||||||
let workerList = [];
|
let workerList = [];
|
||||||
const _onmessage = (msg, newWorker) => {
|
const _onmessage = (msg, newWorker) => {
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -254,12 +254,23 @@ function modPow(b, e, n) {
|
||||||
*
|
*
|
||||||
* @param {number} bitLength The required bit length for the generated prime
|
* @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 {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)
|
if (bitLength < 1)
|
||||||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
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) => {
|
return new Promise((resolve) => {
|
||||||
let workerList = [];
|
let workerList = [];
|
||||||
const _onmessage = (msg, newWorker) => {
|
const _onmessage = (msg, newWorker) => {
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -264,18 +264,21 @@ function modPow(b, e, n) {
|
||||||
*
|
*
|
||||||
* @param {number} bitLength The required bit length for the generated prime
|
* @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 {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)
|
if (bitLength < 1)
|
||||||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
||||||
|
|
||||||
if ( !_useWorkers) {
|
if (( !_useWorkers) || sync) {
|
||||||
let rnd = _ZERO;
|
let rnd = _ZERO;
|
||||||
do {
|
do {
|
||||||
rnd = fromBuffer(randBytesSync(bitLength / 8, true));
|
rnd = fromBuffer(randBytesSync(bitLength / 8, true));
|
||||||
} while (!_isProbablyPrime(rnd, iterations));
|
} while (!_isProbablyPrime(rnd, iterations));
|
||||||
|
if(sync)
|
||||||
|
return rnd;
|
||||||
return new Promise((resolve) => { resolve(rnd); });
|
return new Promise((resolve) => { resolve(rnd); });
|
||||||
}
|
}
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
|
|
@ -281,18 +281,21 @@ export function modPow(b, e, n) {
|
||||||
*
|
*
|
||||||
* @param {number} bitLength The required bit length for the generated prime
|
* @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 {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)
|
if (bitLength < 1)
|
||||||
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
|
||||||
|
|
||||||
if (!process.browser && !_useWorkers) {
|
if ((!process.browser && !_useWorkers) || sync) {
|
||||||
let rnd = _ZERO;
|
let rnd = _ZERO;
|
||||||
do {
|
do {
|
||||||
rnd = fromBuffer(randBytesSync(bitLength / 8, true));
|
rnd = fromBuffer(randBytesSync(bitLength / 8, true));
|
||||||
} while (!_isProbablyPrime(rnd, iterations));
|
} while (!_isProbablyPrime(rnd, iterations));
|
||||||
|
if(sync)
|
||||||
|
return rnd;
|
||||||
return new Promise((resolve) => { resolve(rnd); });
|
return new Promise((resolve) => { resolve(rnd); });
|
||||||
}
|
}
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
|
|
@ -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
|
// For the browser test builder to work you MUST import them module in a variable that
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue