Skip to content

Commit

Permalink
chore: update benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Jun 20, 2024
1 parent 1c95788 commit 9e654bc
Showing 1 changed file with 99 additions and 15 deletions.
114 changes: 99 additions & 15 deletions eip7594/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,112 @@
use bls12_381::Scalar;
use criterion::{criterion_group, criterion_main, Criterion};
use eip7594::{prover::ProverContext, trusted_setup};
use eip7594::{
constants::{BYTES_PER_BLOB, CELLS_PER_EXT_BLOB},
prover::ProverContext,
trusted_setup, Cell, KZGCommitment, KZGProof, VerifierContext,
};

/// This is here for reference, same as the above `bench_compute_proof_without_fk20`.
pub fn bench_compute_cells_and_kzg_proofs(c: &mut Criterion) {
const POLYNOMIAL_LEN: usize = 4096;

let trusted_setup = trusted_setup::TrustedSetup::default();
let prover_context = ProverContext::with_num_threads(&trusted_setup, 100);
const POLYNOMIAL_LEN: usize = 4096;

let polynomial_4096: Vec<_> = (0..POLYNOMIAL_LEN)
fn dummy_blob() -> [u8; BYTES_PER_BLOB] {
let polynomial: Vec<_> = (0..POLYNOMIAL_LEN)
.map(|i| -Scalar::from(i as u64))
.collect();

let blob: Vec<_> = polynomial_4096
let blob: Vec<_> = polynomial
.into_iter()
.flat_map(|scalar| scalar.to_bytes_be())
.collect();
let blob = &blob.try_into().unwrap();
blob.try_into().unwrap()
}

fn dummy_commitment_cells_and_proofs() -> (
KZGCommitment,
([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]),
) {
let ctx = ProverContext::default();
let blob = dummy_blob();

let commitment = ctx.blob_to_kzg_commitment(&blob).unwrap();
(commitment, ctx.compute_cells_and_kzg_proofs(&blob).unwrap())
}

const THREAD_COUNTS: [usize; 5] = [1, 4, 8, 16, 32];

pub fn bench_compute_cells_and_kzg_proofs(c: &mut Criterion) {
let trusted_setup = trusted_setup::TrustedSetup::default();

let blob = dummy_blob();

for num_threads in THREAD_COUNTS {
let prover_context = ProverContext::with_num_threads(&trusted_setup, num_threads);
c.bench_function(
&format!(
"computing cells_and_kzg_proofs - NUM_THREADS: {}",
num_threads
),
|b| b.iter(|| prover_context.compute_cells_and_kzg_proofs(&blob)),
);
}
}

pub fn bench_recover_cells_and_compute_kzg_proofs(c: &mut Criterion) {
let trusted_setup = trusted_setup::TrustedSetup::default();

let (_, (cells, proofs)) = dummy_commitment_cells_and_proofs();
let cell_ids: Vec<u64> = (0..cells.len()).map(|x| x as u64).collect();

// Worse case is when half of the cells are missing
let half_cell_ids = &cell_ids[..CELLS_PER_EXT_BLOB / 2];
let half_cells = &cells[..CELLS_PER_EXT_BLOB / 2];
let half_cells = half_cells
.into_iter()
.map(|cell| cell.as_ref())
.collect::<Vec<_>>();
let half_proofs = &proofs[..CELLS_PER_EXT_BLOB / 2];
let half_proofs = half_proofs.into_iter().collect::<Vec<_>>();

for num_threads in THREAD_COUNTS {
let prover_context = ProverContext::with_num_threads(&trusted_setup, num_threads);
c.bench_function(
&format!(
"worse-case recover_cells_and_compute_proofs - NUM_THREADS: {}",
num_threads
),
|b| {
b.iter(|| {
prover_context.recover_cells_and_proofs(
half_cell_ids.to_vec(),
half_cells.to_vec(),
half_proofs.to_vec(),
)
})
},
);
}
}

pub fn bench_verify_cell_kzg_proofs(c: &mut Criterion) {
let trusted_setup = trusted_setup::TrustedSetup::default();

let (commitment, (cells, proofs)) = dummy_commitment_cells_and_proofs();

c.bench_function("computing cells_and_kzg_proofs", |b| {
b.iter(|| prover_context.compute_cells_and_kzg_proofs(blob))
});
for num_threads in THREAD_COUNTS {
let verifier_context = VerifierContext::with_num_threads(&trusted_setup, num_threads);
c.bench_function(
&format!("verify_cell_kzg_proof - NUM_THREADS: {}", num_threads),
|b| {
b.iter(|| {
verifier_context.verify_cell_kzg_proof(&commitment, 0, &cells[0], &proofs[0])
})
},
);
}
}

criterion_group!(benches, bench_compute_cells_and_kzg_proofs);
criterion_group!(
benches,
bench_compute_cells_and_kzg_proofs,
bench_recover_cells_and_compute_kzg_proofs,
bench_verify_cell_kzg_proofs
);
criterion_main!(benches);

0 comments on commit 9e654bc

Please sign in to comment.