MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / find_mod_inverse

Function find_mod_inverse

ciphers/cryptomath_module.py:4–13  ·  view source on GitHub ↗
(a: int, m: int)

Source from the content-addressed store, hash-verified

2
3
4def find_mod_inverse(a: int, m: int) -> int:
5 if gcd_by_iterative(a, m) != 1:
6 msg = f"mod inverse of {a!r} and {m!r} does not exist"
7 raise ValueError(msg)
8 u1, u2, u3 = 1, 0, a
9 v1, v2, v3 = 0, 1, m
10 while v3 != 0:
11 q = u3 // v3
12 v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
13 return u1 % m

Callers

nothing calls this directly

Calls 1

gcd_by_iterativeFunction · 0.90

Tested by

no test coverage detected