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