From bc559888e6bfa29091992b6058048caa6358fc01 Mon Sep 17 00:00:00 2001 From: Barry Brown Date: Thu, 13 Jun 2024 20:57:17 +0000 Subject: [PATCH] Returns undefined if not invertible --- src/core/Utils.mjs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index 08eadbf1..851044c3 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -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. + * https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm * * @author Barry B [profbbrown@gmail.com] * @param {number} a * @param {number} n - * @returns {number|null} + * @returns {number|undefined} The inverse of a (mod n), or undefined */ static modInv(a, n) { let t = 0, newT = 1, r = n, newR = a; @@ -1282,7 +1283,7 @@ class Utils { [r, newR] = [newR, r - q * newR]; } - if (r > 1) return null; + if (r > 1) return undefined; if (t < 0) t = t + n; return t;