1 | |
---|
2 | /**************************************************************************\ |
---|
3 | |
---|
4 | MODULE: GF2EX |
---|
5 | |
---|
6 | SUMMARY: |
---|
7 | |
---|
8 | The class GF2EX represents polynomials over GF2E, |
---|
9 | and so can be used, for example, for arithmentic in GF(2^n)[X]. |
---|
10 | However, except where mathematically necessary (e.g., GCD computations), |
---|
11 | GF2E need not be a field. |
---|
12 | |
---|
13 | \**************************************************************************/ |
---|
14 | |
---|
15 | #include <NTL/GF2E.h> |
---|
16 | #include <NTL/vec_GF2E.h> |
---|
17 | |
---|
18 | class GF2EX { |
---|
19 | public: |
---|
20 | |
---|
21 | GF2EX(); // initial value 0 |
---|
22 | |
---|
23 | GF2EX(const GF2EX& a); // copy |
---|
24 | |
---|
25 | GF2EX& operator=(const GF2EX& a); // assignment |
---|
26 | GF2EX& operator=(const GF2E& a); |
---|
27 | GF2EX& operator=(GF2 a); |
---|
28 | GF2EX& operator=(long a); |
---|
29 | |
---|
30 | ~GF2EX(); // destructor |
---|
31 | |
---|
32 | GF2EX(long i, const GF2E& c); // initilaize to X^i*c |
---|
33 | GF2EX(long i, GF2 c); |
---|
34 | GF2EX(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 GF2EX& a, const GF2EX& b); |
---|
52 | long operator!=(const GF2EX& a, const GF2EX& b); |
---|
53 | |
---|
54 | long IsZero(const GF2EX& a); // test for 0 |
---|
55 | long IsOne(const GF2EX& a); // test for 1 |
---|
56 | |
---|
57 | // PROMOTIONS: ==, != promote {long,GF2,GF2E} to GF2EX on (a, b). |
---|
58 | |
---|
59 | /**************************************************************************\ |
---|
60 | |
---|
61 | Addition |
---|
62 | |
---|
63 | \**************************************************************************/ |
---|
64 | |
---|
65 | // operator notation: |
---|
66 | |
---|
67 | GF2EX operator+(const GF2EX& a, const GF2EX& b); |
---|
68 | GF2EX operator-(const GF2EX& a, const GF2EX& b); |
---|
69 | GF2EX operator-(const GF2EX& a); |
---|
70 | |
---|
71 | GF2EX& operator+=(GF2EX& x, const GF2EX& a); |
---|
72 | GF2EX& operator+=(GF2EX& x, const GF2E& a); |
---|
73 | GF2EX& operator+=(GF2EX& x, GF2 a); |
---|
74 | GF2EX& operator+=(GF2EX& x, long a); |
---|
75 | |
---|
76 | |
---|
77 | GF2EX& operator++(GF2EX& x); // prefix |
---|
78 | void operator++(GF2EX& x, int); // postfix |
---|
79 | |
---|
80 | GF2EX& operator-=(GF2EX& x, const GF2EX& a); |
---|
81 | GF2EX& operator-=(GF2EX& x, const GF2E& a); |
---|
82 | GF2EX& operator-=(GF2EX& x, GF2 a); |
---|
83 | GF2EX& operator-=(GF2EX& x, long a); |
---|
84 | |
---|
85 | GF2EX& operator--(GF2EX& x); // prefix |
---|
86 | void operator--(GF2EX& x, int); // postfix |
---|
87 | |
---|
88 | // procedural versions: |
---|
89 | |
---|
90 | void add(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a + b |
---|
91 | void sub(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a - b |
---|
92 | void negate(GF2EX& x, const GF2EX& a); // x = - a |
---|
93 | |
---|
94 | // PROMOTIONS: +, -, add, sub promote {long,GF2,GF2E} to GF2EX on (a, b). |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | /**************************************************************************\ |
---|
99 | |
---|
100 | Multiplication |
---|
101 | |
---|
102 | \**************************************************************************/ |
---|
103 | |
---|
104 | // operator notation: |
---|
105 | |
---|
106 | GF2EX operator*(const GF2EX& a, const GF2EX& b); |
---|
107 | |
---|
108 | GF2EX& operator*=(GF2EX& x, const GF2EX& a); |
---|
109 | GF2EX& operator*=(GF2EX& x, const GF2E& a); |
---|
110 | GF2EX& operator*=(GF2EX& x, GF2 a); |
---|
111 | GF2EX& operator*=(GF2EX& x, long a); |
---|
112 | |
---|
113 | |
---|
114 | // procedural versions: |
---|
115 | |
---|
116 | |
---|
117 | void mul(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a * b |
---|
118 | |
---|
119 | void sqr(GF2EX& x, const GF2EX& a); // x = a^2 |
---|
120 | GF2EX sqr(const GF2EX& a); |
---|
121 | |
---|
122 | // PROMOTIONS: *, mul promote {long,GF2,GF2E} to GF2EX on (a, b). |
---|
123 | |
---|
124 | void power(GF2EX& x, const GF2EX& a, long e); // x = a^e (e >= 0) |
---|
125 | GF2EX power(const GF2EX& 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 | GF2EX operator<<(const GF2EX& a, long n); |
---|
142 | GF2EX operator>>(const GF2EX& a, long n); |
---|
143 | |
---|
144 | GF2EX& operator<<=(GF2EX& x, long n); |
---|
145 | GF2EX& operator>>=(GF2EX& x, long n); |
---|
146 | |
---|
147 | // procedural versions: |
---|
148 | |
---|
149 | void LeftShift(GF2EX& x, const GF2EX& a, long n); |
---|
150 | GF2EX LeftShift(const GF2EX& a, long n); |
---|
151 | |
---|
152 | void RightShift(GF2EX& x, const GF2EX& a, long n); |
---|
153 | GF2EX RightShift(const GF2EX& a, long n); |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | /**************************************************************************\ |
---|
158 | |
---|
159 | Division |
---|
160 | |
---|
161 | \**************************************************************************/ |
---|
162 | |
---|
163 | // operator notation: |
---|
164 | |
---|
165 | GF2EX operator/(const GF2EX& a, const GF2EX& b); |
---|
166 | GF2EX operator/(const GF2EX& a, const GF2E& b); |
---|
167 | GF2EX operator/(const GF2EX& a, GF2 b); |
---|
168 | GF2EX operator/(const GF2EX& a, long b); |
---|
169 | |
---|
170 | GF2EX operator%(const GF2EX& a, const GF2EX& b); |
---|
171 | |
---|
172 | GF2EX& operator/=(GF2EX& x, const GF2EX& a); |
---|
173 | GF2EX& operator/=(GF2EX& x, const GF2E& a); |
---|
174 | GF2EX& operator/=(GF2EX& x, GF2 a); |
---|
175 | GF2EX& operator/=(GF2EX& x, long a); |
---|
176 | |
---|
177 | GF2EX& operator%=(GF2EX& x, const GF2EX& a); |
---|
178 | |
---|
179 | // procedural versions: |
---|
180 | |
---|
181 | |
---|
182 | void DivRem(GF2EX& q, GF2EX& r, const GF2EX& a, const GF2EX& b); |
---|
183 | // q = a/b, r = a%b |
---|
184 | |
---|
185 | void div(GF2EX& q, const GF2EX& a, const GF2EX& b); |
---|
186 | void div(GF2EX& q, const GF2EX& a, const GF2E& b); |
---|
187 | void div(GF2EX& q, const GF2EX& a, GF2 b); |
---|
188 | void div(GF2EX& q, const GF2EX& a, long b); |
---|
189 | // q = a/b |
---|
190 | |
---|
191 | void rem(GF2EX& r, const GF2EX& a, const GF2EX& b); |
---|
192 | // r = a%b |
---|
193 | |
---|
194 | long divide(GF2EX& q, const GF2EX& a, const GF2EX& b); |
---|
195 | // if b | a, sets q = a/b and returns 1; otherwise returns 0 |
---|
196 | |
---|
197 | long divide(const GF2EX& a, const GF2EX& 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 GF2E is a field. |
---|
206 | |
---|
207 | \**************************************************************************/ |
---|
208 | |
---|
209 | |
---|
210 | void GCD(GF2EX& x, const GF2EX& a, const GF2EX& b); |
---|
211 | GF2EX GCD(const GF2EX& a, const GF2EX& b); |
---|
212 | // x = GCD(a, b), x is always monic (or zero if a==b==0). |
---|
213 | |
---|
214 | |
---|
215 | void XGCD(GF2EX& d, GF2EX& s, GF2EX& t, const GF2EX& a, const GF2EX& 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 < GF2E::degree() and |
---|
230 | a_n not zero (the zero polynomial is [ ]). On input, the coefficients |
---|
231 | are arbitrary polynomials which are reduced modulo GF2E::modulus(), and leading |
---|
232 | zeros stripped. |
---|
233 | |
---|
234 | \**************************************************************************/ |
---|
235 | |
---|
236 | istream& operator>>(istream& s, GF2EX& x); |
---|
237 | ostream& operator<<(ostream& s, const GF2EX& a); |
---|
238 | |
---|
239 | |
---|
240 | /**************************************************************************\ |
---|
241 | |
---|
242 | Some utility routines |
---|
243 | |
---|
244 | \**************************************************************************/ |
---|
245 | |
---|
246 | long deg(const GF2EX& a); // return deg(a); deg(0) == -1. |
---|
247 | |
---|
248 | const GF2E& coeff(const GF2EX& 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 GF2E& LeadCoeff(const GF2EX& a); |
---|
253 | // read-only reference to leading term of a, or zero if a == 0 |
---|
254 | |
---|
255 | const GF2E& ConstTerm(const GF2EX& a); |
---|
256 | // read-only reference to constant term of a, or zero if a == 0 |
---|
257 | |
---|
258 | void SetCoeff(GF2EX& x, long i, const GF2E& a); |
---|
259 | void SetCoeff(GF2EX& x, long i, GF2 a); |
---|
260 | void SetCoeff(GF2EX& x, long i, long a); |
---|
261 | // makes coefficient of X^i equal to a; error is raised if i < 0 |
---|
262 | |
---|
263 | void SetCoeff(GF2EX& x, long i); |
---|
264 | // makes coefficient of X^i equal to 1; error is raised if i < 0 |
---|
265 | |
---|
266 | void SetX(GF2EX& x); // x is set to the monomial X |
---|
267 | |
---|
268 | long IsX(const GF2EX& a); // test if x = X |
---|
269 | |
---|
270 | void diff(GF2EX& x, const GF2EX& a); // x = derivative of a |
---|
271 | GF2EX diff(const GF2EX& a); |
---|
272 | |
---|
273 | void MakeMonic(GF2EX& x); |
---|
274 | // if x != 0 makes x into its monic associate; LeadCoeff(x) must be |
---|
275 | // invertible in this case |
---|
276 | |
---|
277 | void reverse(GF2EX& x, const GF2EX& a, long hi); |
---|
278 | GF2EX reverse(const GF2EX& a, long hi); |
---|
279 | |
---|
280 | void reverse(GF2EX& x, const GF2EX& a); |
---|
281 | GF2EX reverse(const GF2EX& 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_GF2E& x, const GF2EX& a, long n); |
---|
287 | vec_GF2E VectorCopy(const GF2EX& 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(GF2EX& x, long n); |
---|
301 | GF2EX random_GF2EX(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(GF2EX& x, const vec_GF2E& a); |
---|
313 | GF2EX BuildFromRoots(const vec_GF2E& a); |
---|
314 | // computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length() |
---|
315 | |
---|
316 | void eval(GF2E& b, const GF2EX& f, const GF2E& a); |
---|
317 | GF2E eval(const GF2EX& f, const GF2E& a); |
---|
318 | // b = f(a) |
---|
319 | |
---|
320 | void eval(GF2E& b, const GF2X& f, const GF2E& a); |
---|
321 | GF2E eval(const GF2EX& f, const GF2E& a); |
---|
322 | // b = f(a); uses ModComp algorithm for GF2X |
---|
323 | |
---|
324 | void eval(vec_GF2E& b, const GF2EX& f, const vec_GF2E& a); |
---|
325 | vec_GF2E eval(const GF2EX& f, const vec_GF2E& a); |
---|
326 | // b.SetLength(a.length()); b[i] = f(a[i]) for 0 <= i < a.length() |
---|
327 | |
---|
328 | void interpolate(GF2EX& f, const vec_GF2E& a, const vec_GF2E& b); |
---|
329 | GF2EX interpolate(const vec_GF2E& a, const vec_GF2E& 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(GF2EX& x, const GF2EX& a, long n); // x = a % X^n |
---|
341 | GF2EX trunc(const GF2EX& a, long n); |
---|
342 | |
---|
343 | void MulTrunc(GF2EX& x, const GF2EX& a, const GF2EX& b, long n); |
---|
344 | GF2EX MulTrunc(const GF2EX& a, const GF2EX& b, long n); |
---|
345 | // x = a * b % X^n |
---|
346 | |
---|
347 | void SqrTrunc(GF2EX& x, const GF2EX& a, long n); |
---|
348 | GF2EX SqrTrunc(const GF2EX& a, long n); |
---|
349 | // x = a^2 % X^n |
---|
350 | |
---|
351 | void InvTrunc(GF2EX& x, const GF2EX& a, long n); |
---|
352 | GF2EX InvTrunc(GF2EX& x, const GF2EX& 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 | GF2EXModulus data structure and associated routines below for better |
---|
367 | performance. |
---|
368 | |
---|
369 | \**************************************************************************/ |
---|
370 | |
---|
371 | void MulMod(GF2EX& x, const GF2EX& a, const GF2EX& b, const GF2EX& f); |
---|
372 | GF2EX MulMod(const GF2EX& a, const GF2EX& b, const GF2EX& f); |
---|
373 | // x = (a * b) % f |
---|
374 | |
---|
375 | void SqrMod(GF2EX& x, const GF2EX& a, const GF2EX& f); |
---|
376 | GF2EX SqrMod(const GF2EX& a, const GF2EX& f); |
---|
377 | // x = a^2 % f |
---|
378 | |
---|
379 | void MulByXMod(GF2EX& x, const GF2EX& a, const GF2EX& f); |
---|
380 | GF2EX MulByXMod(const GF2EX& a, const GF2EX& f); |
---|
381 | // x = (a * X) mod f |
---|
382 | |
---|
383 | void InvMod(GF2EX& x, const GF2EX& a, const GF2EX& f); |
---|
384 | GF2EX InvMod(const GF2EX& a, const GF2EX& f); |
---|
385 | // x = a^{-1} % f, error is a is not invertible |
---|
386 | |
---|
387 | long InvModStatus(GF2EX& x, const GF2EX& a, const GF2EX& 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 | GF2EXModulus 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/GF2EX.h> |
---|
404 | |
---|
405 | void product(GF2EX& x, const vec_GF2EX& v, const GF2EX& f) |
---|
406 | { |
---|
407 | GF2EXModulus F(f); |
---|
408 | GF2EX 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 GF2EX may be used wherever a GF2EXModulus is required, |
---|
417 | and a GF2EXModulus may be used wherever a GF2EX is required. |
---|
418 | |
---|
419 | |
---|
420 | \**************************************************************************/ |
---|
421 | |
---|
422 | class GF2EXModulus { |
---|
423 | public: |
---|
424 | GF2EXModulus(); // initially in an unusable state |
---|
425 | |
---|
426 | GF2EXModulus(const GF2EX& f); // initialize with f, deg(f) > 0 |
---|
427 | |
---|
428 | GF2EXModulus(const GF2EXModulus&); // copy |
---|
429 | |
---|
430 | GF2EXModulus& operator=(const GF2EXModulus&); // assignment |
---|
431 | |
---|
432 | ~GF2EXModulus(); // destructor |
---|
433 | |
---|
434 | operator const GF2EX& () const; // implicit read-only access to f |
---|
435 | |
---|
436 | const GF2EX& val() const; // explicit read-only access to f |
---|
437 | }; |
---|
438 | |
---|
439 | void build(GF2EXModulus& F, const GF2EX& f); |
---|
440 | // pre-computes information about f and stores it in F. Must have |
---|
441 | // deg(f) > 0. Note that the declaration GF2EXModulus F(f) is |
---|
442 | // equivalent to GF2EXModulus 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 GF2EXModulus& F); // return n=deg(f) |
---|
449 | |
---|
450 | void MulMod(GF2EX& x, const GF2EX& a, const GF2EX& b, const GF2EXModulus& F); |
---|
451 | GF2EX MulMod(const GF2EX& a, const GF2EX& b, const GF2EXModulus& F); |
---|
452 | // x = (a * b) % f; deg(a), deg(b) < n |
---|
453 | |
---|
454 | void SqrMod(GF2EX& x, const GF2EX& a, const GF2EXModulus& F); |
---|
455 | GF2EX SqrMod(const GF2EX& a, const GF2EXModulus& F); |
---|
456 | // x = a^2 % f; deg(a) < n |
---|
457 | |
---|
458 | void PowerMod(GF2EX& x, const GF2EX& a, const ZZ& e, const GF2EXModulus& F); |
---|
459 | GF2EX PowerMod(const GF2EX& a, const ZZ& e, const GF2EXModulus& F); |
---|
460 | |
---|
461 | void PowerMod(GF2EX& x, const GF2EX& a, long e, const GF2EXModulus& F); |
---|
462 | GF2EX PowerMod(const GF2EX& a, long e, const GF2EXModulus& F); |
---|
463 | |
---|
464 | // x = a^e % f; e >= 0, deg(a) < n. Uses a sliding window algorithm. |
---|
465 | // (e may be negative) |
---|
466 | |
---|
467 | void PowerXMod(GF2EX& x, const ZZ& e, const GF2EXModulus& F); |
---|
468 | GF2EX PowerXMod(const ZZ& e, const GF2EXModulus& F); |
---|
469 | |
---|
470 | void PowerXMod(GF2EX& x, long e, const GF2EXModulus& F); |
---|
471 | GF2EX PowerXMod(long e, const GF2EXModulus& F); |
---|
472 | |
---|
473 | // x = X^e % f (e may be negative) |
---|
474 | |
---|
475 | void rem(GF2EX& x, const GF2EX& a, const GF2EXModulus& F); |
---|
476 | // x = a % f |
---|
477 | |
---|
478 | void DivRem(GF2EX& q, GF2EX& r, const GF2EX& a, const GF2EXModulus& F); |
---|
479 | // q = a/f, r = a%f |
---|
480 | |
---|
481 | void div(GF2EX& q, const GF2EX& a, const GF2EXModulus& F); |
---|
482 | // q = a/f |
---|
483 | |
---|
484 | // operator notation: |
---|
485 | |
---|
486 | GF2EX operator/(const GF2EX& a, const GF2EXModulus& F); |
---|
487 | GF2EX operator%(const GF2EX& a, const GF2EXModulus& F); |
---|
488 | |
---|
489 | GF2EX& operator/=(GF2EX& x, const GF2EXModulus& F); |
---|
490 | GF2EX& operator%=(GF2EX& x, const GF2EXModulus& F); |
---|
491 | |
---|
492 | |
---|
493 | |
---|
494 | /**************************************************************************\ |
---|
495 | |
---|
496 | vectors of GF2EX's |
---|
497 | |
---|
498 | \**************************************************************************/ |
---|
499 | |
---|
500 | NTL_vector_decl(GF2EX,vec_GF2EX) |
---|
501 | // vec_GF2EX |
---|
502 | |
---|
503 | NTL_eq_vector_decl(GF2EX,vec_GF2EX) |
---|
504 | // == and != |
---|
505 | |
---|
506 | NTL_io_vector_decl(GF2EX,vec_GF2EX) |
---|
507 | // I/O operators |
---|
508 | |
---|
509 | |
---|
510 | |
---|
511 | /**************************************************************************\ |
---|
512 | |
---|
513 | Modular Composition |
---|
514 | |
---|
515 | Modular composition is the problem of computing g(h) mod f for |
---|
516 | polynomials f, g, and h. |
---|
517 | |
---|
518 | The algorithm employed is that of Brent & Kung (Fast algorithms for |
---|
519 | manipulating formal power series, JACM 25:581-595, 1978), which uses |
---|
520 | O(n^{1/2}) modular polynomial multiplications, and O(n^2) scalar |
---|
521 | operations. |
---|
522 | |
---|
523 | |
---|
524 | \**************************************************************************/ |
---|
525 | |
---|
526 | void CompMod(GF2EX& x, const GF2EX& g, const GF2EX& h, const GF2EXModulus& F); |
---|
527 | GF2EX CompMod(const GF2EX& g, const GF2EX& h, |
---|
528 | const GF2EXModulus& F); |
---|
529 | |
---|
530 | // x = g(h) mod f; deg(h) < n |
---|
531 | |
---|
532 | void Comp2Mod(GF2EX& x1, GF2EX& x2, const GF2EX& g1, const GF2EX& g2, |
---|
533 | const GF2EX& h, const GF2EXModulus& F); |
---|
534 | // xi = gi(h) mod f (i=1,2); deg(h) < n. |
---|
535 | |
---|
536 | |
---|
537 | void Comp3Mod(GF2EX& x1, GF2EX& x2, GF2EX& x3, |
---|
538 | const GF2EX& g1, const GF2EX& g2, const GF2EX& g3, |
---|
539 | const GF2EX& h, const GF2EXModulus& F); |
---|
540 | // xi = gi(h) mod f (i=1..3); deg(h) < n. |
---|
541 | |
---|
542 | |
---|
543 | |
---|
544 | /**************************************************************************\ |
---|
545 | |
---|
546 | Composition with Pre-Conditioning |
---|
547 | |
---|
548 | If a single h is going to be used with many g's then you should build |
---|
549 | a GF2EXArgument for h, and then use the compose routine below. The |
---|
550 | routine build computes and stores h, h^2, ..., h^m mod f. After this |
---|
551 | pre-computation, composing a polynomial of degree roughly n with h |
---|
552 | takes n/m multiplies mod f, plus n^2 scalar multiplies. Thus, |
---|
553 | increasing m increases the space requirement and the pre-computation |
---|
554 | time, but reduces the composition time. |
---|
555 | |
---|
556 | \**************************************************************************/ |
---|
557 | |
---|
558 | |
---|
559 | struct GF2EXArgument { |
---|
560 | vec_GF2EX H; |
---|
561 | }; |
---|
562 | |
---|
563 | void build(GF2EXArgument& H, const GF2EX& h, const GF2EXModulus& F, long m); |
---|
564 | // Pre-Computes information about h. m > 0, deg(h) < n. |
---|
565 | |
---|
566 | void CompMod(GF2EX& x, const GF2EX& g, const GF2EXArgument& H, |
---|
567 | const GF2EXModulus& F); |
---|
568 | |
---|
569 | GF2EX CompMod(const GF2EX& g, const GF2EXArgument& H, |
---|
570 | const GF2EXModulus& F); |
---|
571 | |
---|
572 | extern long GF2EXArgBound; |
---|
573 | |
---|
574 | // Initially 0. If this is set to a value greater than zero, then |
---|
575 | // composition routines will allocate a table of no than about |
---|
576 | // GF2EXArgBound KB. Setting this value affects all compose routines |
---|
577 | // and the power projection and minimal polynomial routines below, |
---|
578 | // and indirectly affects many routines in GF2EXFactoring. |
---|
579 | |
---|
580 | /**************************************************************************\ |
---|
581 | |
---|
582 | power projection routines |
---|
583 | |
---|
584 | \**************************************************************************/ |
---|
585 | |
---|
586 | void project(GF2E& x, const GF2EVector& a, const GF2EX& b); |
---|
587 | GF2E project(const GF2EVector& a, const GF2EX& b); |
---|
588 | // x = inner product of a with coefficient vector of b |
---|
589 | |
---|
590 | |
---|
591 | void ProjectPowers(vec_GF2E& x, const vec_GF2E& a, long k, |
---|
592 | const GF2EX& h, const GF2EXModulus& F); |
---|
593 | |
---|
594 | vec_GF2E ProjectPowers(const vec_GF2E& a, long k, |
---|
595 | const GF2EX& h, const GF2EXModulus& F); |
---|
596 | |
---|
597 | // Computes the vector |
---|
598 | |
---|
599 | // project(a, 1), project(a, h), ..., project(a, h^{k-1} % f). |
---|
600 | |
---|
601 | // This operation is the "transpose" of the modular composition operation. |
---|
602 | |
---|
603 | void ProjectPowers(vec_GF2E& x, const vec_GF2E& a, long k, |
---|
604 | const GF2EXArgument& H, const GF2EXModulus& F); |
---|
605 | |
---|
606 | vec_GF2E ProjectPowers(const vec_GF2E& a, long k, |
---|
607 | const GF2EXArgument& H, const GF2EXModulus& F); |
---|
608 | |
---|
609 | // same as above, but uses a pre-computed GF2EXArgument |
---|
610 | |
---|
611 | class GF2EXTransMultiplier { /* ... */ }; |
---|
612 | |
---|
613 | void build(GF2EXTransMultiplier& B, const GF2EX& b, const GF2EXModulus& F); |
---|
614 | |
---|
615 | |
---|
616 | |
---|
617 | void UpdateMap(vec_GF2E& x, const vec_GF2E& a, |
---|
618 | const GF2EXMultiplier& B, const GF2EXModulus& F); |
---|
619 | |
---|
620 | vec_GF2E UpdateMap(const vec_GF2E& a, |
---|
621 | const GF2EXMultiplier& B, const GF2EXModulus& F); |
---|
622 | |
---|
623 | // Computes the vector |
---|
624 | |
---|
625 | // project(a, b), project(a, (b*X)%f), ..., project(a, (b*X^{n-1})%f) |
---|
626 | |
---|
627 | // Restriction: a.length() <= deg(F), deg(b) < deg(F). |
---|
628 | // This is "transposed" MulMod by B. |
---|
629 | // Input may have "high order" zeroes stripped. |
---|
630 | // Output always has high order zeroes stripped. |
---|
631 | |
---|
632 | |
---|
633 | /**************************************************************************\ |
---|
634 | |
---|
635 | Minimum Polynomials |
---|
636 | |
---|
637 | These routines should be used only when GF2E is a field. |
---|
638 | |
---|
639 | All of these routines implement the algorithm from [Shoup, J. Symbolic |
---|
640 | Comp. 17:371-391, 1994] and [Shoup, J. Symbolic Comp. 20:363-397, |
---|
641 | 1995], based on transposed modular composition and the |
---|
642 | Berlekamp/Massey algorithm. |
---|
643 | |
---|
644 | \**************************************************************************/ |
---|
645 | |
---|
646 | |
---|
647 | void MinPolySeq(GF2EX& h, const vec_GF2E& a, long m); |
---|
648 | GF2EX MinPolySeq(const vec_GF2E& a, long m); |
---|
649 | // computes the minimum polynomial of a linealy generated sequence; m |
---|
650 | // is a bound on the degree of the polynomial; required: a.length() >= |
---|
651 | // 2*m |
---|
652 | |
---|
653 | |
---|
654 | void ProbMinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F, long m); |
---|
655 | GF2EX ProbMinPolyMod(const GF2EX& g, const GF2EXModulus& F, long m); |
---|
656 | |
---|
657 | void ProbMinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F); |
---|
658 | GF2EX ProbMinPolyMod(const GF2EX& g, const GF2EXModulus& F); |
---|
659 | |
---|
660 | // computes the monic minimal polynomial if (g mod f). m = a bound on |
---|
661 | // the degree of the minimal polynomial; in the second version, this |
---|
662 | // argument defaults to n. The algorithm is probabilistic, always |
---|
663 | // returns a divisor of the minimal polynomial, and returns a proper |
---|
664 | // divisor with probability at most m/2^{GF2E::degree()}. |
---|
665 | |
---|
666 | void MinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F, long m); |
---|
667 | GF2EX MinPolyMod(const GF2EX& g, const GF2EXModulus& F, long m); |
---|
668 | |
---|
669 | void MinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F); |
---|
670 | GF2EX MinPolyMod(const GF2EX& g, const GF2EXModulus& F); |
---|
671 | |
---|
672 | // same as above, but guarantees that result is correct |
---|
673 | |
---|
674 | void IrredPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F, long m); |
---|
675 | GF2EX IrredPolyMod(const GF2EX& g, const GF2EXModulus& F, long m); |
---|
676 | |
---|
677 | void IrredPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F); |
---|
678 | GF2EX IrredPolyMod(const GF2EX& g, const GF2EXModulus& F); |
---|
679 | |
---|
680 | // same as above, but assumes that f is irreducible, or at least that |
---|
681 | // the minimal poly of g is itself irreducible. The algorithm is |
---|
682 | // deterministic (and is always correct). |
---|
683 | |
---|
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 | GF2E need not be a field. |
---|
693 | |
---|
694 | \**************************************************************************/ |
---|
695 | |
---|
696 | |
---|
697 | void CompTower(GF2EX& x, const GF2X& g, const GF2EXArgument& h, |
---|
698 | const GF2EXModulus& F); |
---|
699 | |
---|
700 | GF2EX CompTower(const GF2X& g, const GF2EXArgument& h, |
---|
701 | const GF2EXModulus& F); |
---|
702 | |
---|
703 | void CompTower(GF2EX& x, const GF2X& g, const GF2EX& h, |
---|
704 | const GF2EXModulus& F); |
---|
705 | |
---|
706 | GF2EX CompTower(const GF2X& g, const GF2EX& h, |
---|
707 | const GF2EXModulus& F); |
---|
708 | |
---|
709 | |
---|
710 | // x = g(h) mod f |
---|
711 | |
---|
712 | |
---|
713 | void ProbMinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F, |
---|
714 | long m); |
---|
715 | |
---|
716 | GF2X ProbMinPolyTower(const GF2EX& g, const GF2EXModulus& F, long m); |
---|
717 | |
---|
718 | void ProbMinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F); |
---|
719 | |
---|
720 | GF2X ProbMinPolyTower(const GF2EX& g, const GF2EXModulus& F); |
---|
721 | |
---|
722 | // Uses a probabilistic algorithm to compute the minimal |
---|
723 | // polynomial of (g mod f) over GF2. |
---|
724 | // The parameter m is a bound on the degree of the minimal polynomial |
---|
725 | // (default = deg(f)*GF2E::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(GF2X& h, const GF2EX& g, const GF2EXModulus& F, long m); |
---|
732 | |
---|
733 | GF2X MinPolyTower(const GF2EX& g, const GF2EXModulus& F, long m); |
---|
734 | |
---|
735 | void MinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F); |
---|
736 | |
---|
737 | GF2X MinPolyTower(const GF2EX& g, const GF2EXModulus& F); |
---|
738 | |
---|
739 | // Same as above, but result is always correct. |
---|
740 | |
---|
741 | |
---|
742 | void IrredPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F, long m); |
---|
743 | |
---|
744 | GF2X IrredPolyTower(const GF2EX& g, const GF2EXModulus& F, long m); |
---|
745 | |
---|
746 | void IrredPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F); |
---|
747 | |
---|
748 | GF2X IrredPolyTower(const GF2EX& g, const GF2EXModulus& 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 | |
---|
757 | Traces, norms, resultants |
---|
758 | |
---|
759 | \**************************************************************************/ |
---|
760 | |
---|
761 | |
---|
762 | void TraceMod(GF2E& x, const GF2EX& a, const GF2EXModulus& F); |
---|
763 | GF2E TraceMod(const GF2EX& a, const GF2EXModulus& F); |
---|
764 | |
---|
765 | void TraceMod(GF2E& x, const GF2EX& a, const GF2EX& f); |
---|
766 | GF2E TraceMod(const GF2EX& a, const GF2EXModulus& f); |
---|
767 | // x = Trace(a mod f); deg(a) < deg(f) |
---|
768 | |
---|
769 | |
---|
770 | void TraceVec(vec_GF2E& S, const GF2EX& f); |
---|
771 | vec_GF2E TraceVec(const GF2EX& 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(GF2E& x, const GF2EX& a, const GF2EX& f); |
---|
779 | GF2E NormMod(const GF2EX& a, const GF2EX& f); |
---|
780 | // x = Norm(a mod f); 0 < deg(f), deg(a) < deg(f) |
---|
781 | |
---|
782 | void resultant(GF2E& x, const GF2EX& a, const GF2EX& b); |
---|
783 | GF2E resultant(const GF2EX& a, const GF2EX& b); |
---|
784 | // x = resultant(a, b) |
---|
785 | |
---|
786 | // NormMod and resultant require that GF2E is a field. |
---|
787 | |
---|
788 | |
---|
789 | |
---|
790 | /**************************************************************************\ |
---|
791 | |
---|
792 | Miscellany |
---|
793 | |
---|
794 | A GF2EX f is represented as a vec_GF2E, 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(GF2EX& x) // x = 0 |
---|
807 | void set(GF2EX& x); // x = 1 |
---|
808 | |
---|
809 | void GF2EX::normalize(); |
---|
810 | // f.normalize() strips leading zeros from f.rep. |
---|
811 | |
---|
812 | void GF2EX::SetMaxLength(long n); |
---|
813 | // f.SetMaxLength(n) pre-allocate spaces for n coefficients. The |
---|
814 | // polynomial that f represents is unchanged. |
---|
815 | |
---|
816 | void GF2EX::kill(); |
---|
817 | // f.kill() sets f to 0 and frees all memory held by f. Equivalent to |
---|
818 | // f.rep.kill(). |
---|
819 | |
---|
820 | GF2EX::GF2EX(INIT_SIZE_TYPE, long n); |
---|
821 | // GF2EX(INIT_SIZE, n) initializes to zero, but space is pre-allocated |
---|
822 | // for n coefficients |
---|
823 | |
---|
824 | static const GF2EX& zero(); |
---|
825 | // GF2EX::zero() is a read-only reference to 0 |
---|
826 | |
---|
827 | void swap(GF2EX& x, GF2EX& y); |
---|
828 | // swap x and y (via "pointer swapping") |
---|
829 | |
---|