randBitsSync

This commit is contained in:
juanelas 2020-04-07 19:36:14 +02:00
parent 5c410d60c1
commit eaf7c53ca2
7 changed files with 14 additions and 11 deletions

View File

@ -148,7 +148,10 @@ The sync version is NOT RECOMMENDED since it won't use workers and thus it&#
<dt><a href="#randBetween">randBetween(max, [min])</a><code>bigint</code></dt> <dt><a href="#randBetween">randBetween(max, [min])</a><code>bigint</code></dt>
<dd><p>Returns a cryptographically secure random integer between [min,max]</p> <dd><p>Returns a cryptographically secure random integer between [min,max]</p>
</dd> </dd>
<dt><a href="#randBits">randBits(bitLength, [forceLength])</a><code>Buffer</code> | <code>Uint8Array</code></dt> <dt><a href="#randBits">randBits(bitLength, [forceLength])</a><code>Promise.&lt;(Buffer|Uint8Array)&gt;</code></dt>
<dd><p>Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()</p>
</dd>
<dt><a href="#randBitsSync">randBitsSync(bitLength, [forceLength])</a><code>Buffer</code> | <code>Uint8Array</code></dt>
<dd><p>Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()</p> <dd><p>Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()</p>
</dd> </dd>
<dt><a href="#randBytes">randBytes(byteLength, [forceLength])</a><code>Promise.&lt;(Buffer|Uint8Array)&gt;</code></dt> <dt><a href="#randBytes">randBytes(byteLength, [forceLength])</a><code>Promise.&lt;(Buffer|Uint8Array)&gt;</code></dt>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -216,9 +216,9 @@ function toZn (a, n) {
* @param {number | bigint} w An integer to be tested for primality * @param {number | bigint} w An integer to be tested for primality
* @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3 * @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3
* *
* @return {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite) * @returns {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
*/ */
async function isProbablyPrime (w, iterations = 16) { function isProbablyPrime (w, iterations = 16) {
if (typeof w === 'number') { if (typeof w === 'number') {
w = BigInt(w) w = BigInt(w)
} }
@ -321,7 +321,7 @@ function primeSync (bitLength, iterations = 16) {
if (bitLength < 1) { throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`) } if (bitLength < 1) { throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`) }
let rnd = 0n let rnd = 0n
do { do {
rnd = fromBuffer(randBytesSync(bitLength / 8, true)) rnd = fromBuffer(randBits(bitLength, true))
} while (!_isProbablyPrime(rnd, iterations)) } while (!_isProbablyPrime(rnd, iterations))
return rnd return rnd
} }

View File

@ -8,9 +8,9 @@ export { abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn } from '
* @param {number | bigint} w An integer to be tested for primality * @param {number | bigint} w An integer to be tested for primality
* @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3 * @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3
* *
* @return {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite) * @returns {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
*/ */
async function isProbablyPrime (w, iterations = 16) { function isProbablyPrime (w, iterations = 16) {
if (typeof w === 'number') { if (typeof w === 'number') {
w = BigInt(w) w = BigInt(w)
} }

View File

@ -11,9 +11,9 @@ var bigintModArith = require('bigint-mod-arith')
* @param {number | bigint} w An integer to be tested for primality * @param {number | bigint} w An integer to be tested for primality
* @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3 * @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3
* *
* @return {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite) * @returns {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
*/ */
async function isProbablyPrime (w, iterations = 16) { function isProbablyPrime (w, iterations = 16) {
if (typeof w === 'number') { if (typeof w === 'number') {
w = BigInt(w) w = BigInt(w)
} }

2
types/index.d.ts vendored
View File

@ -53,7 +53,7 @@ export function gcd(a: number | bigint, b: number | bigint): bigint;
* @param {number | bigint} w An integer to be tested for primality * @param {number | bigint} w An integer to be tested for primality
* @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3 * @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3
* *
* @return {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite) * @returns {Promise<boolean>} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
*/ */
export function isProbablyPrime(w: number | bigint, iterations?: number): Promise<boolean>; export function isProbablyPrime(w: number | bigint, iterations?: number): Promise<boolean>;
/** /**