Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
6.0 KiB

  1. // nbtheory.h - written and placed in the public domain by Wei Dai
  2. #ifndef CRYPTOPP_NBTHEORY_H
  3. #define CRYPTOPP_NBTHEORY_H
  4. #include "integer.h"
  5. #include "algparam.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. // obtain pointer to small prime table and get its size
  8. CRYPTOPP_DLL const word16 * CRYPTOPP_API GetPrimeTable(unsigned int &size);
  9. // ************ primality testing ****************
  10. // generate a provable prime
  11. CRYPTOPP_DLL Integer CRYPTOPP_API MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
  12. CRYPTOPP_DLL Integer CRYPTOPP_API MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
  13. CRYPTOPP_DLL bool CRYPTOPP_API IsSmallPrime(const Integer &p);
  14. // returns true if p is divisible by some prime less than bound
  15. // bound not be greater than the largest entry in the prime table
  16. CRYPTOPP_DLL bool CRYPTOPP_API TrialDivision(const Integer &p, unsigned bound);
  17. // returns true if p is NOT divisible by small primes
  18. CRYPTOPP_DLL bool CRYPTOPP_API SmallDivisorsTest(const Integer &p);
  19. // These is no reason to use these two, use the ones below instead
  20. CRYPTOPP_DLL bool CRYPTOPP_API IsFermatProbablePrime(const Integer &n, const Integer &b);
  21. CRYPTOPP_DLL bool CRYPTOPP_API IsLucasProbablePrime(const Integer &n);
  22. CRYPTOPP_DLL bool CRYPTOPP_API IsStrongProbablePrime(const Integer &n, const Integer &b);
  23. CRYPTOPP_DLL bool CRYPTOPP_API IsStrongLucasProbablePrime(const Integer &n);
  24. // Rabin-Miller primality test, i.e. repeating the strong probable prime test
  25. // for several rounds with random bases
  26. CRYPTOPP_DLL bool CRYPTOPP_API RabinMillerTest(RandomNumberGenerator &rng, const Integer &w, unsigned int rounds);
  27. // primality test, used to generate primes
  28. CRYPTOPP_DLL bool CRYPTOPP_API IsPrime(const Integer &p);
  29. // more reliable than IsPrime(), used to verify primes generated by others
  30. CRYPTOPP_DLL bool CRYPTOPP_API VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level = 1);
  31. class CRYPTOPP_DLL PrimeSelector
  32. {
  33. public:
  34. const PrimeSelector *GetSelectorPointer() const {return this;}
  35. virtual bool IsAcceptable(const Integer &candidate) const =0;
  36. };
  37. // use a fast sieve to find the first probable prime in {x | p<=x<=max and x%mod==equiv}
  38. // returns true iff successful, value of p is undefined if no such prime exists
  39. CRYPTOPP_DLL bool CRYPTOPP_API FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
  40. CRYPTOPP_DLL unsigned int CRYPTOPP_API PrimeSearchInterval(const Integer &max);
  41. CRYPTOPP_DLL AlgorithmParameters CRYPTOPP_API MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength);
  42. // ********** other number theoretic functions ************
  43. inline Integer GCD(const Integer &a, const Integer &b)
  44. {return Integer::Gcd(a,b);}
  45. inline bool RelativelyPrime(const Integer &a, const Integer &b)
  46. {return Integer::Gcd(a,b) == Integer::One();}
  47. inline Integer LCM(const Integer &a, const Integer &b)
  48. {return a/Integer::Gcd(a,b)*b;}
  49. inline Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
  50. {return a.InverseMod(b);}
  51. // use Chinese Remainder Theorem to calculate x given x mod p and x mod q, and u = inverse of p mod q
  52. CRYPTOPP_DLL Integer CRYPTOPP_API CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u);
  53. // if b is prime, then Jacobi(a, b) returns 0 if a%b==0, 1 if a is quadratic residue mod b, -1 otherwise
  54. // check a number theory book for what Jacobi symbol means when b is not prime
  55. CRYPTOPP_DLL int CRYPTOPP_API Jacobi(const Integer &a, const Integer &b);
  56. // calculates the Lucas function V_e(p, 1) mod n
  57. CRYPTOPP_DLL Integer CRYPTOPP_API Lucas(const Integer &e, const Integer &p, const Integer &n);
  58. // calculates x such that m==Lucas(e, x, p*q), p q primes, u=inverse of p mod q
  59. CRYPTOPP_DLL Integer CRYPTOPP_API InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u);
  60. inline Integer ModularExponentiation(const Integer &a, const Integer &e, const Integer &m)
  61. {return a_exp_b_mod_c(a, e, m);}
  62. // returns x such that x*x%p == a, p prime
  63. CRYPTOPP_DLL Integer CRYPTOPP_API ModularSquareRoot(const Integer &a, const Integer &p);
  64. // returns x such that a==ModularExponentiation(x, e, p*q), p q primes,
  65. // and e relatively prime to (p-1)*(q-1)
  66. // dp=d%(p-1), dq=d%(q-1), (d is inverse of e mod (p-1)*(q-1))
  67. // and u=inverse of p mod q
  68. CRYPTOPP_DLL Integer CRYPTOPP_API ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u);
  69. // find r1 and r2 such that ax^2 + bx + c == 0 (mod p) for x in {r1, r2}, p prime
  70. // returns true if solutions exist
  71. CRYPTOPP_DLL bool CRYPTOPP_API SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p);
  72. // returns log base 2 of estimated number of operations to calculate discrete log or factor a number
  73. CRYPTOPP_DLL unsigned int CRYPTOPP_API DiscreteLogWorkFactor(unsigned int bitlength);
  74. CRYPTOPP_DLL unsigned int CRYPTOPP_API FactoringWorkFactor(unsigned int bitlength);
  75. // ********************************************************
  76. //! generator of prime numbers of special forms
  77. class CRYPTOPP_DLL PrimeAndGenerator
  78. {
  79. public:
  80. PrimeAndGenerator() {}
  81. // generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime
  82. // Precondition: pbits > 5
  83. // warning: this is slow, because primes of this form are harder to find
  84. PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
  85. {Generate(delta, rng, pbits, pbits-1);}
  86. // generate a random prime p of the form 2*r*q+delta, where q is also prime
  87. // Precondition: qbits > 4 && pbits > qbits
  88. PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
  89. {Generate(delta, rng, pbits, qbits);}
  90. void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits);
  91. const Integer& Prime() const {return p;}
  92. const Integer& SubPrime() const {return q;}
  93. const Integer& Generator() const {return g;}
  94. private:
  95. Integer p, q, g;
  96. };
  97. NAMESPACE_END
  98. #endif