Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworking benchmarking suite #304 #305

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
71 changes: 71 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#---------------------------------------------------------------------------#
# Copyright (c) 2018-2021 Mikhail Komarov <nemo@nil.foundation>
# Copyright (c) 2024 Vasiliy Olekhov <vasiliy.olekhov@nil.foundation>
#
# 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
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>"
${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()


3 changes: 3 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Benchmarks

This folder contains benchmarks for various parts of crypto3 library.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE algebra_curves_bench_test
#define BOOST_TEST_MODULE algebra_benchmark

#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
Expand All @@ -50,6 +50,12 @@
#include <nil/crypto3/algebra/type_traits.hpp>
#include <nil/crypto3/bench/benchmark.hpp>

#include <nil/crypto3/algebra/algorithms/pair.hpp>
#include <nil/crypto3/algebra/pairing/mnt4.hpp>
#include <nil/crypto3/algebra/pairing/mnt6.hpp>
#include <nil/crypto3/algebra/pairing/bls12.hpp>
#include <nil/crypto3/algebra/pairing/alt_bn128.hpp>

using namespace nil::crypto3::algebra;
using namespace nil::crypto3::bench;

Expand All @@ -61,7 +67,7 @@ void benchmark_curve_operations(std::string const& curve_name)
using scalar_field = typename curve_type::scalar_field_type;

run_benchmark<base_field, base_field>(
curve_name + " Fp addition",
curve_name + " Fp addition",
[](typename base_field::value_type& A, typename base_field::value_type const& B) {
return A += B;
});
Expand Down Expand Up @@ -109,11 +115,33 @@ void benchmark_curve_operations(std::string const& curve_name)

if constexpr (has_template_g2_type<curve_type>::value) {
using g2_type = typename curve_type::template g2_type<>;

using g2_field = typename g2_type::field_type;

run_benchmark<g2_field, g2_field>(
curve_name + " G2 Fp addition",
[](typename g2_field::value_type& A, typename g2_field::value_type const& B) {
return A += B;
});

run_benchmark<g2_field, g2_field>(
curve_name + " G2 Fp multiplication",
[](typename g2_field::value_type& A, typename g2_field::value_type const& B) {
return A *= B;
});

run_benchmark<g2_field>(
curve_name + " G2 Fp inverse",
[](typename g2_field::value_type& A) {
return A.inversed();
});

run_benchmark<g2_type, g2_type>(
curve_name + " G2 addition",
[](typename g2_type::value_type& A, typename g2_type::value_type const& B) {
return A += B;
});

run_benchmark<g2_type>(
curve_name + " G2 doubling",
[](typename g2_type::value_type& A) {
Expand All @@ -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<curve_type>::value) {

using gt_type = typename curve_type::gt_type;

run_benchmark<gt_type, gt_type>(
curve_name + " GT addition",
[](typename gt_type::value_type& A, typename gt_type::value_type const& B) {
return A += B;
});

run_benchmark<gt_type, gt_type>(
curve_name + " GT multiplication",
[](typename gt_type::value_type& A, typename gt_type::value_type const& B) {
return A *= B;
});

run_benchmark<gt_type>(
curve_name + " GT inverse",
[](typename gt_type::value_type& A) {
return A.inversed();
});

using g2_type = typename curve_type::template g2_type<>;

run_benchmark<g1_type, g2_type>(
curve_name + " pairing",
[](typename g1_type::value_type& A, typename g2_type::value_type const& B) {
return pair<curve_type>(A, B);
});

run_benchmark<gt_type>(
curve_name + " final_exponentiation",
[](typename gt_type::value_type& A) {
return final_exponentiation<curve_type>(A);
});

} else {
std::cout << "Curve " << curve_name << " does not have GT, skipping benchmarks" << std::endl;
}
}

BOOST_AUTO_TEST_SUITE(curves_benchmark)
Expand All @@ -148,6 +217,11 @@ BOOST_AUTO_TEST_CASE(bls12_381)
benchmark_curve_operations<nil::crypto3::algebra::curves::bls12<381>>("BLS12-381");
}

BOOST_AUTO_TEST_CASE(bls12_377)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::bls12<377>>("BLS12-377");
}

BOOST_AUTO_TEST_CASE(mnt4_298)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::mnt4<298>>("MNT4-298");
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@

#include <nil/crypto3/algebra/curves/alt_bn128.hpp>
#include <nil/crypto3/algebra/curves/bls12.hpp>
#include <nil/crypto3/algebra/curves/edwards.hpp>
#include <nil/crypto3/algebra/curves/mnt4.hpp>
#include <nil/crypto3/algebra/curves/mnt6.hpp>
#include <nil/crypto3/algebra/curves/params/multiexp/alt_bn128.hpp>
#include <nil/crypto3/algebra/curves/params/multiexp/bls12.hpp>
#include <nil/crypto3/algebra/curves/params/multiexp/edwards.hpp>
#include <nil/crypto3/algebra/curves/params/multiexp/mnt4.hpp>
#include <nil/crypto3/algebra/curves/params/multiexp/mnt6.hpp>
#include <nil/crypto3/algebra/curves/params/wnaf/bls12.hpp>
Expand Down
Loading
Loading