Skip to content

Commit

Permalink
more benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
anupsv committed May 24, 2024
1 parent 1918e2a commit 3f49927
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ harness = false
name = "bench_kzg_commit"
harness = false

[[bench]]
name = "bench_kzg_proof"
harness = false

[[bench]]
name = "bench_kzg_verify"
harness = false

[profile.bench]
opt-level = 3
debug = true
Expand Down
6 changes: 3 additions & 3 deletions benches/bench_g1_ifft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ fn generate_powers_of_2(limit: u64) -> Vec<usize> {
fn bench_g1_ifft(c: &mut Criterion) {
c.bench_function("bench_g1_ifft", |b| {
let kzg = Kzg::setup(
"src/test-files/g1.point",
"src/test-files/g2.point",
"src/test-files/g2.point.powerOf2",
"src/test-files/mainnet-data/g1.131072.point",
"",
"src/test-files/mainnet-data/g2.point.powerOf2",
3000,
3000
).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_kzg_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rust_kzg_bn254::{blob::Blob, kzg::Kzg};
fn bench_kzg_commit(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let mut kzg = Kzg::setup(
"src/test-files/mainnet-data/g1.point",
"src/test-files/mainnet-data/g1.131072.point",
"",
"src/test-files/mainnet-data/g2.point.powerOf2",
268435456,
Expand Down
63 changes: 63 additions & 0 deletions benches/bench_kzg_proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::time::Duration;
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg};

fn bench_kzg_proof(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let mut kzg = Kzg::setup(
"src/test-files/mainnet-data/g1.131072.point",
"",
"src/test-files/mainnet-data/g2.point.powerOf2",
268435456,
131072
).unwrap();

c.bench_function("bench_kzg_proof_10000", |b| {
let random_blob: Vec<u8> = (0..10000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap()).unwrap();
let index = rand::thread_rng().gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
b.iter(|| {
kzg.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap()).unwrap()
});
});

c.bench_function("bench_kzg_proof_30000", |b| {
let random_blob: Vec<u8> = (0..30000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap()).unwrap();
let index = rand::thread_rng().gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
b.iter(|| {
kzg.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap()).unwrap()
});
});

c.bench_function("bench_kzg_proof_50000", |b| {
let random_blob: Vec<u8> = (0..50000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap()).unwrap();
let index = rand::thread_rng().gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
b.iter(|| {
kzg.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap()).unwrap()
});
});
}

fn criterion_config() -> Criterion {
Criterion::default()
.warm_up_time(Duration::from_secs(5)) // Warm-up time
.measurement_time(Duration::from_secs(10)) // Measurement time
.sample_size(10) // Number of samples to take
}


criterion_group!(
name = benches;
config = criterion_config();
targets = bench_kzg_proof
);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion benches/bench_kzg_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn bench_kzg_setup(c: &mut Criterion) {

b.iter(|| {
Kzg::setup(
"src/test-files/mainnet-data/g1.point",
"src/test-files/mainnet-data/g1.131072.point",
"",
"src/test-files/mainnet-data/g2.point.powerOf2",
268435456,
Expand Down
75 changes: 75 additions & 0 deletions benches/bench_kzg_verify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::time::Duration;
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg};

fn bench_kzg_verify(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let mut kzg = Kzg::setup(
"src/test-files/mainnet-data/g1.131072.point",
"",
"src/test-files/mainnet-data/g2.point.powerOf2",
268435456,
131072
).unwrap();

c.bench_function("bench_kzg_verify_10000", |b| {
let random_blob: Vec<u8> = (0..10000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap()).unwrap();
let index = rand::thread_rng().gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
let commitment = kzg.commit(&input_poly.clone()).unwrap();
let proof = kzg.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap()).unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
b.iter(|| {
kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone())
});
});

c.bench_function("bench_kzg_verify_30000", |b| {
let random_blob: Vec<u8> = (0..30000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap()).unwrap();
let index = rand::thread_rng().gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
let commitment = kzg.commit(&input_poly.clone()).unwrap();
let proof = kzg.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap()).unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
b.iter(|| {
kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone())
});
});

c.bench_function("bench_kzg_verify_50000", |b| {
let random_blob: Vec<u8> = (0..50000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap()).unwrap();
let index = rand::thread_rng().gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
let commitment = kzg.commit(&input_poly.clone()).unwrap();
let proof = kzg.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap()).unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
b.iter(|| {
kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone())
});
});
}

fn criterion_config() -> Criterion {
Criterion::default()
.warm_up_time(Duration::from_secs(5)) // Warm-up time
.measurement_time(Duration::from_secs(10)) // Measurement time
.sample_size(10) // Number of samples to take
}


criterion_group!(
name = benches;
config = criterion_config();
targets = bench_kzg_verify
);
criterion_main!(benches);

0 comments on commit 3f49927

Please sign in to comment.