Skip to content

Commit 073768f

Browse files
committed
Add doctests to cryptomath_module
Added comprehensive doctests for find_mod_inverse function. Contributes to #9943
1 parent a71618f commit 073768f

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

ciphers/cryptomath_module.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
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+
18
from maths.greatest_common_divisor import gcd_by_iterative
29

310

411
def 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)

0 commit comments

Comments
 (0)