bigint-mod-arith
Advanced tools
Comparing version 1.3.1 to 2.0.0
{ | ||
"name": "bigint-mod-arith", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"description": "Some additional common functions for modular arithmetics using native JS (stage 3) implementation of BigInt", | ||
@@ -23,21 +23,49 @@ "keywords": [ | ||
"repository": "github:juanelas/bigint-mod-arith", | ||
"main": "./dist/bigint-mod-arith-latest.node.js", | ||
"browser": "./dist/bigint-mod-arith-latest.browser.mod.js", | ||
"main": "./lib/index.node.js", | ||
"browser": "./lib/index.browser.mod.js", | ||
"types": "./types/index.d.ts", | ||
"directories": { | ||
"build": "./build", | ||
"dist": "./dist", | ||
"src": "./src" | ||
"lib": "./lib", | ||
"src": "./src", | ||
"test": "./test", | ||
"types": "./types" | ||
}, | ||
"scripts": { | ||
"build": "node build/build.rollup.js", | ||
"build:docs": "jsdoc2md --template=README.hbs --files ./src/main.js > README.md", | ||
"build:all": "npm run build && npm run build:docs", | ||
"prepublishOnly": "npm run build && npm run build:docs" | ||
"test": "mocha", | ||
"build:js": "rollup -c build/rollup.config.js", | ||
"build:standard": "standard --fix", | ||
"build:browserTests": "rollup -c build/rollup.tests.config.js", | ||
"build:docs": "jsdoc2md --template=./src/doc/readme-template.md --files ./lib/index.browser.mod.js -d 3 -g none > README.md", | ||
"build:dts": "node build/build.dts.js", | ||
"build": "run-s build:**", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"standard": { | ||
"env": [ | ||
"mocha" | ||
], | ||
"globals": [ | ||
"BigInt" | ||
], | ||
"ignore": [ | ||
"/test/browser/", | ||
"/lib/index.browser.bundle.js", | ||
"/lib/index.browser.bundle.mod.js" | ||
] | ||
}, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"rollup": "^1.10.1", | ||
"rollup-plugin-babel-minify": "^8.0.0", | ||
"rollup-plugin-commonjs": "^9.3.4" | ||
"@rollup/plugin-commonjs": "^11.0.2", | ||
"@rollup/plugin-multi-entry": "^3.0.0", | ||
"@rollup/plugin-node-resolve": "^7.1.1", | ||
"@rollup/plugin-replace": "^2.3.1", | ||
"chai": "^4.2.0", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
"mocha": "^7.1.1", | ||
"npm-run-all": "^4.1.5", | ||
"rollup": "^2.3.3", | ||
"rollup-plugin-terser": "^5.3.0", | ||
"standard": "^14.3.3", | ||
"typescript": "^3.8.3" | ||
} | ||
} |
187
README.md
@@ -0,17 +1,15 @@ | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
# bigint-mod-arith | ||
**IMPORTANT! This package has been superseded by [bigint-crypto-utils](https://github.com/juanelas/bigint-crypto-utils)**. Please install that package instead. | ||
Some extra functions to work with modular arithmetic using native JS ([ES-2020](https://tc39.es/ecma262/#sec-bigint-objects)) implementation of BigInt. 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). | ||
Some extra functions to work with modular arithmetics using native JS (stage 3) implementation of BigInt. 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). | ||
> 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). | ||
If you are also looking for a cryptographically-secure random generator and for strong probable primes (generation and testing), you should consider moving to [bigint-crypto-utils](https://github.com/juanelas/bigint-crypto-utils) | ||
## Installation | ||
_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)._ | ||
bigint-mod-arith is distributed for [web browsers and/or webviews supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) as an ES6 module or an IIFE file; and for Node.js (>=10.4.0), as a CJS module. | ||
## Installation | ||
bigint-mod-arith is distributed for [web browsers and/or webviews supporting | ||
BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) | ||
as an ES6 module or an IIFE file; and for Node.js (>=10.4.0), as a CJS module. | ||
bigint-mod-arith can be imported to your project with `npm`: | ||
bigint-mod-arith can be imported to your project with `npm`: | ||
```bash | ||
@@ -22,10 +20,41 @@ npm install bigint-mod-arith | ||
For web browsers, you can also directly download the minimised version of the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-mod-arith/master/dist/bigint-mod-arith-latest.browser.min.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-mod-arith/master/dist/bigint-mod-arith-latest.browser.mod.min.js) from GitHub. | ||
For web browsers, you can also directly download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/bigint-mod-arith/master/lib/index.browser.bundle.js) or the [ES6 bundle module](https://raw.githubusercontent.com/juanelas/bigint-mod-arith/master/lib/index.browser.bundle.mod.js) from GitHub. | ||
## Usage example | ||
With node js: | ||
Import your module as : | ||
- Node.js | ||
```javascript | ||
const bigintCryptoUtils = require('bigint-mod-arith') | ||
... // your code here | ||
``` | ||
- JavaScript native project | ||
```javascript | ||
import * as bigintCryptoUtils from 'bigint-mod-arith' | ||
... // your code here | ||
``` | ||
- Javascript native browser ES6 mod | ||
```html | ||
<script type="module"> | ||
import * as bigintCryptoUtils from 'lib/index.browser.bundle.mod.js' // Use you actual path to the broser mod bundle | ||
... // your code here | ||
</script> | ||
import as bcu from 'bigint-mod-arith' | ||
... // your code here | ||
``` | ||
- JavaScript native browser IIFE | ||
```html | ||
<script src="../../lib/index.browser.bundle.js"></script> | ||
<script> | ||
... // your code here | ||
</script> | ||
- TypeScript | ||
```typescript | ||
import * as bigintCryptoUtils from 'bigint-mod-arith' | ||
... // 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 | ||
const bigintModArith = require('bigint-mod-arith'); | ||
/* Stage 3 BigInts with value 666 can be declared as BigInt('666') | ||
@@ -37,74 +66,35 @@ or the shorter new no-so-linter-friendly syntax 666n. | ||
*/ | ||
let a = BigInt('5'); | ||
let b = BigInt('2'); | ||
let n = BigInt('19'); | ||
const a = BigInt('5') | ||
const b = BigInt('2') | ||
const n = BigInt('19') | ||
console.log(bigintCryptoUtils.modPow(a, b, n)); // prints 6 | ||
console.log(bigintCryptoUtils.modPow(a, b, n)) // prints 6 | ||
console.log(bigintCryptoUtils.modInv(BigInt('2'), BigInt('5'))); // prints 3 | ||
console.log(bigintCryptoUtils.modInv(BigInt('2'), BigInt('5'))) // prints 3 | ||
console.log(bigintCryptoUtils.modInv(BigInt('3'), BigInt('5'))); // prints 2 | ||
``` | ||
console.log(bigintCryptoUtils.modInv(BigInt('3'), BigInt('5'))) // prints 2 | ||
From a browser, you can just load the module in a html page as: | ||
```html | ||
<script type="module"> | ||
import * as bigintModArith from 'bigint-mod-arith-latest.browser.mod.min.js'; | ||
let a = BigInt('5'); | ||
let b = BigInt('2'); | ||
let n = BigInt('19'); | ||
console.log(bigintModArith.modPow(a, b, n)); // prints 6 | ||
console.log(bigintModArith.modInv(BigInt('2'), BigInt('5'))); // prints 3 | ||
console.log(bigintModArith.modInv(BigInt('3'), BigInt('5'))); // prints 2 | ||
</script> | ||
``` | ||
# bigint-mod-arith JS Doc | ||
## JS Doc | ||
## Functions | ||
<a name="abs"></a> | ||
<dl> | ||
<dt><a href="#abs">abs(a)</a> ⇒ <code>bigint</code></dt> | ||
<dd><p>Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0</p> | ||
</dd> | ||
<dt><a href="#eGcd">eGcd(a, b)</a> ⇒ <code><a href="#egcdReturn">egcdReturn</a></code></dt> | ||
<dd><p>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).</p> | ||
</dd> | ||
<dt><a href="#gcd">gcd(a, b)</a> ⇒ <code>bigint</code></dt> | ||
<dd><p>Greatest-common divisor of two integers based on the iterative binary algorithm.</p> | ||
</dd> | ||
<dt><a href="#lcm">lcm(a, b)</a> ⇒ <code>bigint</code></dt> | ||
<dd><p>The least common multiple computed as abs(a*b)/gcd(a,b)</p> | ||
</dd> | ||
<dt><a href="#modInv">modInv(a, n)</a> ⇒ <code>bigint</code></dt> | ||
<dd><p>Modular inverse.</p> | ||
</dd> | ||
<dt><a href="#modPow">modPow(a, b, n)</a> ⇒ <code>bigint</code></dt> | ||
<dd><p>Modular exponentiation a**b mod n</p> | ||
</dd> | ||
<dt><a href="#toZn">toZn(a, n)</a> ⇒ <code>bigint</code></dt> | ||
<dd><p>Finds the smallest positive element that is congruent to a in modulo n</p> | ||
</dd> | ||
</dl> | ||
### abs(a) ⇒ <code>bigint</code> | ||
Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 | ||
## Typedefs | ||
**Kind**: global function | ||
**Returns**: <code>bigint</code> - the absolute value of a | ||
<dl> | ||
<dt><a href="#egcdReturn">egcdReturn</a> : <code>Object</code></dt> | ||
<dd><p>A triple (g, x, y), such that ax + by = g = gcd(a, b).</p> | ||
</dd> | ||
</dl> | ||
| Param | Type | | ||
| --- | --- | | ||
| a | <code>number</code> \| <code>bigint</code> | | ||
<a name="abs"></a> | ||
<a name="bitLength"></a> | ||
## abs(a) ⇒ <code>bigint</code> | ||
Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 | ||
### bitLength(a) ⇒ <code>number</code> | ||
Returns the bitlength of a number | ||
**Kind**: global function | ||
**Returns**: <code>bigint</code> - the absolute value of a | ||
**Returns**: <code>number</code> - - the bit length | ||
@@ -117,7 +107,8 @@ | Param | Type | | ||
## eGcd(a, b) ⇒ [<code>egcdReturn</code>](#egcdReturn) | ||
An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. | ||
### eGcd(a, b) ⇒ [<code>egcdReturn</code>](#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). | ||
**Kind**: global function | ||
**Returns**: [<code>egcdReturn</code>](#egcdReturn) - A triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
@@ -131,3 +122,3 @@ | Param | Type | | ||
## gcd(a, b) ⇒ <code>bigint</code> | ||
### gcd(a, b) ⇒ <code>bigint</code> | ||
Greatest-common divisor of two integers based on the iterative binary algorithm. | ||
@@ -145,3 +136,3 @@ | ||
## lcm(a, b) ⇒ <code>bigint</code> | ||
### lcm(a, b) ⇒ <code>bigint</code> | ||
The least common multiple computed as abs(a*b)/gcd(a,b) | ||
@@ -157,9 +148,35 @@ | ||
<a name="max"></a> | ||
### max(a, b) ⇒ <code>bigint</code> | ||
Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b | ||
**Kind**: global function | ||
**Returns**: <code>bigint</code> - maximum of numbers a and b | ||
| Param | Type | | ||
| --- | --- | | ||
| a | <code>number</code> \| <code>bigint</code> | | ||
| b | <code>number</code> \| <code>bigint</code> | | ||
<a name="min"></a> | ||
### min(a, b) ⇒ <code>bigint</code> | ||
Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b | ||
**Kind**: global function | ||
**Returns**: <code>bigint</code> - minimum of numbers a and b | ||
| Param | Type | | ||
| --- | --- | | ||
| a | <code>number</code> \| <code>bigint</code> | | ||
| b | <code>number</code> \| <code>bigint</code> | | ||
<a name="modInv"></a> | ||
## modInv(a, n) ⇒ <code>bigint</code> | ||
### modInv(a, n) ⇒ <code>bigint</code> | ||
Modular inverse. | ||
**Kind**: global function | ||
**Returns**: <code>bigint</code> - the inverse modulo n | ||
**Returns**: <code>bigint</code> - the inverse modulo n or NaN if it does not exist | ||
@@ -173,12 +190,12 @@ | Param | Type | Description | | ||
## modPow(a, b, n) ⇒ <code>bigint</code> | ||
Modular exponentiation a**b mod n | ||
### modPow(b, e, n) ⇒ <code>bigint</code> | ||
Modular exponentiation b**e mod n. Currently using the right-to-left binary method | ||
**Kind**: global function | ||
**Returns**: <code>bigint</code> - a**b mod n | ||
**Returns**: <code>bigint</code> - b**e mod n | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| a | <code>number</code> \| <code>bigint</code> | base | | ||
| b | <code>number</code> \| <code>bigint</code> | exponent | | ||
| b | <code>number</code> \| <code>bigint</code> | base | | ||
| e | <code>number</code> \| <code>bigint</code> | exponent | | ||
| n | <code>number</code> \| <code>bigint</code> | modulo | | ||
@@ -188,3 +205,3 @@ | ||
## toZn(a, n) ⇒ <code>bigint</code> | ||
### toZn(a, n) ⇒ <code>bigint</code> | ||
Finds the smallest positive element that is congruent to a in modulo n | ||
@@ -202,3 +219,3 @@ | ||
## egcdReturn : <code>Object</code> | ||
### egcdReturn : <code>Object</code> | ||
A triple (g, x, y), such that ax + by = g = gcd(a, b). | ||
@@ -215,3 +232,1 @@ | ||
* * * |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
223
1
29712
12
9
519
1