diff --git a/README.md b/README.md index ec03ed2..a577c84 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ bigint-crypto-utils can be imported to your project with `npm`: npm install bigint-crypto-utils ``` -NPM installation defaults to the minified ES6 module for browsers and the CJS one for Node.js. +NPM installation defaults to the 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/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. +For web browsers, you can also directly download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.js) or the [ES6 bundle module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.mod.js) from GitHub. ## Usage examples @@ -29,26 +29,25 @@ Import your module as : const bigintCryptoUtils = require('bigint-crypto-utils') ... // your code here ``` - - Javascript native project + - JavaScript native project ```javascript import * as bigintCryptoUtils from 'bigint-crypto-utils' ... // your code here ``` - - Javascript native browser ES6 mod + - JavaScript native browser ES6 mod ```html - import as bcu from 'bigint-crypto-utils' - ... // your code here ``` - - Javascript native browser IIFE + - JavaScript native browser IIFE ```html - + + ``` - TypeScript ```typescript import * as bigintCryptoUtils from 'bigint-crypto-utils' @@ -56,6 +55,8 @@ Import your module as : ``` > BigInt is [ES-2020](https://tc39.es/ecma262/#sec-bigint-objects). In order to use it with TypeScript you should set `lib` (and probably also `target` and `module`) to `esnext` in `tsconfig.json`. +And you could use it like in the following: + ```javascript /* Stage 3 BigInts with value 666 can be declared as BigInt('666') or the shorter new no-so-linter-friendly syntax 666n. @@ -65,22 +66,22 @@ be raised. */ const a = BigInt('5') const b = BigInt('2') -const n = BigInt('19') +const n = 19n console.log(bigintCryptoUtils.modPow(a, b, n)) // prints 6 -console.log(bigintCryptoUtils.modInv(BigInt('2'), BigInt('5'))) // prints 3 +console.log(bigintCryptoUtils.modInv(2n, 5n)) // prints 3 console.log(bigintCryptoUtils.modInv(BigInt('3'), BigInt('5'))) // prints 2 -console.log(bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256))) // Prints a cryptographically secure random number between 1 and 2**256 bits. +console.log(bigintCryptoUtils.randBetween(2n ** 256n)) // Prints a cryptographically secure random number between 1 and 2**256 bits. async function primeTesting () { // Output of a probable prime of 2048 bits console.log(await bigintCryptoUtils.prime(2048)) // Testing if a number is a probable prime (Miller-Rabin) - const number = 27 + const number = 27n const isPrime = await bigintCryptoUtils.isProbablyPrime(number) if (isPrime) { console.log(`${number} is prime`) @@ -93,78 +94,7 @@ primeTesting() ``` -## bigint-crypto-utils JS Doc - -### Functions - -
bigint
Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
-number
Returns the bitlength of a number
-egcdReturn
An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. -Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).
-bigint
Greatest-common divisor of two integers based on the iterative binary algorithm.
-Promise
The test first tries if any of the first 250 small primes are a factor of the input number and then passes several -iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)
-bigint
The least common multiple computed as abs(a*b)/gcd(a,b)
-bigint
Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
-bigint
Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
-bigint
Modular inverse.
-bigint
Modular exponentiation b**e mod n. Currently using the right-to-left binary method
-Promise
A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator. -The browser version uses web workers to parallelise prime look up. Therefore, it does not lock the UI -main process, and it can be much faster (if several cores or cpu are available). -The node version can also use worker_threads if they are available (enabled by default with Node 11 and -and can be enabled at runtime executing node --experimental-worker with node >=10.5.0).
-bigint
A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator. -The sync version is NOT RECOMMENDED since it won't use workers and thus it'll be slower and may freeze thw window in browser's javascript. Please consider using prime() instead.
-bigint
Returns a cryptographically secure random integer between [min,max]
-Buffer
| Uint8Array
Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
-Promise
Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
-Buffer
| Uint8Array
Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
-bigint
Finds the smallest positive element that is congruent to a in modulo n
-Object
A triple (g, x, y), such that ax + by = g = gcd(a, b).
-number
\| bigint
|
| b | number
\| bigint
|
-
-
-### isProbablyPrime(w, [iterations]) ⇒ Promise
-The test first tries if any of the first 250 small primes are a factor of the input number and then passes several
-iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)
-
-**Kind**: global function
-**Returns**: Promise
- A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| w | number
\| bigint
| | An integer to be tested for primality |
-| [iterations] | number
| 16
| The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3 |
-
### lcm(a, b) ⇒ bigint
@@ -297,89 +213,6 @@ Modular exponentiation b**e mod n. Currently using the right-to-left binary meth
| e | number
\| bigint
| exponent |
| n | number
\| bigint
| modulo |
-
-
-### prime(bitLength, [iterations]) ⇒ Promise
-A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
-The browser version uses web workers to parallelise prime look up. Therefore, it does not lock the UI
-main process, and it can be much faster (if several cores or cpu are available).
-The node version can also use worker_threads if they are available (enabled by default with Node 11 and
-and can be enabled at runtime executing node --experimental-worker with node >=10.5.0).
-
-**Kind**: global function
-**Returns**: Promise
- A promise that resolves to a bigint probable prime of bitLength bits.
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| bitLength | number
| | The required bit length for the generated prime |
-| [iterations] | number
| 16
| The number of iterations for the Miller-Rabin Probabilistic Primality Test |
-
-
-
-### primeSync(bitLength, [iterations]) ⇒ bigint
-A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
-The sync version is NOT RECOMMENDED since it won't use workers and thus it'll be slower and may freeze thw window in browser's javascript. Please consider using prime() instead.
-
-**Kind**: global function
-**Returns**: bigint
- A bigint probable prime of bitLength bits.
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| bitLength | number
| | The required bit length for the generated prime |
-| [iterations] | number
| 16
| The number of iterations for the Miller-Rabin Probabilistic Primality Test |
-
-
-
-### randBetween(max, [min]) ⇒ bigint
-Returns a cryptographically secure random integer between [min,max]
-
-**Kind**: global function
-**Returns**: bigint
- A cryptographically secure random bigint between [min,max]
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| max | bigint
| | Returned value will be <= max |
-| [min] | bigint
| BigInt(1)
| Returned value will be >= min |
-
-
-
-### randBits(bitLength, [forceLength]) ⇒ Buffer
\| Uint8Array
-Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
-
-**Kind**: global function
-**Returns**: Buffer
\| Uint8Array
- A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| bitLength | number
| | The desired number of random bits |
-| [forceLength] | boolean
| false
| If we want to force the output to have a specific bit length. It basically forces the msb to be 1 |
-
-
-
-### randBytes(byteLength, [forceLength]) ⇒ Promise
-Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
-
-**Kind**: global function
-**Returns**: Promise
- A promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bytes
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| byteLength | number
| | The desired number of random bytes |
-| [forceLength] | boolean
| false
| If we want to force the output to have a bit length of 8*byteLength. It basically forces the msb to be 1 |
-
-
-
-### randBytesSync(byteLength, [forceLength]) ⇒ Buffer
\| Uint8Array
-Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
-
-**Kind**: global function
-**Returns**: Buffer
\| Uint8Array
- A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bytes
-
-| Param | Type | Default | Description |
-| --- | --- | --- | --- |
-| byteLength | number
| | The desired number of random bytes |
-| [forceLength] | boolean
| false
| If we want to force the output to have a bit length of 8*byteLength. It basically forces the msb to be 1 |
-
### toZn(a, n) ⇒ bigint
diff --git a/build/build.docs.js b/build/build.docs.js
new file mode 100644
index 0000000..4db18de
--- /dev/null
+++ b/build/build.docs.js
@@ -0,0 +1,24 @@
+'use strict'
+
+const fs = require('fs')
+const jsdoc2md = require('jsdoc-to-markdown')
+const path = require('path')
+const pkgJson = require('../package.json')
+
+const rootDir = path.join(__dirname, '..')
+
+const template = path.join(rootDir, pkgJson.directories.src, 'doc', 'readme-template.md')
+const input = path.join(rootDir, pkgJson.directories.lib, 'index.node.js')
+
+const options = {
+ source: fs.readFileSync(input, { encoding: 'UTF-8' }), // we need to use this instead of files in order to avoid issues with esnext features
+ template: fs.readFileSync(template, { encoding: 'UTF-8' }),
+ 'heading-depth': 3, // The initial heading depth. For example, with a value of 2 the top-level markdown headings look like "## The heading"
+ 'global-index-format': 'none' // none, grouped, table, dl.
+}
+
+const readmeContents = jsdoc2md.renderSync(options)
+
+const readmeFile = path.join(rootDir, 'README.md')
+
+fs.writeFileSync(readmeFile, readmeContents)
diff --git a/build/rollup.config.js b/build/rollup.config.js
index 018fa2c..3467cf5 100644
--- a/build/rollup.config.js
+++ b/build/rollup.config.js
@@ -37,8 +37,8 @@ module.exports = [
replace({
'process.browser': true
})
- ]
- // external: ['bigint-crypto-utils']
+ ],
+ external: ['bigint-mod-arith']
},
{ // Browser bundles
input: input,
@@ -61,8 +61,8 @@ module.exports = [
browser: true
}),
terser({
- mangle: false,
- compress: false
+ // mangle: false,
+ // compress: false
})
]
},
@@ -76,7 +76,7 @@ module.exports = [
output: {
file: path.join(dstDir, 'index.node.js'),
format: 'cjs'
- }
- // external: ['bigint-crypto-utils']
+ },
+ external: ['bigint-mod-arith']
}
]
diff --git a/lib/index.browser.bundle.js b/lib/index.browser.bundle.js
index cd736f1..47b43ac 100644
--- a/lib/index.browser.bundle.js
+++ b/lib/index.browser.bundle.js
@@ -1 +1 @@
-var bigintCryptoUtils=function(exports){"use strict";const _ZERO=BigInt(0);const _ONE=BigInt(1);const _TWO=BigInt(2);function abs(a){a=BigInt(a);return a>=_ZERO?a:-a}function bitLength(a){a=BigInt(a);if(a===_ONE){return 1}let bits=1;do{bits++}while((a>>=_ONE)>_ONE);return bits}function eGcd(a,b){a=BigInt(a);b=BigInt(b);if(a<=_ZERO|b<=_ZERO){return NaN}let x=_ZERO;let y=_ONE;let u=_ONE;let v=_ZERO;while(a!==_ZERO){const q=b/a;const r=b%a;const m=x-u*q;const n=y-v*q;b=a;a=r;x=u;y=v;u=m;v=n}return{b:b,x:x,y:y}}function gcd(a,b){a=abs(a);b=abs(b);if(a===_ZERO){return b}else if(b===_ZERO){return a}let shift=_ZERO;while(!((a|b)&_ONE)){a>>=_ONE;b>>=_ONE;shift++}while(!(a&_ONE))a>>=_ONE;do{while(!(b&_ONE))b>>=_ONE;if(a>b){const x=a;a=b;b=x}b-=a}while(b);return a<