Skip to content

Commit

Permalink
Tests: Removing generic
Browse files Browse the repository at this point in the history
  • Loading branch information
rrtoledo committed Feb 10, 2025
1 parent f72f8a8 commit 6f01c7a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
4 changes: 1 addition & 3 deletions benches/centralized_telescope/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ mod test_utils;
pub mod common;
use common::criterion_helpers::centralized::BenchParam;

use blake2::Blake2bVar;

/// Global variables
pub const NAME: &str = "Centralized";

/// Function generating a random set of elements to bench and calling Alba's centralized setup
pub fn setup(rng: &mut ChaCha20Rng, params: &BenchParam) -> (Vec<[u8; 48]>, Telescope) {
let seed_u32 = rng.next_u32();
let seed = seed_u32.to_ne_bytes().to_vec();
let dataset = test_utils::gen_items::<Blake2bVar, 48>(&seed, params.total_num_elements);
let dataset = test_utils::gen_items::<48>(&seed, params.total_num_elements);
let telescope = Telescope::create(
params.lambda_sec,
params.lambda_rel,
Expand Down
4 changes: 1 addition & 3 deletions benches/simple_lottery/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ mod test_utils;
pub mod common;
use common::criterion_helpers::centralized::BenchParam;

use blake2::Blake2bVar;

/// Global variables
pub const NAME: &str = "Lottery";

/// Function generating a random set of elements to bench and calling Simple Lottery setup
pub fn setup(rng: &mut ChaCha20Rng, params: &BenchParam) -> (Vec<[u8; 48]>, Lottery) {
let seed_u32 = rng.next_u32();
let seed = seed_u32.to_ne_bytes().to_vec();
let dataset = test_utils::gen_items::<Blake2bVar, 48>(&seed, params.total_num_elements);
let dataset = test_utils::gen_items::<48>(&seed, params.total_num_elements);
let telescope = Lottery::create(
params.lambda_sec,
params.lambda_rel,
Expand Down
23 changes: 11 additions & 12 deletions tests/centralized_telescope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
use alba::centralized_telescope::Telescope;
use alba::centralized_telescope::{params::Params, proof::Proof};
use digest::{Digest, FixedOutput};
use rand_chacha::ChaCha20Rng;
use rand_core::{RngCore, SeedableRng};

mod common;
use common::gen_items;

use blake2::Blake2bVar;
use sha2::Sha256;

const DATA_LENGTH: usize = 48;

fn test<H: Digest + FixedOutput>(created_with_params: bool) {
fn test(created_with_params: bool) {
let mut rng = ChaCha20Rng::from_seed(Default::default());
let nb_tests = 1_000;
let nb_elements: u64 = 1_000;
Expand All @@ -24,38 +22,39 @@ fn test<H: Digest + FixedOutput>(created_with_params: bool) {
let lower_bound = nb_elements.saturating_mul(20).div_ceil(100);
for _t in 0..nb_tests {
let seed = rng.next_u32().to_be_bytes().to_vec();
let s_p = gen_items::<Blake2bVar, DATA_LENGTH>(&seed, nb_elements);
let s_p = gen_items::<DATA_LENGTH>(&seed, nb_elements);
let alba = if created_with_params {
Telescope::create(soundness_param, completeness_param, set_size, lower_bound)
} else {
let setup = Params::new(soundness_param, completeness_param, set_size, lower_bound);
Telescope::setup_unsafe(set_size, &setup)
};
let proof = alba.prove::<H>(&s_p).unwrap();
let proof = alba.prove::<Sha256>(&s_p).unwrap();
assert!(alba.verify(&proof));
// Checking that the proof fails if proof.search_counter is erroneous
let proof_t = Proof::<H>::from(
let proof_t = Proof::<Sha256>::from(
proof.retry_counter,
proof.search_counter.wrapping_add(1),
proof.element_sequence.clone(),
);
assert!(!alba.verify(&proof_t));
// Checking that the proof fails if proof.retry_counter is erroneous
let proof_v = Proof::<H>::from(
let proof_v = Proof::<Sha256>::from(
proof.retry_counter.wrapping_add(1),
proof.search_counter,
proof.element_sequence.clone(),
);
assert!(!alba.verify(&proof_v));
// Checking that the proof fails when no elements are included
let proof_item = Proof::<H>::from(proof.retry_counter, proof.search_counter, Vec::new());
let proof_item =
Proof::<Sha256>::from(proof.retry_counter, proof.search_counter, Vec::new());
assert!(!alba.verify(&proof_item));
// Checking that the proof fails when wrong elements are included
// We are trying to trigger proof_hash
let mut wrong_items = proof.element_sequence.clone();
let last_item = wrong_items.pop().unwrap();
let mut penultimate_item = wrong_items.pop().unwrap();
let proof_itembis = Proof::<H>::from(
let proof_itembis = Proof::<Sha256>::from(
proof.retry_counter,
proof.search_counter,
wrong_items.clone(),
Expand All @@ -66,7 +65,7 @@ fn test<H: Digest + FixedOutput>(created_with_params: bool) {
penultimate_item[0] = penultimate_item[0].wrapping_add(42u8);
wrong_items.push(penultimate_item);
wrong_items.push(last_item);
let proof_itembis = Proof::<H>::from(
let proof_itembis = Proof::<Sha256>::from(
proof.retry_counter,
proof.search_counter,
wrong_items.clone(),
Expand All @@ -77,10 +76,10 @@ fn test<H: Digest + FixedOutput>(created_with_params: bool) {

#[test]
fn created_with_params() {
test::<Sha256>(true);
test(true);
}

#[test]
fn created_with_setup() {
test::<Sha256>(false);
test(false);
}
10 changes: 4 additions & 6 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//! Test & Bench helpers
use digest::VariableOutput;
use blake2::digest::{Update, VariableOutput};
use blake2::Blake2bVar;

/// Generate a set of items given the set size and a seed
/// Items are generated by hashing the current index
pub(crate) fn gen_items<H: VariableOutput, const N: usize>(
seed: &[u8],
set_size: u64,
) -> Vec<[u8; N]> {
pub(crate) fn gen_items<const N: usize>(seed: &[u8], set_size: u64) -> Vec<[u8; N]> {
let mut s_p = Vec::with_capacity(set_size as usize);
let mut data_buf: [u8; 8];
let mut digest_buf: [u8; N] = [0u8; N];
for b in 0..set_size {
data_buf = b.to_be_bytes();
let mut hasher = H::new(N).expect("Failed to construct hasher!");
let mut hasher = Blake2bVar::new(N).expect("Failed to construct hasher!");
hasher.update(seed);
hasher.update(&data_buf);
hasher
Expand Down

0 comments on commit 6f01c7a

Please sign in to comment.