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