Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8aba079

Browse files
Add files via upload
1 parent bbe4369 commit 8aba079

13 files changed

+132
-130
lines changed

‎README.txt‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ A suitable command looks like "python main.py --n [your_number]"
6060

6161
I have defined a few parameters directly you can edit in the config file.
6262

63-
- batch_smooth_test: If True, the batch smoothness test from Bernstein will be used. Otherwise, the naive (trial division from the prime factor base) approach will be used. Batch smoothness test is generally faster
63+
- FLAG_USE_BATCH_SMOOTH_TEST: If True, the batch smoothness test from Bernstein will be used. Otherwise, the naive (trial division from the prime factor base) approach will be used. Batch smoothness test is generally faster
6464

65-
- gaussian_pivot: If True, gaussian elimination will ALWAYS be used in the linear algebra step. If you want to use block Lanczos of Wiedemann algorithms, this has to be set to False.
65+
- FLAG_GAUSSIAN_PIVOT: If True, gaussian elimination will ALWAYS be used in the linear algebra step. If you want to use block Lanczos of Wiedemann algorithms, this has to be set to False.
6666

67-
- lanczos: If True, the block Lanczos algorithm will be used. If False, the Wiedemann algorithm will be used. No matter its value, if gaussian_pivot is True then gaussian elimination will be performed.
67+
- FLAG_LANCZOS: If True, the block Lanczos algorithm will be used. If False, the Wiedemann algorithm will be used. No matter its value, if gaussian_pivot is True then gaussian elimination will be performed.
6868

69-
- square_root_couveignes: If True, the Couveignes algorithm is run for the square root step. It requires many primes to be inert. If False, the lifting algorithm is used
69+
- FLAG_SQUARE_ROOT_COUVEIGNES: If True, the Couveignes algorithm is run for the square root step. It requires many primes to be inert. If False, the lifting algorithm is used
7070

71-
- large_primes_constant: Define the constant that will be multiplied by the last prime in the factor base to obtain the bound for the single large primes. For the double large primes, the bound is this constant multiplied by the last prime in the factor base squared. Both the algebraic and rational sides have the same bounds.
71+
- LARGE_PRIME_CONST: Define the constant that will be multiplied by the last prime in the factor base to obtain the bound for the single large primes. For the double large primes, the bound is this constant multiplied by the last prime in the factor base squared. Both the algebraic and rational sides have the same bounds.
7272

73-
- block_size: If Block Lanczos or Wiedemann algorithm are used, this sets the block size. In the Wiedemann algorithm, this is only used to compute many matrix-vector products at once, and not to compute the matrix generator of the matrix sequence you obtain.
73+
- BLOCK_SIZE: If Block Lanczos or Wiedemann algorithm are used, this sets the block size. In the Wiedemann algorithm, this is only used to compute many matrix-vector products at once, and not to compute the matrix generator of the matrix sequence you obtain.
7474

75-
- poly_search_nb_poly_coarse_eval: number of polynomials to be generated before doing precise ranking.
75+
- NB_POLY_COARSE_EVAL: number of polynomials to be generated before doing precise ranking.
7676

77-
- poly_search_nb_poly_precise_eval: number of polynomials to be kept for precise evaluation.
77+
- NB_POLY_PRECISE_EVAL: number of polynomials to be kept for precise evaluation.
7878

79-
- poly_search_prime_bound: maximum size of primes used in the polynomial generation
79+
- PRIME_BOUND: maximum size of primes used in the polynomial generation
8080

81-
- poly_search_nb_roots: number of roots to use in the Kleinjung polynomial generation algorithm. It is the l parameter in the original paper.
81+
- NB_ROOTS: number of roots to use in the Kleinjung polynomial generation algorithm. It is the l parameter in the original paper.
8282

83-
- poly_search_multiplier: leading coefficient of generated polynomials is always a multiple of this multiplier.
83+
- MULTIPLIER: leading coefficient of generated polynomials is always a multiple of this multiplier.
8484

