Skip to content

Commit

Permalink
Adding and updating benches
Browse files Browse the repository at this point in the history
  • Loading branch information
rrtoledo committed Aug 23, 2024
1 parent 0204947 commit 12afe8b
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 16 deletions.
6 changes: 3 additions & 3 deletions caledonia/benches/bounded_repetition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;

use caledonia::bounded::Proof;

mod utils;
pub mod utils;

fn prepetitions(
c: &mut Criterion<utils::Repetitions>,
Expand All @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions caledonia/benches/bounded_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand_core::SeedableRng;

use caledonia::bounded::Proof;

mod utils;
pub mod utils;

fn psteps(
c: &mut Criterion<utils::Steps>,
Expand All @@ -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({
Expand Down
10 changes: 5 additions & 5 deletions caledonia/benches/bounded_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
129 changes: 129 additions & 0 deletions caledonia/benches/decentralised_time.rs
Original file line number Diff line number Diff line change
@@ -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);
93 changes: 88 additions & 5 deletions caledonia/benches/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -27,13 +32,91 @@ pub fn setup_wrapper(
(dataset, Setup::new(&params))
}

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(&params))
}

pub fn setup_weighted_wrapper(
rng: &mut ChaCha20Rng,
l: usize,
sp: usize,
voters: usize,
np: usize,
) -> (
Vec<VerifiableData>,
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(&params);

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(&params))
}

pub fn bench_id(bench_name: &str, pc: usize, l: usize, sp: usize, np: usize) -> BenchmarkId {
BenchmarkId::new(
bench_name,
format!("Security parameter: {l}, Sp:{sp} ({pc}%), n_p:{np}"),
)
}

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
Expand Down
Loading

0 comments on commit 12afe8b

Please sign in to comment.