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

[5.0] Adapt to changes in bls12-381 lib and add tests #1882

Merged
merged 30 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
71a9319
Make code compile with refactored bls12-381.
yarkinwho Nov 9, 2023
eea0ea3
Refactor some interfaces, compiling, not tested yet.
yarkinwho Nov 9, 2023
3d188c7
Merge branch 'release/5.0' into yarkin/update_bls
yarkinwho Nov 9, 2023
fa59891
Refactor bls_gx calls.
yarkinwho Nov 10, 2023
d8dc360
Add bls_fp_mod and bls_fp_exp
yarkinwho Nov 10, 2023
35f375c
Update unit tests.
yarkinwho Nov 13, 2023
77f2be4
Merge branch 'release/5.0' into yarkin/update_bls
yarkinwho Nov 13, 2023
9c42d42
Fix comile.
yarkinwho Nov 13, 2023
49d17e3
Update to use affine form instead of jacobian form for data
yarkinwho Nov 14, 2023
fa5f81e
Merge branch 'release/5.0' into yarkin/update_bls
yarkinwho Nov 14, 2023
4e40590
Fix benchmark
yarkinwho Nov 14, 2023
2a85796
add benchmarking for Montgomery form host functions
linh2931 Nov 15, 2023
9d35e5a
Add tests for fp_mod, fp_exp, fp_mul
yarkinwho Nov 15, 2023
4255d62
Add more test case and update submodule (only contains test updates).
yarkinwho Nov 15, 2023
8819030
fix weighted sum montgomery form flag
linh2931 Nov 15, 2023
f280d5e
add const& to std::string parameters in all _impl methods
linh2931 Nov 15, 2023
dfb05c5
Remove _mont functions.
yarkinwho Nov 16, 2023
e1f67e9
Update comments.
yarkinwho Nov 16, 2023
d9e59b8
Delete unused benchmark.
yarkinwho Nov 16, 2023
36eafc7
Merge remote-tracking branch 'origin/yarkin/update_bls' into more_bls…
linh2931 Nov 16, 2023
65b54ab
resolve merge conflicts
linh2931 Nov 16, 2023
c39e930
remove _mont* functions
linh2931 Nov 16, 2023
6a9a8b1
use std::array instead of std::vector to simply conversion to eosio::…
linh2931 Nov 16, 2023
cc9bba8
Merge pull request #1904 from AntelopeIO/more_bls_benchmark
linh2931 Nov 17, 2023
cb4ceaa
Update feature signature.
yarkinwho Nov 21, 2023
1269951
Update feature manager, fix deep-mind.log accordinglly.
yarkinwho Nov 29, 2023
11640bf
Update to latest bls lib.
yarkinwho Nov 29, 2023
3c5c566
Merge branch 'release/5.0' into yarkin/update_bls
yarkinwho Nov 29, 2023
87738aa
remove `(multi-exponentiation)` from comments.
greg7mdp Nov 29, 2023
804e415
Update bls12 submodule to tip of `main` branch.
greg7mdp Nov 29, 2023
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
145 changes: 45 additions & 100 deletions benchmark/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,26 @@ std::array<uint64_t, 4> random_scalar()
bls12_381::g1 random_g1()
{
std::array<uint64_t, 4> k = random_scalar();
return bls12_381::g1::one().mulScalar(k);
return bls12_381::g1::one().scale(k);
}

// utilility to create a random g2
bls12_381::g2 random_g2()
{
std::array<uint64_t, 4> k = random_scalar();
return bls12_381::g2::one().mulScalar(k);
return bls12_381::g2::one().scale(k);
}