8585
- NB_CPU_POLY_SELECTION: Sets the number of cpu used for running polynomial search. One cpu is always kept as a "leader" that collect polynomial candidates from the workers.
8686

‎config/config.ini‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[DEFAULT]
2-
batch_smooth_test=True
3-
gaussian_pivot=False
4-
lanczos=True
5-
square_root_couveignes=False
6-
large_primes_constant=100
7-
block_size=8
8-
poly_search_nb_poly_coarse_eval=100
9-
poly_search_nb_poly_precise_eval=50
10-
poly_search_prime_bound=300
11-
poly_search_nb_roots=3
12-
poly_search_multiplier=1
2+
FLAG_USE_BATCH_SMOOTH_TEST=True
3+
FLAG_GAUSSIAN_PIVOT=False
4+
FLAG_LANCZOS=True
5+
FLAG_SQUARE_ROOT_COUVEIGNES=False
6+
LARGE_PRIME_CONST=100
7+
BLOCK_SIZE=8
8+
NB_POLY_COARSE_EVAL=100
9+
NB_POLY_PRECISE_EVAL=50
10+
PRIME_BOUND=300
11+
NB_ROOTS=3
12+
MULTIPLIER=1
1313
NB_CPU_POLY_SELECTION=4
1414
NB_CPU_SIEVE=4

‎src/NFS.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from utils import *
1212
from polynomial_functions import *
1313
from generate_primes import *
14+
import utils_polynomial_selection
1415
import mono_cpu_polynomial_selection
1516
import multi_cpu_polynomial_selection
1617
import mono_cpu_sieve
@@ -87,7 +88,7 @@ def NFS(n):
8788
FLAG_GAUSSIAN_PIVOT = parameters[1].lower() in ["true"]
8889
FLAG_LANCZOS = parameters[2].lower() in ["true"]
8990
FLAG_SQUARE_ROOT_COUVEIGNES = parameters[3].lower() in ["true"]
90-
CONST = int(parameters[4])
91+
LARGE_PRIME_CONST = int(parameters[4])
9192
BLOCK_SIZE = int(parameters[5])
9293
NB_POLY_COARSE_EVAL = int(parameters[6])
9394
NB_POLY_PRECISE_EVAL = int(parameters[7])
@@ -107,7 +108,7 @@ def NFS(n):
107108

108109
prod_primes = math.prod(primes)
109110

110-
const1, const2 = CONST*primes[-1], CONST*primes[-1]*primes[-1]
111+
const1, const2 = LARGE_PRIME_CONST*primes[-1], LARGE_PRIME_CONST*primes[-1]*primes[-1]
111112

112113
if NB_CPU_POLY_SELECTION == 1:
113114
f_x,m0,m1,tmp,_ = mono_cpu_polynomial_selection.poly_search(n, primes, NB_ROOTS, PRIME_BOUND, MULTIPLIER,
@@ -121,14 +122,14 @@ def NFS(n):
121122

122123
log.write_log(LOG_PATH, "poly search completed, parameters : m0 = "+str(m0)+" ; m1 = "+str(m1)+" ; d = "+str(d)+"\n")
123124

124-
f_x, m0, M = mono_cpu_polynomial_selection.evaluate_polynomial_quality(f_x, B, m0, m1, primes, LOG_PATH)
125+
f_x, m0, M = utils_polynomial_selection.evaluate_polynomial_quality(f_x, B, m0, m1, primes, LOG_PATH)
125126

126127
leading_coeff = f_x[0]
127128
zeros_f = get_complex_roots(f_x)
128129
zeros = [leading_coeff*i for i in zeros_f]
129130
f_prime = get_derivative(f_x)
130131

131-
g = [1,f_x[1]]
132+
g = [1,f_x[1]]
132133
for i in range(2, len(f_x)): g.append(f_x[i]*pow(leading_coeff, i-1))
133134
g_prime = get_derivative(g)
134135

@@ -166,7 +167,7 @@ def NFS(n):
166167
else:
167168
pairs_used, V = multi_cpu_sieve.find_relations(f_x, leading_coeff, g, primes, R_p, Q, B_prime, divide_leading,
168169
prod_primes, pow_div, pairs_used, const1, const2, logs, m0, m1,
169-
M, FLAG_USE_BATCH_SMOOTH_TEST, LOG_PATH,NB_CPU_SIEVE)
170+
M, FLAG_USE_BATCH_SMOOTH_TEST, LOG_PATH,NB_CPU_SIEVE)
170171

