Team Fortress 2 Source Code as on 22/4/2020
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.

173 lines
8.4 KiB

  1. // nbtheory.h - written and placed in the public domain by Wei Dai
  2. //! \file nbtheory.h
  3. //! \brief Classes and functions for number theoretic operations
  4. #ifndef CRYPTOPP_NBTHEORY_H
  5. #define CRYPTOPP_NBTHEORY_H
  6. #include "cryptlib.h"
  7. #include "integer.h"
  8. #include "algparam.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. // obtain pointer to small prime table and get its size
  11. CRYPTOPP_DLL const word16 * CRYPTOPP_API GetPrimeTable(unsigned int &size);
  12. // ************ primality testing ****************
  13. //! \brief Generates a provable prime
  14. //! \param rng a RandomNumberGenerator to produce keying material
  15. //! \param bits the number of bits in the prime number
  16. //! \returns Integer() meeting Maurer's tests for primality
  17. CRYPTOPP_DLL Integer CRYPTOPP_API MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
  18. //! \brief Generates a provable prime
  19. //! \param rng a RandomNumberGenerator to produce keying material
  20. //! \param bits the number of bits in the prime number
  21. //! \returns Integer() meeting Mihailescu's tests for primality
  22. //! \details Mihailescu's methods performs a search using algorithmic progressions.
  23. CRYPTOPP_DLL Integer CRYPTOPP_API MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
  24. //! \brief Tests whether a number is a small prime
  25. //! \param p a candidate prime to test
  26. //! \returns true if p is a small prime, false otherwise
  27. //! \details Internally, the library maintains a table fo the first 32719 prime numbers
  28. //! in sorted order. IsSmallPrime() searches the table and returns true if p is
  29. //! in the table.
  30. CRYPTOPP_DLL bool CRYPTOPP_API IsSmallPrime(const Integer &p);
  31. //!
  32. //! \returns true if p is divisible by some prime less than bound.
  33. //! \details TrialDivision() true if p is divisible by some prime less than bound. bound not be
  34. //! greater than the largest entry in the prime table, which is 32719.
  35. CRYPTOPP_DLL bool CRYPTOPP_API TrialDivision(const Integer &p, unsigned bound);
  36. // returns true if p is NOT divisible by small primes
  37. CRYPTOPP_DLL bool CRYPTOPP_API SmallDivisorsTest(const Integer &p);
  38. // These is no reason to use these two, use the ones below instead
  39. CRYPTOPP_DLL bool CRYPTOPP_API IsFermatProbablePrime(const Integer &n, const Integer &b);
  40. CRYPTOPP_DLL bool CRYPTOPP_API IsLucasProbablePrime(const Integer &n);
  41. CRYPTOPP_DLL bool CRYPTOPP_API IsStrongProbablePrime(const Integer &n, const Integer &b);
  42. CRYPTOPP_DLL bool CRYPTOPP_API IsStrongLucasProbablePrime(const Integer &n);
  43. // Rabin-Miller primality test, i.e. repeating the strong probable prime test
  44. // for several rounds with random bases
  45. CRYPTOPP_DLL bool CRYPTOPP_API RabinMillerTest(RandomNumberGenerator &rng, const Integer &w, unsigned int rounds);
  46. //! \brief Verifies a prime number
  47. //! \param p a candidate prime to test
  48. //! \returns true if p is a probable prime, false otherwise
  49. //! \details IsPrime() is suitable for testing candidate primes when creating them. Internally,
  50. //! IsPrime() utilizes SmallDivisorsTest(), IsStrongProbablePrime() and IsStrongLucasProbablePrime().
  51. CRYPTOPP_DLL bool CRYPTOPP_API IsPrime(const Integer &p);
  52. //! \brief Verifies a prime number
  53. //! \param rng a RandomNumberGenerator for randomized testing
  54. //! \param p a candidate prime to test
  55. //! \param level the level of thoroughness of testing
  56. //! \returns true if p is a strong probable prime, false otherwise
  57. //! \details VerifyPrime() is suitable for testing candidate primes created by others. Internally,
  58. //! VerifyPrime() utilizes IsPrime() and one-round RabinMillerTest(). If the candiate passes and
  59. //! level is greater than 1, then 10 round RabinMillerTest() primality testing is performed.
  60. CRYPTOPP_DLL bool CRYPTOPP_API VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level = 1);
  61. //! \class PrimeSelector
  62. //! \brief Application callback to signal suitability of a cabdidate prime
  63. class CRYPTOPP_DLL PrimeSelector
  64. {
  65. public:
  66. const PrimeSelector *GetSelectorPointer() const {return this;}
  67. virtual bool IsAcceptable(const Integer &candidate) const =0;
  68. };
  69. //! \brief Finds a random prime of special form
  70. //! \param p an Integer reference to receive the prime
  71. //! \param max the maximum value
  72. //! \param equiv the equivalence class based on the parameter mod
  73. //! \param mod the modulus used to reduce the equivalence class
  74. //! \param pSelector pointer to a PrimeSelector function for the application to signal suitability
  75. //! \returns true if and only if FirstPrime() finds a prime and returns the prime through p. If FirstPrime()
  76. //! returns false, then no such prime exists and the value of p is undefined
  77. //! \details FirstPrime() uses a fast sieve to find the first probable prime
  78. //! in <tt>{x | p<=x<=max and x%mod==equiv}</tt>
  79. CRYPTOPP_DLL bool CRYPTOPP_API FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
  80. CRYPTOPP_DLL unsigned int CRYPTOPP_API PrimeSearchInterval(const Integer &max);
  81. CRYPTOPP_DLL AlgorithmParameters CRYPTOPP_API MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength);
  82. // ********** other number theoretic functions ************
  83. inline Integer GCD(const Integer &a, const Integer &b)
  84. {return Integer::Gcd(a,b);}
  85. inline bool RelativelyPrime(const Integer &a, const Integer &b)
  86. {return Integer::Gcd(a,b) == Integer::One();}
  87. inline Integer LCM(const Integer &a, const Integer &b)
  88. {return a/Integer::Gcd(a,b)*b;}
  89. inline Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
  90. {return a.InverseMod(b);}
  91. // use Chinese Remainder Theorem to calculate x given x mod p and x mod q, and u = inverse of p mod q
  92. CRYPTOPP_DLL Integer CRYPTOPP_API CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u);
  93. // if b is prime, then Jacobi(a, b) returns 0 if a%b==0, 1 if a is quadratic residue mod b, -1 otherwise
  94. // check a number theory book for what Jacobi symbol means when b is not prime
  95. CRYPTOPP_DLL int CRYPTOPP_API Jacobi(const Integer &a, const Integer &b);
  96. // calculates the Lucas function V_e(p, 1) mod n
  97. CRYPTOPP_DLL Integer CRYPTOPP_API Lucas(const Integer &e, const Integer &p, const Integer &n);
  98. // calculates x such that m==Lucas(e, x, p*q), p q primes, u=inverse of p mod q
  99. CRYPTOPP_DLL Integer CRYPTOPP_API InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u);
  100. inline Integer ModularExponentiation(const Integer &a, const Integer &e, const Integer &m)
  101. {return a_exp_b_mod_c(a, e, m);}
  102. // returns x such that x*x%p == a, p prime
  103. CRYPTOPP_DLL Integer CRYPTOPP_API ModularSquareRoot(const Integer &a, const Integer &p);
  104. // returns x such that a==ModularExponentiation(x, e, p*q), p q primes,
  105. // and e relatively prime to (p-1)*(q-1)
  106. // dp=d%(p-1), dq=d%(q-1), (d is inverse of e mod (p-1)*(q-1))
  107. // and u=inverse of p mod q
  108. CRYPTOPP_DLL Integer CRYPTOPP_API ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u);
  109. // find r1 and r2 such that ax^2 + bx + c == 0 (mod p) for x in {r1, r2}, p prime
  110. // returns true if solutions exist
  111. CRYPTOPP_DLL bool CRYPTOPP_API SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p);
  112. // returns log base 2 of estimated number of operations to calculate discrete log or factor a number
  113. CRYPTOPP_DLL unsigned int CRYPTOPP_API DiscreteLogWorkFactor(unsigned int bitlength);
  114. CRYPTOPP_DLL unsigned int CRYPTOPP_API FactoringWorkFactor(unsigned int bitlength);
  115. // ********************************************************
  116. //! generator of prime numbers of special forms
  117. class CRYPTOPP_DLL PrimeAndGenerator
  118. {
  119. public:
  120. PrimeAndGenerator() {}
  121. // generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime
  122. // Precondition: pbits > 5
  123. // warning: this is slow, because primes of this form are harder to find
  124. PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
  125. {Generate(delta, rng, pbits, pbits-1);}
  126. // generate a random prime p of the form 2*r*q+delta, where q is also prime
  127. // Precondition: qbits > 4 && pbits > qbits
  128. PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
  129. {Generate(delta, rng, pbits, qbits);}
  130. void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits);
  131. const Integer& Prime() const {return p;}
  132. const Integer& SubPrime() const {return q;}
  133. const Integer& Generator() const {return g;}
  134. private:
  135. Integer p, q, g;
  136. };
  137. NAMESPACE_END
  138. #endif