better dependencies
This commit is contained in:
parent
e7da55c352
commit
70a32d0286
25
README.md
25
README.md
|
@ -18,7 +18,7 @@ npm install bigint-crypto-utils
|
||||||
|
|
||||||
NPM installation defaults to the 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 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.min.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.iife.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
|
## Usage examples
|
||||||
|
|
||||||
|
@ -29,11 +29,12 @@ Import your module as :
|
||||||
const bigintCryptoUtils = require('bigint-crypto-utils')
|
const bigintCryptoUtils = require('bigint-crypto-utils')
|
||||||
... // your code here
|
... // your code here
|
||||||
```
|
```
|
||||||
- JavaScript native project
|
- JavaScript native or TypeScript project
|
||||||
```javascript
|
```javascript
|
||||||
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
||||||
... // your code here
|
... // your code here
|
||||||
```
|
```
|
||||||
|
> 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`.
|
||||||
- JavaScript native browser ES6 mod
|
- JavaScript native browser ES6 mod
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
|
@ -43,17 +44,17 @@ Import your module as :
|
||||||
```
|
```
|
||||||
- JavaScript native browser IIFE
|
- JavaScript native browser IIFE
|
||||||
```html
|
```html
|
||||||
<script src="../../lib/index.browser.bundle.js"></script> <!-- Use you actual path to the browser bundle -->
|
<head>
|
||||||
<script>
|
...
|
||||||
... // your code here
|
<script src="../../lib/index.browser.bundle.js"></script> <!-- Use you actual path to the browser bundle -->
|
||||||
</script>
|
</head>
|
||||||
|
<body>
|
||||||
|
...
|
||||||
|
<script>
|
||||||
|
... // your code here
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
```
|
```
|
||||||
- TypeScript
|
|
||||||
```typescript
|
|
||||||
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
|
||||||
... // your code here
|
|
||||||
```
|
|
||||||
> 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:
|
And you could use it like in the following:
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ const pkgJson = require('../package.json')
|
||||||
const rootDir = path.join(__dirname, '..')
|
const rootDir = path.join(__dirname, '..')
|
||||||
|
|
||||||
const template = path.join(rootDir, pkgJson.directories.src, 'doc', 'readme-template.md')
|
const template = path.join(rootDir, pkgJson.directories.src, 'doc', 'readme-template.md')
|
||||||
const input = path.join(rootDir, pkgJson.directories.lib, 'index.browser.bundle.mod.js')
|
const input = path.join(rootDir, pkgJson.browser)
|
||||||
const source = fs.readFileSync(input, { encoding: 'UTF-8' }).replace(/([0-9]+)n([,\s\n)])/g, '$1$2')
|
const source = fs.readFileSync(input, { encoding: 'UTF-8' }).replace(/([0-9]+)n([,\s\n)])/g, '$1$2')
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
|
|
|
@ -4,7 +4,7 @@ const path = require('path')
|
||||||
const pkgJson = require('../package.json')
|
const pkgJson = require('../package.json')
|
||||||
|
|
||||||
const rootDir = path.join(__dirname, '..')
|
const rootDir = path.join(__dirname, '..')
|
||||||
const jsFile = path.join(rootDir, pkgJson.directories.lib, 'index.browser.bundle.mod.js')
|
const jsFile = path.join(rootDir, pkgJson.browser)
|
||||||
const dtsFile = path.join(rootDir, pkgJson.types)
|
const dtsFile = path.join(rootDir, pkgJson.types)
|
||||||
|
|
||||||
const compilerOptions = {
|
const compilerOptions = {
|
||||||
|
|
|
@ -25,38 +25,17 @@ const pkgCamelisedName = camelise(pkgName)
|
||||||
const input = path.join(srcDir, 'js', 'index.js')
|
const input = path.join(srcDir, 'js', 'index.js')
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
{ // ESM native module
|
{ // Browser
|
||||||
input: input,
|
input: input,
|
||||||
output: [
|
output: [
|
||||||
{
|
{
|
||||||
file: path.join(rootDir, pkgJson.browser),
|
file: path.join(rootDir, pkgJson.browser),
|
||||||
format: 'esm'
|
format: 'es'
|
||||||
}
|
|
||||||
],
|
|
||||||
plugins: [
|
|
||||||
replace({
|
|
||||||
'process.browser': true
|
|
||||||
})
|
|
||||||
],
|
|
||||||
external: ['bigint-mod-arith']
|
|
||||||
},
|
|
||||||
{ // Browser bundles
|
|
||||||
input: input,
|
|
||||||
output: [
|
|
||||||
{
|
|
||||||
file: path.join(dstDir, 'index.browser.bundle.js'),
|
|
||||||
format: 'iife',
|
|
||||||
name: pkgCamelisedName,
|
|
||||||
plugins: [
|
|
||||||
terser()
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
file: path.join(dstDir, 'index.browser.bundle.min.mod.js'),
|
file: path.join(dstDir, 'index.browser.bundle.iife.js'),
|
||||||
format: 'es',
|
format: 'iife',
|
||||||
plugins: [
|
name: pkgCamelisedName
|
||||||
terser()
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
file: path.join(dstDir, 'index.browser.bundle.mod.js'),
|
file: path.join(dstDir, 'index.browser.bundle.mod.js'),
|
||||||
|
@ -69,20 +48,26 @@ module.exports = [
|
||||||
}),
|
}),
|
||||||
resolve({
|
resolve({
|
||||||
browser: true
|
browser: true
|
||||||
|
// ignore: ['index.browser.mod.js']
|
||||||
|
}),
|
||||||
|
terser({
|
||||||
|
exclude: ['index.browser.mod.js']
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{ // Node
|
{ // Node
|
||||||
input: input,
|
input: input,
|
||||||
plugins: [
|
|
||||||
replace({
|
|
||||||
'process.browser': false
|
|
||||||
})
|
|
||||||
],
|
|
||||||
output: {
|
output: {
|
||||||
file: path.join(dstDir, 'index.node.js'),
|
file: path.join(dstDir, 'index.node.js'),
|
||||||
format: 'cjs'
|
format: 'cjs'
|
||||||
},
|
},
|
||||||
external: ['bigint-mod-arith']
|
plugins: [
|
||||||
|
replace({
|
||||||
|
'process.browser': false
|
||||||
|
}),
|
||||||
|
resolve({
|
||||||
|
browser: true
|
||||||
|
})
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -21,7 +21,7 @@ const dstDir = path.join(rootDir, pkgJson.directories.test, 'browser')
|
||||||
const dstFileName = path.join(dstDir, 'index.html')
|
const dstFileName = path.join(dstDir, 'index.html')
|
||||||
|
|
||||||
const template = fs.readFileSync(templatePath, 'utf-8')
|
const template = fs.readFileSync(templatePath, 'utf-8')
|
||||||
const bundleFile = path.join(rootDir, pkgJson.directories.lib, 'index.browser.bundle.min.mod.js')
|
const bundleFile = path.join(rootDir, pkgJson.directories.lib, 'index.browser.bundle.mod.js')
|
||||||
const testsJs = `
|
const testsJs = `
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import * as _pkg from '${path.relative(templatePath, bundleFile)}'
|
import * as _pkg from '${path.relative(templatePath, bundleFile)}'
|
||||||
|
|
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
|
@ -1,5 +1,213 @@
|
||||||
import { bitLength, eGcd, modInv, modPow, toZn } from 'bigint-mod-arith'
|
/**
|
||||||
export { abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn } from 'bigint-mod-arith'
|
* Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
*
|
||||||
|
* @returns {bigint} the absolute value of a
|
||||||
|
*/
|
||||||
|
function abs (a) {
|
||||||
|
a = BigInt(a)
|
||||||
|
return (a >= 0n) ? a : -a
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the bitlength of a number
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @returns {number} - the bit length
|
||||||
|
*/
|
||||||
|
function bitLength (a) {
|
||||||
|
a = BigInt(a)
|
||||||
|
if (a === 1n) { return 1 }
|
||||||
|
let bits = 1
|
||||||
|
do {
|
||||||
|
bits++
|
||||||
|
} while ((a >>= 1n) > 1n)
|
||||||
|
return bits
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
||||||
|
* @property {bigint} g
|
||||||
|
* @property {bigint} x
|
||||||
|
* @property {bigint} y
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* 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).
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {egcdReturn} A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
||||||
|
*/
|
||||||
|
function eGcd (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
if (a <= 0n | b <= 0n) { return NaN } // a and b MUST be positive
|
||||||
|
|
||||||
|
let x = 0n
|
||||||
|
let y = 1n
|
||||||
|
let u = 1n
|
||||||
|
let v = 0n
|
||||||
|
|
||||||
|
while (a !== 0n) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Greatest-common divisor of two integers based on the iterative binary algorithm.
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} The greatest common divisor of a and b
|
||||||
|
*/
|
||||||
|
function gcd (a, b) {
|
||||||
|
a = abs(a)
|
||||||
|
b = abs(b)
|
||||||
|
if (a === 0n) { return b } else if (b === 0n) { return a }
|
||||||
|
|
||||||
|
let shift = 0n
|
||||||
|
while (!((a | b) & 1n)) {
|
||||||
|
a >>= 1n
|
||||||
|
b >>= 1n
|
||||||
|
shift++
|
||||||
|
}
|
||||||
|
while (!(a & 1n)) a >>= 1n
|
||||||
|
do {
|
||||||
|
while (!(b & 1n)) b >>= 1n
|
||||||
|
if (a > b) {
|
||||||
|
const x = a
|
||||||
|
a = b
|
||||||
|
b = x
|
||||||
|
}
|
||||||
|
b -= a
|
||||||
|
} while (b)
|
||||||
|
|
||||||
|
// rescale
|
||||||
|
return a << shift
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The least common multiple computed as abs(a*b)/gcd(a,b)
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} The least common multiple of a and b
|
||||||
|
*/
|
||||||
|
function lcm (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
if (a === 0n && b === 0n) { return 0n }
|
||||||
|
return abs(a * b) / gcd(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} maximum of numbers a and b
|
||||||
|
*/
|
||||||
|
function max (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
return (a >= b) ? a : b
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} minimum of numbers a and b
|
||||||
|
*/
|
||||||
|
function min (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
return (a >= b) ? b : a
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modular inverse.
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a The number to find an inverse for
|
||||||
|
* @param {number|bigint} n The modulo
|
||||||
|
*
|
||||||
|
* @returns {bigint} the inverse modulo n or NaN if it does not exist
|
||||||
|
*/
|
||||||
|
function modInv (a, n) {
|
||||||
|
const egcd = eGcd(toZn(a, n), n)
|
||||||
|
if (egcd.b !== 1n) {
|
||||||
|
return NaN // modular inverse does not exist
|
||||||
|
} else {
|
||||||
|
return toZn(egcd.x, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
||||||
|
*
|
||||||
|
* @param {number|bigint} b base
|
||||||
|
* @param {number|bigint} e exponent
|
||||||
|
* @param {number|bigint} n modulo
|
||||||
|
*
|
||||||
|
* @returns {bigint} b**e mod n
|
||||||
|
*/
|
||||||
|
function modPow (b, e, n) {
|
||||||
|
n = BigInt(n)
|
||||||
|
if (n === 0n) { return NaN } else if (n === 1n) { return 0n }
|
||||||
|
|
||||||
|
b = toZn(b, n)
|
||||||
|
|
||||||
|
e = BigInt(e)
|
||||||
|
if (e < 0n) {
|
||||||
|
return modInv(modPow(b, abs(e), n), n)
|
||||||
|
}
|
||||||
|
|
||||||
|
let r = 1n
|
||||||
|
while (e > 0) {
|
||||||
|
if ((e % 2n) === 1n) {
|
||||||
|
r = (r * b) % n
|
||||||
|
}
|
||||||
|
e = e / 2n
|
||||||
|
b = b ** 2n % n
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the smallest positive element that is congruent to a in modulo n
|
||||||
|
* @param {number|bigint} a An integer
|
||||||
|
* @param {number|bigint} n The modulo
|
||||||
|
*
|
||||||
|
* @returns {bigint} The smallest positive representation of a in modulo n
|
||||||
|
*/
|
||||||
|
function toZn (a, n) {
|
||||||
|
n = BigInt(n)
|
||||||
|
if (n <= 0) { return NaN }
|
||||||
|
|
||||||
|
a = BigInt(a) % n
|
||||||
|
return (a < 0) ? a + n : a
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The test first tries if any of the first 250 small primes are a factor of the input number and then passes several
|
* The test first tries if any of the first 250 small primes are a factor of the input number and then passes several
|
||||||
|
@ -611,4 +819,4 @@ function _isProbablyPrime (w, iterations = 16) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export { isProbablyPrime, prime, primeSync, randBetween, randBits, randBitsSync, randBytes, randBytesSync }
|
export { abs, bitLength, eGcd, gcd, isProbablyPrime, lcm, max, min, modInv, modPow, prime, primeSync, randBetween, randBits, randBitsSync, randBytes, randBytesSync, toZn }
|
||||||
|
|
|
@ -2,7 +2,216 @@
|
||||||
|
|
||||||
Object.defineProperty(exports, '__esModule', { value: true })
|
Object.defineProperty(exports, '__esModule', { value: true })
|
||||||
|
|
||||||
var bigintModArith = require('bigint-mod-arith')
|
/**
|
||||||
|
* Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
*
|
||||||
|
* @returns {bigint} the absolute value of a
|
||||||
|
*/
|
||||||
|
function abs (a) {
|
||||||
|
a = BigInt(a)
|
||||||
|
return (a >= 0n) ? a : -a
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the bitlength of a number
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @returns {number} - the bit length
|
||||||
|
*/
|
||||||
|
function bitLength (a) {
|
||||||
|
a = BigInt(a)
|
||||||
|
if (a === 1n) { return 1 }
|
||||||
|
let bits = 1
|
||||||
|
do {
|
||||||
|
bits++
|
||||||
|
} while ((a >>= 1n) > 1n)
|
||||||
|
return bits
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
||||||
|
* @property {bigint} g
|
||||||
|
* @property {bigint} x
|
||||||
|
* @property {bigint} y
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* 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).
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {egcdReturn} A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
||||||
|
*/
|
||||||
|
function eGcd (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
if (a <= 0n | b <= 0n) { return NaN } // a and b MUST be positive
|
||||||
|
|
||||||
|
let x = 0n
|
||||||
|
let y = 1n
|
||||||
|
let u = 1n
|
||||||
|
let v = 0n
|
||||||
|
|
||||||
|
while (a !== 0n) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Greatest-common divisor of two integers based on the iterative binary algorithm.
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} The greatest common divisor of a and b
|
||||||
|
*/
|
||||||
|
function gcd (a, b) {
|
||||||
|
a = abs(a)
|
||||||
|
b = abs(b)
|
||||||
|
if (a === 0n) { return b } else if (b === 0n) { return a }
|
||||||
|
|
||||||
|
let shift = 0n
|
||||||
|
while (!((a | b) & 1n)) {
|
||||||
|
a >>= 1n
|
||||||
|
b >>= 1n
|
||||||
|
shift++
|
||||||
|
}
|
||||||
|
while (!(a & 1n)) a >>= 1n
|
||||||
|
do {
|
||||||
|
while (!(b & 1n)) b >>= 1n
|
||||||
|
if (a > b) {
|
||||||
|
const x = a
|
||||||
|
a = b
|
||||||
|
b = x
|
||||||
|
}
|
||||||
|
b -= a
|
||||||
|
} while (b)
|
||||||
|
|
||||||
|
// rescale
|
||||||
|
return a << shift
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The least common multiple computed as abs(a*b)/gcd(a,b)
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} The least common multiple of a and b
|
||||||
|
*/
|
||||||
|
function lcm (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
if (a === 0n && b === 0n) { return 0n }
|
||||||
|
return abs(a * b) / gcd(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} maximum of numbers a and b
|
||||||
|
*/
|
||||||
|
function max (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
return (a >= b) ? a : b
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a
|
||||||
|
* @param {number|bigint} b
|
||||||
|
*
|
||||||
|
* @returns {bigint} minimum of numbers a and b
|
||||||
|
*/
|
||||||
|
function min (a, b) {
|
||||||
|
a = BigInt(a)
|
||||||
|
b = BigInt(b)
|
||||||
|
return (a >= b) ? b : a
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modular inverse.
|
||||||
|
*
|
||||||
|
* @param {number|bigint} a The number to find an inverse for
|
||||||
|
* @param {number|bigint} n The modulo
|
||||||
|
*
|
||||||
|
* @returns {bigint} the inverse modulo n or NaN if it does not exist
|
||||||
|
*/
|
||||||
|
function modInv (a, n) {
|
||||||
|
const egcd = eGcd(toZn(a, n), n)
|
||||||
|
if (egcd.b !== 1n) {
|
||||||
|
return NaN // modular inverse does not exist
|
||||||
|
} else {
|
||||||
|
return toZn(egcd.x, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
||||||
|
*
|
||||||
|
* @param {number|bigint} b base
|
||||||
|
* @param {number|bigint} e exponent
|
||||||
|
* @param {number|bigint} n modulo
|
||||||
|
*
|
||||||
|
* @returns {bigint} b**e mod n
|
||||||
|
*/
|
||||||
|
function modPow (b, e, n) {
|
||||||
|
n = BigInt(n)
|
||||||
|
if (n === 0n) { return NaN } else if (n === 1n) { return 0n }
|
||||||
|
|
||||||
|
b = toZn(b, n)
|
||||||
|
|
||||||
|
e = BigInt(e)
|
||||||
|
if (e < 0n) {
|
||||||
|
return modInv(modPow(b, abs(e), n), n)
|
||||||
|
}
|
||||||
|
|
||||||
|
let r = 1n
|
||||||
|
while (e > 0) {
|
||||||
|
if ((e % 2n) === 1n) {
|
||||||
|
r = (r * b) % n
|
||||||
|
}
|
||||||
|
e = e / 2n
|
||||||
|
b = b ** 2n % n
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the smallest positive element that is congruent to a in modulo n
|
||||||
|
* @param {number|bigint} a An integer
|
||||||
|
* @param {number|bigint} n The modulo
|
||||||
|
*
|
||||||
|
* @returns {bigint} The smallest positive representation of a in modulo n
|
||||||
|
*/
|
||||||
|
function toZn (a, n) {
|
||||||
|
n = BigInt(n)
|
||||||
|
if (n <= 0) { return NaN }
|
||||||
|
|
||||||
|
a = BigInt(a) % n
|
||||||
|
return (a < 0) ? a + n : a
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The test first tries if any of the first 250 small primes are a factor of the input number and then passes several
|
* The test first tries if any of the first 250 small primes are a factor of the input number and then passes several
|
||||||
|
@ -146,7 +355,7 @@ function primeSync (bitLength, iterations = 16) {
|
||||||
function randBetween (max, min = 1n) {
|
function randBetween (max, min = 1n) {
|
||||||
if (max <= min) throw new Error('max must be > min')
|
if (max <= min) throw new Error('max must be > min')
|
||||||
const interval = max - min
|
const interval = max - min
|
||||||
const bitLen = bigintModArith.bitLength(interval)
|
const bitLen = bitLength(interval)
|
||||||
let rnd
|
let rnd
|
||||||
do {
|
do {
|
||||||
const buf = randBitsSync(bitLen)
|
const buf = randBitsSync(bitLen)
|
||||||
|
@ -589,11 +798,11 @@ function _isProbablyPrime (w, iterations = 16) {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const b = randBetween(d, 2n)
|
const b = randBetween(d, 2n)
|
||||||
let z = bigintModArith.modPow(b, m, w)
|
let z = modPow(b, m, w)
|
||||||
if (z === 1n || z === d) { continue }
|
if (z === 1n || z === d) { continue }
|
||||||
let j = 1
|
let j = 1
|
||||||
while (j < a) {
|
while (j < a) {
|
||||||
z = bigintModArith.modPow(z, 2n, w)
|
z = modPow(z, 2n, w)
|
||||||
if (z === d) { break }
|
if (z === d) { break }
|
||||||
if (z === 1n) { return false }
|
if (z === 1n) { return false }
|
||||||
j++
|
j++
|
||||||
|
@ -638,67 +847,16 @@ if (_useWorkers) { // node.js with support for workers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(exports, 'abs', {
|
exports.abs = abs
|
||||||
enumerable: true,
|
exports.bitLength = bitLength
|
||||||
get: function () {
|
exports.eGcd = eGcd
|
||||||
return bigintModArith.abs
|
exports.gcd = gcd
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'bitLength', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.bitLength
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'eGcd', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.eGcd
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'gcd', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.gcd
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'lcm', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.lcm
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'max', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.max
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'min', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.min
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'modInv', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.modInv
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'modPow', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.modPow
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Object.defineProperty(exports, 'toZn', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return bigintModArith.toZn
|
|
||||||
}
|
|
||||||
})
|
|
||||||
exports.isProbablyPrime = isProbablyPrime
|
exports.isProbablyPrime = isProbablyPrime
|
||||||
|
exports.lcm = lcm
|
||||||
|
exports.max = max
|
||||||
|
exports.min = min
|
||||||
|
exports.modInv = modInv
|
||||||
|
exports.modPow = modPow
|
||||||
exports.prime = prime
|
exports.prime = prime
|
||||||
exports.primeSync = primeSync
|
exports.primeSync = primeSync
|
||||||
exports.randBetween = randBetween
|
exports.randBetween = randBetween
|
||||||
|
@ -706,3 +864,4 @@ exports.randBits = randBits
|
||||||
exports.randBitsSync = randBitsSync
|
exports.randBitsSync = randBitsSync
|
||||||
exports.randBytes = randBytes
|
exports.randBytes = randBytes
|
||||||
exports.randBytesSync = randBytesSync
|
exports.randBytesSync = randBytesSync
|
||||||
|
exports.toZn = toZn
|
||||||
|
|
|
@ -14,9 +14,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@babel/helper-validator-identifier": {
|
"@babel/helper-validator-identifier": {
|
||||||
"version": "7.9.0",
|
"version": "7.9.5",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.0.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.5.tgz",
|
||||||
"integrity": "sha512-6G8bQKjOh+of4PV/ThDm/rRqlU7+IGoJuofpagU5GlEl29Vv0RGqqt86ZGRV8ZuSOY3o+8yXl5y782SMcG7SHw==",
|
"integrity": "sha512-/8arLKUFq882w4tWGj9JYzRpAlZgiWUJ+dtteNTDqrRBz9Iguck9Rn3ykuBDoUwh2TO4tSAJlrxDUOXWklJe4g==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"@babel/highlight": {
|
"@babel/highlight": {
|
||||||
|
@ -264,7 +264,8 @@
|
||||||
"bigint-mod-arith": {
|
"bigint-mod-arith": {
|
||||||
"version": "2.0.4",
|
"version": "2.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/bigint-mod-arith/-/bigint-mod-arith-2.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/bigint-mod-arith/-/bigint-mod-arith-2.0.4.tgz",
|
||||||
"integrity": "sha512-sQyEj0XMU4hai3G/+uLwohrGjfUn8rGVWAYnnlFrQhw8YjilptTyJrx7NMimKwQvYr2eXGWGDlYVL3wfE4GIRg=="
|
"integrity": "sha512-sQyEj0XMU4hai3G/+uLwohrGjfUn8rGVWAYnnlFrQhw8YjilptTyJrx7NMimKwQvYr2eXGWGDlYVL3wfE4GIRg==",
|
||||||
|
"dev": true
|
||||||
},
|
},
|
||||||
"binary-extensions": {
|
"binary-extensions": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
|
@ -757,9 +758,9 @@
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"entities": {
|
"entities": {
|
||||||
"version": "1.1.2",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/entities/-/entities-2.0.0.tgz",
|
||||||
"integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==",
|
"integrity": "sha512-D9f7V0JSRwIxlRI2mjMqufDrRDnx8p+eEOz7aUM9SuvF8gsBzra0/6tbjl1m8eQHrZlYj6PxqE00hZ1SAIKPLw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"error-ex": {
|
"error-ex": {
|
||||||
|
@ -876,6 +877,15 @@
|
||||||
"is-extglob": "^2.1.1"
|
"is-extglob": "^2.1.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"mkdirp": {
|
||||||
|
"version": "0.5.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
|
||||||
|
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"minimist": "^1.2.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
"semver": {
|
"semver": {
|
||||||
"version": "6.3.0",
|
"version": "6.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
|
||||||
|
@ -1850,25 +1860,33 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"jsdoc": {
|
"jsdoc": {
|
||||||
"version": "3.6.3",
|
"version": "3.6.4",
|
||||||
"resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-3.6.3.tgz",
|
"resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-3.6.4.tgz",
|
||||||
"integrity": "sha512-Yf1ZKA3r9nvtMWHO1kEuMZTlHOF8uoQ0vyo5eH7SQy5YeIiHM+B0DgKnn+X6y6KDYZcF7G2SPkKF+JORCXWE/A==",
|
"integrity": "sha512-3G9d37VHv7MFdheviDCjUfQoIjdv4TC5zTTf5G9VODLtOnVS6La1eoYBDlbWfsRT3/Xo+j2MIqki2EV12BZfwA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@babel/parser": "^7.4.4",
|
"@babel/parser": "^7.9.4",
|
||||||
"bluebird": "^3.5.4",
|
"bluebird": "^3.7.2",
|
||||||
"catharsis": "^0.8.11",
|
"catharsis": "^0.8.11",
|
||||||
"escape-string-regexp": "^2.0.0",
|
"escape-string-regexp": "^2.0.0",
|
||||||
"js2xmlparser": "^4.0.0",
|
"js2xmlparser": "^4.0.1",
|
||||||
"klaw": "^3.0.0",
|
"klaw": "^3.0.0",
|
||||||
"markdown-it": "^8.4.2",
|
"markdown-it": "^10.0.0",
|
||||||
"markdown-it-anchor": "^5.0.2",
|
"markdown-it-anchor": "^5.2.7",
|
||||||
"marked": "^0.7.0",
|
"marked": "^0.8.2",
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^1.0.4",
|
||||||
"requizzle": "^0.2.3",
|
"requizzle": "^0.2.3",
|
||||||
"strip-json-comments": "^3.0.1",
|
"strip-json-comments": "^3.1.0",
|
||||||
"taffydb": "2.6.2",
|
"taffydb": "2.6.2",
|
||||||
"underscore": "~1.9.1"
|
"underscore": "~1.10.2"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"marked": {
|
||||||
|
"version": "0.8.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/marked/-/marked-0.8.2.tgz",
|
||||||
|
"integrity": "sha512-EGwzEeCcLniFX51DhTpmTom+dSA/MG/OBUDjnWtHbEnjAH180VzUeAw+oE4+Zv+CoYBWyRlYOTR0N8SO9R1PVw==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"jsdoc-api": {
|
"jsdoc-api": {
|
||||||
|
@ -2082,13 +2100,13 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"markdown-it": {
|
"markdown-it": {
|
||||||
"version": "8.4.2",
|
"version": "10.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-8.4.2.tgz",
|
"resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-10.0.0.tgz",
|
||||||
"integrity": "sha512-GcRz3AWTqSUphY3vsUqQSFMbgR38a4Lh3GWlHRh/7MRwz8mcu9n2IO7HOh+bXHrR9kOPDl5RNCaEsrneb+xhHQ==",
|
"integrity": "sha512-YWOP1j7UbDNz+TumYP1kpwnP0aEa711cJjrAQrzd0UXlbJfc5aAq0F/PZHjiioqDC1NKgvIMX+o+9Bk7yuM2dg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"argparse": "^1.0.7",
|
"argparse": "^1.0.7",
|
||||||
"entities": "~1.1.1",
|
"entities": "~2.0.0",
|
||||||
"linkify-it": "^2.0.0",
|
"linkify-it": "^2.0.0",
|
||||||
"mdurl": "^1.0.1",
|
"mdurl": "^1.0.1",
|
||||||
"uc.micro": "^1.0.5"
|
"uc.micro": "^1.0.5"
|
||||||
|
@ -2160,13 +2178,10 @@
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"mkdirp": {
|
"mkdirp": {
|
||||||
"version": "0.5.5",
|
"version": "1.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
|
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
|
||||||
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
|
"integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==",
|
||||||
"dev": true,
|
"dev": true
|
||||||
"requires": {
|
|
||||||
"minimist": "^1.2.5"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"mkdirp2": {
|
"mkdirp2": {
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
|
@ -2953,9 +2968,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"rollup": {
|
"rollup": {
|
||||||
"version": "2.3.3",
|
"version": "2.3.4",
|
||||||
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.3.3.tgz",
|
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.3.4.tgz",
|
||||||
"integrity": "sha512-uJ9VNWk80mb4wDCSfd1AyHoSc9TrWbkZtnO6wbsMTp9muSWkT26Dvc99MX1yGCOTvUN1Skw/KpFzKdUDuZKTXA==",
|
"integrity": "sha512-8U9x54RCVhrUEV1zon4Pp8kokg1HM0fwzf5vkwe2/rOfyTClarx5e27kFlaoZ7ofJiazWkNQ+dgdG4HuZxkQ9A==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"fsevents": "~2.1.2"
|
"fsevents": "~2.1.2"
|
||||||
|
@ -3501,9 +3516,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"underscore": {
|
"underscore": {
|
||||||
"version": "1.9.2",
|
"version": "1.10.2",
|
||||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.2.tgz",
|
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.10.2.tgz",
|
||||||
"integrity": "sha512-D39qtimx0c1fI3ya1Lnhk3E9nONswSKhnffBI0gME9C99fYOkNi04xs8K6pePLhvl1frbDemkaBQ5ikWllR2HQ==",
|
"integrity": "sha512-N4P+Q/BuyuEKFJ43B9gYuOj4TQUHXX+j2FqguVOpjkssLUUrnJofCcBccJSCoeturDoZU6GorDTHSvUDlSQbTg==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"uniq": {
|
"uniq": {
|
||||||
|
@ -3641,6 +3656,17 @@
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"mkdirp": "^0.5.1"
|
"mkdirp": "^0.5.1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"mkdirp": {
|
||||||
|
"version": "0.5.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
|
||||||
|
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"minimist": "^1.2.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"xmlcreate": {
|
"xmlcreate": {
|
||||||
|
|
10
package.json
10
package.json
|
@ -52,8 +52,8 @@
|
||||||
],
|
],
|
||||||
"ignore": [
|
"ignore": [
|
||||||
"/test/browser/",
|
"/test/browser/",
|
||||||
"/lib/index.browser.bundle.js",
|
"/lib/index.browser.bundle.iife.js",
|
||||||
"/lib/index.browser.bundle.min.mod.js"
|
"/lib/index.browser.bundle.mod.js"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -61,17 +61,17 @@
|
||||||
"@rollup/plugin-multi-entry": "^3.0.0",
|
"@rollup/plugin-multi-entry": "^3.0.0",
|
||||||
"@rollup/plugin-node-resolve": "^7.1.1",
|
"@rollup/plugin-node-resolve": "^7.1.1",
|
||||||
"@rollup/plugin-replace": "^2.3.1",
|
"@rollup/plugin-replace": "^2.3.1",
|
||||||
|
"bigint-mod-arith": "^2.0.4",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"jsdoc-to-markdown": "^5.0.3",
|
"jsdoc-to-markdown": "^5.0.3",
|
||||||
"mocha": "^7.1.1",
|
"mocha": "^7.1.1",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"rollup": "^2.3.3",
|
"rollup": "^2.3.4",
|
||||||
"rollup-plugin-terser": "^5.3.0",
|
"rollup-plugin-terser": "^5.3.0",
|
||||||
"standard": "^14.3.3",
|
"standard": "^14.3.3",
|
||||||
"typescript": "^3.8.3"
|
"typescript": "^3.8.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node": "^13.11.0",
|
"@types/node": ">=10"
|
||||||
"bigint-mod-arith": "^2.0.4"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ npm install bigint-crypto-utils
|
||||||
|
|
||||||
NPM installation defaults to the 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 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.min.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.iife.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
|
## Usage examples
|
||||||
|
|
||||||
|
@ -29,11 +29,12 @@ Import your module as :
|
||||||
const bigintCryptoUtils = require('bigint-crypto-utils')
|
const bigintCryptoUtils = require('bigint-crypto-utils')
|
||||||
... // your code here
|
... // your code here
|
||||||
```
|
```
|
||||||
- JavaScript native project
|
- JavaScript native or TypeScript project
|
||||||
```javascript
|
```javascript
|
||||||
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
||||||
... // your code here
|
... // your code here
|
||||||
```
|
```
|
||||||
|
> 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`.
|
||||||
- JavaScript native browser ES6 mod
|
- JavaScript native browser ES6 mod
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
|
@ -43,17 +44,17 @@ Import your module as :
|
||||||
```
|
```
|
||||||
- JavaScript native browser IIFE
|
- JavaScript native browser IIFE
|
||||||
```html
|
```html
|
||||||
<script src="../../lib/index.browser.bundle.js"></script> <!-- Use you actual path to the browser bundle -->
|
<head>
|
||||||
<script>
|
...
|
||||||
... // your code here
|
<script src="../../lib/index.browser.bundle.js"></script> <!-- Use you actual path to the browser bundle -->
|
||||||
</script>
|
</head>
|
||||||
|
<body>
|
||||||
|
...
|
||||||
|
<script>
|
||||||
|
... // your code here
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
```
|
```
|
||||||
- TypeScript
|
|
||||||
```typescript
|
|
||||||
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
|
||||||
... // your code here
|
|
||||||
```
|
|
||||||
> 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:
|
And you could use it like in the following:
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<script>mocha.setup('bdd'); mocha.setup({ timeout: 90000 });</script>
|
<script>mocha.setup('bdd'); mocha.setup({ timeout: 90000 });</script>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import * as _pkg from '../../../lib/index.browser.bundle.min.mod.js'
|
import * as _pkg from '../../../lib/index.browser.bundle.mod.js'
|
||||||
window._pkg = _pkg;
|
window._pkg = _pkg;
|
||||||
import './tests.js';
|
import './tests.js';
|
||||||
mocha.run();
|
mocha.run();
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<div id="syncPrime">result of primeSync() will show up here</div>
|
<div id="syncPrime">result of primeSync() will show up here</div>
|
||||||
<div id="asyncPrime">result of prime() 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 src="../../lib/index.browser.bundle.iife.js"></script>
|
||||||
<script>
|
<script>
|
||||||
(async () => {
|
(async () => {
|
||||||
document.getElementById(
|
document.getElementById(
|
||||||
|
|
Loading…
Reference in New Issue