-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol_dh1.py
67 lines (52 loc) · 1.29 KB
/
sol_dh1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def modinv(g, p):
"""
Finds the modular inverse of g modulo p.
Args:
g: The number to find the inverse of.
p: The modulus.
Returns:
The modular inverse of g modulo p.
"""
# Use the extended Euclidean algorithm to find the modular inverse.
"""
확장 유클리드 알고리즘을 사용하여 g의 p에 대한 모듈러 역수를 찾습니다.
"""
g, x, y = extended_euclidean_algorithm(g, p)
# The modular inverse is the x-coordinate of the extended Euclidean
# algorithm, modulo p.
"""
모듈러 역수는 확장 유클리드 알고리즘의 x-좌표입니다.
"""
return x % p
def extended_euclidean_algorithm(a, b):
"""
Finds the greatest common divisor of a and b, and the x and y coordinates
of the Bezout equation a * x + b * y = gcd(a, b).
Args:
a: The first number.
b: The second number.
Returns:
A tuple of (gcd(a, b), x, y).
"""
# Base case.
"""
기본 사례입니다.
"""
if b == 0:
return (a, 1, 0)
# Recursive case.
"""
재귀 사례입니다.
"""
gcd, x1, y1 = extended_euclidean_algorithm(b, a % b)
x = y1
y = x1 - (a // b) * y1
return (gcd, x, y)
p = 991
g = 209
# Find the modular inverse of g modulo p.
"""
g의 p에 대한 모듈러 역수를 찾습니다.
"""
d = modinv(g, p)
print(d)