File: ZZ_pE.cpp.html

package info (click to toggle)
ntl 10.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,460 kB
  • sloc: cpp: 84,947; sh: 10,577; ansic: 2,462; makefile: 804
file content (435 lines) | stat: -rw-r--r-- 22,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>~/junk6/ntl-10.4.0/doc/ZZ_pE.cpp.html</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="macvim">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
* { font-size: 1em; }
.String { color: #4a708b; }
.PreProc { color: #1874cd; }
.Statement { color: #b03060; font-weight: bold; }
.Comment { color: #0000ee; font-style: italic; }
.Type { color: #008b00; font-weight: bold; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>


<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">MODULE: ZZ_pE</span>

<span class="Comment">SUMMARY:</span>

<span class="Comment">The class ZZ_pE is used to represent polynomials in Z_p[X] modulo a</span>
<span class="Comment">polynomial P.  The modulus P may be any polynomial with deg(P) &gt; 0,</span>
<span class="Comment">not necessarily irreducible.  The modulus p defining Z_p need</span>
<span class="Comment">not be prime either.</span>

<span class="Comment">Objects of the class ZZ_pE are represented as a ZZ_pX of degree &lt; deg(P).</span>

<span class="Comment">An executing program maintains a &quot;current modulus&quot;, which is set to P</span>
<span class="Comment">using ZZ_pE::init(P).  The current modulus for ZZ_pE (as well as for ZZ_p)</span>
<span class="Comment">*must* be initialized before an operations on ZZ_pE's are performed.</span>

<span class="Comment">The modulus may be changed, and a mechanism is provided for saving and</span>
<span class="Comment">restoring a modulus (see classes ZZ_pEPush and ZZ_pEContext below).</span>


<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>

<span class="PreProc">#include </span><span class="String">&lt;NTL/ZZ_pX.h&gt;</span>

<span class="Type">class</span> ZZ_pE {
<span class="Statement">public</span>:

   ZZ_pE(); <span class="Comment">// initial value 0</span>

   ZZ_pE(<span class="Type">const</span> ZZ_pE&amp; a); <span class="Comment">// copy constructor</span>
   <span class="Type">explicit</span> ZZ_pE(<span class="Type">const</span> ZZ_p&amp; a); <span class="Comment">// promotion</span>
   <span class="Type">explicit</span> ZZ_pE(<span class="Type">long</span> a); <span class="Comment">// promotion</span>

   ZZ_pE&amp; <span class="Statement">operator</span>=(<span class="Type">const</span> ZZ_pE&amp; a); <span class="Comment">// assignment</span>
   ZZ_pE&amp; <span class="Statement">operator</span>=(<span class="Type">const</span> ZZ_p&amp; a); <span class="Comment">// assignment</span>
   ZZ_pE&amp; <span class="Statement">operator</span>=(<span class="Type">long</span> a); <span class="Comment">// assignment</span>

   ~ZZ_pE(); <span class="Comment">// destructor</span>

   ZZ_pE(ZZ_pE&amp;&amp; a);
   <span class="Comment">// move constructor (C++11 only)</span>
   <span class="Comment">// declared noexcept unless NTL_EXCEPTIONS flag is set</span>

   ZZ_pE&amp; <span class="Statement">operator</span>=(ZZ_pE&amp;&amp; a);
   <span class="Comment">// move assignment (C++11 only)</span>
   <span class="Comment">// declared noexcept unless NTL_EXCEPTIONS flag is set</span>

   <span class="Type">static</span> <span class="Type">void</span> init(<span class="Type">const</span> ZZ_pX&amp; P);
   <span class="Comment">// ZZ_pE::init(P) initializes the current modulus to P;</span>
   <span class="Comment">// required: deg(P) &gt;= 1.</span>

   <span class="Type">static</span> <span class="Type">const</span> ZZ_pXModulus&amp; modulus();
   <span class="Comment">// ZZ_pE::modulus() yields read-only reference to the current modulus </span>

   <span class="Type">static</span> <span class="Type">long</span> degree();
   <span class="Comment">// ZZ_pE::degree() returns deg(P)</span>


   <span class="Comment">// typedefs to aid generic programming</span>
   <span class="Type">typedef</span> ZZ_pX rep_type;
   <span class="Type">typedef</span> ZZ_pEContext context_type;
   <span class="Type">typedef</span> ZZ_pEBak bak_type;
   <span class="Type">typedef</span> ZZ_pEPush push_type;
   <span class="Type">typedef</span> ZZ_pEX poly_type;

};


<span class="Type">const</span> ZZ_pX&amp; rep(<span class="Type">const</span> ZZ_pE&amp; a); <span class="Comment">// read-only access to representation of a</span>



<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                                  Comparison</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>

<span class="Type">long</span> <span class="Statement">operator</span>==(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);
<span class="Type">long</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);

<span class="Type">long</span> IsZero(<span class="Type">const</span> ZZ_pE&amp; a);  <span class="Comment">// test for 0</span>
<span class="Type">long</span> IsOne(<span class="Type">const</span> ZZ_pE&amp; a);  <span class="Comment">// test for 1</span>

<span class="Comment">// PROMOTIONS: ==, != promote {long, ZZ_p} to ZZ_pE on (a, b).</span>


<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                                    Addition </span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>

<span class="Comment">// operator notation:</span>

ZZ_pE <span class="Statement">operator</span>+(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);

ZZ_pE <span class="Statement">operator</span>-(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);
ZZ_pE <span class="Statement">operator</span>-(<span class="Type">const</span> ZZ_pE&amp; a);

ZZ_pE&amp; <span class="Statement">operator</span>+=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>+=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_p&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>+=(ZZ_pE&amp; x, <span class="Type">long</span> a);

ZZ_pE&amp; <span class="Statement">operator</span>++(ZZ_pE&amp; x); <span class="Comment">// prefix</span>
<span class="Type">void</span> <span class="Statement">operator</span>++(ZZ_pE&amp; x, <span class="Type">int</span>); <span class="Comment">// postfix</span>

ZZ_pE&amp; <span class="Statement">operator</span>-=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>-=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_p&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>-=(ZZ_pE&amp; x, <span class="Type">long</span> a);

ZZ_pE&amp; <span class="Statement">operator</span>--(ZZ_pE&amp; x); <span class="Comment">// prefix</span>
<span class="Type">void</span> <span class="Statement">operator</span>--(ZZ_pE&amp; x, <span class="Type">int</span>); <span class="Comment">// postfix</span>

<span class="Comment">// procedural versions:</span>

<span class="Type">void</span> add(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b); <span class="Comment">// x = a + b</span>
<span class="Type">void</span> sub(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b); <span class="Comment">// x = a - b </span>
<span class="Type">void</span> negate(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a); <span class="Comment">// x = - a </span>

<span class="Comment">// PROMOTIONS: +, -, add, sub promote {long, ZZ_p} to ZZ_pE on (a, b).</span>


<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                                  Multiplication </span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>


<span class="Comment">// operator notation:</span>

ZZ_pE <span class="Statement">operator</span>*(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);

ZZ_pE&amp; <span class="Statement">operator</span>*=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>*=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_p&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>*=(ZZ_pE&amp; x, <span class="Type">long</span> a);

<span class="Comment">// procedural versions:</span>


<span class="Type">void</span> mul(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b); <span class="Comment">// x = a * b</span>

<span class="Type">void</span> sqr(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a); <span class="Comment">// x = a^2</span>
ZZ_pE sqr(<span class="Type">const</span> ZZ_pE&amp; a);

<span class="Comment">// PROMOTIONS: *, mul promote {long, ZZ_p} to ZZ_pE on (a, b).</span>



<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                                     Division</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>


<span class="Comment">// operator notation:</span>

ZZ_pE <span class="Statement">operator</span>/(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);

ZZ_pE&amp; <span class="Statement">operator</span>/=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>/=(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_p&amp; a);
ZZ_pE&amp; <span class="Statement">operator</span>/=(ZZ_pE&amp; x, <span class="Type">long</span> a);


<span class="Comment">// procedural versions:</span>

<span class="Type">void</span> div(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ_pE&amp; b);
<span class="Comment">// x = a/b.  If b is not invertible, an error is raised.</span>

<span class="Type">void</span> inv(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);
ZZ_pE inv(<span class="Type">const</span> ZZ_pE&amp; a);
<span class="Comment">// x = 1/a</span>

<span class="Statement">PROMOTIONS</span>: /, div promote {<span class="Type">long</span>, ZZ_p} to ZZ_pE on (a, b).


<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                                  Exponentiation</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>



<span class="Type">void</span> power(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ&amp; e);
ZZ_pE power(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">const</span> ZZ&amp; e);

<span class="Type">void</span> power(ZZ_pE&amp; x, <span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">long</span> e);
ZZ_pE power(<span class="Type">const</span> ZZ_pE&amp; a, <span class="Type">long</span> e);

<span class="Comment">// x = a^e (e may be negative)</span>



<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                               Random Elements</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>


<span class="Type">void</span> random(ZZ_pE&amp; x);
ZZ_pE random_ZZ_pE();
<span class="Comment">// x = random element in ZZ_pE.</span>

<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                               Norms and Traces</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>



<span class="Type">void</span> trace(ZZ_p&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);  <span class="Comment">// x = trace of a</span>
ZZ_p trace(<span class="Type">const</span> ZZ_pE&amp; a);

<span class="Type">void</span> norm(ZZ_p&amp; x, <span class="Type">const</span> ZZ_pE&amp; a);   <span class="Comment">// x = norm of a</span>
ZZ_p norm(<span class="Type">const</span> ZZ_pE&amp; a);



<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                                Input/Output</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>


ostream&amp; <span class="Statement">operator</span>&lt;&lt;(ostream&amp; s, <span class="Type">const</span> ZZ_pE&amp; a);

istream&amp; <span class="Statement">operator</span>&gt;&gt;(istream&amp; s, ZZ_pE&amp; x);
<span class="Comment">// a ZZ_pX is read and reduced mod p</span>


<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                       Modulus Switching </span>

<span class="Comment">A class ZZ_pEPush is provided for &quot;backing up&quot; the current modulus</span>
<span class="Comment">and installing a new one.</span>

<span class="Comment">Here is what you do to save the current modulus, temporarily</span>
<span class="Comment">set it to P, and automatically restore it:</span>

<span class="Comment">   { </span>
<span class="Comment">      ZZ_pEPush push(P); </span>

<span class="Comment">      ...</span>

<span class="Comment">   }</span>

<span class="Comment">The constructor for push will save the current modulus, and install P as the</span>
<span class="Comment">current modulus.  The destructor for push will restore the old modulus when the</span>
<span class="Comment">scope enclosing it exits.  This is the so-called RAII (resource acquisition is</span>
<span class="Comment">initialization) paradigm.</span>

<span class="Comment">You could also do the following:</span>

<span class="Comment">   {</span>
<span class="Comment">      ZZ_pEPush push; // just backup current modulus</span>

<span class="Comment">        ...</span>

<span class="Comment">      ZZ_pE::init(P1); // install P1 </span>

<span class="Comment">        ...</span>

<span class="Comment">      ZZ_pE::init(P2); // install P2</span>

<span class="Comment">      // reinstall original modulus as close of scope</span>
<span class="Comment">   }</span>

<span class="Comment">      </span>
<span class="Comment">The ZZ_pEPush interface is good for implementing simple stack-like</span>
<span class="Comment">modulus &quot;context switching&quot;.  For more general context switching,</span>
<span class="Comment">see ZZ_pEContext below.  There is also an older ZZ_pEBak class</span>
<span class="Comment">that may also be useful.</span>

<span class="Comment">..........................................................................</span>

<span class="Comment">It is critical that ZZ_pE objects created under one ZZ_pE modulus are not used in</span>
<span class="Comment">any non-trivial way &quot;out of context&quot;, i.e., under a different (or undefined)</span>
<span class="Comment">ZZ_pE modulus.  However, for ease-of-use, some operations may be safely</span>
<span class="Comment">performed out of context.  These safe operations include: the default and copy</span>
<span class="Comment">constructor, the destructor, and the assignment operator.  In addition is is</span>
<span class="Comment">generally safe to read any ZZ_pE object out of context (i.e., printing it out, or</span>
<span class="Comment">fetching its underlying representive using the rep() function).</span>

<span class="Comment">Any unsafe uses out of context are not in general checked, and may </span>
<span class="Comment">lead to unpredictable behavior.</span>


<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>


<span class="Comment">// A convenient interface for common cases</span>

<span class="Type">class</span> ZZ_pEPush {

<span class="Statement">public</span>:
ZZ_pEPush();  <span class="Comment">// backup current modulus</span>
<span class="Type">explicit</span> ZZ_pEPush(<span class="Type">const</span> ZZ_pX&amp; P);
<span class="Type">explicit</span> ZZ_pEPush(<span class="Type">const</span> ZZ_pEContext&amp; context);
  <span class="Comment">// backup current modulus and install the given one</span>

<span class="Statement">private</span>:
ZZ_pEPush(<span class="Type">const</span> ZZ_pEPush&amp;); <span class="Comment">// disabled</span>
<span class="Type">void</span> <span class="Statement">operator</span>=(<span class="Type">const</span> ZZ_pEPush&amp;); <span class="Comment">// disabled</span>

};



<span class="Comment">// more general context switching:</span>
<span class="Comment">// A ZZ_pEContext object has a modulus Q (possibly &quot;null&quot;),</span>

<span class="Type">class</span> ZZ_pEContext {


<span class="Statement">public</span>:

ZZ_pEContext(); <span class="Comment">// Q = &quot;null&quot;</span>
<span class="Type">explicit</span> ZZ_pEContext(<span class="Type">const</span> ZZ_pX&amp; P); <span class="Comment">// Q = P</span>

<span class="Type">void</span> save(); <span class="Comment">// Q = CurrentModulus</span>
<span class="Type">void</span> restore() <span class="Type">const</span>; <span class="Comment">// CurrentModulus = Q</span>

ZZ_pEContext(<span class="Type">const</span> ZZ_pEContext&amp;);  <span class="Comment">// copy</span>
ZZ_pEContext&amp; <span class="Statement">operator</span>=(<span class="Type">const</span> ZZ_pEContext&amp;); <span class="Comment">// assignment</span>
~ZZ_pEContext(); <span class="Comment">// destructor</span>


};


<span class="Comment">// An older interface:</span>
<span class="Comment">// To describe this logic, think of a ZZ_pEBak object</span>
<span class="Comment">// of having two components: a modulus Q (possibly &quot;null&quot;) and </span>
<span class="Comment">// an &quot;auto-restore bit&quot; b.</span>


<span class="Type">class</span> ZZ_pEBak {
<span class="Statement">public</span>:


   ZZ_pEBak();  <span class="Comment">// Q = &quot;null&quot;, b = 0</span>

   ~ZZ_pEBak();  <span class="Comment">// if (b) CurrentModulus = Q</span>

   <span class="Type">void</span> save();  <span class="Comment">// Q = CurrentModulus, b = 1 </span>
   <span class="Type">void</span> restore();  <span class="Comment">// CurrentModulus = Q, b = 0</span>


<span class="Statement">private</span>:
   ZZ_pEBak(<span class="Type">const</span> ZZ_pEBak&amp;);  <span class="Comment">// copy disabled</span>
   <span class="Type">void</span> <span class="Statement">operator</span>=(<span class="Type">const</span> ZZ_pEBak&amp;);  <span class="Comment">// assignment disabled</span>
};






<span class="Comment">/*</span><span class="Comment">*************************************************************************\</span>

<span class="Comment">                               Miscellany</span>

<span class="Comment">\*************************************************************************</span><span class="Comment">*/</span>

<span class="Type">void</span> clear(ZZ_pE&amp; x); <span class="Comment">// x = 0</span>
<span class="Type">void</span> set(ZZ_pE&amp; x); <span class="Comment">// x = 1</span>

<span class="Type">static</span> <span class="Type">const</span> ZZ_pE&amp; ZZ_pE::zero();
<span class="Comment">// ZZ_pE::zero() yields a read-only reference to zero</span>

<span class="Type">void</span> ZZ_pE::swap(ZZ_pE&amp; x);
<span class="Type">void</span> swap(ZZ_pE&amp; x, ZZ_pE&amp; y);
<span class="Comment">// swap (done by &quot;pointer swapping&quot;, if possible).</span>

<span class="Type">static</span> ZZ&amp; ZZ_pE::cardinality();
<span class="Comment">// yields the cardinality, i.e., p^{ZZ_pE::degree()}</span>

ZZ_pE::ZZ_pE(INIT_NO_ALLOC_TYPE);
<span class="Comment">// special constructor: invoke as ZZ_pE x(INIT_NO_ALLOC);</span>
<span class="Comment">// initializes x to 0, but allocates no space (this is now the default)</span>

ZZ_pE::ZZ_pE(INIT_ALLOC_TYPE);
<span class="Comment">// special constructor: invoke as ZZ_pE x(INIT_ALLOC);</span>
<span class="Comment">// initializes x to 0, but allocates space</span>

ZZ_pE::allocate();
<span class="Comment">// useful in conjunction with the INIT_NO_ALLLOC constructor:</span>
<span class="Comment">// x.allocate() will pre-allocate space for x, using the</span>
<span class="Comment">// current modulus</span>

</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->