/
sieve_main.cpp
203 lines (190 loc) · 4.57 KB
/
sieve_main.cpp
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
/*
This provides an implementation of Gauss sieving, including using
tuples of vectors in fplll framework. The Gauss Sieve code is
based on Panagiotis Voulgaris's implementation of the Gauss sieve.
*/
#include "sieve_main.h"
#include "fplll.h"
/**
* help function
*/
static void main_usage(char *myself)
{
cout << "Usage: " << myself << " [options]\n"
<< "List of options:\n"
<< " -a [2|3|4]\n"
<< " 2- or 3- or 4-sieve;\n"
<< " -f filename\n"
<< " Input filename\n"
<< " -r nnn\n"
<< " Generate a random instance of dimension nnn\n"
<< " -t nnn\n"
<< " Targeted norm^2=nnn\n"
<< " -s nnn\n"
<< " Using seed=nnn\n"
<< " -b nnn\n"
<< " BKZ preprocessing of blocksize=nnn\n"
<< " -v\n"
<< " Verbose mode\n";
}
/**
* run sieve
*/
template <class ZT>
int main_run_sieve(ZZ_mat<ZT> B, Z_NR<ZT> target_norm, int alg, int ver, int seed)
{
GaussSieve<ZT, FP_NR<double>> gsieve(B, alg, ver, seed);
gsieve.sieve(target_norm);
return 0;
}
/**
* main function
*/
int main(int argc, char **argv)
{
char *input_file_name = NULL;
char *target_norm_s = NULL;
bool flag_verbose = true, flag_file = false;
int option, alg, dim = 10, seed = 0, bs = 0;
#if 0
dot_time = 0;
dot_num = 0;
count_bad = 0;
#endif
alg = 2;
/* parse */
if (argc == 1)
{
main_usage(argv[0]);
return -1;
}
while ((option = getopt(argc, argv, "a:f:r:t:s:b:v")) != -1)
{
switch (option)
{
case 'a':
alg = atoi(optarg);
if (alg != 2 && alg != 3 && alg != 4)
throw std::invalid_argument("only support 2-, 3- and 4-sieve");
break;
case 'f':
input_file_name = optarg;
flag_file = true;
break;
case 'r':
dim = atoi(optarg);
flag_file = false;
break;
case 's':
seed = atoi(optarg);
break;
case 'b':
bs = atoi(optarg);
break;
case 'v':
flag_verbose = true;
break;
case 't':
// ntarget_norm = atol(optarg);
cout << optarg << endl;
target_norm_s = optarg;
break;
case 'h':
main_usage(argv[0]);
return -1;
case '?':
main_usage(argv[0]);
return -1;
case ':':
main_usage(argv[0]);
return -1;
}
}
/* set lattice */
ZZ_mat<mpz_t> B;
if (flag_file)
{
ifstream input_file(input_file_name);
if (input_file.is_open())
{
input_file >> B;
input_file.close();
}
else
{
cin >> B;
}
if (flag_verbose)
{
cout << "# [info] reading lattice of dimension " << B.get_rows() << "x" << B.get_cols()
<< endl;
}
}
else
{
if (flag_verbose)
{
cout << "# [info] generating random lattice of dimension " << dim << endl;
}
srand(time(NULL));
B.resize(dim, dim);
B.gen_trg(1.1);
}
/* set targeted norm */
Z_NR<mpz_t> target_norm, max;
if (target_norm_s != NULL)
{
target_norm.set_str(target_norm_s);
}
if (target_norm < 0)
target_norm = 0;
if (flag_verbose)
cout << "# [info] target norm^2 is " << target_norm << endl;
/* preprocessing of basis */
clock_t stime = clock();
if (bs > 0)
bkz_reduction(B, bs, BKZ_DEFAULT, FT_DEFAULT, 0);
else
lll_reduction(B, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER);
clock_t etime = clock();
double secs = (etime - stime) / (double)CLOCKS_PER_SEC;
if (flag_verbose)
{
if (bs > 0)
cout << "# [info] BKZ took time " << secs << " s" << endl;
else
cout << "# [info] LLL took time " << secs << " s" << endl;
}
// cout << B << endl;
/* decide integer type */
stime = clock();
max = B.get_max();
#if 1
if (max < std::numeric_limits<int>::max())
{
long target_norm_l = abs(target_norm.get_si());
Z_NR<long> target_norm_lt;
target_norm_lt = target_norm_l;
ZZ_mat<long> B2(B.get_rows(), B.get_cols());
for (int i = 0; i < B.get_rows(); i++)
for (int j = 0; j < B.get_cols(); j++)
B2(i, j) = B(i, j).get_si();
main_run_sieve<long>(B2, target_norm_lt, alg, flag_verbose, seed);
}
else
#endif
main_run_sieve<mpz_t>(B, target_norm, alg, flag_verbose, seed);
etime = clock();
secs = (etime - stime) / (double)CLOCKS_PER_SEC;
if (flag_verbose)
{
cout << "# [info] sieve took time " << secs << " s" << endl;
/* dot product time */
#if 0
cout << "# [info] dot_time " << dot_time << endl;
cout << "# [info] dot_num " << dot_num << endl;
cout << "# [info] dot_time/dot_number " << (double) dot_time/dot_num << endl;
#endif
}
return 1;
}