Skip to content

Commit

Permalink
added benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
10d9e committed Nov 2, 2023
1 parent bdb9b7e commit fc33a4a
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ sha2 = "0.10.8"

[build-dependencies]
tonic-build = "0.10.2"

[dev-dependencies]
criterion = "0.3"
75 changes: 75 additions & 0 deletions benches/byte_convertible_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use num_bigint::BigUint;
use zk_pass::conversion::ByteConvertible;
use curve25519_dalek::Scalar;
use curve25519_dalek::RistrettoPoint;
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use num_bigint::ToBigUint;
use rand::rngs::OsRng;

fn bench_biguint_serialization(c: &mut Criterion) {
let biguint = 123456789u64.to_biguint().unwrap();
c.bench_function("BigUint Serialization", |b| {
b.iter(|| {
black_box(BigUint::to_bytes(&biguint));
});
});
}

fn bench_biguint_deserialization(c: &mut Criterion) {
let bytes = BigUint::to_bytes(&123456789u64.to_biguint().unwrap());
c.bench_function("BigUint Deserialization", |b| {
b.iter(|| {
black_box(BigUint::from_bytes(&bytes).unwrap());
});
});
}

fn bench_ristretto_point_serialization(c: &mut Criterion) {
let point = RISTRETTO_BASEPOINT_POINT * Scalar::random(&mut OsRng);
c.bench_function("RistrettoPoint Serialization", |b| {
b.iter(|| {
black_box(RistrettoPoint::to_bytes(&point));
});
});
}

fn bench_ristretto_point_deserialization(c: &mut Criterion) {
let point = RISTRETTO_BASEPOINT_POINT * Scalar::random(&mut OsRng);
let bytes = RistrettoPoint::to_bytes(&point);
c.bench_function("RistrettoPoint Deserialization", |b| {
b.iter(|| {
black_box(RistrettoPoint::from_bytes(&bytes).unwrap());
});
});
}

fn bench_scalar_serialization(c: &mut Criterion) {
let scalar = Scalar::random(&mut OsRng);
c.bench_function("Scalar Serialization", |b| {
b.iter(|| {
black_box(Scalar::to_bytes(&scalar));
});
});
}

fn bench_scalar_deserialization(c: &mut Criterion) {
let scalar = Scalar::random(&mut OsRng);
let bytes = Scalar::to_bytes(&scalar);
c.bench_function("Scalar Deserialization", |b| {
b.iter(|| {
black_box(Scalar::from_bytes(&bytes).unwrap());
});
});
}

criterion_group!(
benches,
bench_biguint_serialization,
bench_biguint_deserialization,
bench_ristretto_point_serialization,
bench_ristretto_point_deserialization,
bench_scalar_serialization,
bench_scalar_deserialization,
);
criterion_main!(benches);
79 changes: 79 additions & 0 deletions benches/chaum_pedersen_discrete_log_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use zk_pass::chaum_pedersen::discretelog::DiscreteLogChaumPedersen;
use zk_pass::chaum_pedersen::ChaumPedersen;
use zk_pass::chaum_pedersen::GroupParams;
use num_bigint::{BigUint, RandBigInt};
use zk_pass::chaum_pedersen::test::test_execute_protocol;
use rand::rngs::OsRng;
use zk_pass::chaum_pedersen::constants::{RFC5114_MODP_1024_160_BIT_PARAMS, RFC5114_MODP_2048_224_BIT_PARAMS, RFC5114_MODP_2048_256_BIT_PARAMS};

fn discrete_log_commitment_benchmark(c: &mut Criterion) {
let g = BigUint::from(4u32);
let h = BigUint::from(9u32);
let p = BigUint::from(23u32);
let q = BigUint::from(11u32);
let x = BigUint::from(3u32);

let params = GroupParams::<BigUint> {
g: g.clone(),
h: h.clone(),
p: p.clone(),
q: q.clone(),
};

c.bench_function("discrete_log_commitment", |b| {
b.iter(|| {
let _result = DiscreteLogChaumPedersen::commitment(&params, &x);
})
});
}

fn discrete_log_verification_benchmark(c: &mut Criterion) {
let g = BigUint::from(4u32);
let h = BigUint::from(9u32);
let p = BigUint::from(23u32);
let q = BigUint::from(11u32);
let x = BigUint::from(3u32);

let params = GroupParams::<BigUint> {
g: g.clone(),
h: h.clone(),
p: p.clone(),
q: q.clone(),
};

let (cp, _k) = DiscreteLogChaumPedersen::commitment(&params, &x);

c.bench_function("discrete_log_verification", |b| {
b.iter(|| {
let _result = DiscreteLogChaumPedersen::verify(&params, &black_box(BigUint::from(10u32)), &black_box(BigUint::from(0u32)), &cp);
})
});
}

