File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ Cryptographic mathematics module for modular arithmetic operations.
3+
4+ This module provides utilities for cryptographic computations,
5+ particularly modular multiplicative inverse.
6+ """
7+
18from maths .greatest_common_divisor import gcd_by_iterative
29
310
411def find_mod_inverse (a : int , m : int ) -> int :
12+ """
13+ Find the modular multiplicative inverse of a modulo m.
14+
15+ The modular multiplicative inverse of a modulo m is an integer x
16+ such that (a * x) % m == 1. This only exists when gcd(a, m) == 1.
17+
18+ Args:
19+ a: The number to find the inverse of
20+ m: The modulus
21+
22+ Returns:
23+ The modular multiplicative inverse of a modulo m
24+
25+ Raises:
26+ ValueError: If gcd(a, m) != 1 (inverse doesn't exist)
27+
28+ >>> find_mod_inverse(7, 26)
29+ 15
30+ >>> find_mod_inverse(3, 11)
31+ 4
32+ >>> find_mod_inverse(5, 17)
33+ 7
34+ >>> find_mod_inverse(1, 5)
35+ 1
36+ >>> find_mod_inverse(2, 7)
37+ 4
38+ """
539 if gcd_by_iterative (a , m ) != 1 :
640 msg = f"mod inverse of { a !r} and { m !r} does not exist"
741 raise ValueError (msg )
You can’t perform that action at this time.
0 commit comments