c-kzg-4844/bindings/rust/benches/kzg_benches.rs
Justin Traglia 69f6155d75
Bytes-only interface (#62)
* Convert argument types to bytes

* Update java bindings

* Update python bindings

* Update node.js bindings

* Update c# bindings

* Fix java binding compile issues

* Fix incorrect memcpy in nodejs bindings

* Fix bug (called the wrong func)

* Fix issues with java bindings

* Fix issues with node.js bindings

* Remove unnecessary wrapped funcs for c#

* Rename struct member to bytes

* Use goto out for callocs

* Fix nit

* Make un-exported funcs static

* Fix python bindings

* Check commitment length in python bindings

* Update python error message

* Steal good ideas from #37

* Fix tests.py which didn't get copied over

* Convert remaining a[] to *a

* Add missing Py_DECREF

* Bytes only rust (#1)

* Make interface bytes only
* Fix benches
* Avoid newtypes for kzg types
* Fix benches again
* Make fields private
* tidy
* Address review comments

* Fix one small thing in rust bindings

* Use ckzg types where possible

* Remove null terminator from domain bytes in rust

* Update rust binding docs

* Use BYTES_PER_* where applicable

* Add extra check for calloc

Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com>
2023-01-16 20:05:23 +00:00

66 lines
2.2 KiB
Rust

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 {
let mut arr = [0u8; BYTES_PER_BLOB];
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;
}
arr.into()
}
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());
let kzg_settings = Arc::new(KZGSettings::load_trusted_setup_file(trusted_setup_file).unwrap());
let blob = generate_random_blob_for_bench(&mut rng);
c.bench_function("blob_to_kzg_commitment", |b| {
b.iter(|| KZGCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
});
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,
|b, blobs| b.iter(|| KZGProof::compute_aggregate_kzg_proof(blobs, &kzg_settings)),
);
let kzg_commitments: Vec<KZGCommitment> = blobs
.clone()
.into_iter()
.map(|blob| KZGCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
.collect();
let proof = KZGProof::compute_aggregate_kzg_proof(&blobs, &kzg_settings).unwrap();
group.bench_with_input(
BenchmarkId::new("verify_aggregate_kzg_proof", *num_blobs),
&blobs,
|b, blobs| {
b.iter(|| {
proof
.verify_aggregate_kzg_proof(&blobs, &kzg_commitments, &kzg_settings)
.unwrap()
})
},
);
}
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);