fn bench_protocol(params: &GroupParams<BigUint>, c: &mut Criterion, label: &str) {
let mut rng = OsRng;
let x = rng.gen_biguint_below(&params.p);

c.bench_function(&format!("discrete_log_protocol_{}", label), |b| {
b.iter(|| test_execute_protocol::<DiscreteLogChaumPedersen>(&params, &x))
});
}

fn bench_1024_160_bits(c: &mut Criterion) {
let params = RFC5114_MODP_1024_160_BIT_PARAMS.to_owned();
bench_protocol(&params, c, "1024_160");
}

fn bench_2048_224_bits(c: &mut Criterion) {
let params = RFC5114_MODP_2048_224_BIT_PARAMS.to_owned();
bench_protocol(&params, c, "2048_224");
}

fn bench_2048_256_bits(c: &mut Criterion) {
let params = RFC5114_MODP_2048_256_BIT_PARAMS.to_owned();
bench_protocol(&params, c, "2048_256");
}

criterion_group!(benches, discrete_log_commitment_benchmark, discrete_log_verification_benchmark, bench_1024_160_bits, bench_2048_224_bits, bench_2048_256_bits);
criterion_main!(benches);
45 changes: 45 additions & 0 deletions benches/chaum_pedersen_ec_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use curve25519_dalek::{constants::RISTRETTO_BASEPOINT_POINT, scalar::Scalar};
use zk_pass::chaum_pedersen::GroupParams;
use curve25519_dalek::RistrettoPoint;
use zk_pass::chaum_pedersen::curve25519::EllipticCurveChaumPedersen;
use zk_pass::chaum_pedersen::ChaumPedersen;
use rand::rngs::OsRng;

pub fn elliptic_curve_commitment_benchmark(c: &mut Criterion) {
c.bench_function("elliptic_curve_commitment", |b| {
let mut rng = OsRng;
let x = Scalar::from(3u32);
let g = RISTRETTO_BASEPOINT_POINT * Scalar::random(&mut rng);
let h = RISTRETTO_BASEPOINT_POINT * Scalar::random(&mut rng);
let params = GroupParams::<RistrettoPoint> {
g: g.clone(),
h: h.clone(),
p: RISTRETTO_BASEPOINT_POINT,
q: RISTRETTO_BASEPOINT_POINT,
};
b.iter(|| EllipticCurveChaumPedersen::commitment(black_box(&params), black_box(&x)));
});
}

pub fn elliptic_curve_challenge_benchmark(c: &mut Criterion) {
c.bench_function("elliptic_curve_challenge", |b| {
let params = GroupParams::<RistrettoPoint> {
g: RISTRETTO_BASEPOINT_POINT,
h: RISTRETTO_BASEPOINT_POINT,
p: RISTRETTO_BASEPOINT_POINT,
q: RISTRETTO_BASEPOINT_POINT,
};
b.iter(|| EllipticCurveChaumPedersen::challenge(black_box(&params)));
});
}

// Add more benchmarks here following the same pattern...

criterion_group!(
benches,
elliptic_curve_commitment_benchmark,
elliptic_curve_challenge_benchmark,
// Add more benchmarks to the group...
);
criterion_main!(benches);
37 changes: 37 additions & 0 deletions benches/random_generator_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use num_bigint::BigUint;
use zk_pass::rand::RandomGenerator;
use curve25519_dalek::Scalar;
use curve25519_dalek::RistrettoPoint;

fn bench_biguint_random_generation(c: &mut Criterion) {
c.bench_function("BigUint Random Generation", |b| {
b.iter(|| {
let _ = black_box(BigUint::generate_random().unwrap());
});
});
}

fn bench_scalar_random_generation(c: &mut Criterion) {
c.bench_function("Scalar Random Generation", |b| {
b.iter(|| {
let _ = black_box(Scalar::generate_random().unwrap());
});
});
}

fn bench_ristretto_point_random_generation(c: &mut Criterion) {
c.bench_function("RistrettoPoint Random Generation", |b| {
b.iter(|| {
let _ = black_box(RistrettoPoint::generate_random().unwrap());
});
});
}

criterion_group!(
benches,
bench_biguint_random_generation,
bench_scalar_random_generation,
bench_ristretto_point_random_generation,
);
criterion_main!(benches);

0 comments on commit fc33a4a

Please sign in to comment.