Skip to content
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
116 changes: 116 additions & 0 deletions crates/precompile/bench/blake2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use criterion::{black_box, BenchmarkGroup};
use primitives::hex;
use revm_precompile::blake2;

pub fn add_benches(group: &mut BenchmarkGroup<'_, criterion::measurement::WallTime>) {
// Test vectors from the blake2 test
let inputs = [
hex!("0000000248c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 2 rounds
hex!("0000000448c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b616162636465666768696a6b6c6d6e6f700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 4 rounds
hex!("0000004048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 64 rounds
hex!("0000000a48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 10 rounds (Blake2s standard)
hex!("0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 12 rounds (Blake2b standard)
hex!("0000020048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 512 rounds
hex!("0000040048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 1024 rounds
hex!("000186a048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 100000 rounds (100K)
hex!("00030d4048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001"), // 200000 rounds (200K)
];

// Benchmark with 2 rounds
group.bench_function("blake2/2_rounds", |b| {
let input = &inputs[0]; // 2 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 4 rounds
group.bench_function("blake2/4_rounds", |b| {
let input = &inputs[1]; // 4 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 64 rounds
group.bench_function("blake2/64_rounds", |b| {
let input = &inputs[2]; // 64 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 10 rounds (Blake2s standard)
group.bench_function("blake2/10_rounds", |b| {
let input = &inputs[3]; // 10 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 12 rounds (Blake2b standard)
group.bench_function("blake2/12_rounds", |b| {
let input = &inputs[4]; // 12 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 512 rounds
group.bench_function("blake2/512_rounds", |b| {
let input = &inputs[5]; // 512 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 1024 rounds
group.bench_function("blake2/1024_rounds", |b| {
let input = &inputs[6]; // 1024 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 100K rounds
group.bench_function("blake2/100K_rounds", |b| {
let input = &inputs[7]; // 100000 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark with 200K rounds
group.bench_function("blake2/200K_rounds", |b| {
let input = &inputs[8]; // 200000 rounds
b.iter(|| {
black_box(blake2::run(black_box(input), u64::MAX).unwrap());
});
});

// Benchmark just the compression function with different round counts
group.bench_function("blake2/compress_12_rounds", |b| {
let h = [
0x6a09e667f3bcc908u64,
0xbb67ae8584caa73bu64,
0x3c6ef372fe94f82bu64,
0xa54ff53a5f1d36f1u64,
0x510e527fade682d1u64,
0x9b05688c2b3e6c1fu64,
0x1f83d9abfb41bd6bu64,
0x5be0cd19137e2179u64,
];
let m = [0u8; 128];
let t = [0u64, 0u64];
b.iter(|| {
let mut h_copy = h;
blake2::algo::compress(
black_box(12),
&mut h_copy,
black_box(&m),
black_box(t),
Comment on lines +91 to +111
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove -- so we are only benchmarking the precompile in the way that it will be called

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was a reason for measuring 12 rounds. Is it to compare with other blake implementations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was mainly because blake2b uses 12 rounds

black_box(false),
);
});
});
}
4 changes: 4 additions & 0 deletions crates/precompile/bench/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(missing_docs)]
//! Benchmarks for the crypto precompiles

pub mod blake2;
pub mod ecrecover;
pub mod eip1962;
pub mod eip2537;
Expand Down Expand Up @@ -31,6 +32,9 @@ pub fn benchmark_crypto_precompiles(c: &mut Criterion) {

// Run KZG point evaluation benchmarks
eip4844::add_benches(&mut group);

// Run Blake2 benchmarks
blake2::add_benches(&mut group);
}

criterion_group! {
Expand Down
Loading