diff --git a/crypto3/benchmarks/multiprecision/big_int.cpp b/crypto3/benchmarks/multiprecision/big_int.cpp index 233a71a3d..ae39f8c62 100644 --- a/crypto3/benchmarks/multiprecision/big_int.cpp +++ b/crypto3/benchmarks/multiprecision/big_int.cpp @@ -290,3 +290,21 @@ BOOST_AUTO_TEST_CASE(big_mod_mul_perf_test) { } BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(compile_time_inverse_tests) + +BOOST_AUTO_TEST_CASE(inverse_extended_euclidean_algorithm_test) { + auto x_modular = x_mod_ct_odd; + + nil::crypto3::bench::run_benchmark<>( + "[odd modulus][compile time] inverse_extended_euclidean_algorithm_test", [&]() { + x_modular = inverse_extended_euclidean_algorithm(x_modular); + ++x_modular; + return x_modular; + }); + + // Print something so the whole computation is not optimized out. + std::cout << x_modular << std::endl; +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/crypto3/benchmarks/multiprecision/modular_adaptor_fixed.cpp b/crypto3/benchmarks/multiprecision/modular_adaptor_fixed.cpp index 0e2445b5c..a7e0ff3c2 100644 --- a/crypto3/benchmarks/multiprecision/modular_adaptor_fixed.cpp +++ b/crypto3/benchmarks/multiprecision/modular_adaptor_fixed.cpp @@ -7,40 +7,23 @@ // http://www.boost.org/LICENSE_1_0.txt //---------------------------------------------------------------------------// -#include "nil/crypto3/multiprecision/boost_backends/modular/modular_params.hpp" #define BOOST_TEST_MODULE modular_fixed_multiprecision_test #define TEST_CPP_INT -// Suddenly, BOOST_MP_ASSERT is NOT constexpr, and it is used in constexpr -// functions throughout the boost, resulting to compilation errors on all -// compilers in debug mode. We need to switch assertions off inside cpp_int -// to make this code compile in debug mode. So we use this workaround to -// turn off file 'boost/multiprecision/detail/assert.hpp' which contains -// definition of BOOST_MP_ASSERT and BOOST_MP_ASSERT_MSG. -#ifndef BOOST_MP_DETAIL_ASSERT_HPP - #define BOOST_MP_DETAIL_ASSERT_HPP - #define BOOST_MP_ASSERT(expr) ((void)0) - #define BOOST_MP_ASSERT_MSG(expr, msg) ((void)0) -#endif - -#include <boost/test/unit_test.hpp> -#include <boost/test/data/test_case.hpp> #include <boost/test/data/monomorphic.hpp> - +#include <boost/test/data/test_case.hpp> +#include <boost/test/unit_test.hpp> #include <iostream> - +#include <nil/crypto3/bench/benchmark.hpp> #include <nil/crypto3/multiprecision/boost_backends/cpp_int_modular.hpp> #include <nil/crypto3/multiprecision/boost_backends/cpp_int_modular/literals.hpp> #include <nil/crypto3/multiprecision/boost_backends/cpp_modular.hpp> - +#include <nil/crypto3/multiprecision/boost_backends/inverse.hpp> #include <nil/crypto3/multiprecision/boost_backends/modular/modular_adaptor.hpp> +#include <nil/crypto3/multiprecision/boost_backends/modular/modular_params.hpp> #include <nil/crypto3/multiprecision/boost_backends/modular/modular_params_fixed.hpp> -#include <nil/crypto3/multiprecision/boost_backends/inverse.hpp> - -#include <nil/crypto3/bench/benchmark.hpp> - using namespace boost::multiprecision; using boost::multiprecision::backends::cpp_int_modular_backend; @@ -51,8 +34,10 @@ using boost::multiprecision::backends::modular_params_rt; using Backend = cpp_int_modular_backend<256>; using standart_number = boost::multiprecision::number<Backend>; -constexpr standart_number modulus_odd = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_cppui_modular256; -constexpr standart_number modulus_even = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e_cppui_modular256; +constexpr standart_number modulus_odd = + 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_cppui_modular256; +constexpr standart_number modulus_even = + 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e_cppui_modular256; constexpr backends::modular_params<Backend> params_odd{modulus_odd.backend()}; constexpr backends::modular_params<Backend> params_even{modulus_even.backend()}; @@ -68,13 +53,15 @@ using modular_number_ct_odd = boost::multiprecision::number<modular_backend_ct_o using modular_number_ct_even = boost::multiprecision::number<modular_backend_ct_even>; using modular_number_rt = boost::multiprecision::number<modular_backend_rt>; -constexpr standart_number x = 0xb5d724ce6f44c3c587867bbcb417e9eb6fa05e7e2ef029166568f14eb3161387_cppui_modular256; +constexpr standart_number x = + 0xb5d724ce6f44c3c587867bbcb417e9eb6fa05e7e2ef029166568f14eb3161387_cppui_modular256; constexpr modular_number_ct_odd x_mod_ct_odd = modular_backend_ct_odd(x.backend()); constexpr modular_number_ct_even x_mod_ct_even = modular_backend_ct_even(x.backend()); constexpr modular_number_rt x_mod_rt_odd = modular_backend_rt(x.backend(), modulus_odd.backend()); constexpr modular_number_rt x_mod_rt_even = modular_backend_rt(x.backend(), modulus_even.backend()); -constexpr standart_number y = 0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_cppui_modular256; +constexpr standart_number y = + 0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_cppui_modular256; constexpr modular_number_ct_odd y_mod_ct_odd = modular_backend_ct_odd(y.backend()); constexpr modular_number_ct_even y_mod_ct_even = modular_backend_ct_even(y.backend()); constexpr modular_number_rt y_mod_rt_odd = modular_backend_rt(y.backend(), modulus_odd.backend()); @@ -88,12 +75,14 @@ BOOST_AUTO_TEST_CASE(montgomery_mul_perf_test) { const auto &mod_object = x_mod_rt_odd.backend().mod_data().get_mod_obj(); nil::crypto3::bench::run_benchmark<>( - "[odd modulus][runtime] montgomery_mul (direct call)", - [&]() { - mod_object.montgomery_mul(base_data, y_mod_rt_odd.backend().base_data(), - std::integral_constant<bool, boost::multiprecision::backends::is_trivial_cpp_int_modular<Backend>::value>()); - return base_data; - }); + "[odd modulus][runtime] montgomery_mul (direct call)", [&]() { + mod_object.montgomery_mul( + base_data, y_mod_rt_odd.backend().base_data(), + std::integral_constant< + bool, + boost::multiprecision::backends::is_trivial_cpp_int_modular<Backend>::value>()); + return base_data; + }); std::cout << base_data << std::endl; } @@ -103,12 +92,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_sub_perf_test) { auto x_modular = x_mod_rt_odd.backend(); - nil::crypto3::bench::run_benchmark<>( - "[odd modulus][runtime] modular_adaptor_backend_subtract", - [&]() { - eval_subtract(x_modular, y_mod_rt_odd.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[odd modulus][runtime] modular_adaptor_backend_subtract", + [&]() { + eval_subtract(x_modular, y_mod_rt_odd.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -119,12 +107,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { auto x_modular = x_mod_rt_odd.backend(); - nil::crypto3::bench::run_benchmark<>( - "[odd modulus][runtime] modular_adaptor_backend_add", - [&]() { - eval_add(x_modular, y_mod_rt_odd.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[odd modulus][runtime] modular_adaptor_backend_add", + [&]() { + eval_add(x_modular, y_mod_rt_odd.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -133,12 +120,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mul_perf_test) { auto x_modular = x_mod_rt_odd.backend(); - nil::crypto3::bench::run_benchmark<>( - "[odd modulus][runtime] modular_adaptor_backend_multiply", - [&]() { - eval_multiply(x_modular, y_mod_rt_odd.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[odd modulus][runtime] modular_adaptor_backend_multiply", + [&]() { + eval_multiply(x_modular, y_mod_rt_odd.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -147,12 +133,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mul_perf_test) { BOOST_AUTO_TEST_CASE(modular_adaptor_number_mul_perf_test) { auto x = x_mod_rt_odd; - nil::crypto3::bench::run_benchmark<>( - "[odd modulus][runtime] modular_adaptor_number_multiply", - [&]() { - x *= y_mod_rt_odd; - return x; - }); + nil::crypto3::bench::run_benchmark<>("[odd modulus][runtime] modular_adaptor_number_multiply", + [&]() { + x *= y_mod_rt_odd; + return x; + }); // Print something so the whole computation is not optimized out. std::cout << x << std::endl; @@ -168,12 +153,14 @@ BOOST_AUTO_TEST_CASE(montgomery_mul_perf_test) { const auto &mod_object = x_mod_ct_odd.backend().mod_data().get_mod_obj(); nil::crypto3::bench::run_benchmark<>( - "[odd modulus][compile time] montgomery_mul (direct call)", - [&]() { - mod_object.montgomery_mul(base_data, y_mod_ct_odd.backend().base_data(), - std::integral_constant<bool, boost::multiprecision::backends::is_trivial_cpp_int_modular<Backend>::value>()); - return base_data; - }); + "[odd modulus][compile time] montgomery_mul (direct call)", [&]() { + mod_object.montgomery_mul( + base_data, y_mod_ct_odd.backend().base_data(), + std::integral_constant< + bool, + boost::multiprecision::backends::is_trivial_cpp_int_modular<Backend>::value>()); + return base_data; + }); std::cout << base_data << std::endl; } @@ -184,11 +171,10 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_sub_perf_test) { auto x_modular = x_mod_ct_odd.backend(); nil::crypto3::bench::run_benchmark<>( - "[odd modulus][compile time] modular_adaptor_backend_subtract", - [&]() { - eval_subtract(x_modular, y_mod_ct_odd.backend()); - return x_modular; - }); + "[odd modulus][compile time] modular_adaptor_backend_subtract", [&]() { + eval_subtract(x_modular, y_mod_ct_odd.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -199,12 +185,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { auto x_modular = x_mod_ct_odd.backend(); - nil::crypto3::bench::run_benchmark<>( - "[odd modulus][compile time] modular_adaptor_backend_add", - [&]() { - eval_add(x_modular, y_mod_ct_odd.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[odd modulus][compile time] modular_adaptor_backend_add", + [&]() { + eval_add(x_modular, y_mod_ct_odd.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -214,11 +199,10 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mul_perf_test) { auto x_modular = x_mod_ct_odd.backend(); nil::crypto3::bench::run_benchmark<>( - "[odd modulus][compile time] modular_adaptor_backend_multiply", - [&]() { - eval_multiply(x_modular, y_mod_ct_odd.backend()); - return x_modular; - }); + "[odd modulus][compile time] modular_adaptor_backend_multiply", [&]() { + eval_multiply(x_modular, y_mod_ct_odd.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -228,11 +212,10 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_number_mul_perf_test) { auto x = x_mod_ct_odd; nil::crypto3::bench::run_benchmark<>( - "[odd modulus][compile time] modular_adaptor_number_multiply", - [&]() { - x *= y_mod_ct_odd; - return x; - }); + "[odd modulus][compile time] modular_adaptor_number_multiply", [&]() { + x *= y_mod_ct_odd; + return x; + }); // Print something so the whole computation is not optimized out. std::cout << x << std::endl; @@ -248,11 +231,10 @@ BOOST_AUTO_TEST_CASE(barrett_mul_perf_test) { const auto &mod_object = x_mod_rt_even.backend().mod_data().get_mod_obj(); nil::crypto3::bench::run_benchmark<>( - "[even modulus][runtime] regular_mul (direct call)", - [&]() { - mod_object.regular_mul(base_data, y_mod_rt_even.backend().base_data()); - return base_data; - }); + "[even modulus][runtime] regular_mul (direct call)", [&]() { + mod_object.regular_mul(base_data, y_mod_rt_even.backend().base_data()); + return base_data; + }); std::cout << base_data << std::endl; } @@ -262,12 +244,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_sub_perf_test) { auto x_modular = x_mod_rt_even.backend(); - nil::crypto3::bench::run_benchmark<>( - "[even modulus][runtime] modular_adaptor_backend_subtract", - [&]() { - eval_subtract(x_modular, y_mod_rt_even.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[even modulus][runtime] modular_adaptor_backend_subtract", + [&]() { + eval_subtract(x_modular, y_mod_rt_even.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -278,12 +259,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { auto x_modular = x_mod_rt_even.backend(); - nil::crypto3::bench::run_benchmark<>( - "[even modulus][runtime] modular_adaptor_backend_add", - [&]() { - eval_add(x_modular, y_mod_rt_even.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[even modulus][runtime] modular_adaptor_backend_add", + [&]() { + eval_add(x_modular, y_mod_rt_even.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -292,12 +272,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mul_perf_test) { auto x_modular = x_mod_rt_even.backend(); - nil::crypto3::bench::run_benchmark<>( - "[even modulus][runtime] modular_adaptor_backend_multiply", - [&]() { - eval_multiply(x_modular, y_mod_rt_even.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[even modulus][runtime] modular_adaptor_backend_multiply", + [&]() { + eval_multiply(x_modular, y_mod_rt_even.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -306,12 +285,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mul_perf_test) { BOOST_AUTO_TEST_CASE(modular_adaptor_number_mul_perf_test) { auto x = x_mod_rt_even; - nil::crypto3::bench::run_benchmark<>( - "[even modulus][runtime] modular_adaptor_number_multiply", - [&]() { - x *= y_mod_rt_even; - return x; - }); + nil::crypto3::bench::run_benchmark<>("[even modulus][runtime] modular_adaptor_number_multiply", + [&]() { + x *= y_mod_rt_even; + return x; + }); // Print something so the whole computation is not optimized out. std::cout << x << std::endl; @@ -327,11 +305,10 @@ BOOST_AUTO_TEST_CASE(barrett_mul_perf_test) { const auto &mod_object = x_mod_ct_even.backend().mod_data().get_mod_obj(); nil::crypto3::bench::run_benchmark<>( - "[even modulus][compile time] regular_mul (direct call)", - [&]() { - mod_object.regular_mul(base_data, y_mod_ct_even.backend().base_data()); - return base_data; - }); + "[even modulus][compile time] regular_mul (direct call)", [&]() { + mod_object.regular_mul(base_data, y_mod_ct_even.backend().base_data()); + return base_data; + }); std::cout << base_data << std::endl; } @@ -342,11 +319,10 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_sub_perf_test) { auto x_modular = x_mod_ct_even.backend(); nil::crypto3::bench::run_benchmark<>( - "[even modulus][compile time] modular_adaptor_backend_subtract", - [&]() { - eval_subtract(x_modular, y_mod_ct_even.backend()); - return x_modular; - }); + "[even modulus][compile time] modular_adaptor_backend_subtract", [&]() { + eval_subtract(x_modular, y_mod_ct_even.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -357,12 +333,11 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { auto x_modular = x_mod_ct_even.backend(); - nil::crypto3::bench::run_benchmark<>( - "[even modulus][compile time] modular_adaptor_backend_add", - [&]() { - eval_add(x_modular, y_mod_ct_even.backend()); - return x_modular; - }); + nil::crypto3::bench::run_benchmark<>("[even modulus][compile time] modular_adaptor_backend_add", + [&]() { + eval_add(x_modular, y_mod_ct_even.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -372,11 +347,10 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mul_perf_test) { auto x_modular = x_mod_ct_even.backend(); nil::crypto3::bench::run_benchmark<>( - "[even modulus][compile time] modular_adaptor_backend_multiply", - [&]() { - eval_multiply(x_modular, y_mod_ct_even.backend()); - return x_modular; - }); + "[even modulus][compile time] modular_adaptor_backend_multiply", [&]() { + eval_multiply(x_modular, y_mod_ct_even.backend()); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -386,14 +360,44 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_number_mul_perf_test) { auto x = x_mod_ct_even; nil::crypto3::bench::run_benchmark<>( - "[even modulus][compile time] modular_adaptor_number_multiply", - [&]() { - x *= y_mod_ct_even; - return x; - }); + "[even modulus][compile time] modular_adaptor_number_multiply", [&]() { + x *= y_mod_ct_even; + return x; + }); // Print something so the whole computation is not optimized out. std::cout << x << std::endl; } -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(compile_time_inverse_tests) + +BOOST_AUTO_TEST_CASE(inverse_extended_euclidean_algorithm_test) { + auto x_modular = x_mod_ct_odd; + + nil::crypto3::bench::run_benchmark<>( + "[odd modulus][compile time] inverse_extended_euclidean_algorithm_test", [&]() { + x_modular = inverse_extended_euclidean_algorithm(x_modular); + ++x_modular; + return x_modular; + }); + + // Print something so the whole computation is not optimized out. + std::cout << x_modular << std::endl; +} + +BOOST_AUTO_TEST_CASE(inverse_mod_test) { + auto x_modular = x_mod_ct_odd; + + nil::crypto3::bench::run_benchmark<>("[odd modulus][compile time] inverse_mod", [&]() { + x_modular = inverse_mod(x_modular); + ++x_modular; + return x_modular; + }); + + // Print something so the whole computation is not optimized out. + std::cout << x_modular << std::endl; +} + +BOOST_AUTO_TEST_SUITE_END()