171172
print("")
172173
log.write_log(LOG_PATH, "sieving complete, building matrix...")

‎src/block_lanczos.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def block(T, N):
6161

6262
# The whole block lanczos algorithm
6363
# See the README for sources
64-
def block_lanczos(B, nb_relations, N, LOG_PATH):
65-
Y = [random.randint(0, (1<<N)-1) for _ in range(nb_relations)]
64+
def block_lanczos(B, nb_relations, BLOCK_SIZE, LOG_PATH):
65+
Y = [random.randint(0, (1<<BLOCK_SIZE)-1) for _ in range(nb_relations)]
6666

6767
X = [0]*nb_relations
6868
b = transpose_sparse(B, nb_relations)
@@ -72,14 +72,14 @@ def block_lanczos(B, nb_relations, N, LOG_PATH):
7272
P = [0 for _ in range(nb_relations)]
7373
V = Vo
7474
d = 1
75-
while d and i <= int(len(B)/(N-0.764))+10:
75+
while d and i <= int(len(B)/(BLOCK_SIZE-0.764))+10:
7676
Z = sparse_multiply(b, sparse_multiply(B, V))
77-
vAv = dense_multiply(transpose_dense(V, N), Z)
78-
vAAv = dense_multiply(transpose_dense(Z, N), Z)
77+
vAv = dense_multiply(transpose_dense(V, BLOCK_SIZE), Z)
78+
vAAv = dense_multiply(transpose_dense(Z, BLOCK_SIZE), Z)
7979

80-
W_inv, d = block(vAv,N)
80+
W_inv, d = block(vAv,BLOCK_SIZE)
8181

82-
X = add_vector(X, dense_multiply(V, dense_multiply(W_inv, dense_multiply(transpose_dense(V, N), Vo))))
82+
X = add_vector(X, dense_multiply(V, dense_multiply(W_inv, dense_multiply(transpose_dense(V, BLOCK_SIZE), Vo))))
8383

8484
neg_d = switch_indices(d)
8585

@@ -99,9 +99,9 @@ def block_lanczos(B, nb_relations, N, LOG_PATH):
9999

100100
log.write_log(LOG_PATH, "lanczos halted after "+str(i)+" iterations")
101101
x = add_vector(X, Y)
102-
Z = concatenate(x, V, N)
103-
matrix = transpose_dense(sparse_multiply(B, Z), N<<1)
104-
Z = transpose_dense(Z, N<<1)
102+
Z = concatenate(x, V, BLOCK_SIZE)
103+
matrix = transpose_dense(sparse_multiply(B, Z), BLOCK_SIZE<<1)
104+
Z = transpose_dense(Z, BLOCK_SIZE<<1)
105105
matrix, Z = solve(matrix, Z, len(B))
106106

107107
solutions = []
@@ -110,7 +110,7 @@ def block_lanczos(B, nb_relations, N, LOG_PATH):
110110
solutions.append(Z[i])
111111

112112
if len(solutions) == 0:
113-
solutions = block_lanczos(B,nb_relations,N<<1,LOG_PATH)
113+
solutions = block_lanczos(B,nb_relations,BLOCK_SIZE<<1,LOG_PATH)
114114

115115
return solutions
116116

‎src/compute_solutions.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import math, sys
44
from square_roots import *
55
from polynomial_functions import *
6-
from utils import *
6+
from utils import my_norm
77
from datetime import datetime
88
import log
99

@@ -19,7 +19,7 @@ def convert_to_binary_lanczos(z, n):
1919
if (z >> n - i - 1)&1: res[i] = 1
2020
return res
2121

