Advertisement
saleks28

kmzi6_8semv2

Aug 13th, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 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.             continue
  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.  
  45.         print("Elliptic curve: y^2 = x^3 + {}x + {}".format(curve[0], curve[1]))
  46.         print("Point: Q = ({}, {})".format(point[0], point[1]))
  47.  
  48.         i = 0
  49.         Q = list([point[0], point[1]])
  50.         while i < base:
  51.             #Check whether i is prime
  52.             if not isPrime(i):
  53.                 i += 1
  54.                 continue
  55.             r = int((log(n, 2) / log(i, 2)) * (1 / 2))
  56.             if r == 0:
  57.                 r = 1
  58.             p = i ** r
  59.             for j in range(1, p):
  60.                 counter += 1
  61.                 if Q[0] == point[0] and Q[1] == point[1]:
  62.                     Lmbd = inverse((2 * Q[1]), n)
  63.                     if 1 < GCD(Lmbd, n) < n:
  64.                         return GCD(Lmbd, n), counter
  65.                     x3 = (Lmbd ** 2 - 2 * Q[0]) % n
  66.                     y3 = (Lmbd * (Q[0] - x3) - Q[1]) % n
  67.                     Q[1] = y3
  68.                     Q[0] = x3
  69.                 else:
  70.                     Lmbd = inverse((point[0] - Q[0]), n)
  71.                     if 1 < GCD(Lmbd, n) < n:
  72.                         return GCD(Lmbd, n), counter
  73.                     x3 = (Lmbd ** 2 - Q[0] - point[0]) % n
  74.                     y3 = (Lmbd * (Q[0] - x3) - Q[1]) % n
  75.                     Q[1] = y3
  76.                     Q[0] = x3
  77.                 if counter % 10000000 == 0:
  78.                     print(counter)
  79.  
  80.  
  81. # main func
  82. def main():
  83.     user_number = int(input("Enter not prime number for factorizing: "))
  84.     user_base = int(input("Enter base of factorization: "))
  85.     if isPrime(user_number):
  86.         print("Your number is prime, try more\n")
  87.         return
  88.     start_time = datetime.now()
  89.     p, iter_num = lenstra_algorithm(user_number, user_base)
  90.     end_time = datetime.now() - start_time
  91.     q = user_number // p
  92.     print("{} * {} = {}" . format(p, q, user_number))
  93.     print("Number of iterations: {}" . format(iter_num))
  94.     print("Time: {}" . format(end_time))
  95.  
  96. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement