1 | |
---|
2 | |
---|
3 | /**************************************************************************\ |
---|
4 | |
---|
5 | MODULE: GF2E |
---|
6 | |
---|
7 | SUMMARY: |
---|
8 | |
---|
9 | The class GF2E is used to represent polynomials in F_2[X] modulo a |
---|
10 | polynomial P. The modulus P may be any polynomial with deg(P) > 0, |
---|
11 | not necessarily irreducible. |
---|
12 | |
---|
13 | Objects of the class GF2E are represented as a GF2X of degree < deg(P). |
---|
14 | |
---|
15 | An executing program maintains a "current modulus", which is set to P |
---|
16 | using GF2E::init(P). The current modulus *must* be initialized before |
---|
17 | any GF2E constructors are invoked. |
---|
18 | |
---|
19 | The modulus may be changed, and a mechanism is provided for saving and |
---|
20 | restoring a modulus (see classes GF2EBak and GF2EContext below). |
---|
21 | |
---|
22 | |
---|
23 | NOTE: if P is a trinomial X^n + X^k + 1, or a pentanomial |
---|
24 | X^n + X^k3 + X^k2 + X^k1 + 1, or of the form X^n + g, where |
---|
25 | g has low degree, then performance will be somewhat improved. |
---|
26 | Such polynomials are constructed by the routines |
---|
27 | BuildSparseIrred and BuildIrred in GF2XFactoring. |
---|
28 | |
---|
29 | |
---|
30 | \**************************************************************************/ |
---|
31 | |
---|
32 | #include <NTL/GF2X.h> |
---|
33 | |
---|
34 | class GF2E { |
---|
35 | public: |
---|
36 | |
---|
37 | GF2E(); // initial value 0 |
---|
38 | |
---|
39 | GF2E(const GF2E& a); // copy constructor |
---|
40 | |
---|
41 | GF2E& operator=(const GF2E& a); // assignment |
---|
42 | GF2E& operator=(GF2 a); // assignment |
---|
43 | GF2E& operator=(long a); // assignment |
---|
44 | |
---|
45 | ~GF2E(); // destructor |
---|
46 | |
---|
47 | void init(const GF2X& P); |
---|
48 | // GF2E::init(P) initializes the current modulus to P; |
---|
49 | // required: deg(P) >= 1. |
---|
50 | |
---|
51 | static const GF2XModulus& modulus(); |
---|
52 | // GF2E::modulus() yields read-only reference to the current modulus |
---|
53 | |
---|
54 | static long degree(); |
---|
55 | // GF2E::degree() returns deg(P) |
---|
56 | }; |
---|
57 | |
---|
58 | |
---|
59 | const GF2X& rep(const GF2E& a); // read-only access to representation of a |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | /**************************************************************************\ |
---|
64 | |
---|
65 | Comparison |
---|
66 | |
---|
67 | \**************************************************************************/ |
---|
68 | |
---|
69 | long operator==(const GF2E& a, const GF2E& b); |
---|
70 | long operator!=(const GF2E& a, const GF2E& b); |
---|
71 | |
---|
72 | long IsZero(const GF2E& a); // test for 0 |
---|
73 | long IsOne(const GF2E& a); // test for 1 |
---|
74 | |
---|
75 | // PROMOTIONS: ==, != promote {long, GF2} to GF2E on (a, b). |
---|
76 | |
---|
77 | |
---|
78 | /**************************************************************************\ |
---|
79 | |
---|
80 | Addition |
---|
81 | |
---|
82 | \**************************************************************************/ |
---|
83 | |
---|
84 | // operator notation: |
---|
85 | |
---|
86 | GF2E operator+(const GF2E& a, const GF2E& b); |
---|
87 | |
---|
88 | GF2E operator-(const GF2E& a, const GF2E& b); |
---|
89 | GF2E operator-(const GF2E& a); |
---|
90 | |
---|
91 | GF2E& operator+=(GF2E& x, const GF2E& a); |
---|
92 | GF2E& operator+=(GF2E& x, GF2 a); |
---|
93 | GF2E& operator+=(GF2E& x, long a); |
---|
94 | |
---|
95 | GF2E& operator++(GF2E& x); // prefix |
---|
96 | void operator++(GF2E& x, int); // postfix |
---|
97 | |
---|
98 | GF2E& operator-=(GF2E& x, const GF2E& a); |
---|
99 | GF2E& operator-=(GF2E& x, GF2 a); |
---|
100 | GF2E& operator-=(GF2E& x, long a); |
---|
101 | |
---|
102 | GF2E& operator--(GF2E& x); // prefix |
---|
103 | void operator--(GF2E& x, int); // postfix |
---|
104 | |
---|
105 | // procedural versions: |
---|
106 | |
---|
107 | void add(GF2E& x, const GF2E& a, const GF2E& b); // x = a + b |
---|
108 | void sub(GF2E& x, const GF2E& a, const GF2E& b); // x = a - b = a + b |
---|
109 | void negate(GF2E& x, const GF2E& a); // x = - a = a |
---|
110 | |
---|
111 | // PROMOTIONS: +, -, add, sub promote {long, GF2} to GF2E on (a, b). |
---|
112 | |
---|
113 | |
---|
114 | /**************************************************************************\ |
---|
115 | |
---|
116 | Multiplication |
---|
117 | |
---|
118 | \**************************************************************************/ |
---|
119 | |
---|
120 | |
---|
121 | // operator notation: |
---|
122 | |
---|
123 | GF2E operator*(const GF2E& a, const GF2E& b); |
---|
124 | |
---|
125 | GF2E& operator*=(GF2E& x, const GF2E& a); |
---|
126 | GF2E& operator*=(GF2E& x, GF2 a); |
---|
127 | GF2E& operator*=(GF2E& x, long a); |
---|
128 | |
---|
129 | // procedural versions: |
---|
130 | |
---|
131 | |
---|
132 | void mul(GF2E& x, const GF2E& a, const GF2E& b); // x = a * b |
---|
133 | |
---|
134 | void sqr(GF2E& x, const GF2E& a); // x = a^2 |
---|
135 | GF2E sqr(const GF2E& a); |
---|
136 | |
---|
137 | // PROMOTIONS: *, mul promote {long, GF2} to GF2E on (a, b). |
---|
138 | |
---|
139 | |
---|
140 | /**************************************************************************\ |
---|
141 | |
---|
142 | Division |
---|
143 | |
---|
144 | \**************************************************************************/ |
---|
145 | |
---|
146 | |
---|
147 | // operator notation: |
---|
148 | |
---|
149 | GF2E operator/(const GF2E& a, const GF2E& b); |
---|
150 | |
---|
151 | GF2E& operator/=(GF2E& x, const GF2E& a); |
---|
152 | GF2E& operator/=(GF2E& x, GF2 a); |
---|
153 | GF2E& operator/=(GF2E& x, long a); |
---|
154 | |
---|
155 | |
---|
156 | // procedural versions: |
---|
157 | |
---|
158 | void div(GF2E& x, const GF2E& a, const GF2E& b); |
---|
159 | // x = a/b. If b is not invertible, an error is raised. |
---|
160 | |
---|
161 | void inv(GF2E& x, const GF2E& a); |
---|
162 | GF2E inv(const GF2E& a); |
---|
163 | // x = 1/a |
---|
164 | |
---|
165 | PROMOTIONS: /, div promote {long, GF2} to GF2E on (a, b). |
---|
166 | |
---|
167 | |
---|
168 | /**************************************************************************\ |
---|
169 | |
---|
170 | Exponentiation |
---|
171 | |
---|
172 | \**************************************************************************/ |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | void power(GF2E& x, const GF2E& a, const ZZ& e); |
---|
177 | GF2E power(const GF2E& a, const ZZ& e); |
---|
178 | |
---|
179 | void power(GF2E& x, const GF2E& a, long e); |
---|
180 | GF2E power(const GF2E& a, long e); |
---|
181 | |
---|
182 | // x = a^e (e may be negative) |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | /**************************************************************************\ |
---|
187 | |
---|
188 | Random Elements |
---|
189 | |
---|
190 | \**************************************************************************/ |
---|
191 | |
---|
192 | |
---|
193 | void random(GF2E& x); |
---|
194 | GF2E random_GF2E(); |
---|
195 | // x = random element in GF2E. |
---|
196 | |
---|
197 | /**************************************************************************\ |
---|
198 | |
---|
199 | Traces |
---|
200 | |
---|
201 | \**************************************************************************/ |
---|
202 | |
---|
203 | |
---|
204 | void trace(GF2& x, const GF2E& a); // x = trace of a |
---|
205 | GF2 trace(const GF2E& a); |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | /**************************************************************************\ |
---|
210 | |
---|
211 | Input/Output |
---|
212 | |
---|
213 | \**************************************************************************/ |
---|
214 | |
---|
215 | |
---|
216 | ostream& operator<<(ostream& s, const GF2E& a); |
---|
217 | |
---|
218 | istream& operator>>(istream& s, GF2E& x); |
---|
219 | // a GF2X is read and reduced mod p |
---|
220 | |
---|
221 | |
---|
222 | /**************************************************************************\ |
---|
223 | |
---|
224 | Modulus Switching |
---|
225 | |
---|
226 | A class GF2EBak is provided for "backing up" the current modulus. |
---|
227 | |
---|
228 | Here is what you do to save the current modulus, temporarily |
---|
229 | set it to something new, and then restore it: |
---|
230 | |
---|
231 | GF2EBak bak; |
---|
232 | bak.save(); // save current modulus (if any) |
---|
233 | |
---|
234 | GF2E::init(P); // set modulus to desired value P |
---|
235 | |
---|
236 | // ... |
---|
237 | |
---|
238 | bak.restore(); // restore old modulus (if any) |
---|
239 | |
---|
240 | Note that between the save and restore, you may have several calls to |
---|
241 | GF2E::init, each of which simply clobbers the previous modulus. |
---|
242 | |
---|
243 | The GF2EBak interface is good for implementing simple stack-like |
---|
244 | modulus "context switching". For more general context switching, |
---|
245 | see GF2EContext below. |
---|
246 | |
---|
247 | .......................................................................... |
---|
248 | |
---|
249 | When the current modulus is changed, there may be extant |
---|
250 | GF2E objects. If the old modulus was saved and then later restored, |
---|
251 | these objects can be used again as if the modulus had never changed. |
---|
252 | Note, however, that if a GF2E object is created under one modulus |
---|
253 | and then used in any way (except destroyed) under another, |
---|
254 | program behavior is not predictable. This condition is not |
---|
255 | explicitly checked for, but an error is likely to be raised. |
---|
256 | One should also not presume that things will work properly |
---|
257 | if the modulus is changed, but its value happens to be the same--- |
---|
258 | one should restore the same "context", from either a GF2EBak |
---|
259 | or a GF2EContext object. This is anyway more efficient. |
---|
260 | |
---|
261 | \**************************************************************************/ |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | class GF2EBak { |
---|
267 | public: |
---|
268 | |
---|
269 | // To describe this logic, think of a GF2EBak object |
---|
270 | // of having two components: a modulus Q (possibly "null") and |
---|
271 | // an "auto-restore bit" b. |
---|
272 | |
---|
273 | // There is also a global current modulus P (initially "null"). |
---|
274 | |
---|
275 | GF2EBak(); // Q = "null", b = 0 |
---|
276 | |
---|
277 | ~GF2EBak(); // if (b) P = Q |
---|
278 | |
---|
279 | void save(); // Q = P, b = 1 |
---|
280 | void restore(); // P = Q, b = 0 |
---|
281 | |
---|
282 | |
---|
283 | private: |
---|
284 | GF2EBak(const GF2EBak&); // copy disabled |
---|
285 | void operator=(const GF2EBak&); // assignment disabled |
---|
286 | }; |
---|
287 | |
---|
288 | |
---|
289 | // more general context switching: |
---|
290 | |
---|
291 | class GF2EContext { |
---|
292 | |
---|
293 | // A GF2EContext object has a modulus Q (possibly "null"), |
---|
294 | // but has no auto-restore bit like a GF2EBak object. |
---|
295 | // However, these objects can be initialized and copied with |
---|
296 | // complete generality. |
---|
297 | |
---|
298 | // As above, P is the current global modulus (initially "null") |
---|
299 | |
---|
300 | public: |
---|
301 | |
---|
302 | GF2EContext(); // Q = "null" |
---|
303 | GF2EContext(const GF2X& new_Q); // Q = new_Q |
---|
304 | |
---|
305 | void save(); // Q = P |
---|
306 | void restore() const; // P = Q |
---|
307 | |
---|
308 | GF2EContext(const GF2EContext&); // copy |
---|
309 | GF2EContext& operator=(const GF2EContext&); // assignment |
---|
310 | ~GF2EContext(); // destructor |
---|
311 | |
---|
312 | |
---|
313 | }; |
---|
314 | |
---|
315 | |
---|
316 | /**************************************************************************\ |
---|
317 | |
---|
318 | Miscellany |
---|
319 | |
---|
320 | \**************************************************************************/ |
---|
321 | |
---|
322 | void clear(GF2E& x); // x = 0 |
---|
323 | void set(GF2E& x); // x = 1 |
---|
324 | |
---|
325 | static const GF2E& GF2E::zero(); |
---|
326 | // GF2E::zero() yields a read-only reference to zero |
---|
327 | |
---|
328 | static long GF2X::WordLength(); |
---|
329 | // GF2E::size() returns # of words needed to store a polynomial of |
---|
330 | // degree < GF2E::degree() |
---|
331 | |
---|
332 | void swap(GF2E& x, GF2E& y); |
---|
333 | // swap x and y (done by "pointer swapping", if possible). |
---|
334 | |
---|
335 | static ZZ& GF2E::cardinality(); |
---|
336 | // yields the cardinality, i.e., 2^{GF2E::degree()} |
---|
337 | |
---|