bigint-crypto-utils/docs/API.md

521 lines
12 KiB
Markdown
Raw Normal View History

2021-08-06 08:10:32 +00:00
# bigint-crypto-utils
2021-03-25 12:40:04 +00:00
## Table of contents
### Functions
- [abs](API.md#abs)
- [bitLength](API.md#bitlength)
- [eGcd](API.md#egcd)
- [gcd](API.md#gcd)
- [isProbablyPrime](API.md#isprobablyprime)
- [lcm](API.md#lcm)
- [max](API.md#max)
- [min](API.md#min)
- [modInv](API.md#modinv)
- [modPow](API.md#modpow)
- [prime](API.md#prime)
- [primeSync](API.md#primesync)
- [randBetween](API.md#randbetween)
- [randBits](API.md#randbits)
- [randBitsSync](API.md#randbitssync)
- [randBytes](API.md#randbytes)
- [randBytesSync](API.md#randbytessync)
- [toZn](API.md#tozn)
## Functions
### abs
2021-08-04 11:04:27 +00:00
**abs**(`a`): `number` \| `bigint`
2021-03-25 12:40:04 +00:00
Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`number` \| `bigint`
2021-03-25 12:40:04 +00:00
The absolute value of a
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/abs.d.ts:8
2021-03-25 12:40:04 +00:00
___
### bitLength
2021-08-04 11:04:27 +00:00
**bitLength**(`a`): `number`
2021-03-25 12:40:04 +00:00
Returns the bitlength of a number
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`number`
2021-03-25 12:40:04 +00:00
The bit length
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/bitLength.d.ts:7
2021-03-25 12:40:04 +00:00
___
### eGcd
2021-08-04 11:04:27 +00:00
**eGcd**(`a`, `b`): `Egcd`
2021-03-25 12:40:04 +00:00
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).
**`throws`** {RangeError}
This excepction is thrown if a or b are less than 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
| `b` | `number` \| `bigint` |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`Egcd`
2021-03-25 12:40:04 +00:00
A triple (g, x, y), such that ax + by = g = gcd(a, b).
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/egcd.d.ts:18
2021-03-25 12:40:04 +00:00
___
### gcd
2021-08-04 11:04:27 +00:00
**gcd**(`a`, `b`): `bigint`
2021-03-25 12:40:04 +00:00
Greatest-common divisor of two integers based on the iterative binary algorithm.
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
| `b` | `number` \| `bigint` |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`bigint`
2021-03-25 12:40:04 +00:00
The greatest common divisor of a and b
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/gcd.d.ts:9
2021-03-25 12:40:04 +00:00
___
### isProbablyPrime
2021-08-04 11:04:27 +00:00
**isProbablyPrime**(`w`, `iterations?`, `disableWorkers?`): `Promise`<`boolean`\>
2021-03-25 12:40:04 +00:00
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)
**`throws`** {RangeError}
w MUST be >= 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `w` | `number` \| `bigint` | `undefined` | A positive 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 |
| `disableWorkers` | `boolean` | `false` | Disable the use of workers for the primality test |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`Promise`<`boolean`\>
2021-03-25 12:40:04 +00:00
A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/isProbablyPrime.ts#L21)
2021-03-25 12:40:04 +00:00
___
### lcm
2021-08-04 11:04:27 +00:00
**lcm**(`a`, `b`): `bigint`
2021-03-25 12:40:04 +00:00
The least common multiple computed as abs(a*b)/gcd(a,b)
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
| `b` | `number` \| `bigint` |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`bigint`
2021-03-25 12:40:04 +00:00
The least common multiple of a and b
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/lcm.d.ts:8
2021-03-25 12:40:04 +00:00
___
### max
2021-08-04 11:04:27 +00:00
**max**(`a`, `b`): `number` \| `bigint`
2021-03-25 12:40:04 +00:00
Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
| `b` | `number` \| `bigint` |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`number` \| `bigint`
2021-03-25 12:40:04 +00:00
Maximum of numbers a and b
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/max.d.ts:9
2021-03-25 12:40:04 +00:00
___
### min
2021-08-04 11:04:27 +00:00
**min**(`a`, `b`): `number` \| `bigint`
2021-03-25 12:40:04 +00:00
Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type |
| :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` |
| `b` | `number` \| `bigint` |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`number` \| `bigint`
2021-03-25 12:40:04 +00:00
Minimum of numbers a and b
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/min.d.ts:9
2021-03-25 12:40:04 +00:00
___
### modInv
2021-08-04 11:04:27 +00:00
**modInv**(`a`, `n`): `bigint`
2021-03-25 12:40:04 +00:00
Modular inverse.
**`throws`** {RangeError}
Excpeption thorwn when a does not have inverse modulo n
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Description |
| :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` | The number to find an inverse for |
| `n` | `number` \| `bigint` | The modulo |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`bigint`
2021-03-25 12:40:04 +00:00
The inverse modulo n
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/modInv.d.ts:12
2021-03-25 12:40:04 +00:00
___
### modPow
2021-08-04 11:04:27 +00:00
**modPow**(`b`, `e`, `n`): `bigint`
2021-03-25 12:40:04 +00:00
Modular exponentiation b**e mod n. Currently using the right-to-left binary method
**`throws`** {RangeError}
Excpeption thrown when n is not > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Description |
| :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `b` | `number` \| `bigint` | base |
| `e` | `number` \| `bigint` | exponent |
| `n` | `number` \| `bigint` | modulo |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`bigint`
2021-03-25 12:40:04 +00:00
b**e mod n
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/modPow.d.ts:13
2021-03-25 12:40:04 +00:00
___
### prime
2021-08-04 11:04:27 +00:00
**prime**(`bitLength`, `iterations?`): `Promise`<`bigint`\>
2021-03-25 12:40:04 +00:00
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).
**`throws`** {RangeError}
bitLength MUST be > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `bitLength` | `number` | `undefined` | The required bit length for the generated prime |
| `iterations` | `number` | `16` | The number of iterations for the Miller-Rabin Probabilistic Primality Test |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`Promise`<`bigint`\>
2021-03-25 12:40:04 +00:00
A promise that resolves to a bigint probable prime of bitLength bits.
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/prime.ts#L21)
2021-03-25 12:40:04 +00:00
___
### primeSync
2021-08-04 11:04:27 +00:00
**primeSync**(`bitLength`, `iterations?`): `bigint`
2021-03-25 12:40:04 +00:00
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.
**`throws`** {RangeError}
bitLength MUST be > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `bitLength` | `number` | `undefined` | The required bit length for the generated prime |
| `iterations` | `number` | `16` | The number of iterations for the Miller-Rabin Probabilistic Primality Test |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`bigint`
2021-03-25 12:40:04 +00:00
A bigint probable prime of bitLength bits.
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/prime.ts#L100)
2021-03-25 12:40:04 +00:00
___
### randBetween
2021-08-04 11:04:27 +00:00
**randBetween**(`max`, `min?`): `bigint`
2021-03-25 12:40:04 +00:00
Returns a cryptographically secure random integer between [min,max]. Both numbers must be >=0
**`throws`** {RangeError}
Arguments MUST be: max > 0 && min >=0 && max > min
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Description |
| :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `max` | `bigint` | Returned value will be <= max |
| `min` | `bigint` | Returned value will be >= min |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`bigint`
2021-03-25 12:40:04 +00:00
A cryptographically secure random bigint between [min,max]
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/randBetween.ts#L15)
2021-03-25 12:40:04 +00:00
___
### randBits
2021-08-04 11:04:27 +00:00
**randBits**(`bitLength`, `forceLength?`): `Promise`<`Uint8Array` \| `Buffer`\>
2021-03-25 12:40:04 +00:00
Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
**`throws`** {RangeError}
bitLength MUST be > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `bitLength` | `number` | `undefined` | 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 |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`Promise`<`Uint8Array` \| `Buffer`\>
2021-03-25 12:40:04 +00:00
A Promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bits
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/randBits.ts#L14)
2021-03-25 12:40:04 +00:00
___
### randBitsSync
2021-08-04 11:04:27 +00:00
**randBitsSync**(`bitLength`, `forceLength?`): `Uint8Array` \| `Buffer`
2021-03-25 12:40:04 +00:00
Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
**`throws`** {RangeError}
bitLength MUST be > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `bitLength` | `number` | `undefined` | 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 |
#### Returns
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
`Uint8Array` \| `Buffer`
2021-03-25 12:40:04 +00:00
A Uint8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bits
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/randBits.ts#L45)
2021-03-25 12:40:04 +00:00
___
### randBytes
2021-08-04 11:04:27 +00:00
**randBytes**(`byteLength`, `forceLength?`): `Promise`<`Uint8Array` \| `Buffer`\>
2021-03-25 12:40:04 +00:00
Secure random bytes for both node and browsers. Node version uses crypto.randomBytes() and browser one self.crypto.getRandomValues()
**`throws`** {RangeError}
byteLength MUST be > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `byteLength` | `number` | `undefined` | 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 |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`Promise`<`Uint8Array` \| `Buffer`\>
2021-03-25 12:40:04 +00:00
A promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bytes
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/randBytes.ts#L12)
2021-03-25 12:40:04 +00:00
___
### randBytesSync
2021-08-04 11:04:27 +00:00
**randBytesSync**(`byteLength`, `forceLength?`): `Uint8Array` \| `Buffer`
2021-03-25 12:40:04 +00:00
Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
**`throws`** {RangeError}
byteLength MUST be > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `byteLength` | `number` | `undefined` | 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 |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`Uint8Array` \| `Buffer`
2021-03-25 12:40:04 +00:00
A UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bytes
2021-08-04 11:04:27 +00:00
#### Defined in
2021-08-06 15:47:15 +00:00
[src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/1febe0f/src/ts/randBytes.ts#L46)
2021-03-25 12:40:04 +00:00
___
### toZn
2021-08-04 11:04:27 +00:00
**toZn**(`a`, `n`): `bigint`
2021-03-25 12:40:04 +00:00
Finds the smallest positive element that is congruent to a in modulo n
2021-08-04 11:04:27 +00:00
**`remarks`**
2021-03-25 12:40:04 +00:00
a and b must be the same type, either number or bigint
**`throws`** {RangeError}
Excpeption thrown when n is not > 0
2021-08-04 11:04:27 +00:00
#### Parameters
2021-03-25 12:40:04 +00:00
| Name | Type | Description |
| :------ | :------ | :------ |
2021-08-04 11:04:27 +00:00
| `a` | `number` \| `bigint` | An integer |
| `n` | `number` \| `bigint` | The modulo |
2021-03-25 12:40:04 +00:00
2021-08-04 11:04:27 +00:00
#### Returns
`bigint`
2021-03-25 12:40:04 +00:00
A bigint with the smallest positive representation of a modulo n
2021-08-04 11:04:27 +00:00
#### Defined in
node_modules/bigint-mod-arith/dist/esm/types/ts/toZn.d.ts:15