miller_rabin.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_MP_MR_HPP
  6. #define BOOST_MP_MR_HPP
  7. #include <random>
  8. #include <cstdint>
  9. #include <type_traits>
  10. #include <boost/multiprecision/detail/standalone_config.hpp>
  11. #include <boost/multiprecision/integer.hpp>
  12. #include <boost/multiprecision/detail/uniform_int_distribution.hpp>
  13. #include <boost/multiprecision/detail/assert.hpp>
  14. namespace boost {
  15. namespace multiprecision {
  16. namespace detail {
  17. template <class I>
  18. bool check_small_factors(const I& n)
  19. {
  20. constexpr std::uint32_t small_factors1[] = {
  21. 3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u};
  22. constexpr std::uint32_t pp1 = 223092870u;
  23. std::uint32_t m1 = integer_modulus(n, pp1);
  24. for (std::size_t i = 0; i < sizeof(small_factors1) / sizeof(small_factors1[0]); ++i)
  25. {
  26. BOOST_MP_ASSERT(pp1 % small_factors1[i] == 0);
  27. if (m1 % small_factors1[i] == 0)
  28. return false;
  29. }
  30. constexpr std::uint32_t small_factors2[] = {
  31. 29u, 31u, 37u, 41u, 43u, 47u};
  32. constexpr std::uint32_t pp2 = 2756205443u;
  33. m1 = integer_modulus(n, pp2);
  34. for (std::size_t i = 0; i < sizeof(small_factors2) / sizeof(small_factors2[0]); ++i)
  35. {
  36. BOOST_MP_ASSERT(pp2 % small_factors2[i] == 0);
  37. if (m1 % small_factors2[i] == 0)
  38. return false;
  39. }
  40. constexpr std::uint32_t small_factors3[] = {
  41. 53u, 59u, 61u, 67u, 71u};
  42. constexpr std::uint32_t pp3 = 907383479u;
  43. m1 = integer_modulus(n, pp3);
  44. for (std::size_t i = 0; i < sizeof(small_factors3) / sizeof(small_factors3[0]); ++i)
  45. {
  46. BOOST_MP_ASSERT(pp3 % small_factors3[i] == 0);
  47. if (m1 % small_factors3[i] == 0)
  48. return false;
  49. }
  50. constexpr std::uint32_t small_factors4[] = {
  51. 73u, 79u, 83u, 89u, 97u};
  52. constexpr std::uint32_t pp4 = 4132280413u;
  53. m1 = integer_modulus(n, pp4);
  54. for (std::size_t i = 0; i < sizeof(small_factors4) / sizeof(small_factors4[0]); ++i)
  55. {
  56. BOOST_MP_ASSERT(pp4 % small_factors4[i] == 0);
  57. if (m1 % small_factors4[i] == 0)
  58. return false;
  59. }
  60. constexpr std::uint32_t small_factors5[6][4] = {
  61. {101u, 103u, 107u, 109u},
  62. {113u, 127u, 131u, 137u},
  63. {139u, 149u, 151u, 157u},
  64. {163u, 167u, 173u, 179u},
  65. {181u, 191u, 193u, 197u},
  66. {199u, 211u, 223u, 227u}};
  67. constexpr std::uint32_t pp5[6] =
  68. {
  69. 121330189u,
  70. 113u * 127u * 131u * 137u,
  71. 139u * 149u * 151u * 157u,
  72. 163u * 167u * 173u * 179u,
  73. 181u * 191u * 193u * 197u,
  74. 199u * 211u * 223u * 227u};
  75. for (std::size_t k = 0; k < sizeof(pp5) / sizeof(*pp5); ++k)
  76. {
  77. m1 = integer_modulus(n, pp5[k]);
  78. for (std::size_t i = 0; i < 4; ++i)
  79. {
  80. BOOST_MP_ASSERT(pp5[k] % small_factors5[k][i] == 0);
  81. if (m1 % small_factors5[k][i] == 0)
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. inline bool is_small_prime(std::size_t n)
  88. {
  89. constexpr unsigned char p[] =
  90. {
  91. 3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u, 29u, 31u,
  92. 37u, 41u, 43u, 47u, 53u, 59u, 61u, 67u, 71u, 73u,
  93. 79u, 83u, 89u, 97u, 101u, 103u, 107u, 109u, 113u,
  94. 127u, 131u, 137u, 139u, 149u, 151u, 157u, 163u,
  95. 167u, 173u, 179u, 181u, 191u, 193u, 197u, 199u,
  96. 211u, 223u, 227u};
  97. for (std::size_t i = 0; i < sizeof(p) / sizeof(*p); ++i)
  98. {
  99. if (n == p[i])
  100. return true;
  101. }
  102. return false;
  103. }
  104. template <class I>
  105. typename std::enable_if<std::is_convertible<I, unsigned>::value, unsigned>::type
  106. cast_to_unsigned(const I& val)
  107. {
  108. return static_cast<unsigned>(val);
  109. }
  110. template <class I>
  111. typename std::enable_if<!std::is_convertible<I, unsigned>::value, unsigned>::type
  112. cast_to_unsigned(const I& val)
  113. {
  114. return val.template convert_to<unsigned>();
  115. }
  116. } // namespace detail
  117. template <class I, class Engine>
  118. typename std::enable_if<number_category<I>::value == number_kind_integer, bool>::type
  119. miller_rabin_test(const I& n, std::size_t trials, Engine& gen)
  120. {
  121. using number_type = I;
  122. if (n == 2)
  123. return true; // Trivial special case.
  124. if (bit_test(n, 0) == 0)
  125. return false; // n is even
  126. if (n <= 227)
  127. return detail::is_small_prime(detail::cast_to_unsigned(n));
  128. if (!detail::check_small_factors(n))
  129. return false;
  130. number_type nm1 = n - 1;
  131. //
  132. // Begin with a single Fermat test - it excludes a lot of candidates:
  133. //
  134. number_type q(228), x, y; // We know n is greater than this, as we've excluded small factors
  135. x = powm(q, nm1, n);
  136. if (x != 1u)
  137. return false;
  138. q = n - 1;
  139. std::size_t k = lsb(q);
  140. q >>= k;
  141. // Declare our random number generator:
  142. boost::multiprecision::uniform_int_distribution<number_type> dist(2, n - 2);
  143. //
  144. // Execute the trials:
  145. //
  146. for (std::size_t i = 0; i < trials; ++i)
  147. {
  148. x = dist(gen);
  149. y = powm(x, q, n);
  150. std::size_t j = 0;
  151. while (true)
  152. {
  153. if (y == nm1)
  154. break;
  155. if (y == 1)
  156. {
  157. if (j == 0)
  158. break;
  159. return false; // test failed
  160. }
  161. if (++j == k)
  162. return false; // failed
  163. y = powm(y, 2, n);
  164. }
  165. }
  166. return true; // Yeheh! probably prime.
  167. }
  168. template <class I>
  169. typename std::enable_if<number_category<I>::value == number_kind_integer, bool>::type
  170. miller_rabin_test(const I& x, std::size_t trials)
  171. {
  172. static std::mt19937 gen;
  173. return miller_rabin_test(x, trials, gen);
  174. }
  175. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class Engine>
  176. bool miller_rabin_test(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& n, std::size_t trials, Engine& gen)
  177. {
  178. using number_type = typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type;
  179. return miller_rabin_test(number_type(n), trials, gen);
  180. }
  181. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  182. bool miller_rabin_test(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& n, std::size_t trials)
  183. {
  184. using number_type = typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type;
  185. return miller_rabin_test(number_type(n), trials);
  186. }
  187. }} // namespace boost::multiprecision
  188. #endif