2023-01-12 17:50:12 +00:00
|
|
|
use c_kzg::*;
|
2023-03-29 06:59:52 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
|
2023-01-12 17:50:12 +00:00
|
|
|
use rand::{rngs::ThreadRng, Rng};
|
2023-09-05 22:06:03 +00:00
|
|
|
use std::path::Path;
|
2023-01-12 17:50:12 +00:00
|
|
|
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[..]);
|
2023-05-24 13:44:05 +00:00
|
|
|
arr[0] = 0;
|
2023-02-22 16:33:15 +00:00
|
|
|
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 {
|
2023-05-24 13:44:05 +00:00
|
|
|
arr[i * BYTES_PER_FIELD_ELEMENT] = 0;
|
2023-01-12 17:50:12 +00:00
|
|
|
}
|
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();
|
2024-03-15 21:49:33 +00:00
|
|
|
let trusted_setup_file = Path::new("src/trusted_setup.txt");
|
2023-01-12 17:50:12 +00:00
|
|
|
assert!(trusted_setup_file.exists());
|
2023-03-29 06:59:52 +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| {
|
2023-09-05 21:43:46 +00:00
|
|
|
KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings)
|
2023-03-09 16:30:46 +00:00
|
|
|
.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)| {
|
2023-09-05 21:43:46 +00:00
|
|
|
KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings)
|
2023-03-09 16:30:46 +00:00
|
|
|
.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-09-05 21:43:46 +00:00
|
|
|
b.iter(|| KzgCommitment::blob_to_kzg_commitment(&blobs[0], &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| {
|
2023-09-05 21:43:46 +00:00
|
|
|
b.iter(|| KzgProof::compute_kzg_proof(&blobs[0], &fields[0], &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("compute_blob_kzg_proof", |b| {
|
2023-09-05 21:43:46 +00:00
|
|
|
b.iter(|| KzgProof::compute_blob_kzg_proof(&blobs[0], &commitments[0], &kzg_settings))
|
2023-02-22 16:33:15 +00:00
|
|
|
});
|
2023-01-12 17:50:12 +00:00
|
|
|
|
2023-03-29 06:59:52 +00:00
|
|
|
c.bench_function("verify_kzg_proof", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
KzgProof::verify_kzg_proof(
|
2023-09-05 21:43:46 +00:00
|
|
|
&commitments[0],
|
|
|
|
&fields[0],
|
|
|
|
&fields[0],
|
|
|
|
&proofs[0],
|
2023-03-29 06:59:52 +00:00
|
|
|
&kzg_settings,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2023-02-22 16:33:15 +00:00
|
|
|
c.bench_function("verify_blob_kzg_proof", |b| {
|
|
|
|
b.iter(|| {
|
2023-09-05 21:43:46 +00:00
|
|
|
KzgProof::verify_blob_kzg_proof(&blobs[0], &commitments[0], &proofs[0], &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
|
|
|
let mut group = c.benchmark_group("verify_blob_kzg_proof_batch");
|
|
|
|
for count in [1, 2, 4, 8, 16, 32, 64] {
|
2023-09-05 22:06:03 +00:00
|
|
|
assert!(count <= max_count);
|
2023-03-29 06:59:52 +00:00
|
|
|
group.throughput(Throughput::Elements(count as u64));
|
2023-02-22 16:33:15 +00:00
|
|
|
group.bench_with_input(BenchmarkId::from_parameter(count), &count, |b, &count| {
|
2023-03-29 06:59:52 +00:00
|
|
|
b.iter_batched_ref(
|
|
|
|
|| {
|
2023-09-05 22:06:03 +00:00
|
|
|
let blobs_subset = blobs[..count].to_vec();
|
|
|
|
let commitments_subset = commitments[..count].to_vec();
|
|
|
|
let proofs_subset = proofs[..count].to_vec();
|
2023-03-29 06:59:52 +00:00
|
|
|
(blobs_subset, commitments_subset, proofs_subset)
|
|
|
|
},
|
|
|
|
|(blobs_subset, commitments_subset, proofs_subset)| {
|
|
|
|
KzgProof::verify_blob_kzg_proof_batch(
|
2023-09-05 22:06:03 +00:00
|
|
|
blobs_subset,
|
|
|
|
commitments_subset,
|
|
|
|
proofs_subset,
|
2023-03-29 06:59:52 +00:00
|
|
|
&kzg_settings,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
},
|
|
|
|
BatchSize::LargeInput,
|
|
|
|
);
|
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);
|