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