// bls_g1_add benchmarking
void benchmark_bls_g1_add() {
// prepare g1 operand in Jacobian LE format
g1 p = random_g1();
std::vector<char> buf(144);
p.toJacobianBytesLE(std::span<uint8_t, 144>((uint8_t*)buf.data(), 144), true);
std::vector<char> buf(96);
p.toAffineBytesLE(std::span<uint8_t, 96>((uint8_t*)buf.data(), 96), true);
eosio::chain::span<const char> op1(buf.data(), buf.size());

// prepare result operand
std::vector<char> result_buf(144);
std::vector<char> result_buf(96);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g1_add to be benchmarked
Expand All @@ -146,12 +146,12 @@ void benchmark_bls_g1_add() {
void benchmark_bls_g2_add() {
// prepare g2 operand in Jacobian LE format
g2 p = random_g2();
std::vector<char> buf(288);
p.toJacobianBytesLE(std::span<uint8_t, 288>((uint8_t*)buf.data(), 288), true);
std::vector<char> buf(192);
p.toAffineBytesLE(std::span<uint8_t, 192>((uint8_t*)buf.data(), 192), true);
eosio::chain::span<const char> op(buf.data(), buf.size());

// prepare result operand
std::vector<char> result_buf(288);
std::vector<char> result_buf(192);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g2_add to be benchmarked
Expand All @@ -163,66 +163,13 @@ void benchmark_bls_g2_add() {
benchmarking("bls_g2_add", benchmarked_func);
}

// bls_g1_mul benchmarking
void benchmark_bls_g1_mul() {
// prepare g1 operand
g1 p = random_g1();
std::vector<char> buf(144);
p.toJacobianBytesLE(std::span<uint8_t, 144>((uint8_t*)buf.data(), 144), true);
eosio::chain::span<const char> point(buf.data(), buf.size());

// prepare scalar operand
std::array<uint64_t, 4> s = random_scalar();
std::vector<char> scalar_buf(32);
scalar::toBytesLE(s, std::span<uint8_t, 32>((uint8_t*)scalar_buf.data(), 32));
eosio::chain::span<const char> scalar(scalar_buf.data(), scalar_buf.size());

// prepare result operand
std::vector<char> result_buf(144);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g1_mul to be benchmarked
interface_in_benchmark interface;
auto benchmarked_func = [&]() {
interface.interface->bls_g1_mul(point, scalar, result);
};

benchmarking("bls_g1_mul", benchmarked_func);
}

// bls_g2_mul benchmarking
void benchmark_bls_g2_mul() {
g2 p = random_g2();
std::vector<char> buf(288);
p.toJacobianBytesLE(std::span<uint8_t, 288>((uint8_t*)buf.data(), 288), true);
eosio::chain::span<const char> point(buf.data(), buf.size());

// prepare scalar operand
std::array<uint64_t, 4> s = random_scalar();
std::vector<char> scalar_buf(32);
scalar::toBytesLE(s, std::span<uint8_t, 32>((uint8_t*)scalar_buf.data(), 32));
eosio::chain::span<const char> scalar(scalar_buf.data(), scalar_buf.size());

// prepare result operand
std::vector<char> result_buf(288);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g2_mul to be benchmarked
interface_in_benchmark interface;
auto benchmarked_func = [&]() {
interface.interface->bls_g2_mul(point, scalar, result);
};

benchmarking("bls_g2_mul", benchmarked_func);
}

// bls_g1_exp benchmarking utility
void benchmark_bls_g1_exp(std::string test_name, uint32_t num_points) {
// bls_g1_weighted_sum benchmarking utility
void benchmark_bls_g1_weighted_sum(std::string test_name, uint32_t num_points) {
// prepare g1 points operand
std::vector<char> g1_buf(144*num_points);
std::vector<char> g1_buf(96*num_points);
for (auto i=0u; i < num_points; ++i) {
g1 p = random_g1();
p.toJacobianBytesLE(std::span<uint8_t, 144>((uint8_t*)g1_buf.data() + i * 144, 144), true);
p.toAffineBytesLE(std::span<uint8_t, 96>((uint8_t*)g1_buf.data() + i * 96, 96), true);
}
chain::span<const char> g1_points(g1_buf.data(), g1_buf.size());

Expand All @@ -235,35 +182,35 @@ void benchmark_bls_g1_exp(std::string test_name, uint32_t num_points) {
chain::span<const char> scalars(scalars_buf.data(), scalars_buf.size());

// prepare result operand
std::vector<char> result_buf(144);
std::vector<char> result_buf(96);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g1_exp to be benchmarked
// set up bls_g1_weighted_sum to be benchmarked
interface_in_benchmark interface;
auto benchmarked_func = [&]() {
interface.interface->bls_g1_exp(g1_points, scalars, num_points, result);
interface.interface->bls_g1_weighted_sum(g1_points, scalars, num_points, result);
};

benchmarking(test_name, benchmarked_func);
}

// bls_g1_exp benchmarking with 1 input point
void benchmark_bls_g1_exp_one_point() {
benchmark_bls_g1_exp("bls_g1_exp 1 point", 1);
// bls_g1_weighted_sum benchmarking with 1 input point
void benchmark_bls_g1_weighted_sum_one_point() {
benchmark_bls_g1_weighted_sum("bls_g1_weighted_sum 1 point", 1);
}

// bls_g1_exp benchmarking with 3 input points
void benchmark_bls_g1_exp_three_point() {
benchmark_bls_g1_exp("bls_g1_exp 3 points", 3);
// bls_g1_weighted_sum benchmarking with 3 input points
void benchmark_bls_g1_weighted_sum_three_point() {
benchmark_bls_g1_weighted_sum("bls_g1_weighted_sum 3 points", 3);
}

// bls_g2_exp benchmarking utility
void benchmark_bls_g2_exp(std::string test_name, uint32_t num_points) {
// bls_g2_weighted_sum benchmarking utility
void benchmark_bls_g2_weighted_sum(std::string test_name, uint32_t num_points) {
// prepare g2 points operand
std::vector<char> g2_buf(288*num_points);
std::vector<char> g2_buf(192*num_points);
for (auto i=0u; i < num_points; ++i) {
g2 p = random_g2();
p.toJacobianBytesLE(std::span<uint8_t, 288>((uint8_t*)g2_buf.data() + i * 288, 288), true);
p.toAffineBytesLE(std::span<uint8_t, 192>((uint8_t*)g2_buf.data() + i * 192, 192), true);
}
eosio::chain::span<const char> g2_points(g2_buf.data(), g2_buf.size());

Expand All @@ -276,44 +223,44 @@ void benchmark_bls_g2_exp(std::string test_name, uint32_t num_points) {
eosio::chain::span<const char> scalars(scalars_buf.data(), scalars_buf.size());

// prepare result operand
std::vector<char> result_buf(288);
std::vector<char> result_buf(192);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g2_exp to be benchmarked
// set up bls_g2_weighted_sum to be benchmarked
interface_in_benchmark interface;
auto benchmarked_func = [&]() {
interface.interface->bls_g2_exp(g2_points, scalars, num_points, result);
interface.interface->bls_g2_weighted_sum(g2_points, scalars, num_points, result);
};

benchmarking(test_name, benchmarked_func);
}

// bls_g2_exp benchmarking with 1 input point
void benchmark_bls_g2_exp_one_point() {
benchmark_bls_g2_exp("bls_g2_exp 1 point", 1);
// bls_g2_weighted_sum benchmarking with 1 input point
void benchmark_bls_g2_weighted_sum_one_point() {
benchmark_bls_g2_weighted_sum("bls_g2_weighted_sum 1 point", 1);
}

// bls_g2_exp benchmarking with 3 input points
void benchmark_bls_g2_exp_three_point() {
benchmark_bls_g2_exp("bls_g2_exp 3 points", 3);
// bls_g2_weighted_sum benchmarking with 3 input points
void benchmark_bls_g2_weighted_sum_three_point() {
benchmark_bls_g2_weighted_sum("bls_g2_weighted_sum 3 points", 3);
}

// bls_pairing benchmarking utility
void benchmark_bls_pairing(std::string test_name, uint32_t num_pairs) {
// prepare g1 operand
std::vector<char> g1_buf(144*num_pairs);
//g1_buf.reserve(144*num_pairs);
std::vector<char> g1_buf(96*num_pairs);
//g1_buf.reserve(96*num_pairs);
for (auto i=0u; i < num_pairs; ++i) {
g1 p = random_g1();
p.toJacobianBytesLE(std::span<uint8_t, 144>((uint8_t*)g1_buf.data() + i * 144, 144), true);
p.toAffineBytesLE(std::span<uint8_t, 96>((uint8_t*)g1_buf.data() + i * 96, 96), true);
}
eosio::chain::span<const char> g1_points(g1_buf.data(), g1_buf.size());

// prepare g2 operand
std::vector<char> g2_buf(288*num_pairs);
std::vector<char> g2_buf(192*num_pairs);
for (auto i=0u; i < num_pairs; ++i) {
g2 p2 = random_g2();
p2.toJacobianBytesLE(std::span<uint8_t, (288)>((uint8_t*)g2_buf.data() + i * 288, (288)), true);
p2.toAffineBytesLE(std::span<uint8_t, (192)>((uint8_t*)g2_buf.data() + i * 192, (192)), true);
}
eosio::chain::span<const char> g2_points(g2_buf.data(), g2_buf.size());

Expand Down Expand Up @@ -347,7 +294,7 @@ void benchmark_bls_g1_map() {
eosio::chain::span<const char> e((char*)e_buf.data(), e_buf.size());

// prepare result operand
std::vector<char> result_buf(144);
std::vector<char> result_buf(96);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g1_map to be benchmarked
Expand All @@ -366,7 +313,7 @@ void benchmark_bls_g2_map() {
eosio::chain::span<const char> e((char*)e_buf.data(), e_buf.size());

// prepare result operand
std::vector<char> result_buf(288);
std::vector<char> result_buf(192);
eosio::chain::span<char> result(result_buf.data(), result_buf.size());

// set up bls_g2_map to be benchmarked
Expand Down Expand Up @@ -406,14 +353,12 @@ void benchmark_bls_fp_mod() {
void bls_benchmarking() {
benchmark_bls_g1_add();
benchmark_bls_g2_add();
benchmark_bls_g1_mul();
benchmark_bls_g2_mul();
benchmark_bls_pairing_one_pair();
benchmark_bls_pairing_three_pair();
benchmark_bls_g1_exp_one_point();
benchmark_bls_g1_exp_three_point();
benchmark_bls_g2_exp_one_point();
benchmark_bls_g2_exp_three_point();
benchmark_bls_g1_weighted_sum_one_point();
benchmark_bls_g1_weighted_sum_three_point();
benchmark_bls_g2_weighted_sum_one_point();
benchmark_bls_g2_weighted_sum_three_point();
benchmark_bls_g1_map();
benchmark_bls_g2_map();
benchmark_bls_fp_mod();
Expand Down
8 changes: 4 additions & 4 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3845,14 +3845,14 @@ void controller_impl::on_activation<builtin_protocol_feature_t::bls_primitives>(
db.modify( db.get<protocol_state_object>(), [&]( auto& ps ) {
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g1_add" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g2_add" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g1_mul" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g2_mul" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g1_exp" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g2_exp" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g1_weighted_sum" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g2_weighted_sum" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_pairing" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g1_map" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_g2_map" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_fp_mod" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_fp_mul" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "bls_fp_exp" );
} );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ inline constexpr auto get_intrinsic_table() {
"env.get_block_num",
"env.bls_g1_add",
"env.bls_g2_add",
"env.bls_g1_mul",
"env.bls_g2_mul",
"env.bls_g1_exp",
"env.bls_g2_exp",
"env.bls_g1_weighted_sum",
"env.bls_g2_weighted_sum",
"env.bls_pairing",
"env.bls_g1_map",
"env.bls_g2_map",
"env.bls_fp_mod"
"env.bls_fp_mod",
"env.bls_fp_mul",
"env.bls_fp_exp"
);
}
inline constexpr std::size_t find_intrinsic_index(std::string_view hf) {
Expand Down
Loading