2023-01-12 17:50:12 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use c_kzg::*;
|
|
|
|
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
|
|
|
|
use rand::{rngs::ThreadRng, Rng};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
fn generate_random_blob_for_bench(rng: &mut ThreadRng) -> Blob {
|
2023-01-16 20:05:23 +00:00
|
|
|
let mut arr = [0u8; BYTES_PER_BLOB];
|
2023-01-12 17:50:12 +00:00
|
|
|
rng.fill(&mut arr[..]);
|
|
|
|
// Ensure that the blob is canonical by ensuring that
|
|
|
|
// each field element contained in the blob is < BLS_MODULUS
|
|
|
|
for i in 0..FIELD_ELEMENTS_PER_BLOB {
|
|
|
|
arr[i * BYTES_PER_FIELD_ELEMENT + BYTES_PER_FIELD_ELEMENT - 1] = 0;
|
|
|
|
}
|
2023-01-16 20:05:23 +00:00
|
|
|
arr.into()
|
2023-01-12 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let trusted_setup_file = PathBuf::from("../../src/trusted_setup.txt");
|
|
|
|
assert!(trusted_setup_file.exists());
|
2023-01-16 20:05:23 +00:00
|
|
|
let kzg_settings = Arc::new(KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap());
|
2023-01-12 17:50:12 +00:00
|
|
|
|
|
|
|
let blob = generate_random_blob_for_bench(&mut rng);
|
|
|
|
c.bench_function("blob_to_kzg_commitment", |b| {
|
2023-01-16 20:05:23 +00:00
|
|
|
b.iter(|| KZGCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
|
2023-01-12 17:50:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for num_blobs in [4, 8, 16].iter() {
|
|
|
|
let mut group = c.benchmark_group("kzg operations");
|
|
|
|
|
|
|
|
let blobs: Vec<Blob> = (0..*num_blobs)
|
|
|
|
.map(|_| generate_random_blob_for_bench(&mut rng))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
group.bench_with_input(
|
|
|
|
BenchmarkId::new("compute_aggregate_kzg_proof", *num_blobs),
|
|
|
|
&blobs,
|
2023-01-16 20:05:23 +00:00
|
|
|
|b, blobs| b.iter(|| KZGProof::compute_aggregate_kzg_proof(blobs, &kzg_settings)),
|
2023-01-12 17:50:12 +00:00
|
|
|
);
|
|
|
|
|
2023-02-13 07:14:09 +00:00
|
|
|
let kzg_commitments: Vec<Bytes48> = blobs
|
2023-01-12 17:50:12 +00:00
|
|
|
.clone()
|
|
|
|
.into_iter()
|
2023-02-13 07:14:09 +00:00
|
|
|
.map(|blob| KZGCommitment::blob_to_kzg_commitment(blob, &kzg_settings).to_bytes())
|
2023-01-12 17:50:12 +00:00
|
|
|
.collect();
|
2023-01-16 20:05:23 +00:00
|
|
|
let proof = KZGProof::compute_aggregate_kzg_proof(&blobs, &kzg_settings).unwrap();
|
2023-01-12 17:50:12 +00:00
|
|
|
|
|
|
|
group.bench_with_input(
|
|
|
|
BenchmarkId::new("verify_aggregate_kzg_proof", *num_blobs),
|
|
|
|
&blobs,
|
|
|
|
|b, blobs| {
|
|
|
|
b.iter(|| {
|
|
|
|
proof
|
2023-02-13 07:14:09 +00:00
|
|
|
.verify_aggregate_kzg_proof(
|
|
|
|
&blobs,
|
|
|
|
kzg_commitments.as_slice(),
|
|
|
|
&kzg_settings,
|
|
|
|
)
|
2023-01-12 17:50:12 +00:00
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|