JS Standard. Some fixes with the test. Better structure

This commit is contained in:
juanelas 2020-04-06 13:34:03 +02:00
parent c29b520108
commit 4e52ea75e6
8 changed files with 52 additions and 16 deletions

View File

@ -2,7 +2,7 @@
# bigint-crypto-utils
Utils for working with cryptography using native JS ([ES-2020](https://tc39.es/ecma262/#sec-bigint-objects)) implementation of BigInt. It includes some extra functions to work with modular arithmetics along with secure random numbers and a fast strong probable prime generator/tester (parallelised multi-threaded Miller-Rabin primality test). It can be used by any [Web Browser or webview supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) and with Node.js (>=10.4.0). In the latter case, for multi-threaded primality tests, you should use Node.js v11 or newer or enable at runtime with `node --experimental-worker` with Node.js version >= 10.5.0 and < 11.
Utils for working with cryptography using native JS ([ES-2020](https://tc39.es/ecma262/#sec-bigint-objects)) implementation of BigInt. It includes some extra functions to work with modular arithmetic along with secure random numbers and a fast strong probable prime generator/tester (parallelized multi-threaded Miller-Rabin primality test). It can be used by any [Web Browser or webview supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) and with Node.js (>=10.4.0). In the latter case, for multi-threaded primality tests, you should use Node.js v11 or newer or enable at runtime with `node --experimental-worker` with Node.js version >= 10.5.0 and < 11.
> The operations supported on BigInts are not constant time. BigInt can be therefore **[unsuitable for use in cryptography](https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html).** Many platforms provide native support for cryptography, such as [Web Cryptography API](https://w3c.github.io/webcrypto/) or [Node.js Crypto](https://nodejs.org/dist/latest/docs/api/crypto.html).
@ -18,7 +18,7 @@ npm install bigint-crypto-utils
NPM installation defaults to the minified ES6 module for browsers and the CJS one for Node.js.
For web browsers, you can also directly download the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.mod.min.js) from GitHub.
For web browsers, you can also directly download the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.mod.js) from GitHub.
## Usage examples

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -343,14 +343,19 @@ function randBetween (max, min = _ONE) {
* @returns {Buffer|Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
function randBits (bitLength, forceLength = false) {
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}`)
}
const byteLength = Math.ceil(bitLength / 8)
const rndBytes = randBytesSync(byteLength, false)
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1)
const bitLengthMod8 = bitLength % 8
if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
}
if (forceLength) {
const mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
rndBytes[0] = rndBytes[0] | mask
}
return rndBytes

View File

@ -362,14 +362,19 @@ function randBetween (max, min = _ONE) {
* @returns {Buffer|Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
function randBits (bitLength, forceLength = false) {
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}`)
}
const byteLength = Math.ceil(bitLength / 8)
const rndBytes = randBytesSync(byteLength, false)
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1)
const bitLengthMod8 = bitLength % 8
if (bitLengthMod8) {
// Fill with 0's the extra bits
rndBytes[0] = rndBytes[0] & (2 ** bitLengthMod8 - 1)
}
if (forceLength) {
const mask = (bitLength % 8) ? 2 ** ((bitLength % 8) - 1) : 128
const mask = bitLengthMod8 ? 2 ** (bitLengthMod8 - 1) : 128
rndBytes[0] = rndBytes[0] | mask
}
return rndBytes
@ -769,8 +774,8 @@ let _useWorkers = true // The following is just to check whether Node.js can use
} catch (e) {
console.log(`[bigint-crypto-utils] WARNING:
This node version doesn't support worker_threads. You should enable them in order to greatly speedup the generation of big prime numbers.
· With Node 11 it is enabled by default (consider upgrading).
· With Node 10, starting with 10.5.0, you can enable worker_threads at runtime executing node --experimental-worker `)
· With Node >=11 it is enabled by default (consider upgrading).
· With Node 10, starting with 10.4.0, you can enable worker_threads at runtime executing node --experimental-worker `)
return false
}
})()

View File

@ -18,7 +18,7 @@ npm install bigint-crypto-utils
NPM installation defaults to the minified ES6 module for browsers and the CJS one for Node.js.
For web browsers, you can also directly download the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.mod.min.js) from GitHub.
For web browsers, you can also directly download the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.mod.js) from GitHub.
## Usage examples

View File

@ -833,7 +833,7 @@ if (!process.browser) { // Node.js
console.log(`[bigint-crypto-utils] WARNING:
This node version doesn't support worker_threads. You should enable them in order to greatly speedup the generation of big prime numbers.
· With Node >=11 it is enabled by default (consider upgrading).
· With Node 10, starting with 10.5.0, you can enable worker_threads at runtime executing node --experimental-worker `)
· With Node 10, starting with 10.4.0, you can enable worker_threads at runtime executing node --experimental-worker `)
return false
}
})()

View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>Primes</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="syncPrime">result of primeSync() will show up here</div>
<div id="asyncPrime">result of prime() will show up here</div>
<script src="../../lib/index.browser.bundle.js"></script>
<script>
(async () => {
document.getElementById(
"syncPrime"
).innerHTML = bigintCryptoUtils.primeSync(123);
document.getElementById(
"asyncPrime"
).innerHTML = await bigintCryptoUtils.prime(123);
})();
</script>
</body>
</html>