Returns undefined if not invertible
This commit is contained in:
parent
8530c47cb8
commit
bc559888e6
@ -1265,13 +1265,14 @@ class Utils {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds the modular inverse of two values.
|
* Calculates the modular multiplicative inverse of a (mod n).
|
||||||
* Uses the Extended Euclidean Algorithm.
|
* Uses the Extended Euclidean Algorithm.
|
||||||
|
* https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
|
||||||
*
|
*
|
||||||
* @author Barry B [profbbrown@gmail.com]
|
* @author Barry B [profbbrown@gmail.com]
|
||||||
* @param {number} a
|
* @param {number} a
|
||||||
* @param {number} n
|
* @param {number} n
|
||||||
* @returns {number|null}
|
* @returns {number|undefined} The inverse of a (mod n), or undefined
|
||||||
*/
|
*/
|
||||||
static modInv(a, n) {
|
static modInv(a, n) {
|
||||||
let t = 0, newT = 1, r = n, newR = a;
|
let t = 0, newT = 1, r = n, newR = a;
|
||||||
@ -1282,7 +1283,7 @@ class Utils {
|
|||||||
[r, newR] = [newR, r - q * newR];
|
[r, newR] = [newR, r - q * newR];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r > 1) return null;
|
if (r > 1) return undefined;
|
||||||
if (t < 0) t = t + n;
|
if (t < 0) t = t + n;
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user