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()); }
BigInt invmod(BigInt const &a, BigInt const &m) { BigInt s1 = 0, s0 = 1; BigInt r1 = m, r0 = a; BigInt q, r; while (sign(r1) != 0) { divide_qr(r0, r1, q, r); r0 = std::move(r1); r1 = std::move(r); r = s1; s1 = s0 - q * r; s0 = std::move(r); } while (sign(s0) < 0) s0 += m; return s0; }