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