diff --git a/CMakeLists.txt b/CMakeLists.txt index 5466f86a2d..c5d2d8d802 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,10 @@ endif() add_subdirectories("${CMAKE_CURRENT_LIST_DIR}/libs/") add_subdirectories("${CMAKE_CURRENT_LIST_DIR}/libs/marshalling") +if(BUILD_BENCH_TESTS) + add_subdirectory(benchmarks) +endif() + configure_file(${CMAKE_CURRENT_LIST_DIR}/docs/doxygen/${CMAKE_WORKSPACE_NAME}.doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_WORKSPACE_NAME}.doxyfile @ONLY) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt new file mode 100644 index 0000000000..c1f19f6dde --- /dev/null +++ b/benchmarks/CMakeLists.txt @@ -0,0 +1,71 @@ +#---------------------------------------------------------------------------# +# Copyright (c) 2018-2021 Mikhail Komarov +# Copyright (c) 2024 Vasiliy Olekhov +# +# Distributed under the Boost Software License, Version 1.0 +# See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt +#---------------------------------------------------------------------------# + +include(CMTest) + +add_custom_target(crypto3_benchmarks) + +macro(define_benchmark benchmark) + + get_filename_component(name ${benchmark} NAME) + string(REPLACE "/" "_" full_name ${benchmark}_benchmark) + + add_dependencies(crypto3_benchmarks ${full_name}) + + cm_test(NAME ${full_name} SOURCES ${benchmark}.cpp) + + target_include_directories( + ${full_name} PRIVATE + "$" + "$" + ${Boost_INCLUDE_DIRS}) + + target_link_libraries(${full_name} + ${CMAKE_WORKSPACE_NAME}::random + ${CMAKE_WORKSPACE_NAME}::math + ${CMAKE_WORKSPACE_NAME}::algebra + ${CMAKE_WORKSPACE_NAME}::multiprecision + ${CMAKE_WORKSPACE_NAME}::zk + ${CMAKE_WORKSPACE_NAME}::benchmark_tools + + Boost::unit_test_framework + Boost::timer + Boost::random) + + set_target_properties(${full_name} + PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED TRUE) + + if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(${full_name} PRIVATE "-fconstexpr-steps=2147483647") + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + target_compile_options(${full_name} PRIVATE "-fconstexpr-ops-limit=4294967295") + endif() + +endmacro() + +set(BENCHMARK_NAMES + "algebra/curves" + "algebra/fields" + "algebra/multiexp" + + "math/polynomial_dfs" + + "multiprecision/modular_adaptor_fixed" + + "zk/lpc" + "zk/pedersen" +) + +foreach(BENCHMARK_NAME ${BENCHMARK_NAMES}) + define_benchmark(${BENCHMARK_NAME}) +endforeach() + + diff --git a/benchmarks/README.md b/benchmarks/README.md new file mode 100644 index 0000000000..80c312c75b --- /dev/null +++ b/benchmarks/README.md @@ -0,0 +1,3 @@ +# Benchmarks + +This folder contains benchmarks for various parts of crypto3 library. diff --git a/libs/algebra/bench/bench_curves.cpp b/benchmarks/algebra/curves.cpp similarity index 70% rename from libs/algebra/bench/bench_curves.cpp rename to benchmarks/algebra/curves.cpp index 0415e3be92..67264be342 100644 --- a/libs/algebra/bench/bench_curves.cpp +++ b/benchmarks/algebra/curves.cpp @@ -23,7 +23,7 @@ // SOFTWARE. //---------------------------------------------------------------------------// -#define BOOST_TEST_MODULE algebra_curves_bench_test +#define BOOST_TEST_MODULE algebra_benchmark #include #include @@ -50,6 +50,12 @@ #include #include +#include +#include +#include +#include +#include + using namespace nil::crypto3::algebra; using namespace nil::crypto3::bench; @@ -61,7 +67,7 @@ void benchmark_curve_operations(std::string const& curve_name) using scalar_field = typename curve_type::scalar_field_type; run_benchmark( - curve_name + " Fp addition", + curve_name + " Fp addition", [](typename base_field::value_type& A, typename base_field::value_type const& B) { return A += B; }); @@ -109,11 +115,33 @@ void benchmark_curve_operations(std::string const& curve_name) if constexpr (has_template_g2_type::value) { using g2_type = typename curve_type::template g2_type<>; + + using g2_field = typename g2_type::field_type; + + run_benchmark( + curve_name + " G2 Fp addition", + [](typename g2_field::value_type& A, typename g2_field::value_type const& B) { + return A += B; + }); + + run_benchmark( + curve_name + " G2 Fp multiplication", + [](typename g2_field::value_type& A, typename g2_field::value_type const& B) { + return A *= B; + }); + + run_benchmark( + curve_name + " G2 Fp inverse", + [](typename g2_field::value_type& A) { + return A.inversed(); + }); + run_benchmark( curve_name + " G2 addition", [](typename g2_type::value_type& A, typename g2_type::value_type const& B) { return A += B; }); + run_benchmark( curve_name + " G2 doubling", [](typename g2_type::value_type& A) { @@ -126,9 +154,50 @@ void benchmark_curve_operations(std::string const& curve_name) [](typename g2_type::value_type& A, typename scalar_field::value_type const& B) { return A *= B; }); + } else { std::cout << "Curve " << curve_name << " does not have G2, skipping benchmarks" << std::endl; } + + if constexpr (has_type_gt_type::value) { + + using gt_type = typename curve_type::gt_type; + + run_benchmark( + curve_name + " GT addition", + [](typename gt_type::value_type& A, typename gt_type::value_type const& B) { + return A += B; + }); + + run_benchmark( + curve_name + " GT multiplication", + [](typename gt_type::value_type& A, typename gt_type::value_type const& B) { + return A *= B; + }); + + run_benchmark( + curve_name + " GT inverse", + [](typename gt_type::value_type& A) { + return A.inversed(); + }); + + using g2_type = typename curve_type::template g2_type<>; + + run_benchmark( + curve_name + " pairing", + [](typename g1_type::value_type& A, typename g2_type::value_type const& B) { + return pair(A, B); + }); + + run_benchmark( + curve_name + " final_exponentiation", + [](typename gt_type::value_type& A) { + return final_exponentiation(A); + }); + + } else { + std::cout << "Curve " << curve_name << " does not have GT, skipping benchmarks" << std::endl; + } } BOOST_AUTO_TEST_SUITE(curves_benchmark) @@ -148,6 +217,11 @@ BOOST_AUTO_TEST_CASE(bls12_381) benchmark_curve_operations>("BLS12-381"); } +BOOST_AUTO_TEST_CASE(bls12_377) +{ + benchmark_curve_operations>("BLS12-377"); +} + BOOST_AUTO_TEST_CASE(mnt4_298) { benchmark_curve_operations>("MNT4-298"); diff --git a/libs/algebra/bench/bench_fields.cpp b/benchmarks/algebra/fields.cpp similarity index 100% rename from libs/algebra/bench/bench_fields.cpp rename to benchmarks/algebra/fields.cpp diff --git a/libs/algebra/bench/bench_multiexp.cpp b/benchmarks/algebra/multiexp.cpp similarity index 98% rename from libs/algebra/bench/bench_multiexp.cpp rename to benchmarks/algebra/multiexp.cpp index 312321ed45..07fa5c1c2d 100644 --- a/libs/algebra/bench/bench_multiexp.cpp +++ b/benchmarks/algebra/multiexp.cpp @@ -39,12 +39,10 @@ #include #include -#include #include #include #include #include -#include #include #include #include diff --git a/libs/math/test/benchmarks/polynomial_dfs_benchmark.cpp b/benchmarks/math/polynomial_dfs.cpp similarity index 100% rename from libs/math/test/benchmarks/polynomial_dfs_benchmark.cpp rename to benchmarks/math/polynomial_dfs.cpp diff --git a/libs/multiprecision/test/bench_test/bench_test_modular_adaptor_fixed.cpp b/benchmarks/multiprecision/modular_adaptor_fixed.cpp similarity index 69% rename from libs/multiprecision/test/bench_test/bench_test_modular_adaptor_fixed.cpp rename to benchmarks/multiprecision/modular_adaptor_fixed.cpp index 8fa8010153..8c3e8cb13d 100644 --- a/libs/multiprecision/test/bench_test/bench_test_modular_adaptor_fixed.cpp +++ b/benchmarks/multiprecision/modular_adaptor_fixed.cpp @@ -1,5 +1,6 @@ //---------------------------------------------------------------------------// // Copyright (c) 2024 Martun Karapetyan +// Copyright (c) 2024 Vasiliy Olekhov // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at @@ -8,7 +9,14 @@ #define BOOST_TEST_MODULE modular_fixed_multiprecision_test -// 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. +#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) @@ -19,9 +27,7 @@ #include #include -#include #include -#include #include #include @@ -32,11 +38,12 @@ #include +#include + using namespace boost::multiprecision; using boost::multiprecision::backends::cpp_int_modular_backend; using boost::multiprecision::backends::modular_adaptor; -using boost::multiprecision::backends::modular_params; using boost::multiprecision::backends::modular_params_rt; @@ -58,21 +65,18 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_montgomery_mult_perf_test) { constexpr modular_number res(modular_backend( res_value.backend(), modulus.backend())); auto res_modular = res.backend(); - std::chrono::time_point start(std::chrono::high_resolution_clock::now()); - - int SAMPLES = 10000000; auto mod_object = x_modular.mod_data().get_mod_obj(); auto base_data = x_modular.base_data(); - for (int i = 0; i < SAMPLES; ++i) { - mod_object.montgomery_mul(base_data, res_modular.base_data(), - std::integral_constant::value>()); - } + + nil::crypto3::bench::run_benchmark<>( + "montgomery_mul (direct call)", + [&]() { + mod_object.montgomery_mul(base_data, res_modular.base_data(), + std::integral_constant::value>()); + return base_data; + }); std::cout << base_data << std::endl; - auto elapsed = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start); - std::cout << "Multiplication time (when montgomery_mul is called directly): " << std::fixed << std::setprecision(3) - << std::dec << elapsed.count() / SAMPLES << " ns" << std::endl; } BOOST_AUTO_TEST_CASE(modular_adaptor_backend_sub_perf_test) { @@ -91,19 +95,16 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_sub_perf_test) { constexpr standart_number res_value = 0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_cppui_modular256; constexpr modular_number res(modular_backend(res_value.backend(), modulus.backend())); auto res_modular = res.backend(); - std::chrono::time_point start(std::chrono::high_resolution_clock::now()); - int SAMPLES = 10000000; - for (int i = 0; i < SAMPLES; ++i) { - eval_subtract(x_modular, res_modular); - } + nil::crypto3::bench::run_benchmark<>( + "modular_adaptor_backend_subtract", + [&]() { + eval_subtract(x_modular, res_modular); + return x_modular; + }); + // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; - - auto elapsed = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start); - std::cout << "Substraction time: " << std::fixed << std::setprecision(3) - << std::dec << elapsed.count() / SAMPLES << " ns" << std::endl; } BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { @@ -122,19 +123,16 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_add_perf_test) { constexpr standart_number res_value = 0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_cppui_modular256; constexpr modular_number res(modular_backend(res_value.backend(), modulus.backend())); auto res_modular = res.backend(); - std::chrono::time_point start(std::chrono::high_resolution_clock::now()); - int SAMPLES = 10000000; - for (int i = 0; i < SAMPLES; ++i) { - eval_add(x_modular, res_modular); - } + nil::crypto3::bench::run_benchmark<>( + "modular_adaptor_backend_add", + [&]() { + eval_add(x_modular, res_modular); + return x_modular; + }); + // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; - - auto elapsed = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start); - std::cout << "Addition time: " << std::fixed << std::setprecision(3) - << std::dec << elapsed.count() / SAMPLES << " ns" << std::endl; } BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mult_perf_test) { @@ -151,17 +149,13 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_backend_mult_perf_test) { constexpr standart_number res_value = 0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_cppui_modular256; constexpr modular_number res(modular_backend(res_value.backend(), modulus.backend())); constexpr auto res_modular = res.backend(); - std::chrono::time_point start(std::chrono::high_resolution_clock::now()); - int SAMPLES = 10000000; - for (int i = 0; i < SAMPLES; ++i) { - eval_multiply(x_modular, res_modular); - } - - auto elapsed = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start); - std::cout << "Multiplication time (when called from modular adaptor): " << std::fixed << std::setprecision(3) - << elapsed.count() / SAMPLES << " ns" << std::endl; + nil::crypto3::bench::run_benchmark<>( + "modular_adaptor_backend_multiply", + [&]() { + eval_multiply(x_modular, res_modular); + return x_modular; + }); // Print something so the whole computation is not optimized out. std::cout << x_modular << std::endl; @@ -179,17 +173,13 @@ BOOST_AUTO_TEST_CASE(modular_adaptor_number_mult_perf_test) { constexpr standart_number res_value = 0xad6e1fcc680392abfb075838eafa513811112f14c593e0efacb6e9d0d7770b4_cppui_modular256; modular_number res(modular_backend(res_value.backend(), modulus.backend())); - std::chrono::time_point start(std::chrono::high_resolution_clock::now()); - - int SAMPLES = 10000000; - for (int i = 0; i < SAMPLES; ++i) { - x *= res; - } - auto elapsed = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start); - std::cout << "Multiplication time: " << std::fixed << std::setprecision(3) - << elapsed.count() / SAMPLES << " ns" << std::endl; + nil::crypto3::bench::run_benchmark<>( + "modular_adaptor_number_multiply", + [&]() { + x *= res; + return x; + }); // Print something so the whole computation is not optimized out. std::cout << x << std::endl; diff --git a/benchmarks/zk/lpc.cpp b/benchmarks/zk/lpc.cpp new file mode 100644 index 0000000000..7f02f20dd1 --- /dev/null +++ b/benchmarks/zk/lpc.cpp @@ -0,0 +1,215 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2021 Mikhail Komarov +// Copyright (c) 2021 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +//---------------------------------------------------------------------------// + +#define BOOST_TEST_MODULE lpc_test + +// Do it manually for all performance tests +#define PROFILING_ENABLED + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include + +using namespace nil::crypto3; +using namespace nil::crypto3::zk::snark; + +template +std::vector> generate(NumberType degree) { + typedef boost::random::independent_bits_engine + random_polynomial_generator_type; + + std::vector> res; + + boost::random::random_device rd; // Will be used to obtain a seed for the random number engine + boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() + boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), std::numeric_limits::max()); + + random_polynomial_generator_type polynomial_element_gen; + std::size_t height = 1; + res.reserve(height); + + for (int i = 0; i < height; i++) { + math::polynomial poly; + for (int j = 0; j < degree; j++) { + poly.push_back(typename FieldType::value_type(polynomial_element_gen())); + } + res.push_back(poly); + } + + return res; +} + +inline std::vector generate_random_step_list(const std::size_t r, const int max_step) { + using dist_type = std::uniform_int_distribution; + static std::random_device random_engine; + + std::vector step_list; + std::size_t steps_sum = 0; + while (steps_sum != r) { + if (r - steps_sum <= max_step) { + while (r - steps_sum != 1) { + step_list.emplace_back(r - steps_sum - 1); + steps_sum += step_list.back(); + } + step_list.emplace_back(1); + steps_sum += step_list.back(); + } else { + step_list.emplace_back(dist_type(1, max_step)(random_engine)); + steps_sum += step_list.back(); + } + } + return step_list; +} + +BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite) + +void lpc_test_case(std::size_t steps) +{ + PROFILE_SCOPE("LPC step list test " + std::to_string(steps)); + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + + typedef hashes::keccak_1600<256> merkle_hash_type; + typedef hashes::keccak_1600<256> transcript_hash_type; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t k = 1; + + // It's important parameter + constexpr static const std::size_t d = 1 << 24; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + + constexpr static const std::size_t m = 2; + + typedef zk::commitments::fri fri_type; + typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typename fri_type::params_type fri_params( + d - 1, + D, + generate_random_step_list(r, steps), + r, + lambda + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + typedef boost::random::independent_bits_engine< + boost::random::mt19937, FieldType::modulus_bits, + typename FieldType::value_type::integral_type + > random_polynomial_generator_type; + + std::vector> res; + + // Generate polys + boost::random::random_device rd; // Will be used to obtain a seed for the random number engine + boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() + boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), + std::numeric_limits::max()); + + random_polynomial_generator_type polynomial_element_gen; + std::size_t height = 1; + res.reserve(height); + + for (int i = 0; i < height; i++) { + math::polynomial poly(fri_params.max_degree + 1); + for (int j = 0; j < fri_params.max_degree + 1; j++) { + poly[i] = typename FieldType::value_type(polynomial_element_gen()); + } + + std::map commitments; + { + PROFILE_SCOPE("polynomial commitment"); + lpc_scheme_prover.append_to_batch(0, poly); + commitments[0] = lpc_scheme_prover.commit(0); + } + + + typename lpc_scheme_type::proof_type proof; + std::array x_data{}; + { + PROFILE_SCOPE("proof generation"); + lpc_scheme_prover.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + proof = lpc_scheme_prover.proof_eval(transcript); + } + + { + PROFILE_SCOPE("verification"); + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + + lpc_scheme_verifier.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + } + } +} + + +BOOST_AUTO_TEST_CASE(step_list_1) { + lpc_test_case(1); +} + +BOOST_AUTO_TEST_CASE(step_list_3) { + lpc_test_case(3); +} + +BOOST_AUTO_TEST_CASE(step_list_5) { + lpc_test_case(5); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/zk/test/bench_test/pedersen.cpp b/benchmarks/zk/pedersen.cpp similarity index 100% rename from libs/zk/test/bench_test/pedersen.cpp rename to benchmarks/zk/pedersen.cpp diff --git a/cmake/CheckSSE.cmake b/cmake/CheckSSE.cmake index dbc971b364..562b6b17fa 100644 --- a/cmake/CheckSSE.cmake +++ b/cmake/CheckSSE.cmake @@ -11,7 +11,7 @@ macro(check_sse) if(CMAKE_SYSTEM_NAME MATCHES "Linux") - exec_program(cat ARGS "/proc/cpuinfo" OUTPUT_VARIABLE CPUINFO) + execute_process(COMMAND "cat" "/proc/cpuinfo" OUTPUT_VARIABLE CPUINFO) string(REGEX REPLACE "^.*(sse2).*$" "\\1" SSE_THERE ${CPUINFO}) string(COMPARE EQUAL "sse2" "${SSE_THERE}" SSE2_TRUE) diff --git a/libs/algebra/CMakeLists.txt b/libs/algebra/CMakeLists.txt index 3c6bceeb97..6ef66dee54 100644 --- a/libs/algebra/CMakeLists.txt +++ b/libs/algebra/CMakeLists.txt @@ -47,6 +47,3 @@ if(BUILD_EXAMPLES) add_subdirectory(example) endif() -if(BUILD_BENCH_TESTS) - add_subdirectory(bench) -endif() diff --git a/libs/algebra/bench/CMakeLists.txt b/libs/algebra/bench/CMakeLists.txt deleted file mode 100644 index 211d40e49c..0000000000 --- a/libs/algebra/bench/CMakeLists.txt +++ /dev/null @@ -1,52 +0,0 @@ -#---------------------------------------------------------------------------# -# Copyright (c) 2018-2021 Mikhail Komarov -# -# Distributed under the Boost Software License, Version 1.0 -# See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt -#---------------------------------------------------------------------------# - -include(CMTest) - -add_custom_target(algebra_runtime_bench_tests) - -macro(define_algebra_benchmark name) - set(test_name "algebra_${name}_bench_test") - add_dependencies(algebra_runtime_bench_tests ${test_name}) - - cm_test(NAME ${test_name} SOURCES ${name}.cpp) - - target_include_directories(${test_name} PRIVATE - "$" - "$" - - ${Boost_INCLUDE_DIRS}) - - target_link_libraries(${test_name} - ${CMAKE_WORKSPACE_NAME}::benchmark_tools - ) - - set_target_properties(${test_name} - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED TRUE) - - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(${test_name} PRIVATE "-fconstexpr-steps=2147483647") - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(${test_name} PRIVATE "-fconstexpr-ops-limit=4294967295") - endif() - -endmacro() - -set(BENCHMARK_NAMES - "bench_curves" - "bench_fields" - "bench_multiexp" -) - -foreach(BENCH_NAME ${BENCHMARK_NAMES}) - define_algebra_benchmark(${BENCH_NAME}) -endforeach() - - diff --git a/libs/algebra/include/nil/crypto3/algebra/pairing/bls12.hpp b/libs/algebra/include/nil/crypto3/algebra/pairing/bls12.hpp index 5913095403..7781a97d7e 100644 --- a/libs/algebra/include/nil/crypto3/algebra/pairing/bls12.hpp +++ b/libs/algebra/include/nil/crypto3/algebra/pairing/bls12.hpp @@ -1,6 +1,7 @@ //---------------------------------------------------------------------------// // Copyright (c) 2020-2021 Mikhail Komarov // Copyright (c) 2020-2021 Nikita Kaskov +// Copyright (c) 2024 Vasiliy Olekhov // // MIT License // @@ -29,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -45,7 +47,21 @@ namespace nil { struct pairing_policy> { using curve_type = curves::bls12<381>; - using chained_curve_type = curves::jubjub; + using precompute_g1 = pairing::short_weierstrass_jacobian_with_a4_0_ate_precompute_g1; + using precompute_g2 = pairing::short_weierstrass_jacobian_with_a4_0_ate_precompute_g2; + using miller_loop = pairing::short_weierstrass_jacobian_with_a4_0_ate_miller_loop; + using double_miller_loop = + pairing::short_weierstrass_jacobian_with_a4_0_ate_double_miller_loop; + using final_exponentiation = + pairing::short_weierstrass_jacobian_with_a4_0_final_exponentiation; + + using g1_precomputed_type = typename precompute_g1::g1_precomputed_type; + using g2_precomputed_type = typename precompute_g2::g2_precomputed_type; + }; + + template<> + struct pairing_policy> { + using curve_type = curves::bls12<377>; using precompute_g1 = pairing::short_weierstrass_jacobian_with_a4_0_ate_precompute_g1; using precompute_g2 = pairing::short_weierstrass_jacobian_with_a4_0_ate_precompute_g2; @@ -59,6 +75,7 @@ namespace nil { using g2_precomputed_type = typename precompute_g2::g2_precomputed_type; }; + } // namespace pairing } // namespace algebra } // namespace crypto3 diff --git a/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/377/params.hpp b/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/377/params.hpp index 94912ef88b..2c1160c5fa 100644 --- a/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/377/params.hpp +++ b/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/377/params.hpp @@ -26,8 +26,7 @@ #ifndef CRYPTO3_ALGEBRA_PAIRING_BLS12_377_BASIC_PARAMS_HPP #define CRYPTO3_ALGEBRA_PAIRING_BLS12_377_BASIC_PARAMS_HPP -#include -#include +#include namespace nil { namespace crypto3 { @@ -49,20 +48,25 @@ namespace nil { constexpr static const std::size_t integral_type_max_bits = curve_type::base_field_type::modulus_bits; - constexpr static const std::size_t integral_type_max_bits = base_field_bits; - - constexpr static const integral_type ate_loop_count = integral_type(0x8508C00000000001_cppui_modular64); - constexpr static const bool ate_is_loop_count_neg = false; + constexpr static const integral_type ate_loop_count = + 0x8508C00000000001_cppui_modular64; + constexpr static const bool ate_is_loop_count_neg = + false; // constexpr static const extended_integral_type final_exponent = extended_integral_type( // 0x1B2FF68C1ABDC48AB4F04ED12CC8F9B2F161B41C7EB8865B9AD3C9BB0571DD94C6BDE66548DC13624D9D741024CEB315F46A89CC2482605EB6AFC6D8977E5E2CCBEC348DD362D59EC2B5BC62A1B467AE44572215548ABC98BB4193886ED89CCEAEDD0221ABA84FB33E5584AC29619A87A00C315178155496857C995EAB4A8A9AF95F4015DB27955AE408D6927D0AB37D52F3917C4DDEC88F8159F7BCBA7EB65F1AAE4EEB4E70CB20227159C08A7FDFEA9B62BB308918EAC3202569DD1BCDD86B431E3646356FC3FB79F89B30775E006993ADB629586B6C874B7688F86F11EF7AD94A40EB020DA3C532B317232FA56DC564637B331A8E8832EAB84269F00B506602C8594B7F7DA5A5D8D851FFF6AB1D38A354FC8E0B8958E2A9E5CE2D7E50EC36D761D9505FE5E1F317257E2DF2952FCD4C93B85278C20488B4CCAEE94DB3FEC1CE8283473E4B493843FA73ABE99AF8BAFCE29170B2B863B9513B5A47312991F60C5A4F6872B5D574212BF00D797C0BEA3C0F7DFD748E63679FDA9B1C50F2DF74DE38F38E004AE0DF997A10DB31D209CACBF58BA0678BFE7CD0985BC43258D72D8D5106C21635AE1E527EB01FCA3032D50D97756EC9EE756EABA7F21652A808A4E2539E838EF7EC4B178B29E3B976C46BD0ECDD32C1FB75E6E0AEF2D8B5661F595A98023F3520381ABA8DA6CCE785DBB0A0BBA025478D75EE749619CDB7C42A21098ECE86A00C6C2046C1E00000063C69000000000000_cppui_modular4269); constexpr static const integral_type final_exponent_z = - integral_type(0x8508C00000000001_cppui_modular64); - constexpr static const bool final_exponent_is_z_neg = false; + 0x8508C00000000001_cppui_modular64; + constexpr static const bool final_exponent_is_z_neg = + false; + + using g2_field_type_value = typename curve_type::template g2_type<>::field_type::value_type; - using g2_field_type_value = typename curve_type::g2_type::field_type::value_type; + constexpr static const g2_field_type_value twist = + curve_type::template g2_type<>::params_type::twist; - constexpr static const g2_field_type_value twist = g2_type::params_type::twist; + constexpr static const g2_field_type_value twist_coeff_b = + curve_type::template g2_type<>::params_type::b; }; constexpr typename pairing_params>::integral_type const @@ -70,6 +74,10 @@ namespace nil { constexpr typename pairing_params>::integral_type const pairing_params>::final_exponent_z; + constexpr typename pairing_params>::g2_field_type_value const + pairing_params>::twist; + constexpr typename pairing_params>::g2_field_type_value const + pairing_params>::twist_coeff_b; constexpr bool const pairing_params>::final_exponent_is_z_neg; diff --git a/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/381/params.hpp b/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/381/params.hpp index 4e715b9111..c5ae51f23f 100644 --- a/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/381/params.hpp +++ b/libs/algebra/include/nil/crypto3/algebra/pairing/detail/bls12/381/params.hpp @@ -48,13 +48,17 @@ namespace nil { constexpr static const std::size_t integral_type_max_bits = curve_type::base_field_type::modulus_bits; - constexpr static const integral_type ate_loop_count = 0xD201000000010000_cppui_modular64; - constexpr static const bool ate_is_loop_count_neg = true; + constexpr static const integral_type ate_loop_count = + 0xD201000000010000_cppui_modular64; + constexpr static const bool ate_is_loop_count_neg = + true; // constexpr static const extended_integral_type final_exponent = extended_integral_type( // 0x2EE1DB5DCC825B7E1BDA9C0496A1C0A89EE0193D4977B3F7D4507D07363BAA13F8D14A917848517BADC3A43D1073776AB353F2C30698E8CC7DEADA9C0AADFF5E9CFEE9A074E43B9A660835CC872EE83FF3A0F0F1C0AD0D6106FEAF4E347AA68AD49466FA927E7BB9375331807A0DCE2630D9AA4B113F414386B0E8819328148978E2B0DD39099B86E1AB656D2670D93E4D7ACDD350DA5359BC73AB61A0C5BF24C374693C49F570BCD2B01F3077FFB10BF24DDE41064837F27611212596BC293C8D4C01F25118790F4684D0B9C40A68EB74BB22A40EE7169CDC1041296532FEF459F12438DFC8E2886EF965E61A474C5C85B0129127A1B5AD0463434724538411D1676A53B5A62EB34C05739334F46C02C3F0BD0C55D3109CD15948D0A1FAD20044CE6AD4C6BEC3EC03EF19592004CEDD556952C6D8823B19DADD7C2498345C6E5308F1C511291097DB60B1749BF9B71A9F9E0100418A3EF0BC627751BBD81367066BCA6A4C1B6DCFC5CCEB73FC56947A403577DFA9E13C24EA820B09C1D9F7C31759C3635DE3F7A3639991708E88ADCE88177456C49637FD7961BE1A4C7E79FB02FAA732E2F3EC2BEA83D196283313492CAA9D4AFF1C910E9622D2A73F62537F2701AAEF6539314043F7BBCE5B78C7869AEB2181A67E49EEED2161DAF3F881BD88592D767F67C4717489119226C2F011D4CAB803E9D71650A6F80698E2F8491D12191A04406FBC8FBD5F48925F98630E68BFB24C0BCB9B55DF57510_cppui_modular4314); - constexpr static const integral_type final_exponent_z = 0xD201000000010000_cppui_modular64; - constexpr static const bool final_exponent_is_z_neg = true; + constexpr static const integral_type final_exponent_z = + 0xD201000000010000_cppui_modular64; + constexpr static const bool final_exponent_is_z_neg = + true; using g2_field_type_value = typename curve_type::template g2_type<>::field_type::value_type; diff --git a/libs/benchmark_tools/include/nil/crypto3/bench/benchmark.hpp b/libs/benchmark_tools/include/nil/crypto3/bench/benchmark.hpp index 32e3577373..e55f94c650 100644 --- a/libs/benchmark_tools/include/nil/crypto3/bench/benchmark.hpp +++ b/libs/benchmark_tools/include/nil/crypto3/bench/benchmark.hpp @@ -126,7 +126,7 @@ void run_benchmark(std::string const& name, F && func) }; auto run_at_least = [&] (duration const& dur) { - std::size_t WARMUP_BATCH_SIZE = 1000, total_runs = 0; + std::size_t WARMUP_BATCH_SIZE = 10, total_runs = 0; auto start = std::chrono::high_resolution_clock::now(); while (std::chrono::high_resolution_clock::now() - start < dur) { run_batch(WARMUP_BATCH_SIZE); @@ -175,10 +175,37 @@ void run_benchmark(std::string const& name, F && func) std::sort(durations.begin(), durations.end()); double MdAPE = durations[durations.size()/2]; + std::size_t multiplier = 1; + std::string unit; + + while (mean > 10e3) { + ++multiplier; + mean /= 1e3; + median /= 1e3; + stddiv /= 1e3; + } + + switch(multiplier) { + case 1: + unit = "ns"; + break; + case 2: + unit = "µs"; + break; + case 3: + unit = "ms"; + break; + case 4: + unit = "s"; + break; + default: + unit = "??"; + } + std::cout << std::fixed << std::setprecision(3); std::cout << name << - " mean: " << mean << "ns err: " << (MdAPE*100) << - "% median: " << median << "ns stddiv: " << stddiv << + " mean: " << mean << unit << " err: " << (MdAPE*100) << + "% median: " << median << unit << " stddiv: " << stddiv << std::endl; } diff --git a/libs/math/test/CMakeLists.txt b/libs/math/test/CMakeLists.txt index 07ec63f34b..4fb55a179e 100644 --- a/libs/math/test/CMakeLists.txt +++ b/libs/math/test/CMakeLists.txt @@ -42,7 +42,3 @@ set(TESTS_NAMES foreach(TEST_NAME ${TESTS_NAMES}) define_math_test(${TEST_NAME}) endforeach() - -if(ENABLE_BENCHMARKS) - add_subdirectory(benchmarks) -endif() diff --git a/libs/math/test/benchmarks/CMakeLists.txt b/libs/math/test/benchmarks/CMakeLists.txt deleted file mode 100644 index 34644bac80..0000000000 --- a/libs/math/test/benchmarks/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -#---------------------------------------------------------------------------# -# Copyright (c) 2018-2020 Mikhail Komarov -# -# Distributed under the Boost Software License, Version 1.0 -# See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt -#---------------------------------------------------------------------------# - -cm_test_link_libraries(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} - ${CMAKE_WORKSPACE_NAME}::random -) - -set(TESTS_NAMES - "polynomial_dfs_benchmark" -) - -foreach(TEST_NAME ${TESTS_NAMES}) - define_math_test(${TEST_NAME}) -endforeach() - - -#get_target_property(my_include_dirs math_polynomial_dfs_benchmark_test INCLUDE_DIRECTORIES) -#message(include dirs: ${my_include_dirs}) diff --git a/libs/multiprecision/test/CMakeLists.txt b/libs/multiprecision/test/CMakeLists.txt index c283430d60..e2e050e90c 100644 --- a/libs/multiprecision/test/CMakeLists.txt +++ b/libs/multiprecision/test/CMakeLists.txt @@ -66,7 +66,3 @@ endforeach() foreach(TEST_NAME ${MODULAR_TESTS_NAMES}) define_modular_cpp_int_test(${TEST_NAME}) endforeach() - -if(BUILD_BENCH_TESTS) - cm_add_test_subdirectory(bench_test) -endif() diff --git a/libs/multiprecision/test/bench_test/CMakeLists.txt b/libs/multiprecision/test/bench_test/CMakeLists.txt deleted file mode 100644 index 557b8ea962..0000000000 --- a/libs/multiprecision/test/bench_test/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -#---------------------------------------------------------------------------# -# Copyright (c) 2018-2020 Mikhail Komarov -# Copyright (c) 2018-2021 Aleksei Moskvin -# -# Distributed under the Boost Software License, Version 1.0 -# See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt -#---------------------------------------------------------------------------# - -include(CMTest) - -cm_test(NAME ${CURRENT_PROJECT_NAME}_bench_test_modular_adaptor_fixed_cpp_int SOURCES bench_test_modular_adaptor_fixed.cpp) -target_compile_definitions(${CURRENT_PROJECT_NAME}_bench_test_modular_adaptor_fixed_cpp_int PUBLIC -DTEST_CPP_INT) -add_dependencies(${CURRENT_PROJECT_NAME}_test_suite_modular_cpp_int_tests ${CURRENT_PROJECT_NAME}_bench_test_modular_adaptor_fixed_cpp_int) -set_target_properties(${CURRENT_PROJECT_NAME}_bench_test_modular_adaptor_fixed_cpp_int PROPERTIES CXX_STANDARD 17) -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(${CURRENT_PROJECT_NAME}_bench_test_modular_adaptor_fixed_cpp_int PRIVATE "-fconstexpr-steps=2147483647") -elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(${CURRENT_PROJECT_NAME}_bench_test_modular_adaptor_fixed_cpp_int PRIVATE "-fconstexpr-ops-limit=4294967295") -endif() - -include_directories(${CMAKE_WORKSPACE_SOURCES_DIR}) diff --git a/libs/zk/test/CMakeLists.txt b/libs/zk/test/CMakeLists.txt index bb813e1f99..3f824ac4af 100644 --- a/libs/zk/test/CMakeLists.txt +++ b/libs/zk/test/CMakeLists.txt @@ -107,7 +107,4 @@ endforeach() # string(CONCAT TEST_DATA ${CMAKE_CURRENT_SOURCE_DIR} "/systems/plonk/pickles/data/kimchi") # target_compile_definitions(crypto3_zk_systems_plonk_pickles_kimchi_test PRIVATE TEST_DATA="${TEST_DATA}") -if(BUILD_BENCH_TESTS) - cm_add_test_subdirectory(bench_test) -endif() diff --git a/libs/zk/test/bench_test/CMakeLists.txt b/libs/zk/test/bench_test/CMakeLists.txt deleted file mode 100644 index c784162db2..0000000000 --- a/libs/zk/test/bench_test/CMakeLists.txt +++ /dev/null @@ -1,45 +0,0 @@ -#---------------------------------------------------------------------------# -# Copyright (c) 2018-2021 Mikhail Komarov -# -# Distributed under the Boost Software License, Version 1.0 -# See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt -#---------------------------------------------------------------------------# - -include(CMTest) - -add_custom_target(zk_runtime_bench_tests) - -macro(define_runtime_zk_test name) - set(test_name "zk_${name}_bench_test") - add_dependencies(zk_runtime_bench_tests ${test_name}) - - cm_test(NAME ${test_name} SOURCES ${name}.cpp) - - target_include_directories(${test_name} PRIVATE - "$" - "$" - - ${Boost_INCLUDE_DIRS}) - - set_target_properties(${test_name} PROPERTIES CXX_STANDARD 17 - CXX_STANDARD_REQUIRED TRUE) - - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(${test_name} PRIVATE "-fconstexpr-steps=2147483647") - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(${test_name} PRIVATE "-fconstexpr-ops-limit=4294967295") - endif() - - target_compile_definitions(${test_name} PRIVATE TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data/") -endmacro() - -set(RUNTIME_TESTS_NAMES - "pedersen" - "lpc" - ) - -foreach(TEST_NAME ${RUNTIME_TESTS_NAMES}) - define_runtime_zk_test(${TEST_NAME}) -endforeach() - diff --git a/libs/zk/test/bench_test/lpc.cpp b/libs/zk/test/bench_test/lpc.cpp deleted file mode 100644 index d2fc71ee06..0000000000 --- a/libs/zk/test/bench_test/lpc.cpp +++ /dev/null @@ -1,401 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2021 Mikhail Komarov -// Copyright (c) 2021 Nikita Kaskov -// Copyright (c) 2022 Ilia Shirobokov -// -// MIT License -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -//---------------------------------------------------------------------------// - -#define BOOST_TEST_MODULE lpc_test - -// Do it manually for all performance tests -#define PROFILING_ENABLED - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -#include - -using namespace nil::crypto3; -using namespace nil::crypto3::zk::snark; - -namespace boost { - namespace test_tools { - namespace tt_detail { - template<> - struct print_log_value>>>> { - void operator()(std::ostream &, - const nil::crypto3::math::polynomial> - - >> &) { - } - }; -} // namespace tt_detail -} // namespace test_tools -} // namespace boost - -template -std::vector> generate(NumberType degree) { - typedef boost::random::independent_bits_engine - random_polynomial_generator_type; - - std::vector> res; - - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - - for (int i = 0; i < height; i++) { - math::polynomial poly; - for (int j = 0; j < degree; j++) { - poly.push_back(typename FieldType::value_type(polynomial_element_gen())); - } - res.push_back(poly); - } - - return res; -} - -inline std::vector generate_random_step_list(const std::size_t r, const int max_step) { - using dist_type = std::uniform_int_distribution; - static std::random_device random_engine; - - std::vector step_list; - std::size_t steps_sum = 0; - while (steps_sum != r) { - if (r - steps_sum <= max_step) { - while (r - steps_sum != 1) { - step_list.emplace_back(r - steps_sum - 1); - steps_sum += step_list.back(); - } - step_list.emplace_back(1); - steps_sum += step_list.back(); - } else { - step_list.emplace_back(dist_type(1, max_step)(random_engine)); - steps_sum += step_list.back(); - } - } - return step_list; -} - -BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite) - - BOOST_AUTO_TEST_CASE(step_list_1) { - PROFILE_SCOPE("LPC step list 1 test"); - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::keccak_1600<256> transcript_hash_type; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; - - // It's important parameter - constexpr static const std::size_t d = 1 << 24; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - - constexpr static const std::size_t m = 2; - - typedef zk::commitments::fri fri_type; - typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, - D, - generate_random_step_list(r, 1), - r, - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - typedef boost::random::independent_bits_engine< - boost::random::mt19937, FieldType::modulus_bits, - typename FieldType::value_type::integral_type - > random_polynomial_generator_type; - - std::vector> res; - - // Generate polys - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), - std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - - for (int i = 0; i < height; i++) { - math::polynomial poly(fri_params.max_degree + 1); - for (int j = 0; j < fri_params.max_degree + 1; j++) { - poly[i] = typename FieldType::value_type(polynomial_element_gen()); - } - - std::map commitments; - { - PROFILE_SCOPE("polynomial commitment"); - lpc_scheme_prover.append_to_batch(0, poly); - commitments[0] = lpc_scheme_prover.commit(0); - } - - - typename lpc_scheme_type::proof_type proof; - std::array x_data{}; - { - PROFILE_SCOPE("proof generation"); - lpc_scheme_prover.append_eval_point(0, - algebra::fields::arithmetic_params::multiplicative_generator); - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - proof = lpc_scheme_prover.proof_eval(transcript); - } - - { - PROFILE_SCOPE("verification"); - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - - lpc_scheme_verifier.append_eval_point(0, - algebra::fields::arithmetic_params::multiplicative_generator); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - } - } - } - - BOOST_AUTO_TEST_CASE(step_list_3) { - PROFILE_SCOPE("LPC step list 3 test"); - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::keccak_1600<256> transcript_hash_type; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; - - // It's important parameter - constexpr static const std::size_t d = 1 << 24; - - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; - - typedef zk::commitments::fri fri_type; - typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, - D, - generate_random_step_list(r, 3), - r, - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - typedef boost::random::independent_bits_engine< - boost::random::mt19937, FieldType::modulus_bits, - typename FieldType::value_type::integral_type - > random_polynomial_generator_type; - - std::vector> res; - - // Generate polys - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), - std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - - for (int i = 0; i < height; i++) { - math::polynomial poly(fri_params.max_degree + 1); - for (int j = 0; j < fri_params.max_degree + 1; j++) { - poly[i] = typename FieldType::value_type(polynomial_element_gen()); - } - - std::map commitments; - { - PROFILE_SCOPE("polynomial commitment"); - lpc_scheme_prover.append_to_batch(0, poly); - commitments[0] = lpc_scheme_prover.commit(0); - } - - - typename lpc_scheme_type::proof_type proof; - std::array x_data{}; - { - PROFILE_SCOPE("proof generation"); - lpc_scheme_prover.append_eval_point(0, - algebra::fields::arithmetic_params::multiplicative_generator); - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - proof = lpc_scheme_prover.proof_eval(transcript); - } - - { - PROFILE_SCOPE("verification"); - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - - lpc_scheme_verifier.append_eval_point(0, - algebra::fields::arithmetic_params::multiplicative_generator); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - } - } - } - - BOOST_AUTO_TEST_CASE(step_list_5) { - PROFILE_SCOPE("LPC step list 5 test"); - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::keccak_1600<256> transcript_hash_type; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; - - // It's important parameter - constexpr static const std::size_t d = 1 << 24; - constexpr static const std::size_t m = 2; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - - typedef zk::commitments::fri fri_type; - typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, - D, - generate_random_step_list(r, 5), - r, - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - typedef boost::random::independent_bits_engine< - boost::random::mt19937, FieldType::modulus_bits, - typename FieldType::value_type::integral_type - > random_polynomial_generator_type; - - std::vector> res; - - // Generate polys - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), - std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - - for (int i = 0; i < height; i++) { - math::polynomial poly(fri_params.max_degree + 1); - for (int j = 0; j < fri_params.max_degree + 1; j++) { - poly[i] = typename FieldType::value_type(polynomial_element_gen()); - } - - std::map commitments; - { - PROFILE_SCOPE("polynomial commitment"); - lpc_scheme_prover.append_to_batch(0, poly); - commitments[0] = lpc_scheme_prover.commit(0); - } - - - typename lpc_scheme_type::proof_type proof; - std::array x_data{}; - { - PROFILE_SCOPE("proof generation"); - lpc_scheme_prover.append_eval_point(0, - algebra::fields::arithmetic_params::multiplicative_generator); - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - proof = lpc_scheme_prover.proof_eval(transcript); - } - - { - PROFILE_SCOPE("verification"); - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - - lpc_scheme_verifier.append_eval_point(0, - algebra::fields::arithmetic_params::multiplicative_generator); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - } - } - } - -BOOST_AUTO_TEST_SUITE_END()