2021-03-24 17:32:12 +00:00
|
|
|
# bigint-mod-arith - v3.0.0
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Some common functions for modular arithmetic using native JS implementation of BigInt
|
|
|
|
|
|
|
|
## Table of contents
|
|
|
|
|
|
|
|
### Interfaces
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
- [Egcd](interfaces/Egcd.md)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
### Functions
|
|
|
|
|
|
|
|
- [abs](API.md#abs)
|
|
|
|
- [bitLength](API.md#bitlength)
|
|
|
|
- [eGcd](API.md#egcd)
|
|
|
|
- [gcd](API.md#gcd)
|
|
|
|
- [lcm](API.md#lcm)
|
|
|
|
- [max](API.md#max)
|
|
|
|
- [min](API.md#min)
|
|
|
|
- [modInv](API.md#modinv)
|
|
|
|
- [modPow](API.md#modpow)
|
|
|
|
- [toZn](API.md#tozn)
|
|
|
|
|
|
|
|
## Functions
|
|
|
|
|
|
|
|
### abs
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **abs**(`a`): `number` \| `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
`number` \| `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
The absolute value of a
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[abs.ts:8](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/abs.ts#L8)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### bitLength
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **bitLength**(`a`): `number`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Returns the bitlength of a number
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`number`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
The bit length
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[bitLength.ts:7](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/bitLength.ts#L7)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### eGcd
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **eGcd**(`a`, `b`): [`Egcd`](interfaces/Egcd.md)
|
2021-03-24 13:04:30 +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).
|
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
**`throws`** {RangeError}
|
|
|
|
This excepction is thrown if a or b are less than 0
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
|
|
|
| `b` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
[`Egcd`](interfaces/Egcd.md)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[egcd.ts:18](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/egcd.ts#L18)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### gcd
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **gcd**(`a`, `b`): `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Greatest-common divisor of two integers based on the iterative binary algorithm.
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
|
|
|
| `b` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
`bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
The greatest common divisor of a and b
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[gcd.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/gcd.ts#L10)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### lcm
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **lcm**(`a`, `b`): `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
The least common multiple computed as abs(a*b)/gcd(a,b)
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
|
|
|
| `b` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
The least common multiple of a and b
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[lcm.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/lcm.ts#L10)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### max
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **max**(`a`, `b`): `number` \| `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
|
|
|
| `b` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`number` \| `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Maximum of numbers a and b
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[max.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/max.ts#L9)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### min
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **min**(`a`, `b`): `number` \| `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` |
|
|
|
|
| `b` | `number` \| `bigint` |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
`number` \| `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Minimum of numbers a and b
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[min.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/min.ts#L9)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### modInv
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **modInv**(`a`, `n`): `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Modular inverse.
|
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
**`throws`** {RangeError}
|
|
|
|
Excpeption thorwn when a does not have inverse modulo n
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type | Description |
|
|
|
|
| :------ | :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` | The number to find an inverse for |
|
|
|
|
| `n` | `number` \| `bigint` | The modulo |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
`bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
The inverse modulo n
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[modInv.ts:14](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/modInv.ts#L14)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### modPow
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **modPow**(`b`, `e`, `n`): `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
**`throws`** {RangeError}
|
|
|
|
Excpeption thrown when n is not > 0
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type | Description |
|
|
|
|
| :------ | :------ | :------ |
|
|
|
|
| `b` | `number` \| `bigint` | base |
|
|
|
|
| `e` | `number` \| `bigint` | exponent |
|
|
|
|
| `n` | `number` \| `bigint` | modulo |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
`bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
b**e mod n
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[modPow.ts:16](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/modPow.ts#L16)
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### toZn
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
▸ **toZn**(`a`, `n`): `bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
Finds the smallest positive element that is congruent to a in modulo n
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
**`remarks`**
|
2021-03-24 14:11:07 +00:00
|
|
|
a and b must be the same type, either number or bigint
|
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
**`throws`** {RangeError}
|
|
|
|
Excpeption thrown when n is not > 0
|
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Parameters
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
| Name | Type | Description |
|
|
|
|
| :------ | :------ | :------ |
|
|
|
|
| `a` | `number` \| `bigint` | An integer |
|
|
|
|
| `n` | `number` \| `bigint` | The modulo |
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`bigint`
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-03-24 17:30:45 +00:00
|
|
|
A bigint with the smallest positive representation of a modulo n
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2021-08-06 22:14:30 +00:00
|
|
|
#### Defined in
|
|
|
|
|
|
|
|
[toZn.ts:15](https://github.com/juanelas/bigint-mod-arith/blob/12186fd/src/ts/toZn.ts#L15)
|