Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

nbtheory.h

00001 // nbtheory.h - written and placed in the public domain by Wei Dai 00002 00003 #ifndef CRYPTOPP_NBTHEORY_H 00004 #define CRYPTOPP_NBTHEORY_H 00005 00006 #include "integer.h" 00007 #include "algparam.h" 00008 00009 NAMESPACE_BEGIN(CryptoPP) 00010 00011 // export a table of small primes 00012 extern const unsigned int maxPrimeTableSize; 00013 extern const word lastSmallPrime; 00014 extern unsigned int primeTableSize; 00015 extern word primeTable[]; 00016 00017 // build up the table to maxPrimeTableSize 00018 void BuildPrimeTable(); 00019 00020 // ************ primality testing **************** 00021 00022 // generate a provable prime 00023 Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits); 00024 Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits); 00025 00026 bool IsSmallPrime(const Integer &p); 00027 00028 // returns true if p is divisible by some prime less than bound 00029 // bound not be greater than the largest entry in the prime table 00030 bool TrialDivision(const Integer &p, unsigned bound); 00031 00032 // returns true if p is NOT divisible by small primes 00033 bool SmallDivisorsTest(const Integer &p); 00034 00035 // These is no reason to use these two, use the ones below instead 00036 bool IsFermatProbablePrime(const Integer &n, const Integer &b); 00037 bool IsLucasProbablePrime(const Integer &n); 00038 00039 bool IsStrongProbablePrime(const Integer &n, const Integer &b); 00040 bool IsStrongLucasProbablePrime(const Integer &n); 00041 00042 // Rabin-Miller primality test, i.e. repeating the strong probable prime test 00043 // for several rounds with random bases 00044 bool RabinMillerTest(RandomNumberGenerator &rng, const Integer &w, unsigned int rounds); 00045 00046 // primality test, used to generate primes 00047 bool IsPrime(const Integer &p); 00048 00049 // more reliable than IsPrime(), used to verify primes generated by others 00050 bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level = 1); 00051 00052 class PrimeSelector 00053 { 00054 public: 00055 const PrimeSelector *GetSelectorPointer() const {return this;} 00056 virtual bool IsAcceptable(const Integer &candidate) const =0; 00057 }; 00058 00059 // use a fast sieve to find the first probable prime in {x | p<=x<=max and x%mod==equiv} 00060 // returns true iff successful, value of p is undefined if no such prime exists 00061 bool FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector); 00062 00063 unsigned int PrimeSearchInterval(const Integer &max); 00064 00065 AlgorithmParameters<AlgorithmParameters<AlgorithmParameters<NullNameValuePairs, Integer::RandomNumberType>, Integer>, Integer> 00066 MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength); 00067 00068 // ********** other number theoretic functions ************ 00069 00070 inline Integer GCD(const Integer &a, const Integer &b) 00071 {return Integer::Gcd(a,b);} 00072 inline bool RelativelyPrime(const Integer &a, const Integer &b) 00073 {return Integer::Gcd(a,b) == Integer::One();} 00074 inline Integer LCM(const Integer &a, const Integer &b) 00075 {return a/Integer::Gcd(a,b)*b;} 00076 inline Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b) 00077 {return a.InverseMod(b);} 00078 00079 // use Chinese Remainder Theorem to calculate x given x mod p and x mod q 00080 Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q); 00081 // use this one if u = inverse of p mod q has been precalculated 00082 Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u); 00083 00084 // if b is prime, then Jacobi(a, b) returns 0 if a%b==0, 1 if a is quadratic residue mod b, -1 otherwise 00085 // check a number theory book for what Jacobi symbol means when b is not prime 00086 int Jacobi(const Integer &a, const Integer &b); 00087 00088 // calculates the Lucas function V_e(p, 1) mod n 00089 Integer Lucas(const Integer &e, const Integer &p, const Integer &n); 00090 // calculates x such that m==Lucas(e, x, p*q), p q primes 00091 Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q); 00092 // use this one if u=inverse of p mod q has been precalculated 00093 Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u); 00094 00095 inline Integer ModularExponentiation(const Integer &a, const Integer &e, const Integer &m) 00096 {return a_exp_b_mod_c(a, e, m);} 00097 // returns x such that x*x%p == a, p prime 00098 Integer ModularSquareRoot(const Integer &a, const Integer &p); 00099 // returns x such that a==ModularExponentiation(x, e, p*q), p q primes, 00100 // and e relatively prime to (p-1)*(q-1) 00101 Integer ModularRoot(const Integer &a, const Integer &e, const Integer &p, const Integer &q); 00102 // use this one if dp=d%(p-1), dq=d%(q-1), (d is inverse of e mod (p-1)*(q-1)) 00103 // and u=inverse of p mod q have been precalculated 00104 Integer ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u); 00105 00106 // find r1 and r2 such that ax^2 + bx + c == 0 (mod p) for x in {r1, r2}, p prime 00107 // returns true if solutions exist 00108 bool SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p); 00109 00110 // returns log base 2 of estimated number of operations to calculate discrete log or factor a number 00111 unsigned int DiscreteLogWorkFactor(unsigned int bitlength); 00112 unsigned int FactoringWorkFactor(unsigned int bitlength); 00113 00114 // ******************************************************** 00115 00116 //! generator of prime numbers of special forms 00117 class PrimeAndGenerator 00118 { 00119 public: 00120 PrimeAndGenerator() {} 00121 // generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime 00122 // Precondition: pbits > 5 00123 // warning: this is slow, because primes of this form are harder to find 00124 PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits) 00125 {Generate(delta, rng, pbits, pbits-1);} 00126 // generate a random prime p of the form 2*r*q+delta, where q is also prime 00127 // Precondition: qbits > 4 && pbits > qbits 00128 PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits) 00129 {Generate(delta, rng, pbits, qbits);} 00130 00131 void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits); 00132 00133 const Integer& Prime() const {return p;} 00134 const Integer& SubPrime() const {return q;} 00135 const Integer& Generator() const {return g;} 00136 00137 private: 00138 Integer p, q, g; 00139 }; 00140 00141 NAMESPACE_END 00142 00143 #endif

Generated on Wed Jul 28 08:07:08 2004 for Crypto++ by doxygen 1.3.7