fixed browser tests

This commit is contained in:
juanelas 2020-04-21 02:41:32 +02:00
parent 2179954b1e
commit 04cad8250b
9 changed files with 221 additions and 214 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -137,7 +137,7 @@ function primeSync (bitLength, iterations = 16) {
* @returns {bigint} A cryptographically secure random bigint between [min,max]
*/
function randBetween (max, min = 1n) {
if (max <= 0n || min < 0n || max <= min) throw new Error('inputs should be max > 0, min >= 0; max > min')
if (max <= 0n || min < 0n || max <= min) throw new RangeError('inputs should be max > 0, min >= 0; max > min')
const interval = max - min
const bitLen = bitLength(interval)
let rnd
@ -156,15 +156,14 @@ function randBetween (max, min = 1n) {
*
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
async function randBits (bitLength, forceLength = false) {
if (bitLength < 1) {
throw new RangeError('bitLength MUST be > 0')
}
function randBits (bitLength, forceLength = false) {
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
const byteLength = Math.ceil(bitLength / 8)
const bitLengthMod8 = bitLength % 8
const rndBytes = await randBytes(byteLength, false)
return new Promise((resolve) => {
randBytes(byteLength, false).then(function (rndBytes) {
if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
@ -173,7 +172,9 @@ async function randBits (bitLength, forceLength = false) {
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
rndBytes[0] = rndBytes[0] | mask
}
return rndBytes
resolve(rndBytes)
})
})
}
/**
@ -184,9 +185,7 @@ async function randBits (bitLength, forceLength = false) {
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
function randBitsSync (bitLength, forceLength = false) {
if (bitLength < 1) {
throw new RangeError('bitLength MUST be > 0')
}
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
const byteLength = Math.ceil(bitLength / 8)
const rndBytes = randBytesSync(byteLength, false)

View File

@ -145,7 +145,7 @@ function primeSync (bitLength, iterations = 16) {
* @returns {bigint} A cryptographically secure random bigint between [min,max]
*/
function randBetween (max, min = 1n) {
if (max <= 0n || min < 0n || max <= min) throw new Error('inputs should be max > 0, min >= 0; max > min')
if (max <= 0n || min < 0n || max <= min) throw new RangeError('inputs should be max > 0, min >= 0; max > min')
const interval = max - min
const bitLen = bigintModArith.bitLength(interval)
let rnd
@ -164,15 +164,14 @@ function randBetween (max, min = 1n) {
*
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
async function randBits (bitLength, forceLength = false) {
if (bitLength < 1) {
throw new RangeError('bitLength MUST be > 0')
}
function randBits (bitLength, forceLength = false) {
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
const byteLength = Math.ceil(bitLength / 8)
const bitLengthMod8 = bitLength % 8
const rndBytes = await randBytes(byteLength, false)
return new Promise((resolve) => {
randBytes(byteLength, false).then(function (rndBytes) {
if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
@ -181,7 +180,9 @@ async function randBits (bitLength, forceLength = false) {
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
rndBytes[0] = rndBytes[0] | mask
}
return rndBytes
resolve(rndBytes)
})
})
}
/**
@ -192,9 +193,7 @@ async function randBits (bitLength, forceLength = false) {
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
function randBitsSync (bitLength, forceLength = false) {
if (bitLength < 1) {
throw new RangeError('bitLength MUST be > 0')
}
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
const byteLength = Math.ceil(bitLength / 8)
const rndBytes = randBytesSync(byteLength, false)

View File

@ -171,7 +171,7 @@ export function primeSync (bitLength, iterations = 16) {
* @returns {bigint} A cryptographically secure random bigint between [min,max]
*/
export function randBetween (max, min = 1n) {
if (max <= 0n || min < 0n || max <= min) throw new Error('inputs should be max > 0, min >= 0; max > min')
if (max <= 0n || min < 0n || max <= min) throw new RangeError('inputs should be max > 0, min >= 0; max > min')
const interval = max - min
const bitLen = bitLength(interval)
let rnd
@ -190,15 +190,14 @@ export function randBetween (max, min = 1n) {
*
* @returns {Promise<Buffer | Uint8Array>} A Promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
export async function randBits (bitLength, forceLength = false) {
if (bitLength < 1) {
throw new RangeError('bitLength MUST be > 0')
}
export function randBits (bitLength, forceLength = false) {
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
const byteLength = Math.ceil(bitLength / 8)
const bitLengthMod8 = bitLength % 8
const rndBytes = await randBytes(byteLength, false)
return new Promise((resolve) => {
randBytes(byteLength, false).then(function (rndBytes) {
if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
@ -207,7 +206,9 @@ export async function randBits (bitLength, forceLength = false) {
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
rndBytes[0] = rndBytes[0] | mask
}
return rndBytes
resolve(rndBytes)
})
})
}
/**
@ -218,9 +219,7 @@ export async function randBits (bitLength, forceLength = false) {
* @returns {Buffer | Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
export function randBitsSync (bitLength, forceLength = false) {
if (bitLength < 1) {
throw new RangeError('bitLength MUST be > 0')
}
if (bitLength < 1) throw new RangeError('bitLength MUST be > 0')
const byteLength = Math.ceil(bitLength / 8)
const rndBytes = randBytesSync(byteLength, false)

View File

@ -111,30 +111,27 @@ const bitLengths = [
3072
];
describe('prime', function () {
describe('Testing prime generation', function () {
this.timeout(120000);
for (const bitLength of bitLengths) {
describe(`prime(${bitLength})`, function () {
if (bitLength > 0) {
it(`should return a random ${bitLength}-bits probable prime`, async function () {
let primeBitLength = bitLength;
try {
const prime = await _pkg.prime(bitLength);
primeBitLength = _pkg.bitLength(prime);
} catch {}
chai.expect(primeBitLength).to.equal(bitLength);
chai.expect(_pkg.bitLength(prime)).to.equal(bitLength);
});
} else {
it('should throw error', async function () {
chai.expect(() => _pkg.prime(bitLength)).to.throw(RangeError);
});
}
});
}
describe('Testing sync (NOT-RECOMMENDED) version: primeSync()', function () {
it('should return a random 1024-bits probable prime', function () {
const prime = _pkg.primeSync(1024, 16);
const primeBitLength = _pkg.bitLength(prime);
chai.expect(primeBitLength).to.equal(1024);
try {
_pkg.primeSync(0);
} catch (error) {
chai.expect(true).to.equal(true);
}
chai.expect(_pkg.bitLength(prime)).to.equal(1024);
chai.expect(() => _pkg.primeSync(0)).to.throw(RangeError);
});
});
});
@ -204,13 +201,8 @@ describe('randBetween', function () {
chai.expect(ret).to.equal(true);
});
} else {
it('should return error (max <=0 || min <0 || min>=max)', function () {
try {
_pkg.randBetween(num.max, num.min);
chai.expect(num.error).to.equal(false);
} catch (error) {
chai.expect(num.error).to.equal(true);
}
it('should throw RangeError (max <=0 || min <0 || min>=max)', function () {
chai.expect(() => _pkg.randBetween(num.max, num.min)).to.throw(RangeError);
});
}
});
@ -225,13 +217,8 @@ describe('randBetween', function () {
chai.expect(ret).to.equal(true);
});
} else {
it('should return error (max <=0)', function () {
try {
_pkg.randBetween(num.max);
chai.expect(num.errorMax).to.equal(false);
} catch (error) {
chai.expect(num.errorMax).to.equal(true);
}
it('should throw RangeError (max <=0)', function () {
chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError);
});
}
});
@ -245,44 +232,54 @@ describe('randBetween', function () {
// <--
const iterations = 10;
const bitLengths$1 = [0, 3, 8, 16, 511, 2048];
const byteLengths = [0, 1, 8, 33, 40];
const bitLengths$1 = [-1, 0, 3, 8, 16, 511, 2048];
const byteLengths = [-7, 0, 1, 8, 33, 40];
describe('testing randBits', async function () {
for (const bitLength of bitLengths$1) {
describe(`${iterations} of randBitsSync(${bitLength})`, function () {
describe(`${iterations} iterations of randBitsSync(${bitLength})`, function () {
if (bitLength > 0) {
it('should return buffers', function () {
let ret = true;
for (let i = 0; i < iterations; i++) {
it('should return a buffer', function () {
try {
const randbits = _pkg.randBitsSync(bitLength);
// console.log(JSON.stringify(randbits))
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
const randbits2 = _pkg.randBitsSync(bitLength, true);
// console.log(JSON.stringify(randbits2))
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
} catch (error) {
chai.expect(bitLength).to.be.lte(0);
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
ret = false;
break
}
}
chai.expect(ret).to.equal(true);
});
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBitsSync(bitLength)).to.throw(RangeError);
});
}
});
describe(`${iterations} of randBits(${bitLength})`, async function () {
describe(`${iterations} iterations of randBits(${bitLength})`, async function () {
if (bitLength > 0) {
it('should return buffers', async function () {
let ret = true;
for (let i = 0; i < iterations; i++) {
it('should return a buffer', async function () {
try {
const randbits = await _pkg.randBits(bitLength);
// console.log(JSON.stringify(randbits))
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
const randbits2 = await _pkg.randBits(bitLength, true);
// console.log(JSON.stringify(randbits2))
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
} catch (error) {
chai.expect(bitLength).to.be.lte(0);
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
ret = false;
break
}
}
chai.expect(ret).to.equal(true);
});
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBits(bitLength)).to.throw(RangeError);
});
}
});
@ -291,39 +288,47 @@ describe('testing randBits', async function () {
describe('testing randBytes', async function () {
for (const byteLength of byteLengths) {
describe(`${iterations} of randBytesSync(${byteLength})`, function () {
describe(`${iterations} iterations of randBytesSync(${byteLength})`, function () {
if (byteLength > 0) {
it('should return buffers', function () {
let ret = true;
for (let i = 0; i < iterations; i++) {
it('should return a buffer', function () {
try {
const randbits = _pkg.randBytesSync(byteLength);
console.log(JSON.stringify(randbits));
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
const randbits2 = _pkg.randBytesSync(byteLength, true);
console.log(JSON.stringify(randbits2));
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
} catch (error) {
chai.expect(byteLength).to.be.lte(0);
const randbytes = _pkg.randBytesSync(byteLength);
// console.log(JSON.stringify(randbits))
const randbytes2 = _pkg.randBytesSync(byteLength, true);
// console.log(JSON.stringify(randbits2))
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
ret = false;
}
}
chai.expect(ret).to.equal(true);
});
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBytesSync(byteLength)).to.throw(RangeError);
});
}
});
describe(`${iterations} of randBytes(${byteLength})`, async function () {
describe(`${iterations} iterations of randBytes(${byteLength})`, async function () {
if (byteLength > 0) {
it('should return buffers', async function () {
let ret = true;
for (let i = 0; i < iterations; i++) {
it('should return a buffer', async function () {
try {
const randbits = await _pkg.randBytes(byteLength);
console.log(JSON.stringify(randbits));
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits).to.be.an.instanceOf(Buffer);
const randbits2 = await _pkg.randBytes(byteLength, true);
console.log(JSON.stringify(randbits2));
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array);
else chai.expect(randbits2).to.be.an.instanceOf(Buffer);
} catch (error) {
chai.expect(byteLength).to.be.lte(0);
const randbytes = await _pkg.randBytes(byteLength);
// console.log(JSON.stringify(randbits))
const randbytes2 = await _pkg.randBytes(byteLength, true);
// console.log(JSON.stringify(randbits2))
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
ret = false;
}
}
chai.expect(ret).to.equal(true);
});
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBytes(byteLength)).to.throw(RangeError);
});
}
});

View File

@ -18,30 +18,27 @@ const bitLengths = [
3072
]
describe('prime', function () {
describe('Testing prime generation', function () {
this.timeout(120000)
for (const bitLength of bitLengths) {
describe(`prime(${bitLength})`, function () {
if (bitLength > 0) {
it(`should return a random ${bitLength}-bits probable prime`, async function () {
let primeBitLength = bitLength
try {
const prime = await _pkg.prime(bitLength)
primeBitLength = _pkg.bitLength(prime)
} catch {}
chai.expect(primeBitLength).to.equal(bitLength)
chai.expect(_pkg.bitLength(prime)).to.equal(bitLength)
})
} else {
it('should throw error', async function () {
chai.expect(() => _pkg.prime(bitLength)).to.throw(RangeError)
})
}
})
}
describe('Testing sync (NOT-RECOMMENDED) version: primeSync()', function () {
it('should return a random 1024-bits probable prime', function () {
const prime = _pkg.primeSync(1024, 16)
const primeBitLength = _pkg.bitLength(prime)
chai.expect(primeBitLength).to.equal(1024)
try {
_pkg.primeSync(0)
} catch (error) {
chai.expect(true).to.equal(true)
}
chai.expect(_pkg.bitLength(prime)).to.equal(1024)
chai.expect(() => _pkg.primeSync(0)).to.throw(RangeError)
})
})
})

View File

@ -65,13 +65,8 @@ describe('randBetween', function () {
chai.expect(ret).to.equal(true)
})
} else {
it('should return error (max <=0 || min <0 || min>=max)', function () {
try {
_pkg.randBetween(num.max, num.min)
chai.expect(num.error).to.equal(false)
} catch (error) {
chai.expect(num.error).to.equal(true)
}
it('should throw RangeError (max <=0 || min <0 || min>=max)', function () {
chai.expect(() => _pkg.randBetween(num.max, num.min)).to.throw(RangeError)
})
}
})
@ -86,13 +81,8 @@ describe('randBetween', function () {
chai.expect(ret).to.equal(true)
})
} else {
it('should return error (max <=0)', function () {
try {
_pkg.randBetween(num.max)
chai.expect(num.errorMax).to.equal(false)
} catch (error) {
chai.expect(num.errorMax).to.equal(true)
}
it('should throw RangeError (max <=0)', function () {
chai.expect(() => _pkg.randBetween(num.max)).to.throw(RangeError)
})
}
})

View File

@ -7,44 +7,54 @@ const chai = require('chai')
// <--
const iterations = 10
const bitLengths = [0, 3, 8, 16, 511, 2048]
const byteLengths = [0, 1, 8, 33, 40]
const bitLengths = [-1, 0, 3, 8, 16, 511, 2048]
const byteLengths = [-7, 0, 1, 8, 33, 40]
describe('testing randBits', async function () {
for (const bitLength of bitLengths) {
describe(`${iterations} of randBitsSync(${bitLength})`, function () {
describe(`${iterations} iterations of randBitsSync(${bitLength})`, function () {
if (bitLength > 0) {
it('should return buffers', function () {
let ret = true
for (let i = 0; i < iterations; i++) {
it('should return a buffer', function () {
try {
const randbits = _pkg.randBitsSync(bitLength)
// console.log(JSON.stringify(randbits))
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
const randbits2 = _pkg.randBitsSync(bitLength, true)
// console.log(JSON.stringify(randbits2))
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
} catch (error) {
chai.expect(bitLength).to.be.lte(0)
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
ret = false
break
}
}
chai.expect(ret).to.equal(true)
})
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBitsSync(bitLength)).to.throw(RangeError)
})
}
})
describe(`${iterations} of randBits(${bitLength})`, async function () {
describe(`${iterations} iterations of randBits(${bitLength})`, async function () {
if (bitLength > 0) {
it('should return buffers', async function () {
let ret = true
for (let i = 0; i < iterations; i++) {
it('should return a buffer', async function () {
try {
const randbits = await _pkg.randBits(bitLength)
// console.log(JSON.stringify(randbits))
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
const randbits2 = await _pkg.randBits(bitLength, true)
// console.log(JSON.stringify(randbits2))
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
} catch (error) {
chai.expect(bitLength).to.be.lte(0)
if (!(((randbits instanceof Uint8Array) && (randbits2 instanceof Uint8Array)) ||
((randbits instanceof Buffer) && (randbits2 instanceof Buffer)))) {
ret = false
break
}
}
chai.expect(ret).to.equal(true)
})
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBits(bitLength)).to.throw(RangeError)
})
}
})
@ -53,39 +63,47 @@ describe('testing randBits', async function () {
describe('testing randBytes', async function () {
for (const byteLength of byteLengths) {
describe(`${iterations} of randBytesSync(${byteLength})`, function () {
describe(`${iterations} iterations of randBytesSync(${byteLength})`, function () {
if (byteLength > 0) {
it('should return buffers', function () {
let ret = true
for (let i = 0; i < iterations; i++) {
it('should return a buffer', function () {
try {
const randbits = _pkg.randBytesSync(byteLength)
console.log(JSON.stringify(randbits))
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
const randbits2 = _pkg.randBytesSync(byteLength, true)
console.log(JSON.stringify(randbits2))
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
} catch (error) {
chai.expect(byteLength).to.be.lte(0)
const randbytes = _pkg.randBytesSync(byteLength)
// console.log(JSON.stringify(randbits))
const randbytes2 = _pkg.randBytesSync(byteLength, true)
// console.log(JSON.stringify(randbits2))
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
ret = false
}
}
chai.expect(ret).to.equal(true)
})
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBytesSync(byteLength)).to.throw(RangeError)
})
}
})
describe(`${iterations} of randBytes(${byteLength})`, async function () {
describe(`${iterations} iterations of randBytes(${byteLength})`, async function () {
if (byteLength > 0) {
it('should return buffers', async function () {
let ret = true
for (let i = 0; i < iterations; i++) {
it('should return a buffer', async function () {
try {
const randbits = await _pkg.randBytes(byteLength)
console.log(JSON.stringify(randbits))
if (randbits instanceof Uint8Array) chai.expect(randbits).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits).to.be.an.instanceOf(Buffer)
const randbits2 = await _pkg.randBytes(byteLength, true)
console.log(JSON.stringify(randbits2))
if (randbits2 instanceof Uint8Array) chai.expect(randbits2).to.be.an.instanceOf(Uint8Array)
else chai.expect(randbits2).to.be.an.instanceOf(Buffer)
} catch (error) {
chai.expect(byteLength).to.be.lte(0)
const randbytes = await _pkg.randBytes(byteLength)
// console.log(JSON.stringify(randbits))
const randbytes2 = await _pkg.randBytes(byteLength, true)
// console.log(JSON.stringify(randbits2))
if (!(((randbytes instanceof Uint8Array) && (randbytes2 instanceof Uint8Array)) ||
((randbytes instanceof Buffer) && (randbytes2 instanceof Buffer)))) {
ret = false
}
}
chai.expect(ret).to.equal(true)
})
} else {
it('should throw RangeError', function () {
chai.expect(() => _pkg.randBytes(byteLength)).to.throw(RangeError)
})
}
})