diff --git a/caledonia/benches/bounded_repetition.rs b/caledonia/benches/bounded_repetition.rs index 57ec829..d4ff979 100644 --- a/caledonia/benches/bounded_repetition.rs +++ b/caledonia/benches/bounded_repetition.rs @@ -5,7 +5,7 @@ use std::time::Duration; use caledonia::bounded::Proof; -mod utils; +pub mod utils; fn prepetitions( c: &mut Criterion, @@ -14,14 +14,14 @@ fn prepetitions( n_p: &[usize], _hash_size: usize, ) { - let mut group = c.benchmark_group("Alba".to_string()); + let mut group = c.benchmark_group("Alba Bounded".to_string()); fn prove_repetitions(l: usize, sp: usize, np: usize, truncate_size: usize, n: u64) -> u64 { let mut rng = ChaCha20Rng::from_entropy(); let mut total_repetitions = 0; for _ in 0..n { // Setup - let (mut dataset, bench_setup) = utils::setup_wrapper(&mut rng, l, sp, np); + let (mut dataset, bench_setup) = utils::setup_bounded_wrapper(&mut rng, l, sp, np); dataset.truncate(truncate_size); // Bench black_box({ diff --git a/caledonia/benches/bounded_step.rs b/caledonia/benches/bounded_step.rs index 24b93c9..f51e0ad 100644 --- a/caledonia/benches/bounded_step.rs +++ b/caledonia/benches/bounded_step.rs @@ -4,7 +4,7 @@ use rand_core::SeedableRng; use caledonia::bounded::Proof; -mod utils; +pub mod utils; fn psteps( c: &mut Criterion, @@ -13,14 +13,14 @@ fn psteps( n_p: &[usize], _hash_size: usize, ) { - let mut group = c.benchmark_group("Alba".to_string()); + let mut group = c.benchmark_group("Alba Bounded".to_string()); fn prove_steps(l: usize, sp: usize, np: usize, truncate_size: usize, n: u64) -> u64 { let mut rng = ChaCha20Rng::from_entropy(); let mut total_steps = 0; for _ in 0..n { // Setup - let (mut dataset, bench_setup) = utils::setup_wrapper(&mut rng, l, sp, np); + let (mut dataset, bench_setup) = utils::setup_bounded_wrapper(&mut rng, l, sp, np); dataset.truncate(truncate_size); // Bench black_box({ diff --git a/caledonia/benches/bounded_time.rs b/caledonia/benches/bounded_time.rs index e32f7dc..0b71507 100644 --- a/caledonia/benches/bounded_time.rs +++ b/caledonia/benches/bounded_time.rs @@ -5,17 +5,17 @@ use std::time::{Duration, Instant}; use caledonia::bounded::Proof; -mod utils; +pub mod utils; fn prove(c: &mut Criterion, lambdas: &[usize], s_p: &[usize], n_p: &[usize], _hash_size: usize) { - let mut group = c.benchmark_group("Alba".to_string()); + let mut group = c.benchmark_group("Alba Bounded".to_string()); fn prove_duration(l: usize, sp: usize, np: usize, truncate_size: usize, n: u64) -> Duration { let mut rng = ChaCha20Rng::from_entropy(); let mut total_duration = Duration::ZERO; for _ in 0..n { // Setup - let (mut dataset, bench_setup) = utils::setup_wrapper(&mut rng, l, sp, np); + let (mut dataset, bench_setup) = utils::setup_bounded_wrapper(&mut rng, l, sp, np); dataset.truncate(truncate_size); // Bench let start = Instant::now(); @@ -63,7 +63,7 @@ fn verify(c: &mut Criterion, lambdas: &[usize], s_p: &[usize], n_p: &[usize], _h let mut total_duration = Duration::ZERO; for _ in 0..n { // Setup - let (mut dataset, bench_setup) = utils::setup_wrapper(&mut rng, l, sp, np); + let (mut dataset, bench_setup) = utils::setup_bounded_wrapper(&mut rng, l, sp, np); dataset.truncate(truncate_size); // Prove let proof = Proof::prove(&bench_setup, &dataset); @@ -114,7 +114,7 @@ fn prove_benches(c: &mut Criterion) { } fn verify_benches(c: &mut Criterion) { - // verify(c, &[10], &[1000], &[60, 66, 80], 256); + verify(c, &[10], &[1000], &[60, 66, 80], 256); } criterion_group!(name = benches; diff --git a/caledonia/benches/decentralised_time.rs b/caledonia/benches/decentralised_time.rs new file mode 100644 index 0000000..30e5605 --- /dev/null +++ b/caledonia/benches/decentralised_time.rs @@ -0,0 +1,129 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use rand_chacha::ChaCha20Rng; +use rand_core::SeedableRng; +use std::time::{Duration, Instant}; + +use caledonia::decentralised::Proof; + +pub mod utils; + +fn prove(c: &mut Criterion, lambdas: &[usize], s_p: &[usize], n_p: &[usize], _hash_size: usize) { + let mut group = c.benchmark_group("Alba decentralised".to_string()); + + fn prove_duration(l: usize, sp: usize, np: usize, truncate_size: usize, n: u64) -> Duration { + let mut rng = ChaCha20Rng::from_entropy(); + let mut total_duration = Duration::ZERO; + for _ in 0..n { + // Setup + let (mut dataset, bench_setup) = + utils::setup_decentralised_wrapper(&mut rng, l, sp, np); + dataset.truncate(truncate_size); + // Bench + let start = Instant::now(); + black_box({ + Proof::prove(&bench_setup, &dataset); + }); + total_duration = total_duration.saturating_add(start.elapsed()); + } + total_duration + } + + for &l in lambdas { + for &sp in s_p { + for &np in n_p { + // Bench with all of Sp + let high = sp; + + // Bench with all of np% of Sp + let low = (high * np).div_ceil(100); + + // Bench with (100+np)/2 percent of Sp + let mean = (100 + np).div_ceil(2); + let mid = (high + low).div_ceil(2); + + group.bench_function(utils::bench_id("Proving time", np, l, sp, np), move |b| { + b.iter_custom(|n| prove_duration(l, sp, np, low, n)) + }); + group.bench_function(utils::bench_id("Proving time", mean, l, sp, np), move |b| { + b.iter_custom(|n| prove_duration(l, sp, np, mid, n)) + }); + group.bench_function(utils::bench_id("Proving time", 100, l, sp, np), move |b| { + b.iter_custom(|n| prove_duration(l, sp, np, high, n)) + }); + } + } + } + group.finish(); +} + +fn verify(c: &mut Criterion, lambdas: &[usize], s_p: &[usize], n_p: &[usize], _hash_size: usize) { + let mut group = c.benchmark_group("Alba decentralised".to_string()); + + fn verify_duration(l: usize, sp: usize, np: usize, truncate_size: usize, n: u64) -> Duration { + let mut rng = ChaCha20Rng::from_entropy(); + let mut total_duration = Duration::ZERO; + for _ in 0..n { + // Setup + let (mut dataset, bench_setup) = + utils::setup_decentralised_wrapper(&mut rng, l, sp, np); + dataset.truncate(truncate_size); + // Prove + let proof = Proof::prove(&bench_setup, &dataset); + // Bench + let start = Instant::now(); + black_box({ + Proof::verify(&bench_setup, proof); + }); + total_duration = total_duration.saturating_add(start.elapsed()); + } + total_duration + } + + for &l in lambdas { + for &sp in s_p { + for &np in n_p { + // Bench with all of Sp + let high = sp; + + // Bench with all of np% of Sp + let low = (high * np).div_ceil(100); + + // Bench with (100+np)/2 percent of Sp + let mean = (100 + np).div_ceil(2); + let mid = (high + low).div_ceil(2); + + group.bench_function( + utils::bench_id("Verification time", np, l, sp, np), + move |b| b.iter_custom(|n| verify_duration(l, sp, np, low, n)), + ); + group.bench_function( + utils::bench_id("Verification time", mean, l, sp, np), + move |b| b.iter_custom(|n| verify_duration(l, sp, np, mid, n)), + ); + group.bench_function( + utils::bench_id("Verification time", 100, l, sp, np), + move |b| b.iter_custom(|n| verify_duration(l, sp, np, high, n)), + ); + } + } + } + group.finish(); +} + +fn prove_benches(c: &mut Criterion) { + // prove(c, &[128], &[1_000, 5_000], &[60, 66, 80], 256); + prove(c, &[128], &[100_000], &[60, 66, 80], 256); +} + +fn verify_benches(c: &mut Criterion) { + verify(c, &[10], &[1_000], &[60, 66, 80], 256); +} + +criterion_group!(name = benches; + config = Criterion::default().nresamples(1000); + targets = + prove_benches, + verify_benches +); + +criterion_main!(benches); diff --git a/caledonia/benches/utils.rs b/caledonia/benches/utils.rs index ebe8f9f..bafb19d 100644 --- a/caledonia/benches/utils.rs +++ b/caledonia/benches/utils.rs @@ -2,19 +2,24 @@ use criterion::{ measurement::{Measurement, ValueFormatter}, BenchmarkId, Throughput, }; + use rand_chacha::ChaCha20Rng; -use rand_core::RngCore; +use rand_core::{RngCore, SeedableRng}; -use caledonia::bounded::{Params, Setup}; -use caledonia::utils::gen_items; +use caledonia::{ + utils::{gen_items, gen_weighted_items}, + weighted_decentralised::VerifiableData, +}; +use vrf_dalek::vrf::{PublicKey, SecretKey}; // Helper functions -pub fn setup_wrapper( +pub fn setup_bounded_wrapper( rng: &mut ChaCha20Rng, l: usize, sp: usize, np: usize, -) -> (Vec<[u8; 32]>, Setup) { +) -> (Vec<[u8; 32]>, caledonia::bounded::Setup) { + use caledonia::bounded::*; let seed_u32 = rng.next_u32(); let seed = seed_u32.to_ne_bytes().to_vec(); let dataset: Vec<[u8; 32]> = gen_items(seed, sp); @@ -27,6 +32,71 @@ pub fn setup_wrapper( (dataset, Setup::new(¶ms)) } +pub fn setup_decentralised_wrapper( + rng: &mut ChaCha20Rng, + l: usize, + sp: usize, + np: usize, +) -> (Vec<[u8; 32]>, caledonia::decentralised::Setup) { + use caledonia::decentralised::*; + let seed_u32 = rng.next_u32(); + let seed = seed_u32.to_ne_bytes().to_vec(); + let params = Params::new( + l, + l, + (np * sp).div_ceil(100), + ((100 - np) * sp).div_ceil(100), + ); + let dataset = gen_items(seed, sp) + .iter() + .filter_map(|&s| Proof::lottery(params.n_p, params.mu, s).then(|| s)) + .collect(); + (dataset, Setup::new(¶ms)) +} + +pub fn setup_weighted_wrapper( + rng: &mut ChaCha20Rng, + l: usize, + sp: usize, + voters: usize, + np: usize, +) -> ( + Vec, + caledonia::weighted_decentralised::Setup, +) { + use caledonia::weighted_decentralised::*; + let seed_u32 = rng.next_u32(); + let seed = seed_u32.to_ne_bytes().to_vec(); + let dataset = gen_weighted_items(seed, sp, voters); + let params = Params::new( + l, + l, + (np * sp).div_ceil(100), + ((100 - np) * sp).div_ceil(100), + ); + let setup = Setup::new(¶ms); + + let mut verifiable_set = Vec::new(); + let mut rng = ChaCha20Rng::from_seed([0u8; 32]); + for spi in dataset { + let ski = SecretKey::generate(&mut rng); + let pki = PublicKey::from(&ski); + let (data, stake) = spi; + let votes = Proof::prove_lottery(setup.n_p_lottery, setup.mu, data, &ski, &pki, stake); + for v in votes { + verifiable_set.push(v); + } + } + + let params = Params::new( + l, + l, + (np * sp).div_ceil(100), + ((100 - np) * sp).div_ceil(100), + ); + (verifiable_set, Setup::new(¶ms)) +} + pub fn bench_id(bench_name: &str, pc: usize, l: usize, sp: usize, np: usize) -> BenchmarkId { BenchmarkId::new( bench_name, @@ -34,6 +104,19 @@ pub fn bench_id(bench_name: &str, pc: usize, l: usize, sp: usize, np: usize) -> ) } +pub fn bench_id_users( + bench_name: &str, + pc: usize, + l: usize, + sp: usize, + users: usize, + np: usize, +) -> BenchmarkId { + BenchmarkId::new( + bench_name, + format!("Security parameter: {l}, Sp:{sp} (users {users}, {pc}%), n_p:{np}"), + ) +} // Measurements /// Nb of DFS call per proof diff --git a/caledonia/benches/weighted_time.rs b/caledonia/benches/weighted_time.rs new file mode 100644 index 0000000..ea4ec3c --- /dev/null +++ b/caledonia/benches/weighted_time.rs @@ -0,0 +1,164 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use rand_chacha::ChaCha20Rng; +use rand_core::SeedableRng; +use std::time::{Duration, Instant}; + +use caledonia::weighted_decentralised::Proof; + +pub mod utils; + +fn prove( + c: &mut Criterion, + lambdas: &[usize], + s_p: &[usize], + s_u: &[usize], + n_p: &[usize], + _hash_size: usize, +) { + let mut group = c.benchmark_group("Alba weighted decentralised".to_string()); + + fn prove_duration( + l: usize, + sp: usize, + users: usize, + np: usize, + truncate_size: usize, + n: u64, + ) -> Duration { + let mut rng = ChaCha20Rng::from_entropy(); + let mut total_duration = Duration::ZERO; + for _ in 0..n { + // Setup + let (mut dataset, bench_setup) = + utils::setup_weighted_wrapper(&mut rng, l, sp, users, np); + dataset.truncate(truncate_size); + // Bench + let start = Instant::now(); + black_box({ + Proof::prove(&bench_setup, &dataset); + }); + total_duration = total_duration.saturating_add(start.elapsed()); + } + total_duration + } + + for &l in lambdas { + for &sp in s_p { + for &users in s_u { + for &np in n_p { + // Bench with all of Sp + let high = sp; + + // Bench with all of np% of Sp + let low = (high * np).div_ceil(100); + + // Bench with (100+np)/2 percent of Sp + let mean = (100 + np).div_ceil(2); + let mid = (high + low).div_ceil(2); + + group.bench_function( + utils::bench_id_users("Proving time", np, l, sp, users, np), + move |b| b.iter_custom(|n| prove_duration(l, sp, users, np, low, n)), + ); + group.bench_function( + utils::bench_id_users("Proving time", mean, l, sp, users, np), + move |b| b.iter_custom(|n| prove_duration(l, sp, users, np, mid, n)), + ); + group.bench_function( + utils::bench_id_users("Proving time", 100, l, sp, users, np), + move |b| b.iter_custom(|n| prove_duration(l, sp, users, np, high, n)), + ); + } + } + } + } + group.finish(); +} + +fn verify( + c: &mut Criterion, + lambdas: &[usize], + s_p: &[usize], + s_u: &[usize], + n_p: &[usize], + _hash_size: usize, +) { + let mut group = c.benchmark_group("Alba weighted decentralised".to_string()); + + fn verify_duration( + l: usize, + sp: usize, + users: usize, + np: usize, + truncate_size: usize, + n: u64, + ) -> Duration { + let mut rng = ChaCha20Rng::from_entropy(); + let mut total_duration = Duration::ZERO; + for _ in 0..n { + // Setup + let (mut dataset, bench_setup) = + utils::setup_weighted_wrapper(&mut rng, l, sp, users, np); + dataset.truncate(truncate_size); + // Prove + let proof = Proof::prove(&bench_setup, &dataset); + // Bench + let start = Instant::now(); + black_box({ + Proof::verify(&bench_setup, proof); + }); + total_duration = total_duration.saturating_add(start.elapsed()); + } + total_duration + } + + for &l in lambdas { + for &sp in s_p { + for &users in s_u { + for &np in n_p { + // Bench with all of Sp + let high = sp; + + // Bench with all of np% of Sp + let low = (high * np).div_ceil(100); + + // Bench with (100+np)/2 percent of Sp + let mean = (100 + np).div_ceil(2); + let mid = (high + low).div_ceil(2); + + group.bench_function( + utils::bench_id_users("Verification time", np, l, sp, users, np), + move |b| b.iter_custom(|n| verify_duration(l, sp, users, np, low, n)), + ); + group.bench_function( + utils::bench_id_users("Verification time", mean, l, sp, users, np), + move |b| b.iter_custom(|n| verify_duration(l, sp, users, np, mid, n)), + ); + group.bench_function( + utils::bench_id_users("Verification time", 100, l, sp, users, np), + move |b| b.iter_custom(|n| verify_duration(l, sp, users, np, high, n)), + ); + } + } + } + } + group.finish(); +} + +fn prove_benches(c: &mut Criterion) { + // prove(c, &[128], &[1_000, 5_000], &[1_000, 1_000], &[60, 66, 80], 256); + prove(c, &[128], &[100_000], &[1_000], &[60, 66, 80], 256); +} + +fn verify_benches(c: &mut Criterion) { + verify(c, &[10], &[1_000], &[100], &[60, 66, 80], 256); +} + +criterion_group!(name = benches; + config = Criterion::default().nresamples(1000); + targets = + prove_benches, + verify_benches +); + +criterion_main!(benches);