Skip to content

Commit

Permalink
multiprecision: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 25, 2024
1 parent 71cd989 commit 0f07641
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 61 deletions.
98 changes: 40 additions & 58 deletions crypto3/libs/multiprecision/test/big_mod_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <boost/test/unit_test.hpp>

#include <cstdint>
#include <tuple>
#include <type_traits>
#include <utility>

Expand All @@ -36,7 +37,9 @@ static_assert(std::is_same_v<auto_big_mod<odd_mod>, montgomery_big_mod<odd_mod>>
static_assert(std::is_same_v<auto_big_mod<even_mod>, big_mod<even_mod>>);

using montgomery_big_mod_t = montgomery_big_mod<odd_mod>;
using big_mod_t = big_mod<odd_mod>;
using barrett_big_mod_t = big_mod<odd_mod>;

using modular_types = std::tuple<montgomery_big_mod_t, barrett_big_mod_t, goldilocks_mod>;

BOOST_AUTO_TEST_SUITE(smoke)

Expand Down Expand Up @@ -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<montgomery_big_mod_t>(0x1_big_uint)).str(), "0x1");
BOOST_AUTO_TEST_CASE_TEMPLATE(to_string_trivial, big_mod_t, modular_types) {
BOOST_CHECK_EQUAL((static_cast<big_mod_t>(0x1_big_uint)).str(), "0x1");
}

