Merge pull request #5 from coffee-converter/fix-randBits

Fixes bug in randBits()
This commit is contained in:
Juan Hernández Serrano 2020-03-25 00:18:59 +01:00 committed by GitHub
commit 3602667de4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 20 deletions

3
.gitignore vendored
View File

@ -14,3 +14,6 @@ node_modules/
# Visual Studio Code # Visual Studio Code
.vscode .vscode
# IntelliJ
.idea/

View File

@ -360,11 +360,14 @@ var bigintCryptoUtils = (function (exports) {
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`); throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
const byteLength = Math.ceil(bitLength / 8); const byteLength = Math.ceil(bitLength / 8);
let rndBytes = randBytesSync(byteLength, false); const rndBytes = randBytesSync(byteLength, false);
// Fill with 0's the extra bits const bitLengthMod8 = bitLength % 8;
rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1); if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
}
if (forceLength) { if (forceLength) {
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128; const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
rndBytes[0] = rndBytes[0] | mask; rndBytes[0] = rndBytes[0] | mask;
} }
return rndBytes; return rndBytes;

File diff suppressed because one or more lines are too long

View File

@ -357,11 +357,14 @@ function randBits(bitLength, forceLength = false) {
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`); throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
const byteLength = Math.ceil(bitLength / 8); const byteLength = Math.ceil(bitLength / 8);
let rndBytes = randBytesSync(byteLength, false); const rndBytes = randBytesSync(byteLength, false);
// Fill with 0's the extra bits const bitLengthMod8 = bitLength % 8;
rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1); if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
}
if (forceLength) { if (forceLength) {
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128; const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
rndBytes[0] = rndBytes[0] | mask; rndBytes[0] = rndBytes[0] | mask;
} }
return rndBytes; return rndBytes;

File diff suppressed because one or more lines are too long

View File

@ -376,11 +376,14 @@ function randBits(bitLength, forceLength = false) {
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`); throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
const byteLength = Math.ceil(bitLength / 8); const byteLength = Math.ceil(bitLength / 8);
let rndBytes = randBytesSync(byteLength, false); const rndBytes = randBytesSync(byteLength, false);
// Fill with 0's the extra bits const bitLengthMod8 = bitLength % 8;
rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1); if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
}
if (forceLength) { if (forceLength) {
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128; const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
rndBytes[0] = rndBytes[0] | mask; rndBytes[0] = rndBytes[0] | mask;
} }
return rndBytes; return rndBytes;

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "bigint-crypto-utils", "name": "bigint-crypto-utils",
"version": "2.5.0", "version": "2.5.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "bigint-crypto-utils", "name": "bigint-crypto-utils",
"version": "2.5.0", "version": "2.5.1",
"description": "Utils for working with cryptography using native JS implementation of BigInt. It includes arbitrary precision modular arithmetics, cryptographically secure random numbers and strong probable prime generation/testing.", "description": "Utils for working with cryptography using native JS implementation of BigInt. It includes arbitrary precision modular arithmetics, cryptographically secure random numbers and strong probable prime generation/testing.",
"keywords": [ "keywords": [
"modular arithmetics", "modular arithmetics",

View File

@ -400,11 +400,14 @@ export function randBits(bitLength, forceLength = false) {
throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`); throw new RangeError(`bitLength MUST be > 0 and it is ${bitLength}`);
const byteLength = Math.ceil(bitLength / 8); const byteLength = Math.ceil(bitLength / 8);
let rndBytes = randBytesSync(byteLength, false); const rndBytes = randBytesSync(byteLength, false);
// Fill with 0's the extra bits const bitLengthMod8 = bitLength % 8;
rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1); if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1);
}
if (forceLength) { if (forceLength) {
let mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128; const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128;
rndBytes[0] = rndBytes[0] | mask; rndBytes[0] = rndBytes[0] | mask;
} }
return rndBytes; return rndBytes;