Skip to content

Commit

Permalink
evmmax: Add ate pairing benchmark tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Apr 2, 2024
1 parent 07520b0 commit c339934
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/precompiles_bench/precompiles_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,51 @@ BENCHMARK_TEMPLATE(ecmul, evmone::state::ecmul_execute);
BENCHMARK_TEMPLATE(ecmul, evmone::state::silkpre_ecmul_execute);
#endif


template <ExecuteFn Fn>
void ecpairing(benchmark::State& state)
{
const auto input = evmc::from_hex(
"105456a333e6d636854f987ea7bb713dfd0ae8371a72aea313ae0c32c0bf10160cf031d41b41557f3e7e3ba0c5"
"1bebe5da8e6ecd855ec50fc87efcdeac168bcc0476be093a6d2b4bbf907172049874af11e1b6267606e00804d3"
"ff0037ec57fd3010c68cb50161b7d1d96bb71edfec9880171954e56871abf3d93cc94d745fa114c059d74e5b6c"
"4ec14ae5864ebe23a71781d86c29fb8fb6cce94f70d3de7a2101b33461f39d9e887dbb100f170a2345dde3c07e"
"256d1dfa2b657ba5cd030427000000000000000000000000000000000000000000000000000000000000000100"
"000000000000000000000000000000000000000000000000000000000000021a2c3013d2ea92e13c800cde68ef"
"56a294b883f6ac35d25f587c09b1b3c635f7290158a80cd3d66530f74dc94c94adb88f5cdb481acca997b6e600"
"71f08a115f2f997f3dbd66a7afe07fe7862ce239edba9e05c5afff7f8a1259c9733b2dfbb929d1691530ca701b"
"4a106054688728c9972c8512e9789e9567aae23e302ccd75000000000000000000000000000000000000000000"
"000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000")
.value();
uint8_t output[32];

const auto [gas_cost, max_output_size] =
evmone::state::ecpairing_analyze(input, EVMC_LATEST_STABLE_REVISION);
if (max_output_size > std::size(output))
return state.SkipWithError("too small output");

int64_t total_gas_used = 0;
for ([[maybe_unused]] auto _ : state)
{
const auto [status, _2] = Fn(input.data(), input.size(), output, std::size(output));
if (status != EVMC_SUCCESS) [[unlikely]]
return state.SkipWithError("invalid result");
total_gas_used += gas_cost;
}

using benchmark::Counter;
state.counters["gas_used"] = Counter(static_cast<double>(gas_cost));
state.counters["gas_rate"] = Counter(static_cast<double>(total_gas_used), Counter::kIsRate);
}

BENCHMARK_TEMPLATE(ecpairing, evmone::state::ecpairing_execute);
#ifdef EVMONE_PRECOMPILES_SILKPRE
BENCHMARK_TEMPLATE(ecpairing, evmone::state::silkpre_ecpairing_execute);
#endif

} // namespace

BENCHMARK_MAIN();
57 changes: 57 additions & 0 deletions test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,63 @@ ExecutionResult identity_execute(const uint8_t* input, size_t input_size, uint8_
return {EVMC_SUCCESS, input_size};
}

ExecutionResult ecpairing_execute(const uint8_t* input, size_t input_size, uint8_t* output,

Check warning on line 243 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L243

Added line #L243 was not covered by tests
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 32);

Check warning on line 246 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L246

Added line #L246 was not covered by tests

const auto pair_size = 192;

Check warning on line 248 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L248

Added line #L248 was not covered by tests

if (input_size % pair_size != 0)
return {EVMC_PRECOMPILE_FAILURE, 0};

Check warning on line 251 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L250-L251

Added lines #L250 - L251 were not covered by tests

const auto pair_count = input_size / pair_size;

Check warning on line 253 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L253

Added line #L253 was not covered by tests

if (pair_count > 0)

Check warning on line 255 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L255

Added line #L255 was not covered by tests
{
auto input_idx = input;

Check warning on line 257 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L257

Added line #L257 was not covered by tests

std::vector<evmmax::bn254::Point> vG1(pair_count);
std::vector<evmmax::bn254::ExtPoint> vG2(pair_count);

Check warning on line 260 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L259-L260

Added lines #L259 - L260 were not covered by tests

for (size_t i = 0; i < pair_count; ++i)

Check warning on line 262 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L262

Added line #L262 was not covered by tests
{
const evmmax::bn254::Point p = {
intx::be::unsafe::load<intx::uint256>(input_idx),
intx::be::unsafe::load<intx::uint256>(input_idx + 32),

Check warning on line 266 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L265-L266

Added lines #L265 - L266 were not covered by tests
};

const evmmax::bn254::ExtPoint q = {
{intx::be::unsafe::load<intx::uint256>(input_idx + 96),
intx::be::unsafe::load<intx::uint256>(input_idx + 64)},
{intx::be::unsafe::load<intx::uint256>(input_idx + 160),
intx::be::unsafe::load<intx::uint256>(input_idx + 128)},
};

Check warning on line 274 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L270-L274

Added lines #L270 - L274 were not covered by tests

vG1[i] = p;
vG2[i] = q;

Check warning on line 277 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L276-L277

Added lines #L276 - L277 were not covered by tests

input_idx += pair_size;

Check warning on line 279 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L279

Added line #L279 was not covered by tests
}

const auto res = evmmax::bn254::pairing(vG2, vG1);

Check warning on line 282 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L282

Added line #L282 was not covered by tests

if (res.has_value())

Check warning on line 284 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L284

Added line #L284 was not covered by tests
{
intx::be::unsafe::store(output, res.value() ? intx::uint256{1} : intx::uint256{0});
return {EVMC_SUCCESS, 64};

Check warning on line 287 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L286-L287

Added lines #L286 - L287 were not covered by tests
}
else
return {EVMC_PRECOMPILE_FAILURE, 0};
}

Check warning on line 291 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L290-L291

Added lines #L290 - L291 were not covered by tests
else
{
intx::be::unsafe::store(output, intx::uint256{1});
return {EVMC_SUCCESS, 32};

Check warning on line 295 in test/state/precompiles.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/precompiles.cpp#L294-L295

Added lines #L294 - L295 were not covered by tests
}
}


namespace
{
struct PrecompileTraits
Expand Down
2 changes: 2 additions & 0 deletions test/state/precompiles_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ ExecutionResult ecadd_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult ecmul_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
ExecutionResult ecpairing_execute(
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
} // namespace evmone::state

0 comments on commit c339934

Please sign in to comment.