BOOST_AUTO_TEST_CASE(to_string_small) {
BOOST_CHECK_EQUAL((static_cast<montgomery_big_mod_t>(0x20_big_uint)).str(), "0x20");
BOOST_AUTO_TEST_CASE_TEMPLATE(to_string_small, big_mod_t, modular_types) {
BOOST_CHECK_EQUAL((static_cast<big_mod_t>(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
Expand Down Expand Up @@ -123,46 +126,24 @@ BOOST_AUTO_TEST_CASE(ops) {
#undef TEST_BINARY_OP
}

BOOST_AUTO_TEST_CASE(montgomery_increment) {
auto a = static_cast<montgomery_big_mod_t>(0x2_big_uint64);
++a;
BOOST_CHECK_EQUAL(a, static_cast<montgomery_big_mod_t>(0x3_big_uint64));
}

BOOST_AUTO_TEST_CASE(montgomery_decrement) {
auto a = static_cast<montgomery_big_mod_t>(0x2_big_uint64);
--a;
BOOST_CHECK_EQUAL(a, static_cast<montgomery_big_mod_t>(0x1_big_uint64));
}

BOOST_AUTO_TEST_CASE(montgomery_increment_rvalue) {
BOOST_CHECK_EQUAL(++static_cast<montgomery_big_mod_t>(0x2_big_uint64),
static_cast<montgomery_big_mod_t>(0x3_big_uint64));
}

BOOST_AUTO_TEST_CASE(montgomery_decrement_rvalue) {
BOOST_CHECK_EQUAL(--static_cast<montgomery_big_mod_t>(0x2_big_uint64),
static_cast<montgomery_big_mod_t>(0x1_big_uint64));
}

BOOST_AUTO_TEST_CASE(increment) {
BOOST_AUTO_TEST_CASE_TEMPLATE(increment, big_mod_t, modular_types) {
auto a = static_cast<big_mod_t>(0x2_big_uint64);
++a;
BOOST_CHECK_EQUAL(a, static_cast<big_mod_t>(0x3_big_uint64));
}

BOOST_AUTO_TEST_CASE(decrement) {
BOOST_AUTO_TEST_CASE_TEMPLATE(decrement, big_mod_t, modular_types) {
auto a = static_cast<big_mod_t>(0x2_big_uint64);
--a;
BOOST_CHECK_EQUAL(a, static_cast<big_mod_t>(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<big_mod_t>(0x2_big_uint64),
static_cast<big_mod_t>(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<big_mod_t>(0x2_big_uint64),
static_cast<big_mod_t>(0x1_big_uint64));
}
Expand All @@ -171,32 +152,32 @@ BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(addition)

BOOST_AUTO_TEST_CASE(simple) {
BOOST_CHECK_EQUAL(static_cast<montgomery_big_mod_t>(0x2_big_uint64) +
static_cast<montgomery_big_mod_t>(0x3_big_uint64),
static_cast<montgomery_big_mod_t>(0x5_big_uint64));
BOOST_AUTO_TEST_CASE_TEMPLATE(simple, big_mod_t, modular_types) {
BOOST_CHECK_EQUAL(
static_cast<big_mod_t>(0x2_big_uint64) + static_cast<big_mod_t>(0x3_big_uint64),
static_cast<big_mod_t>(0x5_big_uint64));
}

BOOST_AUTO_TEST_CASE(multilimb) {
BOOST_CHECK_EQUAL(static_cast<montgomery_big_mod_t>(0xAFFFFFFFF_big_uint64) +
static_cast<montgomery_big_mod_t>(0x2_big_uint36),
static_cast<montgomery_big_mod_t>(0xB00000001_big_uint64));
BOOST_AUTO_TEST_CASE_TEMPLATE(multilimb, big_mod_t, modular_types) {
BOOST_CHECK_EQUAL(static_cast<big_mod_t>(0xAFFFFFFFF_big_uint64) +
static_cast<big_mod_t>(0x2_big_uint36),
static_cast<big_mod_t>(0xB00000001_big_uint64));
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(multiplication)

BOOST_AUTO_TEST_CASE(simple) {
BOOST_CHECK_EQUAL(static_cast<montgomery_big_mod_t>(0x2_big_uint64) *
static_cast<montgomery_big_mod_t>(0x3_big_uint64),
static_cast<montgomery_big_mod_t>(0x6_big_uint64));
BOOST_AUTO_TEST_CASE_TEMPLATE(simple, big_mod_t, modular_types) {
BOOST_CHECK_EQUAL(
static_cast<big_mod_t>(0x2_big_uint64) * static_cast<big_mod_t>(0x3_big_uint64),
static_cast<big_mod_t>(0x6_big_uint64));
}

BOOST_AUTO_TEST_CASE(multilimb) {
BOOST_CHECK_EQUAL(static_cast<montgomery_big_mod_t>(0xAFFFFFFFF_big_uint64) *
static_cast<montgomery_big_mod_t>(0x2_big_uint36),
static_cast<montgomery_big_mod_t>(0x15FFFFFFFE_big_uint64));
BOOST_AUTO_TEST_CASE_TEMPLATE(multilimb, big_mod_t, modular_types) {
BOOST_CHECK_EQUAL(static_cast<big_mod_t>(0xAFFFFFFFF_big_uint64) *
static_cast<big_mod_t>(0x2_big_uint36),
static_cast<big_mod_t>(0x15FFFFFFFE_big_uint64));
}

BOOST_AUTO_TEST_CASE(big) {
Expand Down Expand Up @@ -237,20 +218,21 @@ BOOST_AUTO_TEST_CASE(from_uint64_t) {
BOOST_CHECK_EQUAL(a, static_cast<montgomery_big_mod_t>(0x123456789ABCDEF_big_uint64));
}

BOOST_AUTO_TEST_CASE(from_int64_t) {
montgomery_big_mod_t a = static_cast<std::int64_t>(0x123456789ABCDEFull);
BOOST_CHECK_EQUAL(a, static_cast<montgomery_big_mod_t>(0x123456789ABCDEF_big_uint64));
BOOST_AUTO_TEST_CASE_TEMPLATE(from_int64_t, big_mod_t, modular_types) {
big_mod_t a = static_cast<std::int64_t>(0x123456789ABCDEFull);
BOOST_CHECK_EQUAL(a, static_cast<big_mod_t>(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()
Expand Down
14 changes: 11 additions & 3 deletions crypto3/libs/multiprecision/test/big_mod_randomized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<modular_number_t, goldilocks_mod>) {
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)
Expand Down

0 comments on commit 0f07641

Please sign in to comment.