source: git/ntl/doc/ZZ.txt @ 09da99

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