void t5() { // // Now integer functions: // ref_type z1, z2; test_type t1, t2; divide_qr(a, b, z1, z2); divide_qr(a1, b1, t1, t2); BOOST_TEST_EQ(z1.str(), t1.str()); BOOST_TEST_EQ(z2.str(), t2.str()); BOOST_TEST_EQ(integer_modulus(a, si), integer_modulus(a1, si)); BOOST_TEST_EQ(lsb(a), lsb(a1)); for(unsigned i = 0; i < 1000; i += 13) { BOOST_TEST_EQ(bit_test(a, i), bit_test(a1, i)); } // We have to take care that our powers don't grow too large, otherwise this takes "forever", // also don't test for modulo types, as these may give a different result from arbitrary // precision types: BOOST_TEST_EQ(ref_type(pow(d, ui % 19)).str(), test_type(pow(d1, ui % 19)).str()); BOOST_TEST_EQ(ref_type(powm(a, b, c)).str(), test_type(powm(a1, b1, c1)).str()); BOOST_TEST_EQ(ref_type(powm(a, b, ui)).str(), test_type(powm(a1, b1, ui)).str()); BOOST_TEST_EQ(ref_type(powm(a, ui, c)).str(), test_type(powm(a1, ui, c1)).str()); }
typename enable_if_c<is_integral<I1>::value && is_unsigned<I2>::value && is_integral<I3>::value, I1>::type powm(const I1& a, I2 b, I3 c) { typedef typename detail::double_integer<I1>::type double_type; I1 x(1), y(a); double_type result; while(b > 0) { if(b & 1) { multiply(result, x, y); x = integer_modulus(result, c); } multiply(result, y, y); y = integer_modulus(result, c); b >>= 1; } return x % c; }
bool check_small_factors(const I& n) { static const boost::uint32_t small_factors1[] = { 3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u }; static const boost::uint32_t pp1 = 223092870u; boost::uint32_t m1 = integer_modulus(n, pp1); for(unsigned i = 0; i < sizeof(small_factors1) / sizeof(small_factors1[0]); ++i) { BOOST_ASSERT(pp1 % small_factors1[i] == 0); if(m1 % small_factors1[i] == 0) return false; } static const boost::uint32_t small_factors2[] = { 29u, 31u, 37u, 41u, 43u, 47u }; static const boost::uint32_t pp2 = 2756205443u; m1 = integer_modulus(n, pp2); for(unsigned i = 0; i < sizeof(small_factors2) / sizeof(small_factors2[0]); ++i) { BOOST_ASSERT(pp2 % small_factors2[i] == 0); if(m1 % small_factors2[i] == 0) return false; } static const boost::uint32_t small_factors3[] = { 53u, 59u, 61u, 67u, 71u }; static const boost::uint32_t pp3 = 907383479u; m1 = integer_modulus(n, pp3); for(unsigned i = 0; i < sizeof(small_factors3) / sizeof(small_factors3[0]); ++i) { BOOST_ASSERT(pp3 % small_factors3[i] == 0); if(m1 % small_factors3[i] == 0) return false; } static const boost::uint32_t small_factors4[] = { 73u, 79u, 83u, 89u, 97u }; static const boost::uint32_t pp4 = 4132280413u; m1 = integer_modulus(n, pp4); for(unsigned i = 0; i < sizeof(small_factors4) / sizeof(small_factors4[0]); ++i) { BOOST_ASSERT(pp4 % small_factors4[i] == 0); if(m1 % small_factors4[i] == 0) return false; } static const boost::uint32_t small_factors5[6][4] = { { 101u, 103u, 107u, 109u }, { 113u, 127u, 131u, 137u }, { 139u, 149u, 151u, 157u }, { 163u, 167u, 173u, 179u }, { 181u, 191u, 193u, 197u }, { 199u, 211u, 223u, 227u } }; static const boost::uint32_t pp5[6] = { 121330189u, 113u * 127u * 131u * 137u, 139u * 149u * 151u * 157u, 163u * 167u * 173u * 179u, 181u * 191u * 193u * 197u, 199u * 211u * 223u * 227u }; for(unsigned k = 0; k < sizeof(pp5) / sizeof(*pp5); ++k) { m1 = integer_modulus(n, pp5[k]); for(unsigned i = 0; i < 4; ++i) { BOOST_ASSERT(pp5[k] % small_factors5[k][i] == 0); if(m1 % small_factors5[k][i] == 0) return false; } } return true; }