/
factorization.py
146 lines (139 loc) · 3.97 KB
/
factorization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from math import ceil, log
from random import randrange
from .euclidean import gcd
from .primality import solovayStrassen, millerRabin
from .util import xxrange
def pollardP1(n, B = None, trace = False, **kargs):
"""
Perform the Pollard p-1 factoring algorithm.
Returns a nontrivial factor of n is one is found,
otherwise returns None.
"""
assert n > 1
if n == 2:
return None
if n % 2 == 0:
return 2
if B is None:
B = 4*int(ceil(log(n, 2)))
if trace:
print("B = %d" % B)
else:
assert B > 1
a = 2
for i in xxrange(2, B+1):
a = int(pow(a, i, n))
if trace:
print("a%d = 2 ^ %d! mod n = %d" % (i, i, a))
d = gcd(a-1, n)
if trace:
print("gcd(a%d-1, n) = %d" % (i, d))
if 1 < d and d < n:
return d
return None
def pollardRho(n, f = None, a = None, x = None, trace = False, **kargs):
"""
Perform the Pollard rho factoring algorithm.
Returns a nontrivial factor of n is one is found,
otherwise returns None.
"""
assert n > 1
if n == 2:
return None
if f is None:
if a is None:
a = randrange(1, n)
f = lambda z: (z*z + a) % n
if trace:
print("f(z) = (z^2 + %d) mod n" % a)
if x is None:
x = randrange(1, n)
y = f(x)
p = gcd(abs(x-y), n)
if trace:
i = 0
print("x = %d" % x)
print("f(x) = %d" % y)
print("gcd(x-f(x), n) = %d" % p)
while p == 1:
x = f(x)
y = f(f(y))
p = gcd(abs(x-y), n)
if trace:
i += 1
print("f^%d(x) = %d" % (i, x))
print("f^%d(x) = %d" % (2*i+1, y))
print("gcd(f^%d(x)-f^%d(x), n) = %d" % (i, 2*i+1, p))
if p != n:
return p
return None
def totalFactorization(n, methods = [pollardRho, pollardP1],
primality = [millerRabin, solovayStrassen],
repeat = 10, **kargs):
"""
Attempt to find a total factorization of n using the available methods.
"""
assert n > 1
factors = {}
trace = kargs.get("trace", False)
if n <= 3:
return {n: 1}
if trace:
print("checking primality for %d" % n)
for i in xxrange(repeat):
for f in primality:
if f(n, **kargs):
if trace:
print("determined that %d is composite, trying to factor" % n)
break
else:
continue
break
else:
if trace:
print("determined that %d is probably prime, not trying to factor" % n)
return {n: 1}
for f in methods:
m = f(n, **kargs)
if m is not None:
if trace:
print("found factorization %d = %d * %d" % (n, m, n//m))
f1 = totalFactorization(m, methods = methods, **kargs)
f2 = totalFactorization(n//m, methods = methods, **kargs)
for p, e in f2.items():
if p in f1:
f1[p] += f2[p]
else:
f1[p] = f2[p]
return f1
return {n: 1}
def factorizeByBase(n, base, m = None):
"""
Find a factorization of n with factors from base,
if one exists.
If the modulus m is given, the base is allowed to contain -1.
All other elements must be positive.
"""
assert n > 0
if m is not None and -1 in base:
idx = base.index(-1)
base = base[:idx] + base[idx+1:]
else:
idx = None
assert all(p > 0 for p in base)
factors = [0 for i in base]
x = n
for i, p in enumerate(base):
while x % p == 0:
x //= p
factors[i] += 1
if x != 1:
if idx is None:
return False
factors = factorizeByBase(-n % m, base)
if factors is False:
return False
factors.insert(idx, 1)
elif idx is not None:
factors.insert(idx, 0)
return factors