bsgs added

master
serr 2025-02-23 22:24:59 +03:00
parent 4a3cc69efc
commit 0f7575215f
2 changed files with 40 additions and 7 deletions

32
bsgs.py Normal file
View File

@ -0,0 +1,32 @@
# Алгоритм Гельфонда-Шенкса дискретного логарифмирования (baby-step giant-step algoritm)
def order(a, p):
"""Возвращает порядок числа a по модулю числа p"""
i, x = 1, 0
while x != 1:
x, i = pow(a, i, p), i + 1
return i - 1
def gelfond(a, b, p, q=None):
if q == None: q = order(a, p)
s = (int)(q**.5) + 1
d = pow(a, p - 1 - s, p)
#print(f"q = {q}, s = {s}, d = {d}")
base = []
for i in range(0, s):
base.append(b % p * pow(d, i , p) % p)
#print(base)
for i in range(0, s):
try:
x = s*base.index(pow(a, i, p))+i
#print(f"x≡{x}mod({q})")
return x
except:
pass
if __name__ == "__main__":
print(gelfond(2, 7123, 1123))

View File

@ -49,18 +49,19 @@ def pollard_rho(a, b, p, ord=None):
log_c, log_d = [1, 1], [1, 1]
while True:
#start_time = time.perf_counter()
# start_time = time.perf_counter()
c = f(c, a, b, p, log_c)
d = f(f(d, a, b, p, log_d), a, b, p, log_d)
#end_time = time.perf_counter()
#print(f"Время выполнения итерации: {end_time - start_time:.10f}")
# end_time = time.perf_counter()
# print(f"Время выполнения итерации: {end_time - start_time:.10f}")
# print(c, d, log_c, log_d)
if c == d: break
if ord == None: ord = order(a, p)
#print(f"{log_c[0]}+{log_c[1]}x≡{log_d[0]}+{log_d[1]}x(mod{ord})")
#print(f"{log_c[1]-log_d[1]}x≡{log_d[0]-log_c[0]}(mod{ord})")
# print(f"{log_c[0]}+{log_c[1]}x≡{log_d[0]}+{log_d[1]}x(mod{ord})")
# print(f"{log_c[1]-log_d[1]}x≡{log_d[0]-log_c[0]}(mod{ord})")
return solve(log_c[1]-log_d[1], log_d[0]-log_c[0], ord)
print(pollard_rho(2, 7, 137))
# print(pollard_rho(2,11,50091285122438801159, ord=25045642561219400579))
if __name__ == "__main__":
print(pollard_rho(2, 7123, 1123))
# print(pollard_rho(2,11,50091285122438801159, ord=25045642561219400579))