-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquadratic_sieve.py
265 lines (230 loc) · 9.17 KB
/
quadratic_sieve.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
from sage.all import *
import numpy as np
from factorization_function import *
from benchmark import *
# Helper struct for factoring table
class QuadraticSieveFactor(object):
def __init__(self, t, Ft):
self.t = t
self.origin = Ft
self.Ft = Ft
self.factors = []
def __str__(self):
return "QuadraticSieveFactor <t: %d, F(t): %d, factors: %s>"%(self.t, self.Ft, "[" + ",".join([str(x) for x in self.factors]) + "]")
def __repr__(self):
return "QuadraticSieveFactor <t: %d, F(t): %d, factors: %s>"%(self.t, self.Ft, "[" + ",".join([str(x) for x in self.factors]) + "]")
def divide(self, x):
self.Ft /= x
self.factors.append(x)
# Quadratic Sieve
class QuadraticSieve(FactorizationFunction):
@classmethod
def getCharacteristics(self):
c = FactorizationFunctionCharacteristics()
c.canFactorizePrimeComposites = True
c.canFactorizeEvenComposites = False
return c
@classmethod
def L(self, n):
# The expected L(n) is defined only for n > 2.72, expect integers
if (n >= 3):
return round(numerical_approx(e**sqrt(ln(n)*ln(ln(n)))))
else:
return None
@classmethod
def testL(self):
case = [1, 2, 5, 10173]
# Truth expects L(n)=e^sqrt(ln(n)*ln(ln(n)))
truth = [None, None, 2, 93]
def noneCheck(value):
return str(value) if value is not None else "None"
for i in range(len(case)):
result = self.L(case[i])
if result == truth[i]:
print u"L(%s)≈%s: ✓"%(noneCheck(case[i]), noneCheck(truth[i]))
else:
print u"L(%s)≈%s: ✘"%(noneCheck(case[i]), noneCheck(truth[i]))
print "\t Expected: ", truth[i]
print "\t Got: ", result
@classmethod
def F(self, x, n):
return x**2-n
@classmethod
def testF(self):
case = [{'x': 2, 'n': 1}, {'x': 5, 'n': 24}]
# Truth expects F(x)=x^2-1
truth = [3, 1]
for i in range(len(case)):
result = self.F(**case[i])
if result == truth[i]:
print u"F(%d, %d)≈%d: ✓"%(case[i]['x'], case[i]['n'], truth[i])
else:
print u"F(%d, %d)≈%d: ✘"%(case[i]['x'], case[i]['n'], truth[i])
print "\t Expected: ", truth[i]
print "\t Got: ", result
@classmethod
def getBase(self, B, benchmark = Benchmark()):
if (B < 2):
return []
P = Primes()
base = [2]
# Sage needs int to be cast as Integer
benchmark.start("get base")
p = P.next(Integer(base[len(base)-1]))
while p <= B:
benchmark.iterate("get base")
base.append(p)
p = P.next(Integer(base[len(base)-1]))
benchmark.stop("get base")
return base
@classmethod
def testGetBase(self):
case = [13]
# Truth expects F(x)=x^2-1
truth = [[2, 3, 5, 7, 11, 13]]
for i in range(len(case)):
result = self.getBase(case[i])[0]
if sorted(result) == sorted(truth[i]):
print u"Base for %d=%s: ✓"%(case[i], "[" + ",".join(str(x) for x in truth[i]) + "]")
else:
print u"Base for %d=%s: ✘"%(case[i], "[" + ",".join(str(x) for x in truth[i]) + "]")
print "\t Expected: ", truth[i]
print "\t Got: ", result
@classmethod
def getAB(self, n, B = None, V = None, benchmark = Benchmark()):
l = self.L(n)
if B is None:
B = l - 1
if V is None:
V = l + 10
u = floor(sqrt(n)) + 1
v = floor(sqrt(n)) + V
base = self.getBase(B, benchmark)
t = [QuadraticSieveFactor(x, self.F(x, n)) for x in range(u, v + 1)]
# For each prime 2 ≤ p ≤ B
benchmark.start("get A and B")
for p in base:
# Recursively solve x^2≡n (mod p^k) where k = 1, 2, 3... for as long as it's possible
_x = var('x')
k = 1
solutions = [int(i[0]) for i in solve_mod(_x**2 == n, p**k)]
while len(solutions) > 0:
benchmark.iterate("for each possible x^k")
# The inclusion could be checked in another, possibly more performant way
included = []
for x in solutions:
benchmark.iterate("for each sieve step")
# Calculate first index which yields t ≤ u (instead of stepping from i=0)
# This could be solved by solving a congruence equation initially
start = x
benchmark.start("determine start")
while start < u:
benchmark.iterate("calculate first step")
start += p**k
i = (u - x) // p
benchmark.stop("determine start")
for i in range(start, v + 1, p**k):
benchmark.iterate("for each simplification")
# Actual index in array
index = i - u
if index not in included:
t[index].divide(p)
included.append(index)
if len(included) > 0:
k += 1
benchmark.start("solve x^2=n mod p^k")
# This could be solved faster by using the Hensel's lift
solutions = [int(i[0]) for i in solve_mod(_x**2 == n, p**k)]
benchmark.stop("solve x^2=n mod p^k")
else:
solutions = []
benchmark.stop("get A and B")
# This could be improved to not filter through the same list multiple times
A = filter(lambda x: x.Ft == 1, t)
# Instead of array of factors, get map of factors and exponents
def factorMap(x):
values, counts = np.unique(x.factors, return_counts=True)
return dict(zip(values, counts))
C = [(x.origin, factorMap(x)) for x in A]
A = map(lambda x: x.t, A)
return (A, C, base)
@classmethod
def factorizeOnce(self, n, B, V, benchmark = Benchmark()):
# Setup
factors = []
# Start of algorithm
R = IntegerModRing(2)
A, C, base = QuadraticSieve.getAB(n, B, V, benchmark)
# If no B-smooth numbers were found
if len(A) < 2 or len(C) < 2:
return (None, benchmark)
# Create matrix where each row is a prime and each column an equation possibly including that prime (1 or 0)
coeffs = []
for p in base:
coeff = [C[i][1][p] % 2 if p in C[i][1] else 0 for i in range(len(C))]
if any(coeff):
coeffs.append(coeff)
# Create variables string for symbolic equations
variablesString = " ".join("x" + str(i) for i in range(len(coeffs[0]) + 1))
variables = var(variablesString)
equations = []
# Create symbolic equations
for coeff in coeffs:
string = ""
for i in range(len(coeff)):
if coeff[i]:
string += str(variables[i]) + "*"
string = string[:-1]
string += "==0"
equations.append(eval(string))
benchmark.start("solve equation")
solutions = solve_mod(equations, 2)
benchmark.stop("solve equation")
benchmark.start("calculate factors")
# Note that solutions[0] will always be the 0 vector - could be removed
for solution in solutions:
if n == 1:
break
a = 1
b = 1
for i in range(len(solution)):
benchmark.iterate("calculate factors")
if solution[i] == 1:
a *= A[i]
b *= C[i][0]
possibleFactors = [gcd(a - b, n), gcd(a + b, n)]
possibleFactors = filter(lambda x: x != 1 and x != n, possibleFactors)
while len(possibleFactors) > 0:
p = possibleFactors.pop()
q = n / p
if floor(q) == q:
factors.append(p)
n = q
if q != 1:
possibleFactors.append(q)
else:
break
benchmark.stop("calculate factors")
return factors
@classmethod
def factorize(self, n, returnBenchmark=False, B = None, V = None):
# Setup
benchmark = Benchmark()
factors = [1]
P = Primes()
stack = [n]
while len(stack) > 0:
a = stack.pop()
benchmark.start("primality test")
isPrime = a in P
benchmark.stop("primality test")
if isPrime:
factors.append(a)
else:
possibleFactors = self.factorizeOnce(a, B, V, benchmark)
if possibleFactors is not None:
stack += possibleFactors
benchmark.iterate("algorithm runs")
# End of algorithm
return (factors, benchmark) if returnBenchmark else factors