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

Generated on Mon Aug 9 2010 15:56:35 for Crypto++ by  doxygen 1.7.1