00001
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
00012 extern const unsigned int maxPrimeTableSize;
00013 extern const word lastSmallPrime;
00014 extern unsigned int primeTableSize;
00015 extern word primeTable[];
00016
00017
00018 void BuildPrimeTable();
00019
00020
00021
00022
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
00029
00030 bool TrialDivision(const Integer &p, unsigned bound);
00031
00032
00033 bool SmallDivisorsTest(const Integer &p);
00034
00035
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
00043
00044 bool RabinMillerTest(RandomNumberGenerator &rng, const Integer &w, unsigned int rounds);
00045
00046
00047 bool IsPrime(const Integer &p);
00048
00049
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
00060
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
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
00080 Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q);
00081
00082 Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u);
00083
00084
00085
00086 int Jacobi(const Integer &a, const Integer &b);
00087
00088
00089 Integer Lucas(const Integer &e, const Integer &p, const Integer &n);
00090
00091 Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q);
00092
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
00098 Integer ModularSquareRoot(const Integer &a, const Integer &p);
00099
00100
00101 Integer ModularRoot(const Integer &a, const Integer &e, const Integer &p, const Integer &q);
00102
00103
00104 Integer ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u);
00105
00106
00107
00108 bool SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p);
00109
00110
00111 unsigned int DiscreteLogWorkFactor(unsigned int bitlength);
00112 unsigned int FactoringWorkFactor(unsigned int bitlength);
00113
00114
00115
00116
00117 class PrimeAndGenerator
00118 {
00119 public:
00120 PrimeAndGenerator() {}
00121
00122
00123
00124 PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
00125 {Generate(delta, rng, pbits, pbits-1);}
00126
00127
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