annotate DEPENDENCIES/generic/include/boost/multiprecision/miller_rabin.hpp @ 125:34e428693f5d vext

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