nbtheory.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // nbtheory.h - originally 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. /// \brief The Small Prime table
  11. /// \details GetPrimeTable obtains pointer to small prime table and provides the size of the table.
  12. CRYPTOPP_DLL const word16 * CRYPTOPP_API GetPrimeTable(unsigned int &size);
  13. // ************ primality testing ****************
  14. /// \brief Generates a provable prime
  15. /// \param rng a RandomNumberGenerator to produce random material
  16. /// \param bits the number of bits in the prime number
  17. /// \return Integer() meeting Maurer's tests for primality
  18. CRYPTOPP_DLL Integer CRYPTOPP_API MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
  19. /// \brief Generates a provable prime
  20. /// \param rng a RandomNumberGenerator to produce random material
  21. /// \param bits the number of bits in the prime number
  22. /// \return Integer() meeting Mihailescu's tests for primality
  23. /// \details Mihailescu's methods performs a search using algorithmic progressions.
  24. CRYPTOPP_DLL Integer CRYPTOPP_API MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
  25. /// \brief Tests whether a number is a small prime
  26. /// \param p a candidate prime to test
  27. /// \return true if p is a small prime, false otherwise
  28. /// \details Internally, the library maintains a table of the first 32719 prime numbers
  29. /// in sorted order. IsSmallPrime searches the table and returns true if p is
  30. /// in the table.
  31. CRYPTOPP_DLL bool CRYPTOPP_API IsSmallPrime(const Integer &p);
  32. /// \brief Tests whether a number is divisible by a small prime
  33. /// \return true if p is divisible by some prime less than bound.
  34. /// \details TrialDivision() returns <tt>true</tt> if <tt>p</tt> is divisible by some prime less
  35. /// than <tt>bound</tt>. <tt>bound</tt> should not be greater than the largest entry in the
  36. /// prime table, which is 32719.
  37. CRYPTOPP_DLL bool CRYPTOPP_API TrialDivision(const Integer &p, unsigned bound);
  38. /// \brief Tests whether a number is divisible by a small prime
  39. /// \return true if p is NOT divisible by small primes.
  40. /// \details SmallDivisorsTest() returns <tt>true</tt> if <tt>p</tt> is NOT divisible by some
  41. /// prime less than 32719.
  42. CRYPTOPP_DLL bool CRYPTOPP_API SmallDivisorsTest(const Integer &p);
  43. /// \brief Determine if a number is probably prime
  44. /// \param n the number to test
  45. /// \param b the base to exponentiate
  46. /// \return true if the number n is probably prime, false otherwise.
  47. /// \details IsFermatProbablePrime raises <tt>b</tt> to the <tt>n-1</tt> power and checks if
  48. /// the result is congruent to 1 modulo <tt>n</tt>.
  49. /// \details These is no reason to use IsFermatProbablePrime, use IsStrongProbablePrime or
  50. /// IsStrongLucasProbablePrime instead.
  51. /// \sa IsStrongProbablePrime, IsStrongLucasProbablePrime
  52. CRYPTOPP_DLL bool CRYPTOPP_API IsFermatProbablePrime(const Integer &n, const Integer &b);
  53. /// \brief Determine if a number is probably prime
  54. /// \param n the number to test
  55. /// \return true if the number n is probably prime, false otherwise.
  56. /// \details These is no reason to use IsLucasProbablePrime, use IsStrongProbablePrime or
  57. /// IsStrongLucasProbablePrime instead.
  58. /// \sa IsStrongProbablePrime, IsStrongLucasProbablePrime
  59. CRYPTOPP_DLL bool CRYPTOPP_API IsLucasProbablePrime(const Integer &n);
  60. /// \brief Determine if a number is probably prime
  61. /// \param n the number to test
  62. /// \param b the base to exponentiate
  63. /// \return true if the number n is probably prime, false otherwise.
  64. CRYPTOPP_DLL bool CRYPTOPP_API IsStrongProbablePrime(const Integer &n, const Integer &b);
  65. /// \brief Determine if a number is probably prime
  66. /// \param n the number to test
  67. /// \return true if the number n is probably prime, false otherwise.
  68. CRYPTOPP_DLL bool CRYPTOPP_API IsStrongLucasProbablePrime(const Integer &n);
  69. /// \brief Determine if a number is probably prime
  70. /// \param rng a RandomNumberGenerator to produce random material
  71. /// \param n the number to test
  72. /// \param rounds the number of tests to perform
  73. /// \details This is the Rabin-Miller primality test, i.e. repeating the strong probable prime
  74. /// test for several rounds with random bases
  75. /// \sa <A HREF="https://crypto.stackexchange.com/q/17707/10496">Trial divisions before
  76. /// Miller-Rabin checks?</A> on Crypto Stack Exchange
  77. CRYPTOPP_DLL bool CRYPTOPP_API RabinMillerTest(RandomNumberGenerator &rng, const Integer &n, unsigned int rounds);
  78. /// \brief Verifies a number is probably prime
  79. /// \param p a candidate prime to test
  80. /// \return true if p is a probable prime, false otherwise
  81. /// \details IsPrime() is suitable for testing candidate primes when creating them. Internally,
  82. /// IsPrime() utilizes SmallDivisorsTest(), IsStrongProbablePrime() and IsStrongLucasProbablePrime().
  83. CRYPTOPP_DLL bool CRYPTOPP_API IsPrime(const Integer &p);
  84. /// \brief Verifies a number is probably prime
  85. /// \param rng a RandomNumberGenerator for randomized testing
  86. /// \param p a candidate prime to test
  87. /// \param level the level of thoroughness of testing
  88. /// \return true if p is a strong probable prime, false otherwise
  89. /// \details VerifyPrime() is suitable for testing candidate primes created by others. Internally,
  90. /// VerifyPrime() utilizes IsPrime() and one-round RabinMillerTest(). If the candidate passes and
  91. /// level is greater than 1, then 10 round RabinMillerTest() primality testing is performed.
  92. CRYPTOPP_DLL bool CRYPTOPP_API VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level = 1);
  93. /// \brief Application callback to signal suitability of a cabdidate prime
  94. class CRYPTOPP_DLL PrimeSelector
  95. {
  96. public:
  97. virtual ~PrimeSelector() {}
  98. const PrimeSelector *GetSelectorPointer() const {return this;}
  99. virtual bool IsAcceptable(const Integer &candidate) const =0;
  100. };
  101. /// \brief Finds a random prime of special form
  102. /// \param p an Integer reference to receive the prime
  103. /// \param max the maximum value
  104. /// \param equiv the equivalence class based on the parameter mod
  105. /// \param mod the modulus used to reduce the equivalence class
  106. /// \param pSelector pointer to a PrimeSelector function for the application to signal suitability
  107. /// \return true if and only if FirstPrime() finds a prime and returns the prime through p. If FirstPrime()
  108. /// returns false, then no such prime exists and the value of p is undefined
  109. /// \details FirstPrime() uses a fast sieve to find the first probable prime
  110. /// in <tt>{x | p<=x<=max and x%mod==equiv}</tt>
  111. CRYPTOPP_DLL bool CRYPTOPP_API FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
  112. CRYPTOPP_DLL unsigned int CRYPTOPP_API PrimeSearchInterval(const Integer &max);
  113. CRYPTOPP_DLL AlgorithmParameters CRYPTOPP_API MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength);
  114. // ********** other number theoretic functions ************
  115. /// \brief Calculate the greatest common divisor
  116. /// \param a the first term
  117. /// \param b the second term
  118. /// \return the greatest common divisor if one exists, 0 otherwise.
  119. inline Integer GCD(const Integer &a, const Integer &b)
  120. {return Integer::Gcd(a,b);}
  121. /// \brief Determine relative primality
  122. /// \param a the first term
  123. /// \param b the second term
  124. /// \return true if <tt>a</tt> and <tt>b</tt> are relatively prime, false otherwise.
  125. inline bool RelativelyPrime(const Integer &a, const Integer &b)
  126. {return Integer::Gcd(a,b) == Integer::One();}
  127. /// \brief Calculate the least common multiple
  128. /// \param a the first term
  129. /// \param b the second term
  130. /// \return the least common multiple of <tt>a</tt> and <tt>b</tt>.
  131. inline Integer LCM(const Integer &a, const Integer &b)
  132. {return a/Integer::Gcd(a,b)*b;}
  133. /// \brief Calculate multiplicative inverse
  134. /// \param a the number to test
  135. /// \param b the modulus
  136. /// \return an Integer <tt>(a ^ -1) % n</tt> or 0 if none exists.
  137. /// \details EuclideanMultiplicativeInverse returns the multiplicative inverse of the Integer
  138. /// <tt>*a</tt> modulo the Integer <tt>b</tt>. If no Integer exists then Integer 0 is returned.
  139. inline Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
  140. {return a.InverseMod(b);}
  141. /// \brief Chinese Remainder Theorem
  142. /// \param xp the first number, mod p
  143. /// \param p the first prime modulus
  144. /// \param xq the second number, mod q
  145. /// \param q the second prime modulus
  146. /// \param u inverse of p mod q
  147. /// \return the CRT value of the parameters
  148. /// \details CRT uses the Chinese Remainder Theorem to calculate <tt>x</tt> given
  149. /// <tt>x mod p</tt> and <tt>x mod q</tt>, and <tt>u</tt> the inverse of <tt>p mod q</tt>.
  150. CRYPTOPP_DLL Integer CRYPTOPP_API CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u);
  151. /// \brief Calculate the Jacobi symbol
  152. /// \param a the first term
  153. /// \param b the second term
  154. /// \return the Jacobi symbol.
  155. /// \details Jacobi symbols are calculated using the following rules:
  156. /// -# if <tt>b</tt> is prime, then <tt>Jacobi(a, b)</tt>, then return 0
  157. /// -# if <tt>a%b</tt>==0 AND <tt>a</tt> is quadratic residue <tt>mod b</tt>, then return 1
  158. /// -# return -1 otherwise
  159. /// \details Refer to a number theory book for what Jacobi symbol means when <tt>b</tt> is not prime.
  160. CRYPTOPP_DLL int CRYPTOPP_API Jacobi(const Integer &a, const Integer &b);
  161. /// \brief Calculate the Lucas value
  162. /// \return the Lucas value
  163. /// \details Lucas() calculates the Lucas function <tt>V_e(p, 1) mod n</tt>.
  164. CRYPTOPP_DLL Integer CRYPTOPP_API Lucas(const Integer &e, const Integer &p, const Integer &n);
  165. /// \brief Calculate the inverse Lucas value
  166. /// \return the inverse Lucas value
  167. /// \details InverseLucas() calculates <tt>x</tt> such that <tt>m==Lucas(e, x, p*q)</tt>,
  168. /// <tt>p q</tt> primes, <tt>u</tt> is inverse of <tt>p mod q</tt>.
  169. CRYPTOPP_DLL Integer CRYPTOPP_API InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u);
  170. /// \brief Modular multiplication
  171. /// \param x the first term
  172. /// \param y the second term
  173. /// \param m the modulus
  174. /// \return an Integer <tt>(x * y) % m</tt>.
  175. inline Integer ModularMultiplication(const Integer &x, const Integer &y, const Integer &m)
  176. {return a_times_b_mod_c(x, y, m);}
  177. /// \brief Modular exponentiation
  178. /// \param x the base
  179. /// \param e the exponent
  180. /// \param m the modulus
  181. /// \return an Integer <tt>(a ^ b) % m</tt>.
  182. inline Integer ModularExponentiation(const Integer &x, const Integer &e, const Integer &m)
  183. {return a_exp_b_mod_c(x, e, m);}
  184. /// \brief Extract a modular square root
  185. /// \param a the number to extract square root
  186. /// \param p the prime modulus
  187. /// \return the modular square root if it exists
  188. /// \details ModularSquareRoot returns <tt>x</tt> such that <tt>x*x%p == a</tt>, <tt>p</tt> prime
  189. CRYPTOPP_DLL Integer CRYPTOPP_API ModularSquareRoot(const Integer &a, const Integer &p);
  190. /// \brief Extract a modular root
  191. /// \return a modular root if it exists
  192. /// \details ModularRoot returns <tt>x</tt> such that <tt>a==ModularExponentiation(x, e, p*q)</tt>,
  193. /// <tt>p</tt> <tt>q</tt> primes, and <tt>e</tt> relatively prime to <tt>(p-1)*(q-1)</tt>,
  194. /// <tt>dp=d%(p-1)</tt>, <tt>dq=d%(q-1)</tt>, (d is inverse of <tt>e mod (p-1)*(q-1)</tt>)
  195. /// and <tt>u=inverse of p mod q</tt>.
  196. CRYPTOPP_DLL Integer CRYPTOPP_API ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u);
  197. /// \brief Solve a Modular Quadratic Equation
  198. /// \param r1 the first residue
  199. /// \param r2 the second residue
  200. /// \param a the first coefficient
  201. /// \param b the second coefficient
  202. /// \param c the third constant
  203. /// \param p the prime modulus
  204. /// \return true if solutions exist
  205. /// \details SolveModularQuadraticEquation() finds <tt>r1</tt> and <tt>r2</tt> such that <tt>ax^2 +
  206. /// bx + c == 0 (mod p)</tt> for x in <tt>{r1, r2}</tt>, <tt>p</tt> prime.
  207. CRYPTOPP_DLL bool CRYPTOPP_API SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p);
  208. /// \brief Estimate work factor
  209. /// \param bitlength the size of the number, in bits
  210. /// \return the estimated work factor, in operations
  211. /// \details DiscreteLogWorkFactor returns log base 2 of estimated number of operations to
  212. /// calculate discrete log or factor a number.
  213. CRYPTOPP_DLL unsigned int CRYPTOPP_API DiscreteLogWorkFactor(unsigned int bitlength);
  214. /// \brief Estimate work factor
  215. /// \param bitlength the size of the number, in bits
  216. /// \return the estimated work factor, in operations
  217. /// \details FactoringWorkFactor returns log base 2 of estimated number of operations to
  218. /// calculate discrete log or factor a number.
  219. CRYPTOPP_DLL unsigned int CRYPTOPP_API FactoringWorkFactor(unsigned int bitlength);
  220. // ********************************************************
  221. /// \brief Generator of prime numbers of special forms
  222. class CRYPTOPP_DLL PrimeAndGenerator
  223. {
  224. public:
  225. /// \brief Construct a PrimeAndGenerator
  226. PrimeAndGenerator() {}
  227. /// \brief Construct a PrimeAndGenerator
  228. /// \param delta +1 or -1
  229. /// \param rng a RandomNumberGenerator derived class
  230. /// \param pbits the number of bits in the prime p
  231. /// \details PrimeAndGenerator() generates a random prime p of the form <tt>2*q+delta</tt>, where delta is 1 or -1 and q is
  232. /// also prime. Internally the constructor calls <tt>Generate(delta, rng, pbits, pbits-1)</tt>.
  233. /// \pre <tt>pbits > 5</tt>
  234. /// \warning This PrimeAndGenerator() is slow because primes of this form are harder to find.
  235. PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
  236. {Generate(delta, rng, pbits, pbits-1);}
  237. /// \brief Construct a PrimeAndGenerator
  238. /// \param delta +1 or -1
  239. /// \param rng a RandomNumberGenerator derived class
  240. /// \param pbits the number of bits in the prime p
  241. /// \param qbits the number of bits in the prime q
  242. /// \details PrimeAndGenerator() generates a random prime p of the form <tt>2*r*q+delta</tt>, where q is also prime.
  243. /// Internally the constructor calls <tt>Generate(delta, rng, pbits, qbits)</tt>.
  244. /// \pre <tt>qbits > 4 && pbits > qbits</tt>
  245. PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
  246. {Generate(delta, rng, pbits, qbits);}
  247. /// \brief Generate a Prime and Generator
  248. /// \param delta +1 or -1
  249. /// \param rng a RandomNumberGenerator derived class
  250. /// \param pbits the number of bits in the prime p
  251. /// \param qbits the number of bits in the prime q
  252. /// \details Generate() generates a random prime p of the form <tt>2*r*q+delta</tt>, where q is also prime.
  253. void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits);
  254. /// \brief Retrieve first prime
  255. /// \return Prime() returns the prime p.
  256. const Integer& Prime() const {return p;}
  257. /// \brief Retrieve second prime
  258. /// \return SubPrime() returns the prime q.
  259. const Integer& SubPrime() const {return q;}
  260. /// \brief Retrieve the generator
  261. /// \return Generator() returns the generator g.
  262. const Integer& Generator() const {return g;}
  263. private:
  264. Integer p, q, g;
  265. };
  266. NAMESPACE_END
  267. #endif