1 | |
---|
2 | /**************************************************************************\ |
---|
3 | |
---|
4 | MODULE: ZZ_pX |
---|
5 | |
---|
6 | SUMMARY: |
---|
7 | |
---|
8 | The class ZZ_pX implements polynomial arithmetic modulo p. |
---|
9 | |
---|
10 | Polynomial arithmetic is implemented using the FFT, combined with the |
---|
11 | Chinese Remainder Theorem. A more detailed description of the |
---|
12 | techniques used here can be found in [Shoup, J. Symbolic |
---|
13 | Comp. 20:363-397, 1995]. |
---|
14 | |
---|
15 | Small degree polynomials are multiplied either with classical |
---|
16 | or Karatsuba algorithms. |
---|
17 | |
---|
18 | \**************************************************************************/ |
---|
19 | |
---|
20 | #include <NTL/ZZ_p.h> |
---|
21 | #include <NTL/vec_ZZ_p.h> |
---|
22 | |
---|
23 | class ZZ_pX { |
---|
24 | public: |
---|
25 | |
---|
26 | ZZ_pX(); // initialize to 0 |
---|
27 | |
---|
28 | ZZ_pX(const ZZ_pX& a); // copy constructor |
---|
29 | |
---|
30 | ZZ_pX& operator=(const ZZ_pX& a); // assignment |
---|
31 | ZZ_pX& operator=(const ZZ_p& a); // assignment |
---|
32 | ZZ_pX& operator=(const long a); // assignment |
---|
33 | |
---|
34 | ZZ_pX(long i, const ZZ_p& c); // initialize to X^i*c |
---|
35 | ZZ_pX(long i, long c); |
---|
36 | |
---|
37 | ~ZZ_pX(); // destructor |
---|
38 | |
---|
39 | }; |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | /**************************************************************************\ |
---|
47 | |
---|
48 | Comparison |
---|
49 | |
---|
50 | \**************************************************************************/ |
---|
51 | |
---|
52 | |
---|
53 | long operator==(const ZZ_pX& a, const ZZ_pX& b); |
---|
54 | long operator!=(const ZZ_pX& a, const ZZ_pX& b); |
---|
55 | |
---|
56 | // PROMOTIONS: operators ==, != promote {long, ZZ_p} to ZZ_pX on (a, b). |
---|
57 | |
---|
58 | long IsZero(const ZZ_pX& a); // test for 0 |
---|
59 | long IsOne(const ZZ_pX& a); // test for 1 |
---|
60 | |
---|
61 | |
---|
62 | /**************************************************************************\ |
---|
63 | |
---|
64 | Addition |
---|
65 | |
---|
66 | \**************************************************************************/ |
---|
67 | |
---|
68 | |
---|
69 | // operator notation: |
---|
70 | |
---|
71 | ZZ_pX operator+(const ZZ_pX& a, const ZZ_pX& b); |
---|
72 | ZZ_pX operator-(const ZZ_pX& a, const ZZ_pX& b); |
---|
73 | |
---|
74 | ZZ_pX operator-(const ZZ_pX& a); // unary - |
---|
75 | |
---|
76 | ZZ_pX& operator+=(ZZ_pX& x, const ZZ_pX& a); |
---|
77 | ZZ_pX& operator+=(ZZ_pX& x, const ZZ_p& a); |
---|
78 | ZZ_pX& operator+=(ZZ_pX& x, long a); |
---|
79 | |
---|
80 | ZZ_pX& operator-=(ZZ_pX& x, const ZZ_pX& a); |
---|
81 | ZZ_pX& operator-=(ZZ_pX& x, const ZZ_p& a); |
---|
82 | ZZ_pX& operator-=(ZZ_pX& x, long a); |
---|
83 | |
---|
84 | ZZ_pX& operator++(ZZ_pX& x); // prefix |
---|
85 | void operator++(ZZ_pX& x, int); // postfix |
---|
86 | |
---|
87 | ZZ_pX& operator--(ZZ_pX& x); // prefix |
---|
88 | void operator--(ZZ_pX& x, int); // postfix |
---|
89 | |
---|
90 | // procedural versions: |
---|
91 | |
---|
92 | |
---|
93 | void add(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); // x = a + b |
---|
94 | void sub(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); // x = a - b |
---|
95 | void negate(ZZ_pX& x, const ZZ_pX& a); // x = -a |
---|
96 | |
---|
97 | |
---|
98 | // PROMOTIONS: binary +, - and procedures add, sub promote |
---|
99 | // {long, ZZ_p} to ZZ_pX on (a, b). |
---|
100 | |
---|
101 | |
---|
102 | /**************************************************************************\ |
---|
103 | |
---|
104 | Multiplication |
---|
105 | |
---|
106 | \**************************************************************************/ |
---|
107 | |
---|
108 | // operator notation: |
---|
109 | |
---|
110 | ZZ_pX operator*(const ZZ_pX& a, const ZZ_pX& b); |
---|
111 | |
---|
112 | ZZ_pX& operator*=(ZZ_pX& x, const ZZ_pX& a); |
---|
113 | ZZ_pX& operator*=(ZZ_pX& x, const ZZ_p& a); |
---|
114 | ZZ_pX& operator*=(ZZ_pX& x, long a); |
---|
115 | |
---|
116 | // procedural versions: |
---|
117 | |
---|
118 | void mul(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); // x = a * b |
---|
119 | |
---|
120 | void sqr(ZZ_pX& x, const ZZ_pX& a); // x = a^2 |
---|
121 | ZZ_pX sqr(const ZZ_pX& a); |
---|
122 | |
---|
123 | // PROMOTIONS: operator * and procedure mul promote {long, ZZ_p} to ZZ_pX |
---|
124 | // on (a, b). |
---|
125 | |
---|
126 | void power(ZZ_pX& x, const ZZ_pX& a, long e); // x = a^e (e >= 0) |
---|
127 | ZZ_pX power(const ZZ_pX& a, long e); |
---|
128 | |
---|
129 | |
---|
130 | /**************************************************************************\ |
---|
131 | |
---|
132 | Shift Operations |
---|
133 | |
---|
134 | LeftShift by n means multiplication by X^n |
---|
135 | RightShift by n means division by X^n |
---|
136 | |
---|
137 | A negative shift amount reverses the direction of the shift. |
---|
138 | |
---|
139 | \**************************************************************************/ |
---|
140 | |
---|
141 | // operator notation: |
---|
142 | |
---|
143 | ZZ_pX operator<<(const ZZ_pX& a, long n); |
---|
144 | ZZ_pX operator>>(const ZZ_pX& a, long n); |
---|
145 | |
---|
146 | ZZ_pX& operator<<=(ZZ_pX& x, long n); |
---|
147 | ZZ_pX& operator>>=(ZZ_pX& x, long n); |
---|
148 | |
---|
149 | // procedural versions: |
---|
150 | |
---|
151 | void LeftShift(ZZ_pX& x, const ZZ_pX& a, long n); |
---|
152 | ZZ_pX LeftShift(const ZZ_pX& a, long n); |
---|
153 | |
---|
154 | void RightShift(ZZ_pX& x, const ZZ_pX& a, long n); |
---|
155 | ZZ_pX RightShift(const ZZ_pX& a, long n); |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | /**************************************************************************\ |
---|
160 | |
---|
161 | Division |
---|
162 | |
---|
163 | \**************************************************************************/ |
---|
164 | |
---|
165 | // operator notation: |
---|
166 | |
---|
167 | ZZ_pX operator/(const ZZ_pX& a, const ZZ_pX& b); |
---|
168 | ZZ_pX operator/(const ZZ_pX& a, const ZZ_p& b); |
---|
169 | ZZ_pX operator/(const ZZ_pX& a, long b); |
---|
170 | |
---|
171 | |
---|
172 | ZZ_pX& operator/=(ZZ_pX& x, const ZZ_pX& b); |
---|
173 | ZZ_pX& operator/=(ZZ_pX& x, const ZZ_p& b); |
---|
174 | ZZ_pX& operator/=(ZZ_pX& x, long b); |
---|
175 | |
---|
176 | ZZ_pX operator%(const ZZ_pX& a, const ZZ_pX& b); |
---|
177 | |
---|
178 | ZZ_pX& operator%=(ZZ_pX& x, const ZZ_pX& b); |
---|
179 | |
---|
180 | |
---|
181 | // procedural versions: |
---|
182 | |
---|
183 | |
---|
184 | void DivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b); |
---|
185 | // q = a/b, r = a%b |
---|
186 | |
---|
187 | void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b); |
---|
188 | void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_p& b); |
---|
189 | void div(ZZ_pX& q, const ZZ_pX& a, long b); |
---|
190 | // q = a/b |
---|
191 | |
---|
192 | void rem(ZZ_pX& r, const ZZ_pX& a, const ZZ_pX& b); |
---|
193 | // r = a%b |
---|
194 | |
---|
195 | long divide(ZZ_pX& q, const ZZ_pX& a, const ZZ_pX& b); |
---|
196 | // if b | a, sets q = a/b and returns 1; otherwise returns 0 |
---|
197 | |
---|
198 | long divide(const ZZ_pX& a, const ZZ_pX& b); |
---|
199 | // if b | a, sets q = a/b and returns 1; otherwise returns 0 |
---|
200 | |
---|
201 | |
---|
202 | /**************************************************************************\ |
---|
203 | |
---|
204 | GCD's |
---|
205 | |
---|
206 | These routines are intended for use when p is prime. |
---|
207 | |
---|
208 | \**************************************************************************/ |
---|
209 | |
---|
210 | |
---|
211 | void GCD(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b); |
---|
212 | ZZ_pX GCD(const ZZ_pX& a, const ZZ_pX& b); |
---|
213 | // x = GCD(a, b), x is always monic (or zero if a==b==0). |
---|
214 | |
---|
215 | |
---|
216 | void XGCD(ZZ_pX& d, ZZ_pX& s, ZZ_pX& t, const ZZ_pX& a, const ZZ_pX& b); |
---|
217 | // d = gcd(a,b), a s + b t = d |
---|
218 | |
---|
219 | |
---|
220 | // NOTE: A classical algorithm is used, switching over to a |
---|
221 | // "half-GCD" algorithm for large degree |
---|
222 | |
---|
223 | |
---|
224 | /**************************************************************************\ |
---|
225 | |
---|
226 | Input/Output |
---|
227 | |
---|
228 | I/O format: |
---|
229 | |
---|
230 | [a_0 a_1 ... a_n], |
---|
231 | |
---|
232 | represents the polynomial a_0 + a_1*X + ... + a_n*X^n. |
---|
233 | |
---|
234 | On output, all coefficients will be integers between 0 and p-1, and |
---|
235 | a_n not zero (the zero polynomial is [ ]). On input, the coefficients |
---|
236 | are arbitrary integers which are reduced modulo p, and leading zeros |
---|
237 | stripped. |
---|
238 | |
---|
239 | \**************************************************************************/ |
---|
240 | |
---|
241 | istream& operator>>(istream& s, ZZ_pX& x); |
---|
242 | ostream& operator<<(ostream& s, const ZZ_pX& a); |
---|
243 | |
---|
244 | |
---|
245 | /**************************************************************************\ |
---|
246 | |
---|
247 | Some utility routines |
---|
248 | |
---|
249 | \**************************************************************************/ |
---|
250 | |
---|
251 | long deg(const ZZ_pX& a); // return deg(a); deg(0) == -1. |
---|
252 | |
---|
253 | const ZZ_p& coeff(const ZZ_pX& a, long i); |
---|
254 | // returns a read-only reference to the coefficient of X^i, or zero if |
---|
255 | // i not in range |
---|
256 | |
---|
257 | const ZZ_p& LeadCoeff(const ZZ_pX& a); |
---|
258 | // read-only reference to leading term of a, or zero if a == 0 |
---|
259 | |
---|
260 | const ZZ_p& ConstTerm(const ZZ_pX& a); |
---|
261 | // read-only reference to constant term of a, or zero if a == 0 |
---|
262 | |
---|
263 | void SetCoeff(ZZ_pX& x, long i, const ZZ_p& a); |
---|
264 | void SetCoeff(ZZ_pX& x, long i, long a); |
---|
265 | // makes coefficient of X^i equal to a; error is raised if i < 0 |
---|
266 | |
---|
267 | void SetCoeff(ZZ_pX& x, long i); |
---|
268 | // makes coefficient of X^i equal to 1; error is raised if i < 0 |
---|
269 | |
---|
270 | void SetX(ZZ_pX& x); // x is set to the monomial X |
---|
271 | |
---|
272 | long IsX(const ZZ_pX& a); // test if x = X |
---|
273 | |
---|
274 | void diff(ZZ_pX& x, const ZZ_pX& a); // x = derivative of a |
---|
275 | ZZ_pX diff(const ZZ_pX& a); |
---|
276 | |
---|
277 | void MakeMonic(ZZ_pX& x); |
---|
278 | // if x != 0 makes x into its monic associate; LeadCoeff(x) must be |
---|
279 | // invertible in this case. |
---|
280 | |
---|
281 | void reverse(ZZ_pX& x, const ZZ_pX& a, long hi); |
---|
282 | ZZ_pX reverse(const ZZ_pX& a, long hi); |
---|
283 | |
---|
284 | void reverse(ZZ_pX& x, const ZZ_pX& a); |
---|
285 | ZZ_pX reverse(const ZZ_pX& a); |
---|
286 | |
---|
287 | // x = reverse of a[0]..a[hi] (hi >= -1); |
---|
288 | // hi defaults to deg(a) in second version |
---|
289 | |
---|
290 | void VectorCopy(vec_ZZ_p& x, const ZZ_pX& a, long n); |
---|
291 | vec_ZZ_p VectorCopy(const ZZ_pX& a, long n); |
---|
292 | // x = copy of coefficient vector of a of length exactly n. |
---|
293 | // input is truncated or padded with zeroes as appropriate. |
---|
294 | |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | /**************************************************************************\ |
---|
299 | |
---|
300 | Random Polynomials |
---|
301 | |
---|
302 | \**************************************************************************/ |
---|
303 | |
---|
304 | void random(ZZ_pX& x, long n); |
---|
305 | ZZ_pX random_ZZ_pX(long n); |
---|
306 | // generate a random polynomial of degree < n |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | /**************************************************************************\ |
---|
311 | |
---|
312 | Polynomial Evaluation and related problems |
---|
313 | |
---|
314 | \**************************************************************************/ |
---|
315 | |
---|
316 | |
---|
317 | void BuildFromRoots(ZZ_pX& x, const vec_ZZ_p& a); |
---|
318 | ZZ_pX BuildFromRoots(const vec_ZZ_p& a); |
---|
319 | // computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length() |
---|
320 | |
---|
321 | void eval(ZZ_p& b, const ZZ_pX& f, const ZZ_p& a); |
---|
322 | ZZ_p eval(const ZZ_pX& f, const ZZ_p& a); |
---|
323 | // b = f(a) |
---|
324 | |
---|
325 | void eval(vec_ZZ_p& b, const ZZ_pX& f, const vec_ZZ_p& a); |
---|
326 | vec_ZZ_p eval(const ZZ_pX& f, const vec_ZZ_p& a); |
---|
327 | // b.SetLength(a.length()). b[i] = f(a[i]) for 0 <= i < a.length() |
---|
328 | |
---|
329 | void interpolate(ZZ_pX& f, const vec_ZZ_p& a, const vec_ZZ_p& b); |
---|
330 | ZZ_pX interpolate(const vec_ZZ_p& a, const vec_ZZ_p& b); |
---|
331 | // interpolates the polynomial f satisfying f(a[i]) = b[i]. p should |
---|
332 | // be prime. |
---|
333 | |
---|
334 | /**************************************************************************\ |
---|
335 | |
---|
336 | Arithmetic mod X^n |
---|
337 | |
---|
338 | All routines require n >= 0, otherwise an error is raised. |
---|
339 | |
---|
340 | \**************************************************************************/ |
---|
341 | |
---|
342 | void trunc(ZZ_pX& x, const ZZ_pX& a, long n); // x = a % X^n |
---|
343 | ZZ_pX trunc(const ZZ_pX& a, long n); |
---|
344 | |
---|
345 | void MulTrunc(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, long n); |
---|
346 | ZZ_pX MulTrunc(const ZZ_pX& a, const ZZ_pX& b, long n); |
---|
347 | // x = a * b % X^n |
---|
348 | |
---|
349 | void SqrTrunc(ZZ_pX& x, const ZZ_pX& a, long n); |
---|
350 | ZZ_pX SqrTrunc(const ZZ_pX& a, long n); |
---|
351 | // x = a^2 % X^n |
---|
352 | |
---|
353 | void InvTrunc(ZZ_pX& x, const ZZ_pX& a, long n); |
---|
354 | ZZ_pX InvTrunc(const ZZ_pX& a, long n); |
---|
355 | // computes x = a^{-1} % X^m. Must have ConstTerm(a) invertible. |
---|
356 | |
---|
357 | /**************************************************************************\ |
---|
358 | |
---|
359 | Modular Arithmetic (without pre-conditioning) |
---|
360 | |
---|
361 | Arithmetic mod f. |
---|
362 | |
---|
363 | All inputs and outputs are polynomials of degree less than deg(f), and |
---|
364 | deg(f) > 0. |
---|
365 | |
---|
366 | NOTE: if you want to do many computations with a fixed f, use the |
---|
367 | ZZ_pXModulus data structure and associated routines below for better |
---|
368 | performance. |
---|
369 | |
---|
370 | \**************************************************************************/ |
---|
371 | |
---|
372 | void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, const ZZ_pX& f); |
---|
373 | ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pX& b, const ZZ_pX& f); |
---|
374 | // x = (a * b) % f |
---|
375 | |
---|
376 | void SqrMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f); |
---|
377 | ZZ_pX SqrMod(const ZZ_pX& a, const ZZ_pX& f); |
---|
378 | // x = a^2 % f |
---|
379 | |
---|
380 | void MulByXMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f); |
---|
381 | ZZ_pX MulByXMod(const ZZ_pX& a, const ZZ_pX& f); |
---|
382 | // x = (a * X) mod f |
---|
383 | |
---|
384 | void InvMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f); |
---|
385 | ZZ_pX InvMod(const ZZ_pX& a, const ZZ_pX& f); |
---|
386 | // x = a^{-1} % f, error is a is not invertible |
---|
387 | |
---|
388 | long InvModStatus(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& f); |
---|
389 | // if (a, f) = 1, returns 0 and sets x = a^{-1} % f; otherwise, |
---|
390 | // returns 1 and sets x = (a, f) |
---|
391 | |
---|
392 | |
---|
393 | // for modular exponentiation, see below |
---|
394 | |
---|
395 | |
---|
396 | |
---|
397 | /**************************************************************************\ |
---|
398 | |
---|
399 | Modular Arithmetic with Pre-Conditioning |
---|
400 | |
---|
401 | If you need to do a lot of arithmetic modulo a fixed f, build a |
---|
402 | ZZ_pXModulus F for f. This pre-computes information about f that |
---|
403 | speeds up subsequent computations. |
---|
404 | |
---|
405 | It is required that deg(f) > 0 and that LeadCoeff(f) is invertible. |
---|
406 | |
---|
407 | As an example, the following routine computes the product modulo f of a vector |
---|
408 | of polynomials. |
---|
409 | |
---|
410 | #include <NTL/ZZ_pX.h> |
---|
411 | |
---|
412 | void product(ZZ_pX& x, const vec_ZZ_pX& v, const ZZ_pX& f) |
---|
413 | { |
---|
414 | ZZ_pXModulus F(f); |
---|
415 | ZZ_pX res; |
---|
416 | res = 1; |
---|
417 | long i; |
---|
418 | for (i = 0; i < v.length(); i++) |
---|
419 | MulMod(res, res, v[i], F); |
---|
420 | x = res; |
---|
421 | } |
---|
422 | |
---|
423 | Note that automatic conversions are provided so that a ZZ_pX can |
---|
424 | be used wherever a ZZ_pXModulus is required, and a ZZ_pXModulus |
---|
425 | can be used wherever a ZZ_pX is required. |
---|
426 | |
---|
427 | \**************************************************************************/ |
---|
428 | |
---|
429 | |
---|
430 | class ZZ_pXModulus { |
---|
431 | public: |
---|
432 | ZZ_pXModulus(); // initially in an unusable state |
---|
433 | |
---|
434 | ZZ_pXModulus(const ZZ_pXModulus&); // copy |
---|
435 | |
---|
436 | ZZ_pXModulus& operator=(const ZZ_pXModulus&); // assignment |
---|
437 | |
---|
438 | ~ZZ_pXModulus(); |
---|
439 | |
---|
440 | ZZ_pXModulus(const ZZ_pX& f); // initialize with f, deg(f) > 0 |
---|
441 | |
---|
442 | operator const ZZ_pX& () const; |
---|
443 | // read-only access to f, implicit conversion operator |
---|
444 | |
---|
445 | const ZZ_pX& val() const; |
---|
446 | // read-only access to f, explicit notation |
---|
447 | |
---|
448 | }; |
---|
449 | |
---|
450 | void build(ZZ_pXModulus& F, const ZZ_pX& f); |
---|
451 | // pre-computes information about f and stores it in F. |
---|
452 | // Note that the declaration ZZ_pXModulus F(f) is equivalent to |
---|
453 | // ZZ_pXModulus F; build(F, f). |
---|
454 | |
---|
455 | // In the following, f refers to the polynomial f supplied to the |
---|
456 | // build routine, and n = deg(f). |
---|
457 | |
---|
458 | long deg(const ZZ_pXModulus& F); // return n=deg(f) |
---|
459 | |
---|
460 | void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, const ZZ_pXModulus& F); |
---|
461 | ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pX& b, const ZZ_pXModulus& F); |
---|
462 | // x = (a * b) % f; deg(a), deg(b) < n |
---|
463 | |
---|
464 | void SqrMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
465 | ZZ_pX SqrMod(const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
466 | // x = a^2 % f; deg(a) < n |
---|
467 | |
---|
468 | void PowerMod(ZZ_pX& x, const ZZ_pX& a, const ZZ& e, const ZZ_pXModulus& F); |
---|
469 | ZZ_pX PowerMod(const ZZ_pX& a, const ZZ& e, const ZZ_pXModulus& F); |
---|
470 | |
---|
471 | void PowerMod(ZZ_pX& x, const ZZ_pX& a, long e, const ZZ_pXModulus& F); |
---|
472 | ZZ_pX PowerMod(const ZZ_pX& a, long e, const ZZ_pXModulus& F); |
---|
473 | |
---|
474 | // x = a^e % f; deg(a) < n (e may be negative) |
---|
475 | |
---|
476 | void PowerXMod(ZZ_pX& x, const ZZ& e, const ZZ_pXModulus& F); |
---|
477 | ZZ_pX PowerXMod(const ZZ& e, const ZZ_pXModulus& F); |
---|
478 | |
---|
479 | void PowerXMod(ZZ_pX& x, long e, const ZZ_pXModulus& F); |
---|
480 | ZZ_pX PowerXMod(long e, const ZZ_pXModulus& F); |
---|
481 | |
---|
482 | // x = X^e % f (e may be negative) |
---|
483 | |
---|
484 | void PowerXPlusAMod(ZZ_pX& x, const ZZ_p& a, const ZZ& e, |
---|
485 | const ZZ_pXModulus& F); |
---|
486 | |
---|
487 | ZZ_pX PowerXPlusAMod(const ZZ_p& a, const ZZ& e, |
---|
488 | const ZZ_pXModulus& F); |
---|
489 | |
---|
490 | void PowerXPlusAMod(ZZ_pX& x, const ZZ_p& a, long e, |
---|
491 | const ZZ_pXModulus& F); |
---|
492 | |
---|
493 | ZZ_pX PowerXPlusAMod(const ZZ_p& a, long e, |
---|
494 | const ZZ_pXModulus& F); |
---|
495 | |
---|
496 | // x = (X + a)^e % f (e may be negative) |
---|
497 | |
---|
498 | |
---|
499 | void rem(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
500 | // x = a % f |
---|
501 | |
---|
502 | void DivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
503 | // q = a/f, r = a%f |
---|
504 | |
---|
505 | void div(ZZ_pX& q, const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
506 | // q = a/f |
---|
507 | |
---|
508 | // operator notation: |
---|
509 | |
---|
510 | ZZ_pX operator/(const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
511 | ZZ_pX operator%(const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
512 | |
---|
513 | ZZ_pX& operator/=(ZZ_pX& x, const ZZ_pXModulus& F); |
---|
514 | ZZ_pX& operator%=(ZZ_pX& x, const ZZ_pXModulus& F); |
---|
515 | |
---|
516 | |
---|
517 | |
---|
518 | /**************************************************************************\ |
---|
519 | |
---|
520 | |
---|
521 | More Pre-Conditioning |
---|
522 | |
---|
523 | If you need to compute a * b % f for a fixed b, but for many a's, it |
---|
524 | is much more efficient to first build a ZZ_pXMultiplier B for b, and |
---|
525 | then use the MulMod routine below. |
---|
526 | |
---|
527 | Here is an example that multiplies each element of a vector by a fixed |
---|
528 | polynomial modulo f. |
---|
529 | |
---|
530 | #include <NTL/ZZ_pX.h> |
---|
531 | |
---|
532 | void mul(vec_ZZ_pX& v, const ZZ_pX& b, const ZZ_pX& f) |
---|
533 | { |
---|
534 | ZZ_pXModulus F(f); |
---|
535 | ZZ_pXMultiplier B(b, F); |
---|
536 | long i; |
---|
537 | for (i = 0; i < v.length(); i++) |
---|
538 | MulMod(v[i], v[i], B, F); |
---|
539 | } |
---|
540 | |
---|
541 | \**************************************************************************/ |
---|
542 | |
---|
543 | |
---|
544 | class ZZ_pXMultiplier { |
---|
545 | public: |
---|
546 | ZZ_pXMultiplier(); // initially zero |
---|
547 | |
---|
548 | ZZ_pXMultiplier(const ZZ_pX& b, const ZZ_pXModulus& F); |
---|
549 | // initializes with b mod F, where deg(b) < deg(F) |
---|
550 | |
---|
551 | ZZ_pXMultiplier(const ZZ_pXMultiplier&); // copy |
---|
552 | |
---|
553 | ZZ_pXMultiplier& operator=(const ZZ_pXMultiplier&); // assignment |
---|
554 | |
---|
555 | ~ZZ_pXMultiplier(); |
---|
556 | |
---|
557 | const ZZ_pX& val() const; // read-only access to b |
---|
558 | |
---|
559 | }; |
---|
560 | |
---|
561 | |
---|
562 | void build(ZZ_pXMultiplier& B, const ZZ_pX& b, const ZZ_pXModulus& F); |
---|
563 | // pre-computes information about b and stores it in B; deg(b) < |
---|
564 | // deg(F) |
---|
565 | |
---|
566 | void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXMultiplier& B, |
---|
567 | const ZZ_pXModulus& F); |
---|
568 | |
---|
569 | // x = (a * b) % F; deg(a) < deg(F) |
---|
570 | |
---|
571 | /**************************************************************************\ |
---|
572 | |
---|
573 | vectors of ZZ_pX's |
---|
574 | |
---|
575 | \**************************************************************************/ |
---|
576 | |
---|
577 | NTL_vector_decl(ZZ_pX,vec_ZZ_pX) |
---|
578 | // vec_ZZ_pX |
---|
579 | |
---|
580 | NTL_eq_vector_decl(ZZ_pX,vec_ZZ_pX) |
---|
581 | // == and != |
---|
582 | |
---|
583 | NTL_io_vector_decl(ZZ_pX,vec_ZZ_pX) |
---|
584 | // I/O operators |
---|
585 | |
---|
586 | |
---|
587 | /**************************************************************************\ |
---|
588 | |
---|
589 | Modular Composition |
---|
590 | |
---|
591 | Modular composition is the problem of computing g(h) mod f for |
---|
592 | polynomials f, g, and h. |
---|
593 | |
---|
594 | The algorithm employed is that of Brent & Kung (Fast algorithms for |
---|
595 | manipulating formal power series, JACM 25:581-595, 1978), which uses |
---|
596 | O(n^{1/2}) modular polynomial multiplications, and O(n^2) scalar |
---|
597 | operations. |
---|
598 | |
---|
599 | |
---|
600 | \**************************************************************************/ |
---|
601 | |
---|
602 | void CompMod(ZZ_pX& x, const ZZ_pX& g, const ZZ_pX& h, const ZZ_pXModulus& F); |
---|
603 | ZZ_pX CompMod(const ZZ_pX& g, const ZZ_pX& h, |
---|
604 | const ZZ_pXModulus& F); |
---|
605 | |
---|
606 | // x = g(h) mod f; deg(h) < n |
---|
607 | |
---|
608 | void Comp2Mod(ZZ_pX& x1, ZZ_pX& x2, const ZZ_pX& g1, const ZZ_pX& g2, |
---|
609 | const ZZ_pX& h, const ZZ_pXModulus& F); |
---|
610 | // xi = gi(h) mod f (i=1,2); deg(h) < n. |
---|
611 | |
---|
612 | void Comp3Mod(ZZ_pX& x1, ZZ_pX& x2, ZZ_pX& x3, |
---|
613 | const ZZ_pX& g1, const ZZ_pX& g2, const ZZ_pX& g3, |
---|
614 | const ZZ_pX& h, const ZZ_pXModulus& F); |
---|
615 | // xi = gi(h) mod f (i=1..3); deg(h) < n. |
---|
616 | |
---|
617 | |
---|
618 | /**************************************************************************\ |
---|
619 | |
---|
620 | Composition with Pre-Conditioning |
---|
621 | |
---|
622 | If a single h is going to be used with many g's then you should build |
---|
623 | a ZZ_pXArgument for h, and then use the compose routine below. The |
---|
624 | routine build computes and stores h, h^2, ..., h^m mod f. After this |
---|
625 | pre-computation, composing a polynomial of degree roughly n with h |
---|
626 | takes n/m multiplies mod f, plus n^2 scalar multiplies. Thus, |
---|
627 | increasing m increases the space requirement and the pre-computation |
---|
628 | time, but reduces the composition time. |
---|
629 | |
---|
630 | \**************************************************************************/ |
---|
631 | |
---|
632 | |
---|
633 | struct ZZ_pXArgument { |
---|
634 | vec_ZZ_pX H; |
---|
635 | }; |
---|
636 | |
---|
637 | void build(ZZ_pXArgument& H, const ZZ_pX& h, const ZZ_pXModulus& F, long m); |
---|
638 | // Pre-Computes information about h. m > 0, deg(h) < n. |
---|
639 | |
---|
640 | void CompMod(ZZ_pX& x, const ZZ_pX& g, const ZZ_pXArgument& H, |
---|
641 | const ZZ_pXModulus& F); |
---|
642 | |
---|
643 | ZZ_pX CompMod(const ZZ_pX& g, const ZZ_pXArgument& H, |
---|
644 | const ZZ_pXModulus& F); |
---|
645 | |
---|
646 | extern long ZZ_pXArgBound; |
---|
647 | |
---|
648 | // Initially 0. If this is set to a value greater than zero, then |
---|
649 | // composition routines will allocate a table of no than about |
---|
650 | // ZZ_pXArgBound KB. Setting this value affects all compose routines |
---|
651 | // and the power projection and minimal polynomial routines below, |
---|
652 | // and indirectly affects many routines in ZZ_pXFactoring. |
---|
653 | |
---|
654 | /**************************************************************************\ |
---|
655 | |
---|
656 | power projection routines |
---|
657 | |
---|
658 | \**************************************************************************/ |
---|
659 | |
---|
660 | void project(ZZ_p& x, const ZZ_pVector& a, const ZZ_pX& b); |
---|
661 | ZZ_p project(const ZZ_pVector& a, const ZZ_pX& b); |
---|
662 | // x = inner product of a with coefficient vector of b |
---|
663 | |
---|
664 | |
---|
665 | void ProjectPowers(vec_ZZ_p& x, const vec_ZZ_p& a, long k, |
---|
666 | const ZZ_pX& h, const ZZ_pXModulus& F); |
---|
667 | |
---|
668 | vec_ZZ_p ProjectPowers(const vec_ZZ_p& a, long k, |
---|
669 | const ZZ_pX& h, const ZZ_pXModulus& F); |
---|
670 | |
---|
671 | // Computes the vector |
---|
672 | |
---|
673 | // project(a, 1), project(a, h), ..., project(a, h^{k-1} % f). |
---|
674 | |
---|
675 | // This operation is the "transpose" of the modular composition operation. |
---|
676 | |
---|
677 | void ProjectPowers(vec_ZZ_p& x, const vec_ZZ_p& a, long k, |
---|
678 | const ZZ_pXArgument& H, const ZZ_pXModulus& F); |
---|
679 | |
---|
680 | vec_ZZ_p ProjectPowers(const vec_ZZ_p& a, long k, |
---|
681 | const ZZ_pXArgument& H, const ZZ_pXModulus& F); |
---|
682 | |
---|
683 | // same as above, but uses a pre-computed ZZ_pXArgument |
---|
684 | |
---|
685 | |
---|
686 | void UpdateMap(vec_ZZ_p& x, const vec_ZZ_p& a, |
---|
687 | const ZZ_pXMultiplier& B, const ZZ_pXModulus& F); |
---|
688 | |
---|
689 | vec_ZZ_p UpdateMap(const vec_ZZ_p& a, |
---|
690 | const ZZ_pXMultiplier& B, const ZZ_pXModulus& F); |
---|
691 | |
---|
692 | // Computes the vector |
---|
693 | |
---|
694 | // project(a, b), project(a, (b*X)%f), ..., project(a, (b*X^{n-1})%f) |
---|
695 | |
---|
696 | // Restriction: must have a.length() <= deg(F). |
---|
697 | // This is "transposed" MulMod by B. |
---|
698 | // Input may have "high order" zeroes stripped. |
---|
699 | // Output will always have high order zeroes stripped. |
---|
700 | |
---|
701 | |
---|
702 | /**************************************************************************\ |
---|
703 | |
---|
704 | Minimum Polynomials |
---|
705 | |
---|
706 | These routines should be used with prime p. |
---|
707 | |
---|
708 | All of these routines implement the algorithm from [Shoup, J. Symbolic |
---|
709 | Comp. 17:371-391, 1994] and [Shoup, J. Symbolic Comp. 20:363-397, |
---|
710 | 1995], based on transposed modular composition and the |
---|
711 | Berlekamp/Massey algorithm. |
---|
712 | |
---|
713 | \**************************************************************************/ |
---|
714 | |
---|
715 | |
---|
716 | void MinPolySeq(ZZ_pX& h, const vec_ZZ_p& a, long m); |
---|
717 | ZZ_pX MinPolySeq(const vec_ZZ_p& a, long m); |
---|
718 | // computes the minimum polynomial of a linealy generated sequence; m |
---|
719 | // is a bound on the degree of the polynomial; required: a.length() >= |
---|
720 | // 2*m |
---|
721 | |
---|
722 | void ProbMinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m); |
---|
723 | ZZ_pX ProbMinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m); |
---|
724 | |
---|
725 | void ProbMinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F); |
---|
726 | ZZ_pX ProbMinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F); |
---|
727 | |
---|
728 | // computes the monic minimal polynomial if (g mod f). m = a bound on |
---|
729 | // the degree of the minimal polynomial; in the second version, this |
---|
730 | // argument defaults to n. The algorithm is probabilistic, always |
---|
731 | // returns a divisor of the minimal polynomial, and returns a proper |
---|
732 | // divisor with probability at most m/p. |
---|
733 | |
---|
734 | void MinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m); |
---|
735 | ZZ_pX MinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m); |
---|
736 | |
---|
737 | void MinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F); |
---|
738 | ZZ_pX MinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F); |
---|
739 | |
---|
740 | // same as above, but guarantees that result is correct |
---|
741 | |
---|
742 | void IrredPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m); |
---|
743 | ZZ_pX IrredPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m); |
---|
744 | |
---|
745 | void IrredPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F); |
---|
746 | ZZ_pX IrredPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F); |
---|
747 | |
---|
748 | // same as above, but assumes that f is irreducible, or at least that |
---|
749 | // the minimal poly of g is itself irreducible. The algorithm is |
---|
750 | // deterministic (and is always correct). |
---|
751 | |
---|
752 | |
---|
753 | /**************************************************************************\ |
---|
754 | |
---|
755 | Traces, norms, resultants |
---|
756 | |
---|
757 | These routines should be used with prime p. |
---|
758 | |
---|
759 | \**************************************************************************/ |
---|
760 | |
---|
761 | |
---|
762 | void TraceMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
763 | ZZ_p TraceMod(const ZZ_pX& a, const ZZ_pXModulus& F); |
---|
764 | |
---|
765 | void TraceMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& f); |
---|
766 | ZZ_p TraceMod(const ZZ_pX& a, const ZZ_pXModulus& f); |
---|
767 | // x = Trace(a mod f); deg(a) < deg(f) |
---|
768 | |
---|
769 | |
---|
770 | void TraceVec(vec_ZZ_p& S, const ZZ_pX& f); |
---|
771 | vec_ZZ_p TraceVec(const ZZ_pX& f); |
---|
772 | // S[i] = Trace(X^i mod f), i = 0..deg(f)-1; 0 < deg(f) |
---|
773 | |
---|
774 | // The above trace routines implement the asymptotically fast trace |
---|
775 | // algorithm from [von zur Gathen and Shoup, Computational Complexity, |
---|
776 | // 1992]. |
---|
777 | |
---|
778 | void NormMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& f); |
---|
779 | ZZ_p NormMod(const ZZ_pX& a, const ZZ_pX& f); |
---|
780 | // x = Norm(a mod f); 0 < deg(f), deg(a) < deg(f) |
---|
781 | |
---|
782 | void resultant(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& b); |
---|
783 | ZZ_p resultant(const ZZ_pX& a, const ZZ_pX& b); |
---|
784 | // x = resultant(a, b) |
---|
785 | |
---|
786 | void CharPolyMod(ZZ_pX& g, const ZZ_pX& a, const ZZ_pX& f); |
---|
787 | ZZ_pX CharPolyMod(const ZZ_pX& a, const ZZ_pX& f); |
---|
788 | // g = charcteristic polynomial of (a mod f); 0 < deg(f), deg(g) < |
---|
789 | // deg(f); this routine works for arbitrary f; if f is irreducible, |
---|
790 | // it is faster to use the IrredPolyMod routine, and then exponentiate |
---|
791 | // if necessary (since in this case the CharPoly is just a power of |
---|
792 | // the IrredPoly). |
---|
793 | |
---|
794 | |
---|
795 | /**************************************************************************\ |
---|
796 | |
---|
797 | Miscellany |
---|
798 | |
---|
799 | A ZZ_pX f is represented as a vec_ZZ_p, which can be accessed as |
---|
800 | f.rep. The constant term is f.rep[0] and the leading coefficient is |
---|
801 | f.rep[f.rep.length()-1], except if f is zero, in which case |
---|
802 | f.rep.length() == 0. Note that the leading coefficient is always |
---|
803 | nonzero (unless f is zero). One can freely access and modify f.rep, |
---|
804 | but one should always ensure that the leading coefficient is nonzero, |
---|
805 | which can be done by invoking f.normalize(). |
---|
806 | |
---|
807 | \**************************************************************************/ |
---|
808 | |
---|
809 | |
---|
810 | void clear(ZZ_pX& x) // x = 0 |
---|
811 | void set(ZZ_pX& x); // x = 1 |
---|
812 | |
---|
813 | void ZZ_pX::normalize(); |
---|
814 | // f.normalize() strips leading zeros from f.rep. |
---|
815 | |
---|
816 | void ZZ_pX::SetMaxLength(long n); |
---|
817 | // f.SetMaxLength(n) pre-allocate spaces for n coefficients. The |
---|
818 | // polynomial that f represents is unchanged. |
---|
819 | |
---|
820 | void ZZ_pX::kill(); |
---|
821 | // f.kill() sets f to 0 and frees all memory held by f; Equivalent to |
---|
822 | // f.rep.kill(). |
---|
823 | |
---|
824 | ZZ_pX::ZZ_pX(INIT_SIZE_TYPE, long n); |
---|
825 | // ZZ_pX(INIT_SIZE, n) initializes to zero, but space is pre-allocated |
---|
826 | // for n coefficients |
---|
827 | |
---|
828 | static const ZZ_pX& ZZ_pX::zero(); |
---|
829 | // ZZ_pX::zero() is a read-only reference to 0 |
---|
830 | |
---|
831 | void swap(ZZ_pX& x, ZZ_pX& y); |
---|
832 | // swap x and y (via "pointer swapping") |
---|
833 | |
---|