Skip to content

Commit

Permalink
multiprecision: test: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 15, 2024
1 parent 82f639a commit 9467f2f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 68 deletions.
18 changes: 16 additions & 2 deletions crypto3/libs/multiprecision/test/big_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ BOOST_AUTO_TEST_CASE(ops) {
nil::crypto3::multiprecision::big_uint<60> a = 2u, b;

auto c1{a};
auto c2{std::move(a)};
auto c2{std::move(a)}; // NOLINT
auto c3{2};
auto c4{2u};
b = a;
b = std::move(a);
b = std::move(a); // NOLINT
b = 2;
b = 2u;

Expand Down Expand Up @@ -222,3 +222,17 @@ BOOST_AUTO_TEST_CASE(from_int64_t) {
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(truncation)

BOOST_AUTO_TEST_CASE(conversion_to_shorter_number) {
using standart_number = nil::crypto3::multiprecision::big_uint<256>;
using short_number = nil::crypto3::multiprecision::big_uint<128>;
constexpr standart_number x =
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_big_uint256;
short_number s = x.truncate<128>();
// 2nd half of the number must stay.
BOOST_CHECK_EQUAL(s, 0xfffffffffffffffffffffffefffffc2f_big_uint128);
}

BOOST_AUTO_TEST_SUITE_END()
3 changes: 2 additions & 1 deletion crypto3/libs/multiprecision/test/big_int_inverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

#define BOOST_TEST_MODULE big_int_inverse_test

#include <stdexcept>

#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <stdexcept>

#include "nil/crypto3/multiprecision/big_mod.hpp"
#include "nil/crypto3/multiprecision/big_uint.hpp"
Expand Down
66 changes: 48 additions & 18 deletions crypto3/libs/multiprecision/test/big_int_modular.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#define BOOST_TEST_MODULE big_int_modular_test

#include <boost/test/unit_test.hpp>
#include <cstdint>
#include <utility>

#include <boost/test/unit_test.hpp>

#include "nil/crypto3/multiprecision/big_mod.hpp"
#include "nil/crypto3/multiprecision/big_uint.hpp"
#include "nil/crypto3/multiprecision/literals.hpp"

using namespace nil::crypto3::multiprecision;
Expand Down Expand Up @@ -71,11 +74,11 @@ BOOST_AUTO_TEST_CASE(ops) {
big_mod_t a = 2u, b;

auto c1{a};
auto c2{std::move(a)};
auto c2{std::move(a)}; // NOLINT
auto c3{2};
auto c4{2u};
b = a;
b = std::move(a);
b = std::move(a); // NOLINT
b = 2;
b = 2u;

Expand Down Expand Up @@ -162,22 +165,49 @@ BOOST_AUTO_TEST_CASE(big_assign) {

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(bugs)

BOOST_AUTO_TEST_CASE(secp256k1_incorrect_multiplication) {
using standart_number = nil::crypto3::multiprecision::big_uint<256>;
using modular_number = nil::crypto3::multiprecision::montgomery_big_mod_rt<256>;

constexpr standart_number modulus =
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_big_uint256;
constexpr standart_number x_standard =
0xb5d724ce6f44c3c587867bbcb417e9eb6fa05e7e2ef029166568f14eb3161387_big_uint256;
constexpr standart_number res_standard =
0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_big_uint256;
constexpr modular_number x(x_standard, modulus);
constexpr modular_number res(res_standard, modulus);
BOOST_CHECK_EQUAL(x * x, res);
}

BOOST_AUTO_TEST_CASE(bad_negation) {
using standart_number = nil::crypto3::multiprecision::big_uint<256>;
using modular_number = nil::crypto3::multiprecision::montgomery_big_mod_rt<256>;

constexpr standart_number modulus =
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_big_uint256;
constexpr modular_number x(0u, modulus);
constexpr modular_number res = -x;

BOOST_CHECK(res == 0u);
BOOST_CHECK(res == x);
BOOST_CHECK(-res == x);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(convert)

// BOOST_AUTO_TEST_CASE(to_uint64_t) {
// std::uint64_t a =
// static_cast<std::uint64_t>(static_cast<big_mod_t>(0x123456789ABCDEF_big_uint64));
// BOOST_CHECK_EQUAL(a, 0x123456789ABCDEF);
// }

// BOOST_AUTO_TEST_CASE(from_uint64_t) {
// big_mod_impl a = static_cast<std::uint64_t>(0x123456789ABCDEFull);
// BOOST_CHECK_EQUAL(a, static_cast<big_mod_t>(0x123456789ABCDEF_big_uint64));
// }

// BOOST_AUTO_TEST_CASE(from_int64_t) {
// big_mod_impl a = static_cast<std::int64_t>(0x123456789ABCDEFull);
// BOOST_CHECK_EQUAL(a, static_cast<big_mod_t>(0x123456789ABCDEF_big_uint64));
// }
BOOST_AUTO_TEST_CASE(from_uint64_t) {
big_mod_t a = static_cast<std::uint64_t>(0x123456789ABCDEFull);
BOOST_CHECK_EQUAL(a, static_cast<big_mod_t>(0x123456789ABCDEF_big_uint64));
}

BOOST_AUTO_TEST_CASE(from_int64_t) {
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_SUITE_END()
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include "nil/crypto3/multiprecision/big_mod.hpp"
#include "nil/crypto3/multiprecision/big_uint.hpp"
#include "nil/crypto3/multiprecision/literals.hpp"

using namespace nil::crypto3::multiprecision;

Expand Down Expand Up @@ -104,7 +103,7 @@ void base_operations_test(const ModularComprehensiveSample<Bits, Montgomery> sam
BOOST_CHECK_EQUAL(pow(a_m, b), sample.a_m_pow_b);
}

BOOST_AUTO_TEST_SUITE(static_tests)
BOOST_AUTO_TEST_SUITE(base_operations)

BOOST_DATA_TEST_CASE(prime_mod_montgomery_130, (test_dataset<ModularComprehensiveSample<130, true>>(
"prime_mod_montgomery_130"))) {
Expand Down Expand Up @@ -132,47 +131,3 @@ BOOST_DATA_TEST_CASE(montgomery_17,
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(runtime_tests)

BOOST_AUTO_TEST_CASE(secp256k1_incorrect_multiplication) {
using standart_number = nil::crypto3::multiprecision::big_uint<256>;
using modular_number = nil::crypto3::multiprecision::montgomery_big_mod_rt<256>;

constexpr standart_number modulus =
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_big_uint256;
constexpr standart_number x_standard =
0xb5d724ce6f44c3c587867bbcb417e9eb6fa05e7e2ef029166568f14eb3161387_big_uint256;
constexpr standart_number res_standard =
0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_big_uint256;
constexpr modular_number x(x_standard, modulus);
constexpr modular_number res(res_standard, modulus);
BOOST_CHECK_EQUAL(x * x, res);
}

BOOST_AUTO_TEST_CASE(bad_negation) {
using standart_number = nil::crypto3::multiprecision::big_uint<256>;
using modular_number = nil::crypto3::multiprecision::montgomery_big_mod_rt<256>;

constexpr standart_number modulus =
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_big_uint256;
constexpr modular_number x(0u, modulus);
constexpr modular_number res = -x;

BOOST_CHECK(res == 0u);
BOOST_CHECK(res == x);
BOOST_CHECK(-res == x);
}

// TODO(ioxid): this is not a modular test
BOOST_AUTO_TEST_CASE(conversion_to_shorter_number) {
using standart_number = nil::crypto3::multiprecision::big_uint<256>;
using short_number = nil::crypto3::multiprecision::big_uint<128>;
constexpr standart_number x =
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_big_uint256;
short_number s = x.truncate<128>();
// 2nd half of the number must stay.
BOOST_CHECK_EQUAL(s, 0xfffffffffffffffffffffffefffffc2f_big_uint128);
}

BOOST_AUTO_TEST_SUITE_END()
3 changes: 2 additions & 1 deletion crypto3/libs/multiprecision/test/big_int_ressol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

#define BOOST_TEST_MODULE big_int_ressol_test

#include <stdexcept>

#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <stdexcept>

#include "nil/crypto3/multiprecision/big_uint.hpp"

Expand Down

0 comments on commit 9467f2f

Please sign in to comment.