randBetween now accepts negative values for min and max based on suggestion from #11

This commit is contained in:
Juan Hernández Serrano 2022-01-17 12:30:59 +01:00
parent 3b0da39f60
commit 87b061a76e
11 changed files with 56 additions and 49 deletions

2
dist/bundles/esm.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/bundles/umd.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -153,7 +153,7 @@ A promise that resolves to a boolean that is either true (a probably prime numbe
#### Defined in #### Defined in
[src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/isProbablyPrime.ts#L21) [src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/isProbablyPrime.ts#L21)
___ ___
@ -317,7 +317,7 @@ A promise that resolves to a bigint probable prime of bitLength bits.
#### Defined in #### Defined in
[src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/prime.ts#L21) [src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/prime.ts#L21)
___ ___
@ -346,7 +346,7 @@ A bigint probable prime of bitLength bits.
#### Defined in #### Defined in
[src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/prime.ts#L100) [src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/prime.ts#L100)
___ ___
@ -354,10 +354,10 @@ ___
**randBetween**(`max`, `min?`): `bigint` **randBetween**(`max`, `min?`): `bigint`
Returns a cryptographically secure random integer between [min,max]. Both numbers must be >=0 Returns a cryptographically secure random integer between [min,max].
**`throws`** {RangeError} **`throws`** {RangeError}
Arguments MUST be: max > 0 && min >=0 && max > min Arguments MUST be: max > min
#### Parameters #### Parameters
@ -374,7 +374,7 @@ A cryptographically secure random bigint between [min,max]
#### Defined in #### Defined in
[src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/randBetween.ts#L15) [src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/randBetween.ts#L15)
___ ___
@ -402,7 +402,7 @@ A Promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cry
#### Defined in #### Defined in
[src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/randBits.ts#L14) [src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/randBits.ts#L14)
___ ___
@ -430,7 +430,7 @@ A Uint8Array/Buffer (Browser/Node.js) filled with cryptographically secure rando
#### Defined in #### Defined in
[src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/randBits.ts#L45) [src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/randBits.ts#L45)
___ ___
@ -458,7 +458,7 @@ A promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cry
#### Defined in #### Defined in
[src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/randBytes.ts#L12) [src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/randBytes.ts#L12)
___ ___
@ -486,7 +486,7 @@ A UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure rando
#### Defined in #### Defined in
[src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/472c88a/src/ts/randBytes.ts#L46) [src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/3b0da39/src/ts/randBytes.ts#L46)
___ ___

View File

@ -3,17 +3,17 @@ import { fromBuffer } from './fromBuffer'
import { randBitsSync } from './randBits' import { randBitsSync } from './randBits'
/** /**
* Returns a cryptographically secure random integer between [min,max]. Both numbers must be >=0 * Returns a cryptographically secure random integer between [min,max].
* @param max Returned value will be <= max * @param max Returned value will be <= max
* @param min Returned value will be >= min * @param min Returned value will be >= min
* *
* @throws {RangeError} * @throws {RangeError}
* Arguments MUST be: max > 0 && min >=0 && max > min * Arguments MUST be: max > min
* *
* @returns A cryptographically secure random bigint between [min,max] * @returns A cryptographically secure random bigint between [min,max]
*/ */
export function randBetween (max: bigint, min: bigint = 1n): bigint { export function randBetween (max: bigint, min: bigint = 1n): bigint {
if (max <= 0n || min < 0n || max <= min) throw new RangeError('Arguments MUST be: max > 0 && min >=0 && max > min') if (max <= min) throw new RangeError('Arguments MUST be: max > min')
const interval = max - min const interval = max - min
const bitLen = bitLength(interval) const bitLen = bitLength(interval)
let rnd let rnd

View File

@ -14,9 +14,23 @@ describe('randBetween', function () {
error: false, error: false,
errorMax: false errorMax: false
}, },
{
min: BigInt(-41536),
max: BigInt(213),
iterations: 100,
error: false,
errorMax: false
},
{
min: BigInt('-41446134643671357134674615124613467356734646146125'),
max: BigInt('-125246'),
iterations: 100,
error: false,
errorMax: true
},
{ {
min: BigInt(146347), min: BigInt(146347),
max: BigInt(2), max: BigInt(232),
iterations: 1, iterations: 1,
error: true, error: true,
errorMax: false errorMax: false
@ -28,13 +42,6 @@ describe('randBetween', function () {
error: true, error: true,
errorMax: false errorMax: false
}, },
{
min: BigInt(-4),
max: BigInt(2),
iterations: 1,
error: true,
errorMax: false
},
{ {
min: BigInt(1), min: BigInt(1),
max: BigInt(-1), max: BigInt(-1),
@ -47,11 +54,11 @@ describe('randBetween', function () {
for (const num of numbers) { for (const num of numbers) {
describe(`randBetween(${num.max}, ${num.min})`, function () { describe(`randBetween(${num.max}, ${num.min})`, function () {
if (!num.error) { if (!num.error) {
it(`[${num.iterations} iterations] should return x such that min < x < max`, function () { it(`[${num.iterations} iterations] should return x such that min <= x <= max`, function () {
let ret = true let ret = true
for (let i = 0; i < num.iterations; i++) { for (let i = 0; i < num.iterations; i++) {
const x = _pkg.randBetween(num.max, num.min) const x = _pkg.randBetween(num.max, num.min)
ret = ret && x > num.min && x < num.max ret = ret && x >= num.min && x <= num.max
} }
chai.expect(ret).to.equal(true) chai.expect(ret).to.equal(true)
}) })
@ -72,7 +79,7 @@ describe('randBetween', function () {
chai.expect(ret).to.equal(true) chai.expect(ret).to.equal(true)
}) })
} else { } else {
it('should throw RangeError (max <=0)', function () { it('should throw RangeError (max <= min)', function () {
chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError) chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError)
}) })
} }

View File

@ -1,10 +1,10 @@
/** /**
* Returns a cryptographically secure random integer between [min,max]. Both numbers must be >=0 * Returns a cryptographically secure random integer between [min,max].
* @param max Returned value will be <= max * @param max Returned value will be <= max
* @param min Returned value will be >= min * @param min Returned value will be >= min
* *
* @throws {RangeError} * @throws {RangeError}
* Arguments MUST be: max > 0 && min >=0 && max > min * Arguments MUST be: max > min
* *
* @returns A cryptographically secure random bigint between [min,max] * @returns A cryptographically secure random bigint between [min,max]
*/ */