22-
def create_solution(pairs, null_space, n, len_primes, primes, f_x, m0, m1, inert, f_prime_sq, leading, f_prime_eval, u):
22+
def create_solution(pairs, null_space, n, len_primes, primes, f_x, m0, m1, inert, f_prime_sq, leading, f_prime_eval, u, LOG_PATH):
2323
f_norm = 0
2424
tmp = 1
2525
for x in f_x:
@@ -42,7 +42,7 @@ def create_solution(pairs, null_space, n, len_primes, primes, f_x, m0, m1, inert
4242

4343
coeff_bound = [fd*pow(f_norm, len(f_x)-1-i)*pow(2*(leading*u)*f_norm, S>>1) for i in range(len(f_x)-1)]
4444

45-
y = square_root(f_x, rational_square, inert, m0, m1, leading, max(coeff_bound))
45+
y = square_root(f_x, rational_square, inert, m0, m1, leading, max(coeff_bound), LOG_PATH)
4646
y = y*pow(m1, S>>1, n)%n
4747

4848
return x, y
@@ -166,17 +166,17 @@ def create_solution_couveignes(pairs, null_space, n, len_primes, primes, f_x, f_
166166

167167
r = r+round(rest)
168168
y = pow(m1, S>>1, n)*(y-r*P%n)%n
169-
return x,y
169+
return x,y
170170

171171
def compute_factors(pairs_used, vec, n, primes, g, g_prime, g_prime_sq, g_prime_eval, m0, m1, leading_coeff, d,
172-
inert_set, zeros, delta, M, flag_square_root_couveignes, time_1, LOG_PATH):
173-
if flag_square_root_couveignes:
172+
inert_set, zeros, delta, M, FLAG_SQUARE_ROOT_COUVEIGNES, time_1, LOG_PATH):
173+
if FLAG_SQUARE_ROOT_COUVEIGNES:
174174
x,y = create_solution_couveignes(pairs_used, vec, n, len(primes), primes, g, g_prime, m0, m1, g_prime_sq,
175175
leading_coeff, g_prime_eval, d, inert_set, zeros, delta, M<<1)
176176

177177
else:
178178
x,y = create_solution(pairs_used, vec, n, len(primes), primes, g, m0, m1, inert_set[-1], g_prime_sq,
179-
leading_coeff, g_prime_eval, M<<1)
179+
leading_coeff, g_prime_eval, M<<1, LOG_PATH)
180180

181181
if x != y and math.gcd(x-y, n) != 1 and math.gcd(x+y, n) != 1:
182182
print_final_message(x, y, n, time_1, LOG_PATH)

‎src/gaussian_elimination.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file contains the functions that perform gaussian elimination on a dense matrix
22

3-
from utils import *
3+
from utils import lowest_set_bit
44

