( n: bigint, exponent: number )
| 17 | * where `g = gcd(a, b)` (its sign follows `a`/`b`; callers that need a |
| 18 | * non-negative `g` normalize the triple). |
| 19 | */ |
| 20 | export function extGcd(a: bigint, b: bigint): [bigint, bigint, bigint] { |
| 21 | let [oldR, r] = [a, b]; |
| 22 | let [oldS, s] = [1n, 0n]; |
| 23 | let [oldT, t] = [0n, 1n]; |
| 24 | while (r !== 0n) { |
| 25 | const q = oldR / r; |
| 26 | [oldR, r] = [r, oldR - q * r]; |
| 27 | [oldS, s] = [s, oldS - q * s]; |
| 28 | [oldT, t] = [t, oldT - q * t]; |
| 29 | } |
| 30 | return [oldR, oldS, oldT]; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * The modular multiplicative inverse of `a` modulo `m` (`m > 0`): the integer |
| 35 | * `x` in `[0, m)` with `a·x ≡ 1 (mod m)`, or `null` when `a` and `m` are not |
| 36 | * coprime (i.e. `gcd(a mod m, m) ≠ 1`). |
| 37 | */ |
no test coverage detected