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;
|
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
fn generate_random_field_element(rng: &mut ThreadRng) -> Bytes32 {
|
|
|
|
let mut arr = [0u8; BYTES_PER_FIELD_ELEMENT];
|
|
|
|
rng.fill(&mut arr[..]);
|
|
|
|
arr[BYTES_PER_FIELD_ELEMENT - 1] = 0;
|
|
|
|
arr.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_random_blob(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) {
|
2023-02-22 16:33:15 +00:00
|
|
|
let max_count: usize = 64;
|
2023-01-12 17:50:12 +00:00
|
|
|
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
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
let blobs: Vec<Blob> = (0..max_count)
|
|
|
|
.map(|_| generate_random_blob(&mut rng))
|
|
|
|
.collect();
|
2023-03-09 16:30:46 +00:00
|
|
|
let commitments: Vec<Bytes48> = blobs
|
|
|
|
.iter()
|
|
|
|
.map(|blob| {
|
|
|
|
KZGCommitment::blob_to_kzg_commitment(*blob, &kzg_settings)
|
|
|
|
.unwrap()
|
|
|
|
.to_bytes()
|
|
|
|
})
|
2023-02-22 16:33:15 +00:00
|
|
|
.collect();
|
2023-03-09 16:30:46 +00:00
|
|
|
let proofs: Vec<Bytes48> = blobs
|
|
|
|
.iter()
|
|
|
|
.zip(commitments.iter())
|
|
|
|
.map(|(blob, commitment)| {
|
|
|
|
KZGProof::compute_blob_kzg_proof(*blob, *commitment, &kzg_settings)
|
|
|
|
.unwrap()
|
|
|
|
.to_bytes()
|
|
|
|
})
|
2023-02-22 16:33:15 +00:00
|
|
|
.collect();
|
|
|
|
let fields: Vec<Bytes32> = (0..max_count)
|
|
|
|
.map(|_| generate_random_field_element(&mut rng))
|
|
|
|
.collect();
|
|
|
|
|
2023-01-12 17:50:12 +00:00
|
|
|
c.bench_function("blob_to_kzg_commitment", |b| {
|
2023-02-22 16:33:15 +00:00
|
|
|
b.iter(|| KZGCommitment::blob_to_kzg_commitment(*blobs.first().unwrap(), &kzg_settings))
|
2023-01-12 17:50:12 +00:00
|
|
|
});
|
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
c.bench_function("compute_kzg_proof", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
KZGProof::compute_kzg_proof(
|
|
|
|
*blobs.first().unwrap(),
|
|
|
|
*fields.first().unwrap(),
|
|
|
|
&kzg_settings,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
2023-01-12 17:50:12 +00:00
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
c.bench_function("compute_blob_kzg_proof", |b| {
|
2023-03-09 16:30:46 +00:00
|
|
|
b.iter(|| {
|
|
|
|
KZGProof::compute_blob_kzg_proof(
|
|
|
|
*blobs.first().unwrap(),
|
|
|
|
*commitments.first().unwrap(),
|
|
|
|
&kzg_settings,
|
|
|
|
)
|
|
|
|
})
|
2023-02-22 16:33:15 +00:00
|
|
|
});
|
2023-01-12 17:50:12 +00:00
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
c.bench_function("verify_blob_kzg_proof", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
KZGProof::verify_blob_kzg_proof(
|
|
|
|
*blobs.first().unwrap(),
|
|
|
|
*commitments.first().unwrap(),
|
|
|
|
*proofs.first().unwrap(),
|
|
|
|
&kzg_settings,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
2023-01-12 17:50:12 +00:00
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
let mut group = c.benchmark_group("verify_blob_kzg_proof_batch");
|
|
|
|
for count in [1, 2, 4, 8, 16, 32, 64] {
|
|
|
|
group.bench_with_input(BenchmarkId::from_parameter(count), &count, |b, &count| {
|
|
|
|
b.iter(|| {
|
|
|
|
KZGProof::verify_blob_kzg_proof_batch(
|
|
|
|
&blobs.clone().into_iter().take(count).collect::<Vec<Blob>>(),
|
|
|
|
&commitments
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.take(count)
|
|
|
|
.collect::<Vec<Bytes48>>(),
|
|
|
|
&proofs
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.take(count)
|
|
|
|
.collect::<Vec<Bytes48>>(),
|
|
|
|
&kzg_settings,
|
2023-03-03 23:01:56 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-02-22 16:33:15 +00:00
|
|
|
})
|
|
|
|
});
|
2023-01-12 17:50:12 +00:00
|
|
|
}
|
2023-02-22 16:33:15 +00:00
|
|
|
group.finish();
|
2023-01-12 17:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|