mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 06:55:08 -04:00
Merge 5de1bcf711
into 7c8be12d52
This commit is contained in:
commit
dd43ce1259
1 changed files with 19 additions and 11 deletions
|
@ -1265,20 +1265,28 @@ 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 Matt C [matt@artemisbot.uk]
|
* @author Barry B [profbbrown@gmail.com]
|
||||||
* @param {number} x
|
* @param {number} a
|
||||||
* @param {number} y
|
* @param {number} n
|
||||||
* @returns {number}
|
* @returns {number|undefined} The inverse of a (mod n), or undefined
|
||||||
*/
|
*/
|
||||||
static modInv(x, y) {
|
static modInv(a, n) {
|
||||||
x %= y;
|
let t = 0, newT = 1, r = n, newR = a;
|
||||||
for (let i = 1; i < y; i++) {
|
|
||||||
if ((x * i) % 26 === 1) {
|
while (newR !== 0) {
|
||||||
return i;
|
const q = Math.floor(r / newR);
|
||||||
}
|
[t, newT] = [newT, t - q * newT];
|
||||||
|
[r, newR] = [newR, r - q * newR];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (r > 1) return undefined;
|
||||||
|
if (t < 0) t = t + n;
|
||||||
|
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue