Skip to content

Commit

Permalink
Added templated function run_benchmark<group_type...>(lambda).
Browse files Browse the repository at this point in the history
It initializes samples array of given template types and runs series of
calls to lambda. Samples are traversed with strides so they do not sit
in cache.

After the benchmark the statistics are printed to std::cout.

renamed folder, moved profiler to benchmark_tools

Moved scoped profiler to benchmark_tools

Fix warnings
  • Loading branch information
vo-nil committed Aug 12, 2024
1 parent 8fb1f02 commit beb7504
Show file tree
Hide file tree
Showing 24 changed files with 531 additions and 400 deletions.
2 changes: 1 addition & 1 deletion libs/algebra/example/short_weierstrass_coordinates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void coordinates_examples() {
std::cout << "c2 value: " << (c2) << std::endl;
std::cout << "c1 + c2 value: " << (c1 + c2) << std::endl;
std::cout << "c1 - c2 value: " << (c1 - c2) << std::endl;
std::cout << "Doubled c1 value: " << (c1.doubled()) << std::endl;
// std::cout << "Doubled c1 value: " << (c1.doubled()) << std::endl;
}

int main() {
Expand Down
2 changes: 2 additions & 0 deletions libs/algebra/include/nil/crypto3/algebra/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace nil {
BOOST_TTI_HAS_TYPE(g2_type)
BOOST_TTI_HAS_TYPE(gt_type)

BOOST_TTI_HAS_TEMPLATE(g2_type)

BOOST_TTI_HAS_TYPE(group_type)

BOOST_TTI_HAS_STATIC_MEMBER_DATA(value_bits)
Expand Down
20 changes: 13 additions & 7 deletions libs/algebra/test/bench_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include(CMTest)

add_custom_target(algebra_runtime_bench_tests)

macro(define_runtime_algebra_test name)
macro(define_algebra_benchmark name)
set(test_name "algebra_${name}_bench_test")
add_dependencies(algebra_runtime_bench_tests ${test_name})

Expand All @@ -22,7 +22,13 @@ macro(define_runtime_algebra_test name)

${Boost_INCLUDE_DIRS})

set_target_properties(${test_name} PROPERTIES CXX_STANDARD 17
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")
Expand All @@ -31,16 +37,16 @@ macro(define_runtime_algebra_test name)
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
set(BENCHMARK_NAMES
"bench_curves"
"bench_fields"
"bench_multiexp"
)
)

foreach(TEST_NAME ${RUNTIME_TESTS_NAMES})
define_runtime_algebra_test(${TEST_NAME})
foreach(BENCH_NAME ${BENCHMARK_NAMES})
define_algebra_benchmark(${BENCH_NAME})
endforeach()


287 changes: 97 additions & 190 deletions libs/algebra/test/bench_test/bench_curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,18 @@

#define BOOST_TEST_MODULE algebra_curves_bench_test

#include <iostream>
#include <chrono>
#include <ratio>
#include <type_traits>

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

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/test/execution_monitor.hpp>

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/mpl/list.hpp>

#include <nil/crypto3/algebra/curves/detail/forms/short_weierstrass/coordinates.hpp>
#include <nil/crypto3/algebra/curves/detail/forms/twisted_edwards/coordinates.hpp>
#include <nil/crypto3/algebra/curves/forms.hpp>
#include <iostream>

#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/jubjub.hpp>
#include <nil/crypto3/algebra/curves/babyjubjub.hpp>
#include <nil/crypto3/algebra/curves/mnt4.hpp>
Expand All @@ -54,201 +45,117 @@
#include <nil/crypto3/algebra/curves/secp_r1.hpp>
#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/crypto3/algebra/curves/curve25519.hpp>

#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/algebra/fields/fp2.hpp>
#include <nil/crypto3/algebra/fields/fp3.hpp>

#include <nil/crypto3/algebra/random_element.hpp>
#include <nil/crypto3/algebra/type_traits.hpp>
#include <nil/crypto3/bench/benchmark.hpp>

using namespace nil::crypto3::algebra;

BOOST_AUTO_TEST_SUITE(curves_manual_tests)
/**/

template<typename CurveGroup, typename AffineCurveGroup>
void curve_operations_perf_test(std::string const& curve_name) {
using namespace nil::crypto3;
using namespace nil::crypto3::algebra;

typedef typename AffineCurveGroup::value_type affine_value_type;
typedef typename CurveGroup::value_type value_type;
typedef typename CurveGroup::curve_type::scalar_field_type::value_type scalar_type;

std::vector<value_type> points1;
std::vector<affine_value_type> points2;
std::vector<scalar_type> constants;

size_t SAMPLE_POINTS = 1000;
for (int i = 0; i < SAMPLE_POINTS; ++i) {
auto p1 = algebra::random_element<CurveGroup>();
auto p1a = p1.to_affine();

points1.push_back(value_type::from_affine(p1a));
points2.push_back(algebra::random_element<AffineCurveGroup>());
constants.push_back(algebra::random_element<typename CurveGroup::curve_type::scalar_field_type>());
}

using duration = std::chrono::duration<double, std::nano>;

auto run_batched_test = [&](
std::string const& test_name,
std::size_t BATCHES,
std::size_t samples_per_batch,
std::vector<value_type> const& B,
std::function<void (value_type & A, value_type const& B)> opfunc)
{
std::vector<duration> batch_duration;
batch_duration.resize(BATCHES);

auto res = B[0];

for(size_t b = 0; b < BATCHES; ++b) {
if (b % (BATCHES/10) == 0) std::cerr << "Batch progress:" << b << std::endl;
auto start = std::chrono::high_resolution_clock::now();
for(size_t i = 0; i < samples_per_batch; ++i) {
opfunc(res, B[i*i % SAMPLE_POINTS]);
}

auto finish = std::chrono::high_resolution_clock::now();
batch_duration[b] = (finish - start) * 1.0 / samples_per_batch;
}

std::cout << res << std::endl;

/* To filter 10% outliers, sort results and set margin to BATCHES/20 = 5% */
// sort(batch_duration.begin(), batch_duration.end());
std::size_t margin = 0; // BATCHES/20;
auto s = batch_duration[margin];
for(size_t b = margin+1; b < batch_duration.size()-margin; ++b) {
s += batch_duration[b];
}


s /= batch_duration.size() - margin*2;
std::cout << test_name << ": " << std::fixed << std::setprecision(3) << s.count() << std::endl;

return batch_duration;
};

size_t SAMPLES_PER_BATCH = 10000;
size_t BATCHES = 1000;

for(int MULTIPLICATOR = 1; MULTIPLICATOR <= 10; ++MULTIPLICATOR) {
std::cout << "MULT: " << MULTIPLICATOR << std::endl;

auto madd_res = run_batched_test(
"madd",
BATCHES, SAMPLES_PER_BATCH / MULTIPLICATOR,
points1,
[&]( value_type & A, value_type const& B) {
for(int m = 0; m < MULTIPLICATOR; ++m)
A.mixed_add(B);
} );

auto add_res = run_batched_test(
"add",
BATCHES, SAMPLES_PER_BATCH / MULTIPLICATOR,
points1,
[&]( value_type & A, value_type const& B) {
for(int m = 0; m < MULTIPLICATOR; ++m)
A += B;
} );

auto dbl_res = run_batched_test(
"dbl",
BATCHES, SAMPLES_PER_BATCH / MULTIPLICATOR,
points1,
[&]( value_type & A, value_type const& B) {
for(int m = 0; m < MULTIPLICATOR; ++m)
using namespace nil::crypto3::bench;

template <typename curve_type>
void benchmark_curve_operations(std::string const& curve_name)
{
using g1_type = typename curve_type::template g1_type<>;
using base_field = typename curve_type::base_field_type;
using scalar_field = typename curve_type::scalar_field_type;

run_benchmark<base_field, base_field>(
curve_name + " Fp addition",
[](typename base_field::value_type& A, typename base_field::value_type const& B) {
return A += B;
});
run_benchmark<base_field, base_field>(
curve_name + " Fp multiplication",
[](typename base_field::value_type& A, typename base_field::value_type const& B) {
return A *= B;
});
run_benchmark<base_field>(
curve_name + " Fp inverse",
[](typename base_field::value_type& A) {
return A.inversed();
});
run_benchmark<scalar_field, scalar_field>(
curve_name + " Fq addition",
[](typename scalar_field::value_type& A, typename scalar_field::value_type const& B) {
return A += B;
});
run_benchmark<scalar_field, scalar_field>(
curve_name + " Fq multiplication",
[](typename scalar_field::value_type& A, typename scalar_field::value_type const& B) {
return A *= B;
});
run_benchmark<scalar_field>(
curve_name + " Fq inverse",
[](typename scalar_field::value_type& A) {
return A.inversed();
});
run_benchmark<g1_type, g1_type>(
curve_name + " G1 addition",
[](typename g1_type::value_type& A, typename g1_type::value_type const& B) {
return A += B;
});
run_benchmark<g1_type>(
curve_name + " G1 doubling",
[](typename g1_type::value_type& A) {
A.double_inplace();// += A;
return A;
});
run_benchmark<g1_type, scalar_field>(
curve_name + " G1 scalar multiplication",
[](typename g1_type::value_type& A, typename scalar_field::value_type const& B) {
return A *= B;
});

if constexpr (has_template_g2_type<curve_type>::value) {
using g2_type = typename curve_type::template g2_type<>;
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) {
A.double_inplace();
} );

auto smul_res = run_batched_test(
"smul",
BATCHES, SAMPLES_PER_BATCH / 256 / MULTIPLICATOR,
points1,
[&]( value_type & A, value_type const& B) {
for(int m = 0; m < MULTIPLICATOR; ++m)
A = A * constants[0];
} );

char filename[200]= {0};
sprintf(filename,"%s-curve-ops-%03d.csv", curve_name.c_str(), MULTIPLICATOR);

std::ofstream f(filename, std::ofstream::out);
f << "# " << typeid(CurveGroup).name() << std::endl;
f << "madd,add,dbl,smul" << std::endl;
std::size_t prec = 4;
for(std::size_t i = 0; i < BATCHES; ++i) {
f
<< std::fixed << std::setprecision(prec) << madd_res[i].count() << ","
<< std::fixed << std::setprecision(prec) << add_res[i].count() << ","
<< std::fixed << std::setprecision(prec) << dbl_res[i].count() << ","
<< std::fixed << std::setprecision(prec) << smul_res[i].count()
<< std::endl;
}

return A;
//return A += A;
});
run_benchmark<g2_type, scalar_field>(
curve_name + " G2 scalar multiplication",
[](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;
}
}

BOOST_AUTO_TEST_CASE(perf_test_bls12_381_g1) {
using policy_type = nil::crypto3::algebra::curves::bls12<381>::g1_type<
nil::crypto3::algebra::curves::coordinates::jacobian_with_a4_0,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

using affine_policy_type = nil::crypto3::algebra::curves::bls12<381>::g1_type<nil::crypto3::algebra::curves::coordinates::affine,
nil::crypto3::algebra::curves::forms::short_weierstrass>;
BOOST_AUTO_TEST_SUITE(curves_benchmark)

curve_operations_perf_test<policy_type, affine_policy_type>("bls12-381-j0");
BOOST_AUTO_TEST_CASE(pallas)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::pallas>("Pallas");
}

BOOST_AUTO_TEST_CASE(perf_test_pallas) {
using policy_type = nil::crypto3::algebra::curves::pallas::g1_type<
nil::crypto3::algebra::curves::coordinates::jacobian_with_a4_0,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

using affine_policy_type = nil::crypto3::algebra::curves::pallas::g1_type<nil::crypto3::algebra::curves::coordinates::affine,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

curve_operations_perf_test<policy_type, affine_policy_type>("pallas-j0");
BOOST_AUTO_TEST_CASE(vesta)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::vesta>("Vesta");
}

BOOST_AUTO_TEST_CASE(perf_test_mnt4) {
using policy_type = nil::crypto3::algebra::curves::mnt4<298>::g1_type<
nil::crypto3::algebra::curves::coordinates::projective,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

using affine_policy_type = nil::crypto3::algebra::curves::mnt4<298>::g1_type<
nil::crypto3::algebra::curves::coordinates::affine,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

curve_operations_perf_test<policy_type, affine_policy_type>("mnt4-p");
BOOST_AUTO_TEST_CASE(bls12_381)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::bls12<381>>("BLS12-381");
}

BOOST_AUTO_TEST_CASE(perf_test_mnt6) {
using policy_type = nil::crypto3::algebra::curves::mnt6<298>::g1_type<
nil::crypto3::algebra::curves::coordinates::projective,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

using affine_policy_type = nil::crypto3::algebra::curves::mnt6<298>::g1_type<
nil::crypto3::algebra::curves::coordinates::affine,
nil::crypto3::algebra::curves::forms::short_weierstrass>;

curve_operations_perf_test<policy_type, affine_policy_type>("mnt6-p");
BOOST_AUTO_TEST_CASE(mnt4_298)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::mnt4<298>>("MNT4-298");
}

BOOST_AUTO_TEST_CASE(perf_test_ed25519) {
using policy_type = nil::crypto3::algebra::curves::ed25519::g1_type<
nil::crypto3::algebra::curves::coordinates::extended_with_a_minus_1,
nil::crypto3::algebra::curves::forms::twisted_edwards>;

using affine_policy_type = nil::crypto3::algebra::curves::ed25519::g1_type<
nil::crypto3::algebra::curves::coordinates::affine,
nil::crypto3::algebra::curves::forms::twisted_edwards>;

curve_operations_perf_test<policy_type, affine_policy_type>("ed25519-ex-1");
BOOST_AUTO_TEST_CASE(mnt6_298)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::mnt6<298>>("MNT6-298");
}


Expand Down
2 changes: 1 addition & 1 deletion libs/algebra/test/bench_test/bench_fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void run_perf_test(std::string const& field_name) {

std::ofstream f(filename, std::ofstream::out);
f << "# " << typeid(Field).name() << std::endl;
f << "sum,mul,sqr,inv" << std::endl;
f << "sum,mul,sub,sqr,inv" << std::endl;

for(size_t i = 0; i < plus_results.size(); ++i) {
f << std::fixed << std::setprecision(3) << plus_results[i].count() << ","
Expand Down
Loading

0 comments on commit beb7504

Please sign in to comment.