2016-08-28 20:26:06 +03:00
|
|
|
from random import randint
|
|
|
|
|
|
|
|
|
2017-05-21 14:59:16 +03:00
|
|
|
class Factorization:
|
2017-11-03 14:59:17 +03:00
|
|
|
@classmethod
|
|
|
|
def factorize(cls, pq):
|
|
|
|
if pq % 2 == 0:
|
|
|
|
return 2, pq // 2
|
|
|
|
|
|
|
|
y, c, m = randint(1, pq - 1), randint(1, pq - 1), randint(1, pq - 1)
|
|
|
|
g = r = q = 1
|
|
|
|
x = ys = 0
|
|
|
|
|
|
|
|
while g == 1:
|
|
|
|
x = y
|
|
|
|
for i in range(r):
|
|
|
|
y = (pow(y, 2, pq) + c) % pq
|
|
|
|
|
|
|
|
k = 0
|
|
|
|
while k < r and g == 1:
|
|
|
|
ys = y
|
|
|
|
for i in range(min(m, r - k)):
|
|
|
|
y = (pow(y, 2, pq) + c) % pq
|
|
|
|
q = q * (abs(x - y)) % pq
|
|
|
|
|
|
|
|
g = cls.gcd(q, pq)
|
|
|
|
k += m
|
|
|
|
|
|
|
|
r *= 2
|
|
|
|
|
|
|
|
if g == pq:
|
|
|
|
while True:
|
|
|
|
ys = (pow(ys, 2, pq) + c) % pq
|
|
|
|
g = cls.gcd(abs(x - ys), pq)
|
|
|
|
if g > 1:
|
2016-08-28 20:26:06 +03:00
|
|
|
break
|
|
|
|
|
2017-11-03 14:59:17 +03:00
|
|
|
return g, pq // g
|
2016-08-28 20:26:06 +03:00
|
|
|
|
2016-09-03 11:54:58 +03:00
|
|
|
@staticmethod
|
|
|
|
def gcd(a, b):
|
2017-11-03 14:59:17 +03:00
|
|
|
while b:
|
|
|
|
a, b = b, a % b
|
2016-09-03 11:54:58 +03:00
|
|
|
|
2017-11-03 14:59:17 +03:00
|
|
|
return a
|