55
def add_column_opt(M_opt, tgt, src):
66
"""For a matrix produced by siqs_build_matrix_opt, add the column
@@ -48,4 +48,5 @@ def siqs_solve_matrix_opt(M_opt, n, m):
4848
if (M_opt[j] >> i) & 1: # test M[i][j] == 1
4949
perfect_sq_indices.append(pivots[j])
5050
perf_squares.append(perfect_sq_indices)
51+
5152
return perf_squares

‎src/mono_cpu_polynomial_selection.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def degree_one_poly_selection(n, primes):
6565
return best_poly
6666

6767
# Kleinjung first polynomial search algorithm
68-
def Kleinjung_poly_search(n, primes, nb_roots, prime_bound, c, M, d, NB_POLY_COARSE_EVAL, NB_POLY_PRECISE_EVAL, LOG_PATH):
68+
def Kleinjung_poly_search(n, primes, NB_ROOTS, PRIME_BOUND, MULTIPLIER, M, d, NB_POLY_COARSE_EVAL, NB_POLY_PRECISE_EVAL, LOG_PATH):
6969
t1 = datetime.now()
7070
P = []
7171
polys = []
7272
for p in primes:
73-
if p > prime_bound: break
73+
if p > PRIME_BOUND: break
7474
if p%d == 1: P.append(p)
75-
a_d = c
75+
a_d = MULTIPLIER
7676
if d >= 4: admax = round(pow(pow(M, 2*d-2)/n, 1/(d-3)))
7777
else: admax = M
7878

@@ -84,7 +84,7 @@ def Kleinjung_poly_search(n, primes, nb_roots, prime_bound, c, M, d, NB_POLY_COA
8484
while not tmp%p: tmp//= p
8585

8686
if tmp > 1: # If a_d is not primes[-1] smooth
87-
a_d += c
87+
a_d += MULTIPLIER
8888
continue
8989

9090
mw = math.ceil(pow(n/a_d, 1/d))
@@ -104,40 +104,40 @@ def Kleinjung_poly_search(n, primes, nb_roots, prime_bound, c, M, d, NB_POLY_COA
104104
Q.append(p)
105105
roots.append(root)
106106

107-
if len(roots) >= nb_roots:
107+
if len(roots) >= NB_ROOTS:
108108

109-
combinations = prime_combinations_with_indices(Q, nb_roots, ad1max)
109+
combinations = prime_combinations_with_indices(Q, NB_ROOTS, ad1max)
110110

111111
for set in combinations:
112112
Q_used = []
113113
prod = 1
114-
for i in range(nb_roots):
114+
for i in range(NB_ROOTS):
115115
Q_used.append(Q[set[i]])
116116
prod *= Q[set[i]]
117117

118-
root_used = [roots[set[i]] for i in range(nb_roots)]
119-
for i in range(nb_roots): # Do some CRT
118+
root_used = [roots[set[i]] for i in range(NB_ROOTS)]
119+
for i in range(NB_ROOTS): # Do some CRT
120120
x = prod//Q_used[i]
121121
tmp2 = x*invmod(x, Q_used[i])
122122
for j in range(d): root_used[i][j] = root_used[i][j]*tmp2%prod
123123

124124
m0 = mw+(-mw)%prod
125-
e = compute_e(m0, root_used, nb_roots, prod, a_d, n, d)
126-
f, f0 = compute_f(n, a_d, m0, d, prod, root_used, nb_roots, e)
125+
e = compute_e(m0, root_used, NB_ROOTS, prod, a_d, n, d)
126+
f, f0 = compute_f(n, a_d, m0, d, prod, root_used, NB_ROOTS, e)
127127

128128
epsilon = ad2max/m0
129-
array1 = create_first_array(nb_roots, f0, f, d)
130-
len_vec = nb_roots>>1
131-
array2 = create_second_array(nb_roots, len_vec, d, f)
129+
array1 = create_first_array(NB_ROOTS, f0, f, d)
130+
len_vec = NB_ROOTS>>1
131+
array2 = create_second_array(NB_ROOTS, len_vec, d, f)
132132

133133
min = 0
134134
for j in range(len(array2)):
135135
while min < len(array1) and array2[j][0]-epsilon > array1[min][0]: min += 1
136136
if min == len(array1): break
137137
z = min
138138
while z < len(array1) and abs(array2[j][0]-array1[z][0]) < epsilon:
139-
tmp = [poly(m_mu(m0, root_used, array1[z][1]+array2[j][1], nb_roots), prod, a_d, n, d),
140-
m_mu(m0, root_used, array1[z][1]+array2[j][1], nb_roots),
139+
tmp = [poly(m_mu(m0, root_used, array1[z][1]+array2[j][1], NB_ROOTS), prod, a_d, n, d),
140+
m_mu(m0, root_used, array1[z][1]+array2[j][1], NB_ROOTS),
141141
prod]
142142
cpt += 1
143143
sys.stdout.write('\r'+str(cpt)+" polynomials tested")
@@ -176,7 +176,7 @@ def Kleinjung_poly_search(n, primes, nb_roots, prime_bound, c, M, d, NB_POLY_COA
176176
return select_best_poly_candidate(polys, primes)
177177

178178
z += 1
179-
a_d += c
179+
a_d += MULTIPLIER
180180

181181
def select_best_poly_candidate(polys, primes):
182182
best_poly = None

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /