pollard rho updated
parent
0f7575215f
commit
bfb40c93c2
|
@ -0,0 +1,5 @@
|
||||||
|
2025-02-23 22:39:29,156 - Starting Pollard's Rho algorithm
|
||||||
|
2025-02-23 22:39:38,952 - Iteration 10000000 | c = -21066148124649499654 | 4761990+5238012x | d = 12490635180447373915 | 9523240+10476762x
|
||||||
|
2025-02-23 22:39:48,697 - Iteration 20000000 | c = 12490635180447373915 | 9523240+10476762x | d = -13863527009688412944 | 19045759+20954243x
|
||||||
|
2025-02-23 22:39:58,496 - Iteration 30000000 | c = 10826988314848297953 | 14283559+15716443x | d = -1961120103059297340 | 28569398+31430604x
|
||||||
|
2025-02-23 22:40:08,240 - Iteration 40000000 | c = -13863527009688412944 | 19045759+20954243x | d = 6984277127831374919 | 38096038+41903964x
|
|
@ -1,6 +1,13 @@
|
||||||
import time
|
import time
|
||||||
# ро-метод Полларда для дискретного логарифмирования
|
import logging
|
||||||
# Задача - найти такое x, что выполняется a^x ≡ b (mod p)
|
|
||||||
|
# Configure the logger
|
||||||
|
logging.basicConfig(
|
||||||
|
filename='pollard_rho.log', # Log file name
|
||||||
|
level=logging.INFO, # Logging level (INFO, DEBUG, WARNING, ERROR, CRITICAL)
|
||||||
|
format='%(asctime)s - %(message)s', # Log format
|
||||||
|
filemode='w' # 'w' to overwrite the file, 'a' to append to the file
|
||||||
|
)
|
||||||
|
|
||||||
def gcd(a, b):
|
def gcd(a, b):
|
||||||
r0, r1 = a, b
|
r0, r1 = a, b
|
||||||
|
@ -17,13 +24,13 @@ def gcd(a, b):
|
||||||
return x0, y0, r0
|
return x0, y0, r0
|
||||||
|
|
||||||
def v(a, b):
|
def v(a, b):
|
||||||
"""Возвращает абсолютно наименьший вычет числа a по модулю числа b"""
|
"""Returns the absolutely smallest residue of a modulo b"""
|
||||||
r1 = a % b
|
r1 = a % b
|
||||||
r2 = r1 - b
|
r2 = r1 - b
|
||||||
return r1 if abs(r1) < abs(r2) else r2
|
return r1 if abs(r1) < abs(r2) else r2
|
||||||
|
|
||||||
def order(a, p):
|
def order(a, p):
|
||||||
"""Возвращает порядок числа a по модулю числа p"""
|
"""Returns the order of a modulo p"""
|
||||||
i, x = 1, 0
|
i, x = 1, 0
|
||||||
while x != 1:
|
while x != 1:
|
||||||
x, i = pow(a, i, p), i + 1
|
x, i = pow(a, i, p), i + 1
|
||||||
|
@ -48,20 +55,35 @@ def pollard_rho(a, b, p, ord=None):
|
||||||
d = c = pow(a, u, p) * pow(b, v, p) % p
|
d = c = pow(a, u, p) * pow(b, v, p) % p
|
||||||
log_c, log_d = [1, 1], [1, 1]
|
log_c, log_d = [1, 1], [1, 1]
|
||||||
|
|
||||||
|
i = 0
|
||||||
while True:
|
while True:
|
||||||
# start_time = time.perf_counter()
|
i += 1
|
||||||
c = f(c, a, b, p, log_c)
|
c = f(c, a, b, p, log_c)
|
||||||
d = f(f(d, a, b, p, log_d), a, b, p, log_d)
|
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}")
|
# Log every 10 million iterations
|
||||||
# print(c, d, log_c, log_d)
|
if i % 10_000_000 == 0:
|
||||||
if c == d: break
|
print(f"Iteration {i} | c = {c} | {log_c[0]}+{log_c[1]}x | d = {d} | {log_d[0]}+{log_d[1]}x")
|
||||||
|
logging.info(f"Iteration {i} | c = {c} | {log_c[0]}+{log_c[1]}x | d = {d} | {log_d[0]}+{log_d[1]}x")
|
||||||
|
|
||||||
|
if c == d:
|
||||||
|
logging.info(f"Match found at iteration {i}: c = {c}, d = {d}")
|
||||||
|
break
|
||||||
|
|
||||||
|
if ord is None:
|
||||||
|
ord = order(a, p)
|
||||||
|
logging.info(f"Order of element a modulo p: {ord}")
|
||||||
|
|
||||||
|
# Log the final comparison
|
||||||
|
logging.info(f"Comparison: {log_c[0]}+{log_c[1]}x = {log_d[0]}+{log_d[1]}x (mod {ord})")
|
||||||
|
logging.info(f"Simplified comparison: {log_c[1]-log_d[1]}x = {log_d[0]-log_c[0]} (mod {ord})")
|
||||||
|
|
||||||
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})")
|
|
||||||
return solve(log_c[1]-log_d[1], log_d[0]-log_c[0], ord)
|
return solve(log_c[1]-log_d[1], log_d[0]-log_c[0], ord)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(pollard_rho(2, 7123, 1123))
|
# Example usage
|
||||||
# print(pollard_rho(2,11,50091285122438801159, ord=25045642561219400579))
|
logging.info("Starting Pollard's Rho algorithm")
|
||||||
|
# result = pollard_rho(2, 7, 137)
|
||||||
|
result = pollard_rho(2, 11, 50091285122438801159, ord=25045642561219400579)
|
||||||
|
logging.info(f"Result: x = {result}")
|
||||||
|
print(f"Result: x = {result}")
|
Loading…
Reference in New Issue