Advertisement
saleks28

kmzi5

Sep 16th, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. from random import randint as rndint
  2. from Cryptodome.Util.number import inverse
  3. from datetime import datetime as date
  4.  
  5.  
  6. def generate_L(n):
  7.     return randint(0, n - 1)
  8.  
  9. # return elliptic curve params
  10. def return_params():
  11.     p = 9103
  12.     A, B = 3, 16
  13.     P = [4515, 5197]
  14.     Q = [8132, 7165]
  15.     return [p, A, B, P, Q]
  16.  
  17.  
  18. # H-func
  19. def H_function(T, L):
  20.     x_T = T[0]
  21.     j = lift(x_T) % L
  22.     return j
  23.  
  24.  
  25. # RHO method
  26. def RHO_Method(E, P, Q, q):
  27.     iter_counter = 0
  28.     while 1:
  29.         R, a, b = [], [], []
  30.         L = generate_L(q)
  31.         for j in range(0, L):
  32.             a_j = rndint(0, q - 1)
  33.             b_j = rndint(0, q - 1)
  34.             R_j = a_j * P + b_j * Q
  35.             R.append(R_j)
  36.             a.append(a_j)
  37.             b.append(b_j)
  38.         a_1 = rndint(0, q - 1)
  39.         b_1 = rndint(0, q - 1)
  40.         T_1 = a_1 * P + b_1 * Q
  41.         T_2 = T_1
  42.         a_2 = a_1
  43.         b_2 = b_1
  44.         while 1:
  45.             iter_counter += 1
  46.             j = H_function(T_1, L)
  47.             T_1 = T_1 + R[j]
  48.             a_1 = (a_1 + a[j]) % q
  49.             b_1 = (b_1 + b[j]) % q
  50.             j = H_function(T_2, L)
  51.             T_2 = T_2 + R[j]
  52.             a_2 = (a_2 + a[j]) % q
  53.             b_2 = (b_2 + b[j]) % q
  54.             j = H_function(T_2, L)
  55.             T_2 = T_2 + R[j]
  56.             a_2 = (a_2 + a[j]) % q
  57.             b_2 = (b_2 + b[j]) % q
  58.             if T_1 == T_2:
  59.                 break
  60.         if a_1 == a_2 and b_1 == b_2:
  61.             R.clear()
  62.             a.clear()
  63.             b.clear()
  64.             continue
  65.         elif T_1 == T_2:
  66.             a_delta = a_1 - a_2
  67.             b_delta = (b_2 - b_1 + q) % q
  68.             d = a_delta * inverse(b_delta, q) % q
  69.             if Q != d * P:
  70.                 continue
  71.             else:
  72.                 return d, iter_counter
  73.  
  74.  
  75. # main
  76. def main():
  77.     iter_num = 0
  78.     params = return_params()
  79.     E = EllipticCurve(GF(params[0]), [params[1], params[2]])
  80.     P = E(params[3][0], params[3][1])
  81.     q = P.order()
  82.     Q = E(params[4][0], params[4][1])
  83.     print(E)
  84.     print("P = {}" . format(P))
  85.     print("Q = {}" . format(Q))
  86.     d, iter_num = RHO_Method(E, P, Q, q)
  87.     print("d = {}\nIterations: {}" . format(d, iter_num))
  88.  
  89.  
  90.  
  91. start_time = date.now()
  92. main()
  93. work_time = date.now() - start_time
  94. print("Time: {}" . format(work_time))
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement