Skip to content

Commit

Permalink
improve and add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
linh2931 committed Nov 9, 2023
1 parent f54724f commit a96d6de
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions benchmark/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ using namespace eosio::chain;
using namespace eosio::testing;
using namespace bls12_381;

// benchmark BLS host functions directly without relying on CDT wrappers.
// to run a benchmarking session, in the build directory, type
// benchmark/benchmark -f bls
// Benchmark BLS host functions without relying on CDT wrappers.
//
// To run a benchmarking session, in the build directory, type
// benchmark/benchmark -f bls

namespace eosio::benchmark {

// To benchmark host functions directly without CDT wrappers,
// we need to contruct a eosio::chain::webassembly::interface object,
// as host functions are implemented in eosio::chain::webassembly::interface
// we need to contruct an eosio::chain::webassembly::interface object,
// because host functions are implemented in
// eosio::chain::webassembly::interface class.
struct interface_in_benchmark {
interface_in_benchmark() {
// prevent logging from messing up benchmark results display
// prevent logging from interwined with output benchmark results
fc::logger::get(DEFAULT_LOGGER).set_log_level(fc::log_level::off);

// create a chain
Expand Down Expand Up @@ -71,12 +73,12 @@ struct interface_in_benchmark {
trx_ctx = std::make_unique<transaction_context>(*chain->control.get(), *ptrx, ptrx->id(), std::move(*trx_timer));
trx_ctx->max_transaction_time_subjective = fc::microseconds::maximum();
trx_ctx->init_for_input_trx( ptrx->get_unprunable_size(), ptrx->get_prunable_size() );
trx_ctx->exec();
trx_ctx->exec(); // this is required to generate action traces to be used by apply_context constructor

// build apply context from the control and transaction context
apply_ctx = std::make_unique<apply_context>(*chain->control.get(), *trx_ctx, 1);

// finally build the interface
// finally construct the interface
interface = std::make_unique<webassembly::interface>(*apply_ctx);
}

Expand Down Expand Up @@ -119,6 +121,7 @@ bls12_381::g2 random_g2()
return bls12_381::g2::one().mulScalar(k);
}

// bls_g1_add benchmarking
void benchmark_bls_g1_add() {
// prepare g1 operand in Jacobian LE format
g1 p = random_g1();
Expand All @@ -139,6 +142,7 @@ void benchmark_bls_g1_add() {
benchmarking("bls_g1_add", g1_add_func);
}

// bls_g2_add benchmarking
void benchmark_bls_g2_add() {
// prepare g2 operand in Jacobian LE format
g2 p = random_g2();
Expand All @@ -159,6 +163,7 @@ 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();
Expand All @@ -185,6 +190,7 @@ void benchmark_bls_g1_mul() {
benchmarking("bls_g1_mul", benchmarked_func);
}

// bls_g2_mul benchmarking
void benchmark_bls_g2_mul() {
g2 p = random_g2();
std::vector<char> buf(288);
Expand All @@ -210,6 +216,7 @@ void benchmark_bls_g2_mul() {
benchmarking("bls_g2_mul", benchmarked_func);
}

// bls_g1_exp benchmarking utility
void benchmark_bls_g1_exp(std::string test_name, uint32_t num_points) {
// prepare g1 points operand
std::vector<char> g1_buf(144*num_points);
Expand Down Expand Up @@ -240,14 +247,17 @@ void benchmark_bls_g1_exp(std::string test_name, uint32_t num_points) {
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_exp benchmarking with 3 input points
void benchmark_bls_g1_exp_three_point() {
benchmark_bls_g1_exp("bls_g1_exp 3 points", 3);
}

// bls_g2_exp benchmarking utility
void benchmark_bls_g2_exp(std::string test_name, uint32_t num_points) {
// prepare g2 points operand
std::vector<char> g2_buf(288*num_points);
Expand Down Expand Up @@ -278,14 +288,17 @@ void benchmark_bls_g2_exp(std::string test_name, uint32_t num_points) {
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_exp benchmarking with 3 input points
void benchmark_bls_g2_exp_three_point() {
benchmark_bls_g2_exp("bls_g2_exp 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);
Expand Down Expand Up @@ -317,14 +330,17 @@ void benchmark_bls_pairing(std::string test_name, uint32_t num_pairs) {
benchmarking(test_name, benchmarked_func);
}

// bls_pairing benchmarking with 1 input pair
void benchmark_bls_pairing_one_pair() {
benchmark_bls_pairing("bls_pairing 1 pair", 1);
}

// bls_pairing benchmarking with 3 input pairs
void benchmark_bls_pairing_three_pair() {
benchmark_bls_pairing("bls_pairing 3 pairs", 3);
}

// bls_g1_map benchmarking
void benchmark_bls_g1_map() {
// prepare e operand. Must be fp LE.
std::vector<uint8_t> e_buf = {0xc9, 0x3f,0x81,0x7b, 0x15, 0x9b, 0xdf, 0x84, 0x04, 0xdc, 0x37, 0x85, 0x14, 0xf8, 0x45, 0x19, 0x2b, 0xba, 0xe4, 0xfa, 0xac, 0x7f, 0x4a, 0x56, 0x89, 0x24, 0xf2, 0xd9, 0x72, 0x51, 0x25, 0x00, 0x04, 0x89, 0x40, 0x8f, 0xd7, 0x96, 0x46, 0x1c, 0x28, 0x89, 0x00, 0xad, 0xd0, 0x0d, 0x46, 0x18};
Expand All @@ -343,6 +359,7 @@ void benchmark_bls_g1_map() {
benchmarking("bls_g1_map", benchmarked_func);
}

// bls_g2_map benchmarking
void benchmark_bls_g2_map() {
// prepare e operand. Must be fp2 LE.
std::vector<uint8_t> e_buf = {0xd4, 0xf2, 0xcf, 0xec, 0x99, 0x38, 0x78, 0x09, 0x57, 0x4f, 0xcc, 0x2d, 0xba, 0x10, 0x56, 0x03, 0xd9, 0x50, 0xd4, 0x90, 0xe2, 0xbe, 0xbe, 0x0c, 0x21, 0x2c, 0x05, 0xe1, 0x6b, 0x78, 0x47, 0x45, 0xef, 0x4f, 0xe8, 0xe7, 0x0b, 0x55, 0x4d, 0x0a, 0x52, 0xfe, 0x0b, 0xed, 0x5e, 0xa6, 0x69, 0x0a, 0xde, 0x23, 0x48, 0xeb, 0x89, 0x72, 0xa9, 0x67, 0x40, 0xa4, 0x30, 0xdf, 0x16, 0x2d, 0x92, 0x0e, 0x17, 0x5f, 0x59, 0x23, 0xa7, 0x6d, 0x18, 0x65, 0x0e, 0xa2, 0x4a, 0x8e, 0xc0, 0x6d, 0x41, 0x4c, 0x6d, 0x1d, 0x21, 0x8d, 0x67, 0x3d, 0xac, 0x36, 0x19, 0xa1, 0xa5, 0xc1, 0x42, 0x78, 0x57, 0x08};
Expand All @@ -361,6 +378,7 @@ void benchmark_bls_g2_map() {
benchmarking("bls_g2_map", benchmarked_func);
}

// bls_fp_mod benchmarking
void benchmark_bls_fp_mod() {
// prepare scalar operand
std::vector<char> scalar_buf(64);
Expand All @@ -384,6 +402,7 @@ void benchmark_bls_fp_mod() {
benchmarking("bls_fp_mod", benchmarked_func);
}

// register benchmarking functions
void bls_benchmarking() {
benchmark_bls_g1_add();
benchmark_bls_g2_add();
Expand Down

0 comments on commit a96d6de

Please sign in to comment.