diff --git a/crypto3/libs/multiprecision/test/big_mod_basic.cpp b/crypto3/libs/multiprecision/test/big_mod_basic.cpp index 612188312..85f8642b1 100644 --- a/crypto3/libs/multiprecision/test/big_mod_basic.cpp +++ b/crypto3/libs/multiprecision/test/big_mod_basic.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -36,7 +37,9 @@ static_assert(std::is_same_v, montgomery_big_mod> static_assert(std::is_same_v, big_mod>); using montgomery_big_mod_t = montgomery_big_mod; -using big_mod_t = big_mod; +using barrett_big_mod_t = big_mod; + +using modular_types = std::tuple; BOOST_AUTO_TEST_SUITE(smoke) @@ -78,16 +81,16 @@ BOOST_AUTO_TEST_CASE(construct_modular_rt_small) { BOOST_CHECK_EQUAL(a.str(), "0x64"); } -BOOST_AUTO_TEST_CASE(to_string_trivial) { - BOOST_CHECK_EQUAL((static_cast(0x1_big_uint)).str(), "0x1"); +BOOST_AUTO_TEST_CASE_TEMPLATE(to_string_trivial, big_mod_t, modular_types) { + BOOST_CHECK_EQUAL((static_cast(0x1_big_uint)).str(), "0x1"); } -BOOST_AUTO_TEST_CASE(to_string_small) { - BOOST_CHECK_EQUAL((static_cast(0x20_big_uint)).str(), "0x20"); +BOOST_AUTO_TEST_CASE_TEMPLATE(to_string_small, big_mod_t, modular_types) { + BOOST_CHECK_EQUAL((static_cast(0x20_big_uint)).str(), "0x20"); } -BOOST_AUTO_TEST_CASE(ops) { - montgomery_big_mod_t a = 2u, b; +BOOST_AUTO_TEST_CASE_TEMPLATE(ops, big_mod_t, modular_types) { + big_mod_t a = 2u, b; auto c1{a}; auto c2{std::move(a)}; // NOLINT @@ -123,46 +126,24 @@ BOOST_AUTO_TEST_CASE(ops) { #undef TEST_BINARY_OP } -BOOST_AUTO_TEST_CASE(montgomery_increment) { - auto a = static_cast(0x2_big_uint64); - ++a; - BOOST_CHECK_EQUAL(a, static_cast(0x3_big_uint64)); -} - -BOOST_AUTO_TEST_CASE(montgomery_decrement) { - auto a = static_cast(0x2_big_uint64); - --a; - BOOST_CHECK_EQUAL(a, static_cast(0x1_big_uint64)); -} - -BOOST_AUTO_TEST_CASE(montgomery_increment_rvalue) { - BOOST_CHECK_EQUAL(++static_cast(0x2_big_uint64), - static_cast(0x3_big_uint64)); -} - -BOOST_AUTO_TEST_CASE(montgomery_decrement_rvalue) { - BOOST_CHECK_EQUAL(--static_cast(0x2_big_uint64), - static_cast(0x1_big_uint64)); -} - -BOOST_AUTO_TEST_CASE(increment) { +BOOST_AUTO_TEST_CASE_TEMPLATE(increment, big_mod_t, modular_types) { auto a = static_cast(0x2_big_uint64); ++a; BOOST_CHECK_EQUAL(a, static_cast(0x3_big_uint64)); } -BOOST_AUTO_TEST_CASE(decrement) { +BOOST_AUTO_TEST_CASE_TEMPLATE(decrement, big_mod_t, modular_types) { auto a = static_cast(0x2_big_uint64); --a; BOOST_CHECK_EQUAL(a, static_cast(0x1_big_uint64)); } -BOOST_AUTO_TEST_CASE(increment_rvalue) { +BOOST_AUTO_TEST_CASE_TEMPLATE(increment_rvalue, big_mod_t, modular_types) { BOOST_CHECK_EQUAL(++static_cast(0x2_big_uint64), static_cast(0x3_big_uint64)); } -BOOST_AUTO_TEST_CASE(decrement_rvalue) { +BOOST_AUTO_TEST_CASE_TEMPLATE(decrement_rvalue, big_mod_t, modular_types) { BOOST_CHECK_EQUAL(--static_cast(0x2_big_uint64), static_cast(0x1_big_uint64)); } @@ -171,32 +152,32 @@ BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(addition) -BOOST_AUTO_TEST_CASE(simple) { - BOOST_CHECK_EQUAL(static_cast(0x2_big_uint64) + - static_cast(0x3_big_uint64), - static_cast(0x5_big_uint64)); +BOOST_AUTO_TEST_CASE_TEMPLATE(simple, big_mod_t, modular_types) { + BOOST_CHECK_EQUAL( + static_cast(0x2_big_uint64) + static_cast(0x3_big_uint64), + static_cast(0x5_big_uint64)); } -BOOST_AUTO_TEST_CASE(multilimb) { - BOOST_CHECK_EQUAL(static_cast(0xAFFFFFFFF_big_uint64) + - static_cast(0x2_big_uint36), - static_cast(0xB00000001_big_uint64)); +BOOST_AUTO_TEST_CASE_TEMPLATE(multilimb, big_mod_t, modular_types) { + BOOST_CHECK_EQUAL(static_cast(0xAFFFFFFFF_big_uint64) + + static_cast(0x2_big_uint36), + static_cast(0xB00000001_big_uint64)); } BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(multiplication) -BOOST_AUTO_TEST_CASE(simple) { - BOOST_CHECK_EQUAL(static_cast(0x2_big_uint64) * - static_cast(0x3_big_uint64), - static_cast(0x6_big_uint64)); +BOOST_AUTO_TEST_CASE_TEMPLATE(simple, big_mod_t, modular_types) { + BOOST_CHECK_EQUAL( + static_cast(0x2_big_uint64) * static_cast(0x3_big_uint64), + static_cast(0x6_big_uint64)); } -BOOST_AUTO_TEST_CASE(multilimb) { - BOOST_CHECK_EQUAL(static_cast(0xAFFFFFFFF_big_uint64) * - static_cast(0x2_big_uint36), - static_cast(0x15FFFFFFFE_big_uint64)); +BOOST_AUTO_TEST_CASE_TEMPLATE(multilimb, big_mod_t, modular_types) { + BOOST_CHECK_EQUAL(static_cast(0xAFFFFFFFF_big_uint64) * + static_cast(0x2_big_uint36), + static_cast(0x15FFFFFFFE_big_uint64)); } BOOST_AUTO_TEST_CASE(big) { @@ -237,20 +218,21 @@ BOOST_AUTO_TEST_CASE(from_uint64_t) { BOOST_CHECK_EQUAL(a, static_cast(0x123456789ABCDEF_big_uint64)); } -BOOST_AUTO_TEST_CASE(from_int64_t) { - montgomery_big_mod_t a = static_cast(0x123456789ABCDEFull); - BOOST_CHECK_EQUAL(a, static_cast(0x123456789ABCDEF_big_uint64)); +BOOST_AUTO_TEST_CASE_TEMPLATE(from_int64_t, big_mod_t, modular_types) { + big_mod_t a = static_cast(0x123456789ABCDEFull); + BOOST_CHECK_EQUAL(a, static_cast(0x123456789ABCDEF_big_uint64)); } -BOOST_AUTO_TEST_CASE(init_from_big_uint512_is_modulo) { - montgomery_big_mod_t a = +BOOST_AUTO_TEST_CASE_TEMPLATE(init_from_big_uint512_is_modulo, big_mod_t, modular_types) { + auto base = 0xAA9E37FDB4756C822359B5D50B63A666C1E8D71142E315D224BF596CD169F7B60F01A02DEB2B562B8D51AFD478E1C21155F0E950C265CB32656FC073CDF19DA2_big_uint512; - BOOST_CHECK_EQUAL(a.base(), 0xD7C1CF32317E71_big_uint); + big_mod_t a = base; + BOOST_CHECK_EQUAL(a.base(), base % a.mod()); } -BOOST_AUTO_TEST_CASE(init_from_signed_is_modulo) { - montgomery_big_mod_t a = -1; - BOOST_CHECK_EQUAL(a.base(), 0x123456789ABCDEE_big_uint64); +BOOST_AUTO_TEST_CASE_TEMPLATE(init_from_signed_is_modulo, big_mod_t, modular_types) { + big_mod_t a = -1; + BOOST_CHECK_EQUAL(a.base(), a.mod() - 1); } BOOST_AUTO_TEST_SUITE_END() diff --git a/crypto3/libs/multiprecision/test/big_mod_randomized.cpp b/crypto3/libs/multiprecision/test/big_mod_randomized.cpp index 5edac3700..6b1ddfc87 100644 --- a/crypto3/libs/multiprecision/test/big_mod_randomized.cpp +++ b/crypto3/libs/multiprecision/test/big_mod_randomized.cpp @@ -119,12 +119,20 @@ void base_operations_test( BOOST_CHECK_EQUAL(a_m - b_m, sample.a_m_sub_b_m); BOOST_CHECK_EQUAL(a_m * b_m, sample.a_m_mul_b_m); + modular_number_t x = a_m; + x += b_m; + BOOST_CHECK_EQUAL(x, sample.a_m_add_b_m); + x = a_m; + x -= b_m; + BOOST_CHECK_EQUAL(x, sample.a_m_sub_b_m); + x = a_m; + x *= b_m; + BOOST_CHECK_EQUAL(x, sample.a_m_mul_b_m); + BOOST_CHECK_EQUAL(a_m == b_m, sample.a_eq_b); BOOST_CHECK_EQUAL(a_m != b_m, !sample.a_eq_b); - if constexpr (!std::is_same_v) { - BOOST_CHECK_EQUAL(pow(a_m, b), sample.a_m_pow_b); - } + BOOST_CHECK_EQUAL(pow(a_m, b), sample.a_m_pow_b); } BOOST_AUTO_TEST_SUITE(base_operations)