1 | |
---|
2 | #include <NTL/ZZ_pX.h> |
---|
3 | #include <NTL/lzz_pX.h> |
---|
4 | |
---|
5 | #include <NTL/version.h> |
---|
6 | |
---|
7 | NTL_CLIENT |
---|
8 | |
---|
9 | |
---|
10 | #define make_string_aux(x) #x |
---|
11 | #define make_string(x) make_string_aux(x) |
---|
12 | |
---|
13 | int SmallModulusTest(long p, long n) |
---|
14 | { |
---|
15 | zz_pBak bak; |
---|
16 | |
---|
17 | bak.save(); |
---|
18 | |
---|
19 | |
---|
20 | zz_p::init(p); |
---|
21 | |
---|
22 | zz_pX a, b, c, cc; |
---|
23 | |
---|
24 | random(a, n); |
---|
25 | random(b, n); |
---|
26 | PlainMul(c, a, b); |
---|
27 | FFTMul(cc, a, b); |
---|
28 | |
---|
29 | int res; |
---|
30 | res = (c != cc); |
---|
31 | |
---|
32 | bak.restore(); |
---|
33 | |
---|
34 | return res; |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | int main() |
---|
41 | { |
---|
42 | |
---|
43 | |
---|
44 | cerr << "This is NTL version " << NTL_VERSION << "\n"; |
---|
45 | |
---|
46 | cerr << "Basic Configuration Options:\n"; |
---|
47 | |
---|
48 | |
---|
49 | #ifdef NTL_STD_CXX |
---|
50 | cerr << "NTL_STD_CXX\n"; |
---|
51 | #endif |
---|
52 | |
---|
53 | #ifdef NTL_PSTD_NNS |
---|
54 | cerr << "NTL_PSTD_NNS\n"; |
---|
55 | #endif |
---|
56 | |
---|
57 | #ifdef NTL_PSTD_NHF |
---|
58 | cerr << "NTL_PSTD_NHF\n"; |
---|
59 | #endif |
---|
60 | |
---|
61 | #ifdef NTL_PSTD_NTN |
---|
62 | cerr << "NTL_PSTD_NTN\n"; |
---|
63 | #endif |
---|
64 | |
---|
65 | #ifdef NTL_GMP_LIP |
---|
66 | cerr << "NTL_GMP_LIP\n"; |
---|
67 | #endif |
---|
68 | |
---|
69 | #ifdef NTL_GMP_HACK |
---|
70 | cerr << "NTL_GMP_HACK\n"; |
---|
71 | #endif |
---|
72 | |
---|
73 | #ifdef NTL_LONG_LONG_TYPE |
---|
74 | cerr << "NTL_LONG_LONG_TYPE: "; |
---|
75 | cerr << make_string(NTL_LONG_LONG_TYPE) << "\n"; |
---|
76 | #endif |
---|
77 | |
---|
78 | #ifdef NTL_UNSIGNED_LONG_LONG_TYPE |
---|
79 | cerr << "NTL_UNSIGNED_LONG_LONG_TYPE: "; |
---|
80 | cerr << make_string(NTL_UNSIGNED_LONG_LONG_TYPE) << "\n"; |
---|
81 | #endif |
---|
82 | |
---|
83 | #ifdef NTL_CXX_ONLY |
---|
84 | cerr << "NTL_CXX_ONLY\n"; |
---|
85 | #endif |
---|
86 | |
---|
87 | |
---|
88 | #ifdef NTL_X86_FIX |
---|
89 | cerr << "NTL_X86_FIX\n"; |
---|
90 | #endif |
---|
91 | |
---|
92 | #ifdef NTL_NO_X86_FIX |
---|
93 | cerr << "NTL_NO_X86_FIX\n"; |
---|
94 | #endif |
---|
95 | |
---|
96 | #ifdef NTL_NO_INIT_TRANS |
---|
97 | cerr << "NTL_NO_INIT_TRANS\n"; |
---|
98 | #endif |
---|
99 | |
---|
100 | #ifdef NTL_CLEAN_INT |
---|
101 | cerr << "NTL_CLEAN_INT\n"; |
---|
102 | #endif |
---|
103 | |
---|
104 | #ifdef NTL_CLEAN_PTR |
---|
105 | cerr << "NTL_CLEAN_PTR\n"; |
---|
106 | #endif |
---|
107 | |
---|
108 | #ifdef NTL_RANGE_CHECK |
---|
109 | cerr << "NTL_RANGE_CHECK\n"; |
---|
110 | #endif |
---|
111 | |
---|
112 | |
---|
113 | cerr << "\n"; |
---|
114 | cerr << "Resolution of double-word types:\n"; |
---|
115 | cerr << make_string(NTL_LL_TYPE) << "\n"; |
---|
116 | cerr << make_string(NTL_ULL_TYPE) << "\n"; |
---|
117 | |
---|
118 | |
---|
119 | cerr << "\n"; |
---|
120 | cerr << "Performance Options:\n"; |
---|
121 | |
---|
122 | #ifdef NTL_LONG_LONG |
---|
123 | cerr << "NTL_LONG_LONG\n"; |
---|
124 | #endif |
---|
125 | |
---|
126 | #ifdef NTL_AVOID_FLOAT |
---|
127 | cerr << "NTL_AVOID_FLOAT\n"; |
---|
128 | #endif |
---|
129 | |
---|
130 | #ifdef NTL_SPMM_UL |
---|
131 | cerr << "NTL_SPMM_UL\n"; |
---|
132 | #endif |
---|
133 | |
---|
134 | |
---|
135 | #ifdef NTL_SPMM_ULL |
---|
136 | cerr << "NTL_SPMM_ULL\n"; |
---|
137 | #endif |
---|
138 | |
---|
139 | |
---|
140 | #ifdef NTL_SPMM_ASM |
---|
141 | cerr << "NTL_SPMM_ASM\n"; |
---|
142 | #endif |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | #ifdef NTL_AVOID_BRANCHING |
---|
148 | cerr << "NTL_AVOID_BRANCHING\n"; |
---|
149 | #endif |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | #ifdef NTL_TBL_REM |
---|
154 | cerr << "NTL_TBL_REM\n"; |
---|
155 | #endif |
---|
156 | |
---|
157 | |
---|
158 | #ifdef NTL_GF2X_ALTCODE |
---|
159 | cerr << "NTL_GF2X_ALTCODE\n"; |
---|
160 | #endif |
---|
161 | |
---|
162 | #ifdef NTL_GF2X_ALTCODE1 |
---|
163 | cerr << "NTL_GF2X_ALTCODE1\n"; |
---|
164 | #endif |
---|
165 | |
---|
166 | |
---|
167 | #ifdef NTL_GF2X_NOINLINE |
---|
168 | cerr << "NTL_GF2X_NOINLINE\n"; |
---|
169 | #endif |
---|
170 | |
---|
171 | cerr << "\n\n"; |
---|
172 | |
---|
173 | if (_ntl_gmp_hack) |
---|
174 | cerr << "using GMP hack\n"; |
---|
175 | |
---|
176 | long n, k; |
---|
177 | |
---|
178 | n = 200; |
---|
179 | k = 10*NTL_ZZ_NBITS; |
---|
180 | |
---|
181 | ZZ p; |
---|
182 | |
---|
183 | GenPrime(p, k); |
---|
184 | |
---|
185 | |
---|
186 | ZZ_p::init(p); // initialization |
---|
187 | |
---|
188 | ZZ_pX f, g, h, r1, r2, r3; |
---|
189 | |
---|
190 | random(g, n); // g = random polynomial of degree < n |
---|
191 | random(h, n); // h = " " |
---|
192 | random(f, n); // f = " " |
---|
193 | |
---|
194 | // SetCoeff(f, n); // Sets coefficient of X^n to 1 |
---|
195 | |
---|
196 | ZZ_p lc; |
---|
197 | |
---|
198 | do { |
---|
199 | random(lc); |
---|
200 | } while (IsZero(lc)); |
---|
201 | |
---|
202 | SetCoeff(f, n, lc); |
---|
203 | |
---|
204 | |
---|
205 | // For doing arithmetic mod f quickly, one must pre-compute |
---|
206 | // some information. |
---|
207 | |
---|
208 | ZZ_pXModulus F; |
---|
209 | build(F, f); |
---|
210 | |
---|
211 | PlainMul(r1, g, h); // this uses classical arithmetic |
---|
212 | PlainRem(r1, r1, f); |
---|
213 | |
---|
214 | MulMod(r2, g, h, F); // this uses the FFT |
---|
215 | |
---|
216 | MulMod(r3, g, h, f); // uses FFT, but slower |
---|
217 | |
---|
218 | // compare the results... |
---|
219 | |
---|
220 | if (r1 != r2) { |
---|
221 | cerr << "r1 != r2!!\n"; |
---|
222 | return 1; |
---|
223 | } |
---|
224 | else if (r1 != r3) { |
---|
225 | cerr << "r1 != r3!!\n"; |
---|
226 | return 1; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | // small prime tests...I've made some changes in v5.3 |
---|
231 | // that should be checked on various platforms, so |
---|
232 | // we might as well check them here. |
---|
233 | |
---|
234 | if (SmallModulusTest(17, 1000)) { |
---|
235 | cerr << "first SmallModulusTest failed!!\n"; |
---|
236 | return 1; |
---|
237 | } |
---|
238 | |
---|
239 | if (SmallModulusTest((1L << (NTL_SP_NBITS))-1, 1000)) { |
---|
240 | cerr << "second SmallModulusTest failed!!\n"; |
---|
241 | return 1; |
---|
242 | } |
---|
243 | |
---|
244 | cerr << "test is OK\n"; |
---|
245 | |
---|
246 | ZZ x1, x2, x3, x4; |
---|
247 | double t; |
---|
248 | long i; |
---|
249 | |
---|
250 | RandomLen(x1, 1024); |
---|
251 | RandomBnd(x2, x1); |
---|
252 | RandomBnd(x3, x1); |
---|
253 | |
---|
254 | mul(x4, x2, x3); |
---|
255 | |
---|
256 | t = GetTime(); |
---|
257 | for (i = 0; i < 100000; i++) |
---|
258 | mul(x4, x2, x3); |
---|
259 | t = GetTime()-t; |
---|
260 | |
---|
261 | cerr << "time for 1024-bit mul: " << t*10 << "us"; |
---|
262 | |
---|
263 | if (_ntl_gmp_hack) { |
---|
264 | _ntl_gmp_hack = 0; |
---|
265 | mul(x4, x2, x3); |
---|
266 | |
---|
267 | t = GetTime(); |
---|
268 | for (i = 0; i < 100000; i++) |
---|
269 | mul(x4, x2, x3); |
---|
270 | t = GetTime()-t; |
---|
271 | |
---|
272 | cerr << " (" << (t*10) << "us without GMP)"; |
---|
273 | |
---|
274 | _ntl_gmp_hack = 1; |
---|
275 | } |
---|
276 | |
---|
277 | cerr << "\n"; |
---|
278 | |
---|
279 | rem(x2, x4, x1); |
---|
280 | |
---|
281 | t = GetTime(); |
---|
282 | for (i = 0; i < 100000; i++) |
---|
283 | rem(x2, x4, x1); |
---|
284 | t = GetTime()-t; |
---|
285 | |
---|
286 | cerr << "time for 2048/1024-bit rem: " << t*10 << "us"; |
---|
287 | |
---|
288 | if (_ntl_gmp_hack) { |
---|
289 | _ntl_gmp_hack = 0; |
---|
290 | rem(x2, x4, x1); |
---|
291 | |
---|
292 | t = GetTime(); |
---|
293 | for (i = 0; i < 100000; i++) |
---|
294 | rem(x2, x4, x1); |
---|
295 | t = GetTime()-t; |
---|
296 | cerr << " (" << (t*10) << "us without GMP)"; |
---|
297 | |
---|
298 | _ntl_gmp_hack = 1; |
---|
299 | } |
---|
300 | |
---|
301 | cerr << "\n"; |
---|
302 | |
---|
303 | |
---|
304 | GenPrime(p, 1024); |
---|
305 | RandomBnd(x1, p); |
---|
306 | if (IsZero(x1)) set(x1); |
---|
307 | |
---|
308 | InvMod(x2, x1, p); |
---|
309 | |
---|
310 | t = GetTime(); |
---|
311 | for (i = 0; i < 1000; i++) |
---|
312 | InvMod(x2, x1, p); |
---|
313 | t = GetTime()-t; |
---|
314 | |
---|
315 | cerr << "time for 1024-bit modular inverse: " << t*1000 << "us"; |
---|
316 | |
---|
317 | if (_ntl_gmp_hack) { |
---|
318 | _ntl_gmp_hack = 0; |
---|
319 | InvMod(x2, x1, p); |
---|
320 | |
---|
321 | t = GetTime(); |
---|
322 | for (i = 0; i < 1000; i++) |
---|
323 | InvMod(x2, x1, p); |
---|
324 | t = GetTime()-t; |
---|
325 | cerr << " (" << (t*1000) << "us without GMP)"; |
---|
326 | |
---|
327 | _ntl_gmp_hack = 1; |
---|
328 | } |
---|
329 | |
---|
330 | cerr << "\n"; |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | // test modulus switching |
---|
335 | |
---|
336 | n = 1024; |
---|
337 | k = 1024; |
---|
338 | RandomLen(p, k); |
---|
339 | |
---|
340 | ZZ_p::init(p); |
---|
341 | ZZ_pInfo->check(); |
---|
342 | |
---|
343 | ZZ_pX j1, j2, j3; |
---|
344 | |
---|
345 | random(j1, n); |
---|
346 | random(j2, n); |
---|
347 | |
---|
348 | t = GetTime(); |
---|
349 | for (i = 0; i < 20; i++) mul(j3, j1, j2); |
---|
350 | t = GetTime()-t; |
---|
351 | |
---|
352 | cerr << "time to multiply degree 1023 polynomials\n modulo a 1024-bit number: "; |
---|
353 | cerr << (t/20) << "s"; |
---|
354 | |
---|
355 | if (_ntl_gmp_hack) { |
---|
356 | _ntl_gmp_hack = 0; |
---|
357 | |
---|
358 | ZZ_p::init(p); |
---|
359 | ZZ_pInfo->check(); |
---|
360 | |
---|
361 | t = GetTime(); |
---|
362 | for (i = 0; i < 20; i++) mul(j3, j1, j2); |
---|
363 | t = GetTime()-t; |
---|
364 | |
---|
365 | cerr << " (" << (t/20) << "s without GMP)"; |
---|
366 | _ntl_gmp_hack = 1; |
---|
367 | } |
---|
368 | |
---|
369 | cerr << "\n"; |
---|
370 | |
---|
371 | return 0; |
---|
372 | } |
---|