randBetween now accepts negative values for min and max based on suggestion from #11
This commit is contained in:
parent
3b0da39f60
commit
87b061a76e
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
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
20
docs/API.md
20
docs/API.md
|
@ -153,7 +153,7 @@ A promise that resolves to a boolean that is either true (a probably prime numbe
|
|||
|
||||
#### 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
|
||||
|
||||
[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
|
||||
|
||||
[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`
|
||||
|
||||
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}
|
||||
Arguments MUST be: max > 0 && min >=0 && max > min
|
||||
Arguments MUST be: max > min
|
||||
|
||||
#### Parameters
|
||||
|
||||
|
@ -374,7 +374,7 @@ A cryptographically secure random bigint between [min,max]
|
|||
|
||||
#### 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
|
||||
|
||||
[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
|
||||
|
||||
[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
|
||||
|
||||
[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
|
||||
|
||||
[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)
|
||||
|
||||
___
|
||||
|
||||
|
|
|
@ -3,17 +3,17 @@ import { fromBuffer } from './fromBuffer'
|
|||
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 min Returned value will be >= min
|
||||
*
|
||||
* @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]
|
||||
*/
|
||||
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 bitLen = bitLength(interval)
|
||||
let rnd
|
||||
|
|
|
@ -14,9 +14,23 @@ describe('randBetween', function () {
|
|||
error: 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),
|
||||
max: BigInt(2),
|
||||
max: BigInt(232),
|
||||
iterations: 1,
|
||||
error: true,
|
||||
errorMax: false
|
||||
|
@ -28,13 +42,6 @@ describe('randBetween', function () {
|
|||
error: true,
|
||||
errorMax: false
|
||||
},
|
||||
{
|
||||
min: BigInt(-4),
|
||||
max: BigInt(2),
|
||||
iterations: 1,
|
||||
error: true,
|
||||
errorMax: false
|
||||
},
|
||||
{
|
||||
min: BigInt(1),
|
||||
max: BigInt(-1),
|
||||
|
@ -47,11 +54,11 @@ describe('randBetween', function () {
|
|||
for (const num of numbers) {
|
||||
describe(`randBetween(${num.max}, ${num.min})`, function () {
|
||||
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
|
||||
for (let i = 0; i < num.iterations; i++) {
|
||||
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)
|
||||
})
|
||||
|
@ -72,7 +79,7 @@ describe('randBetween', function () {
|
|||
chai.expect(ret).to.equal(true)
|
||||
})
|
||||
} else {
|
||||
it('should throw RangeError (max <=0)', function () {
|
||||
it('should throw RangeError (max <= min)', function () {
|
||||
chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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 min Returned value will be >= min
|
||||
*
|
||||
* @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]
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue