Skip to content

Commit

Permalink
Merge branch 'validity-checks' into verify-simple-tdec-shares
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 30, 2023
2 parents d499c28 + dd9e458 commit a34b995
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 132 deletions.
12 changes: 9 additions & 3 deletions ferveo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@ version = "0.10.0"
features = ["alloc"]

[dev-dependencies]
criterion = "0.3"
criterion = "0.3" # supports pprof, # TODO: Figure out if/how we can update to 0.4
pprof = { version = "0.6", features = ["flamegraph", "criterion"] }

[[example]]
name = "pvdkg"
path = "examples/pvdkg.rs"

#[[bench]]
#name = "pvdkg"
#path = "benches/benchmarks/pvdkg.rs"
#harness = false

[[bench]]
name = "pvdkg"
path = "benches/benchmarks/pvdkg.rs"
name = "benchmarks"
path = "benches/bench_main.rs"
harness = false


[profile.release]
opt-level = 3
lto = true
5 changes: 3 additions & 2 deletions ferveo/benches/bench_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use criterion::criterion_main;
mod benchmarks;

criterion_main! {
benchmarks::pairing::micro,//bench_batch_inverse,
benchmarks::pairing::ec,
// benchmarks::pairing::micro,//bench_batch_inverse,
// benchmarks::pairing::ec,
benchmarks::validity_checks::validity_checks,
}
3 changes: 2 additions & 1 deletion ferveo/benches/benchmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//pub mod block_proposer;
pub mod pairing;
// pub mod pairing;
pub mod validity_checks;
5 changes: 3 additions & 2 deletions ferveo/benches/benchmarks/pvdkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ pub fn setup_dkg(
let keypairs = gen_keypairs(num);
let validators = gen_validators(&keypairs);
let me = validators[validator].clone();
let shares_num = 300;
PubliclyVerifiableDkg::new(
validators,
Params {
tau: 0,
security_threshold: 300 / 3,
shares_num: 300,
security_threshold: shares_num / 3,
shares_num,
retry_after: 2,
},
&me,
Expand Down
109 changes: 109 additions & 0 deletions ferveo/benches/benchmarks/validity_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#![allow(clippy::redundant_closure)]
#![allow(clippy::unit_arg)]

use ark_bls12_381::Bls12_381;
pub use ark_bls12_381::Bls12_381 as EllipticCurve;
use criterion::{black_box, criterion_group, BenchmarkId, Criterion};
use digest::crypto_common::rand_core::SeedableRng;
use ferveo::*;
use ferveo_common::ExternalValidator;
use rand::prelude::StdRng;

const NUM_SHARES_CASES: [usize; 5] = [4, 8, 16, 32, 64];

// TODO: Can we expose ferveo test methods to reuse `setup_dkg` et al instead of reimplementing it here?

fn gen_keypairs(num: u32) -> Vec<ferveo_common::Keypair<EllipticCurve>> {
let rng = &mut ark_std::test_rng();
(0..num)
.map(|_| ferveo_common::Keypair::<EllipticCurve>::new(rng))
.collect()
}

fn gen_validators(
keypairs: &[ferveo_common::Keypair<EllipticCurve>],
) -> Vec<ExternalValidator<EllipticCurve>> {
(0..keypairs.len())
.map(|i| ExternalValidator {
address: format!("validator_{}", i),
public_key: keypairs[i].public(),
})
.collect()
}

fn setup_dkg(
validator: usize,
shares_num: u32,
) -> PubliclyVerifiableDkg<EllipticCurve> {
let keypairs = gen_keypairs(shares_num);
let validators = gen_validators(&keypairs);
let me = validators[validator].clone();
PubliclyVerifiableDkg::new(
validators,
Params {
tau: 0,
security_threshold: shares_num / 3,
shares_num,
retry_after: 1,
},
&me,
keypairs[validator],
)
.expect("Setup failed")
}

fn setup(
shares_num: u32,
rng: &mut StdRng,
) -> (PubliclyVerifiableDkg<Bls12_381>, Message<Bls12_381>) {
let mut transcripts = vec![];
for i in 0..shares_num {
let mut dkg = setup_dkg(i as usize, shares_num);
transcripts.push(dkg.share(rng).expect("Test failed"));
}
let dkg = setup_dkg(0, shares_num);
let transcript = transcripts[0].clone();
(dkg, transcript)
}

pub fn bench_verify_full(c: &mut Criterion) {
let mut group = c.benchmark_group("PVSS VALIDITY CHECKS");
group.sample_size(10);

let rng = &mut StdRng::seed_from_u64(0);

for shares_num in NUM_SHARES_CASES {
let (dkg, transcript) = setup(shares_num as u32, rng);
let transcript = &transcript;

let pvss_verify_optimistic = {
move || {
if let Message::Deal(ss) = transcript {
black_box(ss.verify_optimistic());
} else {
panic!("Expected Deal");
}
}
};
let pvss_verify_full = {
move || {
if let Message::Deal(ss) = transcript {
black_box(ss.verify_full(&dkg));
} else {
panic!("Expected Deal");
}
}
};

group.bench_function(
BenchmarkId::new("pvss_verify_optimistic", shares_num),
|b| b.iter(|| pvss_verify_optimistic()),
);
group.bench_function(
BenchmarkId::new("pvss_verify_full", shares_num),
|b| b.iter(|| pvss_verify_full()),
);
}
}

criterion_group!(validity_checks, bench_verify_full);
1 change: 0 additions & 1 deletion ferveo/src/dkg/pv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ pub(crate) mod test_common {
my_index: usize,
) -> PubliclyVerifiableDkg<EllipticCurve> {
let keypairs = gen_n_keypairs(shares_num);
for _keypair in &keypairs {}
let validators = gen_n_validators(&keypairs, shares_num);
let me = validators[my_index].clone();
PubliclyVerifiableDkg::new(
Expand Down
1 change: 1 addition & 0 deletions tpke-wasm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
Cargo.lock
bin/
pkg
!pkg/.gitignore
wasm-pack.log
4 changes: 2 additions & 2 deletions tpke-wasm/benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::redundant_closure)]

use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn bench_encrypt_combine(c: &mut Criterion) {
Expand Down Expand Up @@ -55,14 +57,12 @@ pub fn bench_encrypt_combine(c: &mut Criterion) {
let encrypt_fn = bench_encrypt(*num_shares, *num_shares);
group.measurement_time(core::time::Duration::new(45, 0));
group.bench_function(format!("tpke-wasm::encrypt - num_shares={}, num_entities={}, threshold={}", num_shares, num_shares, num_shares), |b| {
#[allow(clippy::redundant_closure)]
b.iter(|| encrypt_fn())
});

let combine_fn = bench_combine(*num_shares, *num_shares);
group.measurement_time(core::time::Duration::new(45, 0));
group.bench_function(format!("tpke-wasm::combine - num_shares={}, num_entities={}, threshold={}", num_shares, num_shares, num_shares), |b| {
#[allow(clippy::redundant_closure)]
b.iter(|| combine_fn())
});
}
Expand Down
10 changes: 2 additions & 8 deletions tpke/benches/arkworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,11 @@ pub fn bench_random_poly(c: &mut Criterion) {
};
group.bench_function(
BenchmarkId::new("random_polynomial_ark", threshold),
|b| {
#[allow(clippy::redundant_closure)]
b.iter(|| ark())
},
|b| b.iter(|| ark()),
);
group.bench_function(
BenchmarkId::new("random_polynomial_naive", threshold),
|b| {
#[allow(clippy::redundant_closure)]
b.iter(|| naive())
},
|b| b.iter(|| naive()),
);
}
}
Expand Down
Loading

0 comments on commit a34b995

Please sign in to comment.