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