Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement simple lottery. #97

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/centralized_telescope/proof.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! ALBA's Proof structure
//! Centralized Telescope Proof structure

#![doc = include_str!("../../docs/centralized_telescope/proof.md")]

use crate::utils::types::Element;

/// Alba proof
/// Centralized Telescope proof
#[derive(Debug, Clone)]
pub struct Proof {
/// Numbers of retries done to find the proof
pub retry_counter: u64,
/// Index of the searched subtree to find the proof
pub search_counter: u64,
/// Sequence of elements from prover set
/// Sequence of elements from prover's set
pub element_sequence: Vec<Element>,
}
2 changes: 1 addition & 1 deletion src/centralized_telescope/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::proof::Proof;
use super::setup::Setup;
use crate::utils::types::Element;

/// The main ALBA struct with prove and verify functions.
/// The main centralized Telescope struct with prove and verify functions.
#[derive(Debug, Clone, Copy)]
pub struct Wrapper {
setup: Setup,
Expand Down
37 changes: 37 additions & 0 deletions src/simple_lottery/algorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Simple lottery prove and verify functions

use super::proof::Proof;
use super::setup::Setup;
use super::types::Hash;
use crate::utils::sample;
use crate::utils::types::Element;
use blake2::{Blake2s256, Digest};

pub(super) fn prove(setup: &Setup, prover_set: &[Element]) -> Option<Proof> {
let mut element_sequence = Vec::with_capacity(setup.proof_size as usize);
for &element in prover_set {
if lottery_hash(setup, element) {
element_sequence.push(element);
}
if prover_set.len() as u64 >= setup.proof_size {
return Some(Proof { element_sequence });
}
}

None
}

pub(super) fn verify(setup: &Setup, proof: &Proof) -> bool {
(proof.element_sequence.len() as u64 == setup.proof_size)
&& proof
.element_sequence
.iter()
.all(|&element| lottery_hash(setup, element))
}

fn lottery_hash(setup: &Setup, element: Element) -> bool {
let mut hasher = Blake2s256::new();
hasher.update(element);
let digest: Hash = hasher.finalize().into();
sample::sample_bernoulli(&digest, setup.lottery_probability)
}
11 changes: 11 additions & 0 deletions src/simple_lottery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ pub mod init;
pub mod params;

pub mod setup;

pub mod proof;

mod types;

mod algorithm;

mod wrapper;

// Re-exports
pub use wrapper::Wrapper as SimpleLottery;
10 changes: 10 additions & 0 deletions src/simple_lottery/proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Simple lottery Proof structure

use crate::utils::types::Element;

/// Simple lottery proof
#[derive(Debug, Clone)]
pub struct Proof {
/// Sequence of elements from prover's set
pub element_sequence: Vec<Element>,
}
7 changes: 7 additions & 0 deletions src/simple_lottery/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Types specific to simple lottery

/// Digest size for internal hashes
pub(super) const DIGEST_SIZE: usize = 32;

/// Hash type for internal hashes
pub(super) type Hash = [u8; DIGEST_SIZE];
36 changes: 36 additions & 0 deletions src/simple_lottery/wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::algorithm;
use super::init::make_setup;
use super::params::Params;
use super::proof::Proof;
use super::setup::Setup;
use crate::utils::types::Element;

/// The main simple lottery struct with prove and verify functions.
#[derive(Debug, Clone, Copy)]
pub struct Wrapper {
setup: Setup,
}

impl Wrapper {
/// Initialize ALBA with `Params`.
pub fn create(params: &Params) -> Self {
let setup = make_setup(params);
Self::create_unsafe(&setup)
}

/// This function is unsafe to use and should be avoided.
rrtoledo marked this conversation as resolved.
Show resolved Hide resolved
rrtoledo marked this conversation as resolved.
Show resolved Hide resolved
/// Initialize ALBA with `Setup`.
rrtoledo marked this conversation as resolved.
Show resolved Hide resolved
pub fn create_unsafe(setup: &Setup) -> Self {
Self { setup: *setup }
}

/// Returns either a `Proof` or `None` if no proof is found.
pub fn prove(&self, prover_set: &[Element]) -> Option<Proof> {
algorithm::prove(&self.setup, prover_set)
}

/// Returns true if and only if the proof is successfully verified.
pub fn verify(&self, proof: &Proof) -> bool {
algorithm::verify(&self.setup, proof)
}
}
Loading