shorter but better readme
This commit is contained in:
parent
0526c330ed
commit
4758f28ef0
108
README.md
108
README.md
|
@ -22,75 +22,77 @@ For web browsers, you can also directly download the [IIFE file](https://raw.git
|
||||||
|
|
||||||
## Usage examples
|
## Usage examples
|
||||||
|
|
||||||
### Node.js:
|
Import your module as :
|
||||||
|
|
||||||
|
- Node.js
|
||||||
|
```javascript
|
||||||
|
const bigintCryptoUtils = require('bigint-crypto-utils')
|
||||||
|
... // your code here
|
||||||
|
```
|
||||||
|
- Javascript native project
|
||||||
|
```javascript
|
||||||
|
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
||||||
|
... // 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-crypto-utils'
|
||||||
|
... // 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-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`.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const bigintCryptoUtils = require("bigint-crypto-utils");
|
|
||||||
|
|
||||||
/* Stage 3 BigInts with value 666 can be declared as BigInt('666')
|
/* Stage 3 BigInts with value 666 can be declared as BigInt('666')
|
||||||
or the shorter new no-so-linter-friendly syntax 666n.
|
or the shorter new no-so-linter-friendly syntax 666n.
|
||||||
Notice that you can also pass a number, e.g. BigInt(666), but it is not
|
Notice that you can also pass a number, e.g. BigInt(666), but it is not
|
||||||
recommended since values over 2**53 - 1 won't be safe but no warning will
|
recommended since values over 2**53 - 1 won't be safe but no warning will
|
||||||
be raised.
|
be raised.
|
||||||
*/
|
*/
|
||||||
let a = BigInt("5");
|
const a = BigInt('5')
|
||||||
let b = BigInt("2");
|
const b = BigInt('2')
|
||||||
let n = BigInt("19");
|
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
|
||||||
|
|
||||||
// Generation of a probable prime of 2048 bits
|
console.log(bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256))) // Prints a cryptographically secure random number between 1 and 2**256 bits.
|
||||||
const prime = await bigintCryptoUtils.prime(2048);
|
|
||||||
|
|
||||||
// Testing if a prime is a probable prime (Miller-Rabin)
|
async function primeTesting () {
|
||||||
if (await bigintCryptoUtils.isProbablyPrime(prime))
|
// Output of a probable prime of 2048 bits
|
||||||
// code if is prime
|
console.log(await bigintCryptoUtils.prime(2048))
|
||||||
|
|
||||||
|
// Testing if a number is a probable prime (Miller-Rabin)
|
||||||
|
const number = 27
|
||||||
|
const isPrime = await bigintCryptoUtils.isProbablyPrime(number)
|
||||||
|
if (isPrime) {
|
||||||
|
console.log(`${number} is prime`)
|
||||||
|
} else {
|
||||||
|
console.log(`${number} is composite`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
primeTesting()
|
||||||
|
|
||||||
// Get a cryptographically secure random number between 1 and 2**256 bits.
|
|
||||||
const rnd = bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256));
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Javascript native from a browser
|
|
||||||
|
|
||||||
You can just load the module in a html page as:
|
|
||||||
|
|
||||||
```html
|
|
||||||
<script type="module">
|
|
||||||
import * as bigintCryptoUtils from "<path to the downloaded index.browser.bundle.mod.js>";
|
|
||||||
|
|
||||||
let a = BigInt("5");
|
|
||||||
let b = BigInt("2");
|
|
||||||
let n = BigInt("19");
|
|
||||||
|
|
||||||
console.log(bigintCryptoUtils.modPow(a, b, n)); // prints 6
|
|
||||||
|
|
||||||
console.log(bigintCryptoUtils.modInv(BigInt("2"), BigInt("5"))); // prints 3
|
|
||||||
|
|
||||||
console.log(bigintCryptoUtils.modInv(BigInt("3"), BigInt("5"))); // prints 2
|
|
||||||
|
|
||||||
(async function() {
|
|
||||||
// Generation of a probable prime of 2018 bits
|
|
||||||
const p = await bigintCryptoUtils.prime(2048);
|
|
||||||
|
|
||||||
// Testing if a prime is a probable prime (Miller-Rabin)
|
|
||||||
const isPrime = await bigintCryptoUtils.isProbablyPrime(p);
|
|
||||||
alert(p.toString() + "\nIs prime?\n" + isPrime);
|
|
||||||
|
|
||||||
// Get a cryptographically secure random number between 1 and 2**256 bits.
|
|
||||||
const rnd = bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256));
|
|
||||||
alert(rnd);
|
|
||||||
})();
|
|
||||||
</script>
|
|
||||||
```
|
|
||||||
|
|
||||||
### TypeScript
|
|
||||||
|
|
||||||
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`.
|
|
||||||
|
|
||||||
## bigint-crypto-utils JS Doc
|
## bigint-crypto-utils JS Doc
|
||||||
|
|
||||||
### Functions
|
### Functions
|
||||||
|
|
|
@ -22,75 +22,77 @@ For web browsers, you can also directly download the [IIFE file](https://raw.git
|
||||||
|
|
||||||
## Usage examples
|
## Usage examples
|
||||||
|
|
||||||
### Node.js:
|
Import your module as :
|
||||||
|
|
||||||
|
- Node.js
|
||||||
|
```javascript
|
||||||
|
const bigintCryptoUtils = require('bigint-crypto-utils')
|
||||||
|
... // your code here
|
||||||
|
```
|
||||||
|
- Javascript native project
|
||||||
|
```javascript
|
||||||
|
import * as bigintCryptoUtils from 'bigint-crypto-utils'
|
||||||
|
... // 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-crypto-utils'
|
||||||
|
... // 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-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`.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const bigintCryptoUtils = require("bigint-crypto-utils");
|
|
||||||
|
|
||||||
/* Stage 3 BigInts with value 666 can be declared as BigInt('666')
|
/* Stage 3 BigInts with value 666 can be declared as BigInt('666')
|
||||||
or the shorter new no-so-linter-friendly syntax 666n.
|
or the shorter new no-so-linter-friendly syntax 666n.
|
||||||
Notice that you can also pass a number, e.g. BigInt(666), but it is not
|
Notice that you can also pass a number, e.g. BigInt(666), but it is not
|
||||||
recommended since values over 2**53 - 1 won't be safe but no warning will
|
recommended since values over 2**53 - 1 won't be safe but no warning will
|
||||||
be raised.
|
be raised.
|
||||||
*/
|
*/
|
||||||
let a = BigInt("5");
|
const a = BigInt('5')
|
||||||
let b = BigInt("2");
|
const b = BigInt('2')
|
||||||
let n = BigInt("19");
|
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
|
||||||
|
|
||||||
// Generation of a probable prime of 2048 bits
|
console.log(bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256))) // Prints a cryptographically secure random number between 1 and 2**256 bits.
|
||||||
const prime = await bigintCryptoUtils.prime(2048);
|
|
||||||
|
|
||||||
// Testing if a prime is a probable prime (Miller-Rabin)
|
async function primeTesting () {
|
||||||
if (await bigintCryptoUtils.isProbablyPrime(prime))
|
// Output of a probable prime of 2048 bits
|
||||||
// code if is prime
|
console.log(await bigintCryptoUtils.prime(2048))
|
||||||
|
|
||||||
|
// Testing if a number is a probable prime (Miller-Rabin)
|
||||||
|
const number = 27
|
||||||
|
const isPrime = await bigintCryptoUtils.isProbablyPrime(number)
|
||||||
|
if (isPrime) {
|
||||||
|
console.log(`${number} is prime`)
|
||||||
|
} else {
|
||||||
|
console.log(`${number} is composite`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
primeTesting()
|
||||||
|
|
||||||
// Get a cryptographically secure random number between 1 and 2**256 bits.
|
|
||||||
const rnd = bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256));
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Javascript native from a browser
|
|
||||||
|
|
||||||
You can just load the module in a html page as:
|
|
||||||
|
|
||||||
```html
|
|
||||||
<script type="module">
|
|
||||||
import * as bigintCryptoUtils from "<path to the downloaded index.browser.bundle.mod.js>";
|
|
||||||
|
|
||||||
let a = BigInt("5");
|
|
||||||
let b = BigInt("2");
|
|
||||||
let n = BigInt("19");
|
|
||||||
|
|
||||||
console.log(bigintCryptoUtils.modPow(a, b, n)); // prints 6
|
|
||||||
|
|
||||||
console.log(bigintCryptoUtils.modInv(BigInt("2"), BigInt("5"))); // prints 3
|
|
||||||
|
|
||||||
console.log(bigintCryptoUtils.modInv(BigInt("3"), BigInt("5"))); // prints 2
|
|
||||||
|
|
||||||
(async function() {
|
|
||||||
// Generation of a probable prime of 2018 bits
|
|
||||||
const p = await bigintCryptoUtils.prime(2048);
|
|
||||||
|
|
||||||
// Testing if a prime is a probable prime (Miller-Rabin)
|
|
||||||
const isPrime = await bigintCryptoUtils.isProbablyPrime(p);
|
|
||||||
alert(p.toString() + "\nIs prime?\n" + isPrime);
|
|
||||||
|
|
||||||
// Get a cryptographically secure random number between 1 and 2**256 bits.
|
|
||||||
const rnd = bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256));
|
|
||||||
alert(rnd);
|
|
||||||
})();
|
|
||||||
</script>
|
|
||||||
```
|
|
||||||
|
|
||||||
### TypeScript
|
|
||||||
|
|
||||||
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`.
|
|
||||||
|
|
||||||
## bigint-crypto-utils JS Doc
|
## bigint-crypto-utils JS Doc
|
||||||
|
|
||||||
{{>main}}
|
{{>main}}
|
||||||
|
|
Loading…
Reference in New Issue