source: git/ntl/doc/ZZ.txt @ 26e030

spielwiese
Last change on this file since 26e030 was 26e030, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: update to 5.5.1 git-svn-id: file:///usr/local/Singular/svn/trunk@11949 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 25.2 KB
Line 
1
2/**************************************************************************\
3
4MODULE: ZZ
5
6SUMMARY:
7
8The class ZZ is used to represent signed, arbitrary length integers.
9
10Routines are provided for all of the basic arithmetic operations, as
11well as for some more advanced operations such as primality testing.
12Space is automatically managed by the constructors and destructors.
13
14This module also provides routines for generating small primes, and
15fast routines for performing modular arithmetic on single-precision
16numbers.
17
18
19\**************************************************************************/
20
21#include <NTL/tools.h>
22
23
24class ZZ {
25public:
26
27   ZZ(); // initial value is 0
28
29   ZZ& operator=(const ZZ& a);  // assignment operator
30   ZZ& operator=(long a); 
31
32   ZZ(const ZZ& a);  // copy constructor
33   ~ZZ(); // destructor
34
35
36   // ...
37
38};
39
40
41// NOTE: A ZZ is represented as a sequence of "zzigits",
42// where each zzigit is between 0 and 2^{NTL_ZZ_NBITS-1}.
43
44// NTL_ZZ_NBITS is  macros defined in <NTL/ZZ.h>.
45
46// SIZE INVARIANT: the number of bits in a ZZ is always less than
47// 2^(NTL_BITS_PER_LONG-4).
48
49
50
51/**************************************************************************\
52
53                                 Comparison
54
55\**************************************************************************/
56
57
58
59// The usual comparison operators:
60   
61long operator==(const ZZ& a, const ZZ& b);
62long operator!=(const ZZ& a, const ZZ& b);
63long operator<(const ZZ& a, const ZZ& b);
64long operator>(const ZZ& a, const ZZ& b);
65long operator<=(const ZZ& a, const ZZ& b);
66long operator>=(const ZZ& a, const ZZ& b);
67
68// other stuff:
69
70long sign(const ZZ& a); // returns sign of a (-1, 0, +1)
71long IsZero(const ZZ& a); // test for 0
72long IsOne(const ZZ& a); // test for 1
73
74long compare(const ZZ& a, const ZZ& b); // returns sign of a-b (-1, 0, or 1).
75
76// PROMOTIONS: the comparison operators and the function compare
77// support promotion from long to ZZ on (a, b).
78
79
80/**************************************************************************\
81
82                                 Addition
83
84\**************************************************************************/
85
86
87// operator notation:
88
89ZZ operator+(const ZZ& a, const ZZ& b);
90ZZ operator-(const ZZ& a, const ZZ& b);
91ZZ operator-(const ZZ& a); // unary -
92
93ZZ& operator+=(ZZ& x, const ZZ& a);
94ZZ& operator+=(ZZ& x, long a);
95
96ZZ& operator-=(ZZ& x, const ZZ& a);
97ZZ& operator-=(ZZ& x, long a);
98
99ZZ& operator++(ZZ& x);  // prefix
100void operator++(ZZ& x, int);  // postfix
101
102ZZ& operator--(ZZ& x);  // prefix
103void operator--(ZZ& x, int);  // postfix
104
105
106
107// procedural versions:
108
109void add(ZZ& x, const ZZ& a, const ZZ& b); // x = a + b
110void sub(ZZ& x, const ZZ& a, const ZZ& b); // x = a - b
111void SubPos(ZZ& x, const ZZ& a, const ZZ& b); // x = a-b; assumes a >= b >= 0.
112void negate(ZZ& x, const ZZ& a); // x = -a
113
114void abs(ZZ& x, const ZZ& a); // x = |a|
115ZZ abs(const ZZ& a);
116
117// PROMOTIONS: binary +, -, as well as the procedural versions add, sub
118// support promotions from long to ZZ on (a, b).
119
120
121/**************************************************************************\
122
123                             Multiplication
124
125\**************************************************************************/
126
127// operator notation:
128
129ZZ operator*(const ZZ& a, const ZZ& b);
130
131ZZ& operator*=(ZZ& x, const ZZ& a);
132ZZ& operator*=(ZZ& x, long a);
133
134// procedural versions:
135
136void mul(ZZ& x, const ZZ& a, const ZZ& b); // x = a * b
137
138void sqr(ZZ& x, const ZZ& a); // x = a*a
139ZZ sqr(const ZZ& a);
140
141// PROMOTIONS: operator * and procedure mul support promotion
142// from long to ZZ on (a, b).
143
144
145/**************************************************************************\
146
147                                 Division
148
149\**************************************************************************/
150
151
152// operator notation:
153
154ZZ operator/(const ZZ& a, const ZZ& b);
155ZZ operator/(const ZZ& a, long  b);
156
157ZZ operator%(const ZZ& a, const ZZ& b);
158long operator%(const ZZ& a, long b);
159
160ZZ& operator/=(ZZ& x, const ZZ& b);
161ZZ& operator/=(ZZ& x, long b);
162
163ZZ& operator%=(ZZ& x, const ZZ& b);
164
165
166// procedural versions:
167
168void DivRem(ZZ& q, ZZ& r, const ZZ& a, const ZZ& b);
169// q = floor(a/b), r = a - b*q.
170// This implies that:
171//    |r| < |b|, and if r != 0, sign(r) = sign(b)
172
173void div(ZZ& q, const ZZ& a, const ZZ& b);
174// q = floor(a/b)
175
176void rem(ZZ& r, const ZZ& a, const ZZ& b);
177// q = floor(a/b), r = a - b*q
178
179
180// single-precision variants:
181
182long DivRem(ZZ& q, const ZZ& a, long b);
183// q = floor(a/b), r = a - b*q, return value is r.
184
185long rem(const ZZ& a, long b);
186// q = floor(a/b), r = a - b*q, return value is r.
187
188
189// divisibility testing:
190
191long divide(ZZ& q, const ZZ& a, const ZZ& b);
192long divide(ZZ& q, const ZZ& a, long b);
193// if b | a, sets q = a/b and returns 1; otherwise returns 0.
194
195long divide(const ZZ& a, const ZZ& b);
196long divide(const ZZ& a, long b);
197// if b | a, returns 1; otherwise returns 0.
198
199
200/**************************************************************************\
201
202                                    GCD's
203
204\**************************************************************************/
205
206
207void GCD(ZZ& d, const ZZ& a, const ZZ& b);
208ZZ GCD(const ZZ& a, const ZZ& b);
209
210// d = gcd(a, b) (which is always non-negative).  Uses a binary GCD
211// algorithm.
212
213
214
215void XGCD(ZZ& d, ZZ& s, ZZ& t, const ZZ& a, const ZZ& b);
216
217//  d = gcd(a, b) = a*s + b*t.
218
219// The coefficients s and t are defined according to the standard
220// Euclidean algorithm applied to |a| and |b|, with the signs then
221// adjusted according to the signs of a and b.
222
223// The implementation may or may not Euclid's algorithm,
224// but the coefficients a and t are always computed as if
225// it did.
226
227
228// special-purpose single-precision variants:
229
230long GCD(long a, long b);
231// return value is gcd(a, b) (which is always non-negative)
232
233void XGCD(long& d, long& s, long& t, long a, long b);
234//  d = gcd(a, b) = a*s + b*t.
235
236//  The coefficients s and t are defined according to the standard
237//  Euclidean algorithm applied to |a| and |b|, with the signs then
238//  adjusted according to the signs of a and b.
239
240
241
242/**************************************************************************\
243
244                             Modular Arithmetic
245
246The following routines perform arithmetic mod n, where n > 1.
247
248All arguments (other than exponents) are assumed to be in the range
2490..n-1.  Some routines may check this and raise an error if this
250does not hold.  Others may not, and the behaviour is unpredictable
251in this case.
252
253\**************************************************************************/
254
255
256
257void AddMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n); // x = (a+b)%n
258ZZ AddMod(const ZZ& a, const ZZ& b, const ZZ& n);
259
260void SubMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n); // x = (a-b)%n
261ZZ SubMod(const ZZ& a, const ZZ& b, const ZZ& n);
262
263void NegateMod(ZZ& x, const ZZ& a, const ZZ& n); // x = -a % n
264ZZ NegateMod(const ZZ& a, const ZZ& n);
265
266void MulMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n); // x = (a*b)%n
267ZZ MulMod(const ZZ& a, const ZZ& b, const ZZ& n);
268
269void SqrMod(ZZ& x, const ZZ& a, const ZZ& n); // x = a^2 % n
270ZZ SqrMod(const ZZ& a, const ZZ& n);
271
272void InvMod(ZZ& x, const ZZ& a, const ZZ& n);
273ZZ InvMod(const ZZ& a, const ZZ& n);
274// x = a^{-1} mod n (0 <= x < n); error is raised occurs if inverse
275// not defined
276
277long InvModStatus(ZZ& x, const ZZ& a, const ZZ& n);
278// if gcd(a,b) = 1, then return-value = 0, x = a^{-1} mod n;
279// otherwise, return-value = 1, x = gcd(a, n)
280
281void PowerMod(ZZ& x, const ZZ& a, const ZZ& e, const ZZ& n);
282ZZ PowerMod(const ZZ& a, const ZZ& e, const ZZ& n);
283
284void PowerMod(ZZ& x, const ZZ& a, long e, const ZZ& n);
285ZZ PowerMod(const ZZ& a, long e, const ZZ& n);
286
287// x = a^e % n (e may be negative)
288
289
290// PROMOTIONS: AddMod, SubMod, and MulMod (both procedural and functional
291// forms) support promotions from long to ZZ on (a, b).
292
293
294/**************************************************************************\
295
296                        Single-precision modular arithmetic
297
298These routines implement single-precision modular arithmetic.  If n is
299the modulus, all inputs should be in the range 0..n-1.  The number n
300itself should be in the range 2..NTL_SP_BOUND-1.
301
302Most of these routines are, of course, implemented as fast inline
303functions.  No checking is done that inputs are in range.
304
305\**************************************************************************/
306
307
308
309
310long AddMod(long a, long b, long n); // return (a+b)%n
311
312long SubMod(long a, long b, long n); // return (a-b)%n
313
314long NegateMod(long a, long n); // return (-a)%n
315
316long MulMod(long a, long b, long n); // return (a*b)%n
317
318long MulMod(long a, long b, long n, double ninv);
319// return (a*b)%n.  ninv = 1/((double) n).  This is faster if n is
320// fixed for many multiplications.
321
322
323long MulMod2(long a, long b, long n, double bninv);
324// return (a*b)%n.  bninv = ((double) b)/((double) n).  This is faster
325// if both n and b are fixed for many multiplications.
326// Note: This is OBSOLETE -- use MulModPrecon (see below) for
327// better performance.
328
329
330long MulDivRem(long& q, long a, long b, long n, double bninv);
331// return (a*b)%n, set q = (a*b)/n.  bninv = ((double) b)/((double) n)
332
333long InvMod(long a, long n);
334// computes a^{-1} mod n.  Error is raised if undefined.
335
336long PowerMod(long a, long e, long n);
337// computes a^e mod n (e may be negative)
338
339
340
341// The following are variants of MulMod2 above that may be significantly
342// faster on certain machines.  The implmentation varies depending
343// on the settings of the flags NTL_SPMM_ULL and NTL_SPMM_UL.
344// By default (no flags), the implementation is the same as MulMod2 above.
345// It is best to let the Wizard script select the optimal flag.
346
347typedef mulmod_precon_t  /*  depends on implementation */ ;
348
349mulmod_precon_t PrepMulModPrecon(long b, long n, double ninv);
350// Prepares preconditioning. ninv = 1/((double) n)
351
352long MulModPrecon(long a, long b, long n, mulmod_precon_t bninv);
353// return (a*b)%n.  bninv = MulModPrecon(b, n, ninv).
354
355// Example of use:
356//    long a, b, n, c;
357//      ...
358//    double ninv = 1/((double) n);
359//    mulmod_precon_t bninv = PrepMulModPrecon(b, n, ninv);
360//     ...
361//    c = MulModPrecon(a, b, n, bninv);  // c = (a*b) % n
362
363
364
365
366
367
368/**************************************************************************\
369
370                               Shift Operations
371
372LeftShift by n means multiplication by 2^n
373RightShift by n means division by 2^n, with truncation toward zero
374  (so the sign is preserved).
375
376A negative shift amount reverses the direction of the shift.
377
378\**************************************************************************/
379
380// operator notation:
381
382ZZ operator<<(const ZZ& a, long n);
383ZZ operator>>(const ZZ& a, long n);
384
385ZZ& operator<<=(ZZ& x, long n);
386ZZ& operator>>=(ZZ& x, long n);
387
388// procedural versions:
389
390void LeftShift(ZZ& x, const ZZ& a, long n);
391ZZ LeftShift(const ZZ& a, long n);
392
393void RightShift(ZZ& x, const ZZ& a, long n);
394ZZ RightShift(const ZZ& a, long n);
395
396
397
398/**************************************************************************\
399
400                              Bits and Bytes
401
402\**************************************************************************/
403
404
405
406long MakeOdd(ZZ& x);
407// removes factors of 2 from x, returns the number of 2's removed
408// returns 0 if x == 0
409
410long NumTwos(const ZZ& x);
411// returns max e such that 2^e divides x if x != 0, and returns 0 if x == 0.
412
413long IsOdd(const ZZ& a); // test if a is odd
414
415long NumBits(const ZZ& a);
416long NumBits(long a); 
417// returns the number of bits in binary represenation of |a|;
418// NumBits(0) = 0
419
420
421long bit(const ZZ& a, long k);
422long bit(long a, long k);
423// returns bit k of |a|, position 0 being the low-order bit.
424// If  k < 0 or k >= NumBits(a), returns 0.
425
426
427void trunc(ZZ& x, const ZZ& a, long k);
428// x = low order k bits of |a|.
429// If k <= 0, x = 0.
430
431// two functional variants:
432ZZ trunc_ZZ(const ZZ& a, long k); 
433long trunc_long(const ZZ& a, long k);
434
435long SetBit(ZZ& x, long p);
436// returns original value of p-th bit of |a|, and replaces p-th bit of
437// a by 1 if it was zero; low order bit is bit 0; error if p < 0;
438// the sign of x is maintained
439
440long SwitchBit(ZZ& x, long p);
441// returns original value of p-th bit of |a|, and switches the value
442// of p-th bit of a; low order bit is bit 0; error if p < 0
443// the sign of x is maintained
444
445long weight(const ZZ& a); // returns Hamming weight of |a|
446long weight(long a);
447
448// bit-wise Boolean operations, procedural form:
449
450void bit_and(ZZ& x, const ZZ& a, const ZZ& b); // x = |a| AND |b|
451void bit_or(ZZ& x, const ZZ& a, const ZZ& b); // x = |a| OR |b|
452void bit_xor(ZZ& x, const ZZ& a, const ZZ& b); // x = |a| XOR |b|
453
454// bit-wise Boolean operations, operator notation:
455
456ZZ operator&(const ZZ& a, const ZZ& b);
457ZZ operator|(const ZZ& a, const ZZ& b);
458ZZ operator^(const ZZ& a, const ZZ& b);
459
460// PROMOTIONS: the above bit-wise operations (both procedural
461// and operator forms) provide promotions from long to ZZ on (a, b).
462
463ZZ& operator&=(ZZ& x, const ZZ& b);
464ZZ& operator&=(ZZ& x, long b);
465
466ZZ& operator|=(ZZ& x, const ZZ& b);
467ZZ& operator|=(ZZ& x, long b);
468
469ZZ& operator^=(ZZ& x, const ZZ& b);
470ZZ& operator^=(ZZ& x, long b);
471
472
473
474// conversions between byte sequences and ZZ's
475
476void ZZFromBytes(ZZ& x, const unsigned char *p, long n);
477ZZ ZZFromBytes(const unsigned char *p, long n);
478// x = sum(p[i]*256^i, i=0..n-1).
479// NOTE: in the unusual event that a char is more than 8 bits,
480//       only the low order 8 bits of p[i] are used
481
482void BytesFromZZ(unsigned char *p, const ZZ& a, long n);
483// Computes p[0..n-1] such that abs(a) == sum(p[i]*256^i, i=0..n-1) mod 256^n.
484
485long NumBytes(const ZZ& a);
486long NumBytes(long a);
487// returns # of base 256 digits needed to represent abs(a).
488// NumBytes(0) == 0.
489
490
491
492/**************************************************************************\
493
494                            Pseudo-Random Numbers
495
496\**************************************************************************/
497
498
499// Routines for generating pseudo-random numbers.
500
501// These routines generate high qualtity, cryptographically strong
502// pseudo-random numbers.  They are implemented so that their behaviour
503// is completely independent of the underlying hardware and long
504// integer implementation.  Note, however, that other routines
505// throughout NTL use pseudo-random numbers, and because of this,
506// the word size of the machine can impact the sequence of numbers
507// seen by a client program.
508
509
510void SetSeed(const ZZ& s);
511// Initializes generator with a "seed" s.
512// s is first hashed to generate the initial state, so it is
513// not necessary that s itself looks random, just that
514// it has a lot of "entropy".
515// If SetSeed is not called before using the routines below,
516// a default initial state is used.
517// Calling SetSeed with s == 0, e.g. SetSeed(ZZ::zero()),
518// has the effect of re-setting the state to the default initial state.
519// Routine ZZFromBytes (above) may be useful.
520
521
522void RandomBnd(ZZ& x, const ZZ& n);
523ZZ RandomBnd(const ZZ& n);
524long RandomBnd(long n);
525// x = pseudo-random number in the range 0..n-1, or 0 if n <= 0
526
527void RandomBits(ZZ& x, long l);
528ZZ RandomBits_ZZ(long l);
529long RandomBits_long(long l);
530// x = pseudo-random number in the range 0..2^l-1.
531
532void RandomLen(ZZ& x, long l);
533ZZ RandomLen_ZZ(long l);
534long RandomLen_long(long l);
535// x = psuedo-random number with precisely l bits,
536// or 0 of l <= 0.
537
538unsigned long RandomBits_ulong(long l);
539// returns a pseudo-random number in the range 0..2^l-1
540
541unsigned long RandomWord();
542// returns a word filled with pseudo-random bits.
543// Equivalent to RandomBits_ulong(NTL_BITS_PER_LONG).
544
545
546/**************************************************************************\
547
548             Incremental Chinese Remaindering
549
550\**************************************************************************/
551
552long CRT(ZZ& a, ZZ& p, const ZZ& A, const ZZ& P);
553long CRT(ZZ& a, ZZ& p, long A, long P);
554
555// 0 <= A < P, (p, P) = 1; computes a' such that a' = a mod p,
556// a' = A mod P, and -p*P/2 < a' <= p*P/2; sets a := a', p := p*P, and
557// returns 1 if a's value has changed, otherwise 0
558
559
560/**************************************************************************\
561
562                  Rational Reconstruction
563
564\**************************************************************************/
565
566long ReconstructRational(ZZ& a, ZZ& b, const ZZ& x, const ZZ& m,
567                         const ZZ& a_bound, const ZZ& b_bound);
568
569// 0 <= x < m, m > 2 * a_bound * b_bound,
570// a_bound >= 0, b_bound > 0
571
572// This routine either returns 0, leaving a and b unchanged,
573// or returns 1 and sets a and b so that
574//   (1) a = b x (mod m),
575//   (2) |a| <= a_bound, 0 < b <= b_bound, and
576//   (3) gcd(m, b) = gcd(a, b).
577
578// If there exist a, b satisfying (1), (2), and
579//   (3') gcd(m, b) = 1,
580// then a, b are uniquely determined if we impose the additional
581// condition that gcd(a, b) = 1;  moreover, if such a, b exist,
582// then these values are returned by the routine.
583
584// Unless the calling routine can *a priori* guarantee the existence
585// of a, b satisfying (1), (2), and (3'),
586// then to ensure correctness, the calling routine should check
587// that gcd(m, b) = 1, or equivalently, gcd(a, b) = 1.
588
589// This is implemented using a variant of Lehmer's extended
590// Euclidean algorithm.
591
592// Literature:  see G. Collins and M. Encarnacion, J. Symb. Comp. 20:287-297,
593// 1995; P. Wang, M. Guy, and J. Davenport, SIGSAM Bulletin 16:2-3, 1982.
594
595
596/**************************************************************************\
597
598                                Primality Testing
599                           and Prime Number Generation
600
601\**************************************************************************/
602
603void GenPrime(ZZ& n, long l, long err = 80);
604ZZ GenPrime_ZZ(long l, long err = 80);
605long GenPrime_long(long l, long err = 80);
606
607// GenPrime generates a random prime n of length l so that the
608// probability that the resulting n is composite is bounded by 2^(-err).
609// This calls the routine RandomPrime below, and uses results of
610// Damgard, Landrock, Pomerance to "optimize"
611// the number of Miller-Rabin trials at the end.
612
613void GenGermainPrime(ZZ& n, long l, long err = 80);
614ZZ GenGermainPrime_ZZ(long l, long err = 80);
615long GenGermainPrime_long(long l, long err = 80);
616
617// A (Sophie) Germain prime is a prime p such that p' = 2*p+1 is also a prime.
618// Such primes are useful for cryptographic applications...cryptographers
619// sometimes call p' a "strong" or "safe" prime.
620// GenGermainPrime generates a random Germain prime n of length l
621// so that the probability that either n or 2*n+1 is not a prime
622// is bounded by 2^(-err).
623
624
625long ProbPrime(const ZZ& n, long NumTrials = 10);
626long ProbPrime(long n, long NumTrials = 10);
627// performs up to NumTrials Miller-witness tests (after some trial division).
628
629void RandomPrime(ZZ& n, long l, long NumTrials=10);
630ZZ RandomPrime_ZZ(long l, long NumTrials=10);
631long RandomPrime_long(long l, long NumTrials=10);
632// n = random l-bit prime.  Uses ProbPrime with NumTrials.
633
634void NextPrime(ZZ& n, const ZZ& m, long NumTrials=10);
635ZZ NextPrime(const ZZ& m, long NumTrials=10);
636// n = smallest prime >= m.  Uses ProbPrime with NumTrials.
637
638long NextPrime(long m, long NumTrials=10);
639// Single precision version of the above.
640// Result will always be bounded by NTL_ZZ_SP_BOUND, and an
641// error is raised if this cannot be satisfied.
642
643long MillerWitness(const ZZ& n, const ZZ& w);
644// Tests if w is a witness to compositeness a la Miller.  Assumption: n is
645// odd and positive, 0 <= w < n.
646// Return value of 1 implies n is composite.
647// Return value of 0 indicates n might be prime.
648
649
650/**************************************************************************\
651
652                               Exponentiation
653
654\**************************************************************************/
655
656
657void power(ZZ& x, const ZZ& a, long e); // x = a^e (e >= 0)
658ZZ power(const ZZ& a, long e);
659
660void power(ZZ& x, long a, long e);
661
662// two functional variants:
663ZZ power_ZZ(long a, long e);
664long power_long(long a, long e);
665
666void power2(ZZ& x, long e); // x = 2^e (e >= 0)
667ZZ power2_ZZ(long e);
668
669
670/**************************************************************************\
671
672                               Square Roots
673
674\**************************************************************************/
675
676
677void SqrRoot(ZZ& x, const ZZ& a); // x = floor(a^{1/2}) (a >= 0)
678ZZ SqrRoot(const ZZ& a);
679
680long SqrRoot(long a);
681
682
683
684
685/**************************************************************************\
686
687                    Jacobi symbol and modular square roots
688
689\**************************************************************************/
690
691
692long Jacobi(const ZZ& a, const ZZ& n);
693//  compute Jacobi symbol of a and n; assumes 0 <= a < n, n odd
694
695void SqrRootMod(ZZ& x, const ZZ& a, const ZZ& n);
696ZZ SqrRootMod(const ZZ& a, const ZZ& n);
697//  computes square root of a mod n; assumes n is an odd prime, and
698//  that a is a square mod n, with 0 <= a < n.
699
700
701
702
703/**************************************************************************\
704
705                             Input/Output
706
707I/O Format:
708
709Numbers are written in base 10, with an optional minus sign.
710
711\**************************************************************************/
712
713istream& operator>>(istream& s, ZZ& x); 
714ostream& operator<<(ostream& s, const ZZ& a);
715
716
717
718/**************************************************************************\
719
720                            Miscellany
721
722\**************************************************************************/
723
724
725// The following macros are defined:
726
727#define NTL_ZZ_NBITS (...)  // number of bits in a zzigit;
728                            // a ZZ is represented as a sequence of zzigits.
729
730#define NTL_SP_NBITS (...)  // max number of bits in a "single-precision" number
731
732#define NTL_WSP_NBITS (...)  // max number of bits in a "wide single-precision"
733                             // number
734
735// The following relations hold:
736//    NTL_SP_NBITS <= NTL_WSP_NBITS <= NTL_ZZ_NBITS
737//    26 <= NTL_SP_NBITS <= min(NTL_BITS_PER_LONG-2, NTL_DOUBLE_PRECISION-3)
738//    NTL_WSP_NBITS <= NTL_BITS_PER_LONG-2
739//
740// Note that NTL_ZZ_NBITS may be less than, equal to, or greater than
741// NTL_BITS_PER_LONG  -- no particular relationship should be assumed to hold.
742// In particular, expressions like (1L << NTL_ZZ_BITS) might overflow.
743//
744// "single-precision" numbers are meant to be used in conjunction with the
745//  single-precision modular arithmetic routines.
746//
747// "wide single-precision" numbers are meant to be used in conjunction
748//  with the ZZ arithmetic routines for optimal efficiency.
749
750// The following auxilliary macros are also defined
751
752#define NTL_FRADIX (...) // double-precision value of 2^NTL_ZZ_NBITS
753
754#define NTL_SP_BOUND (1L << NTL_SP_NBITS)
755#define NTL_WSP_BOUND (1L << NTL_WSP_NBITS)
756
757
758// Backward compatability note:
759// Prior to version 5.0, the macro NTL_NBITS was defined,
760// along with the macro NTL_RADIX defined to be (1L << NTL_NBITS).
761// While these macros are still available when using NTL's traditional
762// long integer package (i.e., when NTL_GMP_LIP is not set),
763// they are not available when using the GMP as the primary long integer
764// package (i.e., when NTL_GMP_LIP is set).
765// Furthermore, when writing portable programs, one should avoid these macros.
766// Note that when using traditional long integer arithmetic, we have
767//    NTL_ZZ_NBITS = NTL_SP_NBITS = NTL_WSP_NBITS = NTL_NBITS.
768
769
770// Here are some additional functions.
771
772void clear(ZZ& x); // x = 0
773void set(ZZ& x);   // x = 1
774
775void swap(ZZ& x, ZZ& y);
776// swap x and y (done by "pointer swapping", if possible).
777
778double log(const ZZ& a);
779// returns double precision approximation to log(a)
780
781long NextPowerOfTwo(long m);
782// returns least nonnegative k such that 2^k >= m
783
784long ZZ::size() const;
785// a.size() returns the number of zzigits of |a|; the
786// size of 0 is 0.
787
788void ZZ::SetSize(long k)
789// a.SetSize(k) does not change the value of a, but simply pre-allocates
790// space for k zzigits.
791
792long ZZ::SinglePrecision() const;
793// a.SinglePrecision() is a predicate that tests if abs(a) < NTL_SP_BOUND
794
795long ZZ::WideSinglePrecision() const;
796// a.WideSinglePrecision() is a predicate that tests if abs(a) < NTL_WSP_BOUND
797
798long digit(const ZZ& a, long k);
799// returns k-th zzigit of |a|, position 0 being the low-order
800// zzigit.
801// NOTE: this routine is only available when using NTL's traditional
802// long integer arithmetic, and should not be used in programs
803// that are meant to be portable.
804
805void ZZ::kill();
806// a.kill() sets a to zero and frees the space held by a.
807
808ZZ::ZZ(INIT_SIZE_TYPE, long k);
809// ZZ(INIT_SIZE, k) initializes to 0, but space is pre-allocated so
810// that numbers x with x.size() <= k can be stored without
811// re-allocation.
812
813static const ZZ& ZZ::zero();
814// ZZ::zero() yields a read-only reference to zero, if you need it.
815
816
817
818
819/**************************************************************************\
820
821                    Small Prime Generation
822
823primes are generated in sequence, starting at 2, and up to a maximum
824that is no more than min(NTL_SP_BOUND, 2^30).
825
826Example: print the primes up to 1000
827
828#include <NTL/ZZ.h>
829
830main()
831{
832   PrimeSeq s;
833   long p;
834
835   p = s.next();
836   while (p <= 1000) {
837      cout << p << "\n";
838      p = s.next();
839   }
840}
841
842\**************************************************************************/
843
844
845
846class PrimeSeq {
847public:
848   PrimeSeq();
849   ~PrimeSeq();
850
851   long next();
852   // returns next prime in the sequence.  returns 0 if list of small
853   // primes is exhausted.
854
855   void reset(long b);
856   // resets generator so that the next prime in the sequence is the
857   // smallest prime >= b.
858
859private:
860   PrimeSeq(const PrimeSeq&);        // disabled
861   void operator=(const PrimeSeq&);  // disabled
862
863};
864
865
Note: See TracBrowser for help on using the repository browser.