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