Advertisement
saleks28

kmzi6_8sem

Aug 13th, 2020
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. from math import log
  2. from datetime import datetime
  3. from Cryptodome.Random.random import randint
  4. from Cryptodome.Util.number import isPrime, GCD, inverse
  5.  
  6.  
  7. # Params generations
  8. def generate_params(num):
  9.     while 1:
  10.         a, x, y = randint(0, num - 1), randint(0, num - 1), randint(0, num - 1)
  11.         b = (y ** 2 - x ** 3 - a * x) % num
  12.         g = GCD(num, 4 * a ** 3 + 27 * b ** 2)
  13.         result = list()
  14.         if g == num:
  15.             continue
  16.         if g > 1:
  17.             result.append(g)
  18.             return result
  19.         result.append(a)
  20.         result.append(b)
  21.         result.append(x)
  22.         result.append(y)
  23.         return result
  24.  
  25.  
  26. # Lenstra algorithm for factorizing numbers
  27. def lenstra_algorithm(n, base):
  28.     point = list()
  29.     curve = list()
  30.     counter = 0
  31.     while True:
  32.         temp = generate_params(n)
  33.         if len(temp) == 1:
  34.             return temp[0]
  35.         # A and B
  36.         curve.append(temp[0])
  37.         curve.append(temp[1])
  38.         # X and Y
  39.         point.append(temp[2])
  40.         point.append(temp[3])
  41.         i = 0
  42.         # Q = (X, Y)
  43.         Q = [point[0], point[1]]
  44.         while i < base:
  45.             if not isPrime(i):
  46.                 i += 1
  47.                 continue
  48.             r = int((log(n, 2) / log(i, 2)) * (1 / 2))
  49.             if r == 0:
  50.                 r = 1
  51.             p = i ** r
  52.             for j in range(1, p):
  53.                 counter += 1
  54.                 if Q[0] == point[0] and Q[1] == point[1]:
  55.                     chisl = (3 * Q[0] ** 2 + curve[0]) % n
  56.                     znam = inverse((2 * Q[1]), n)
  57.                     L = chisl * znam % n
  58.                     if GCD(L, n) > 1 and GCD(L, n) < n:
  59.                         return GCD(L, n), counter
  60.                     x3 = (L ** 2 - 2 * Q[0]) % n
  61.                     y3 = (L * (Q[0] - x3) - Q[1]) % n
  62.                     Q[1] = y3
  63.                     Q[0] = x3
  64.                 else:
  65.                     chisl = (point[1] - Q[1]) % n
  66.                     znam = inverse((point[0] - Q[0]), n)
  67.                     L = chisl * znam % n
  68.                     if GCD(L, n) > 1 and GCD(L, n) < n:
  69.                         return GCD(L, n), counter
  70.                     x3 = (L ** 2 - Q[0] - point[0]) % n
  71.                     y3 = (L * (Q[0] - x3) - Q[1]) % n
  72.                     Q[1] = y3
  73.                     Q[0] = x3
  74.                 if counter % 10000000 == 0:
  75.                     print(counter)
  76.  
  77.  
  78. # main func
  79. def main():
  80.     user_number = int(input("Enter not prime number for factorizing: "))
  81.     user_base = int(input("Enter base of factorization: "))
  82.     if isPrime(user_number):
  83.         print("Your number is prime, try more\n")
  84.         return
  85.     start_time = datetime.now()
  86.     p, iter_num = lenstra_algorithm(user_number, user_base)
  87.     end_time = datetime.now() - start_time
  88.     q = user_number // p
  89.     print("{} * {} = {}" . format(p, q, user_number))
  90.     print("Number of iterations: {}" . format(iter_num))
  91.     print("Time: {}" . format(end_time))
  92.  
  93. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement