Skip to content

Commit

Permalink
Implement simple lottery. (#97)
Browse files Browse the repository at this point in the history
## Content

Implement simple lottery's prove and verify functions. This PR doesn't
have tests. Let's see what property tests will test and if it still
makes sense to add other tests.

## Pre-submit checklist

- Branch
    - [x] Tests are provided (if possible)
    - [x] Commit sequence broadly makes sense
    - [x] Key commits have useful messages
- PR
    - [x] No clippy warnings in the CI
    - [x] Self-reviewed the diff
    - [x] Useful pull request description
    - [x] Reviewer requested
- Documentation
    - [x] Update README file (if relevant)
    - [x] Update documentation website (if relevant)

## Issue(s)

Closes #69
  • Loading branch information
tolikzinovyev authored Dec 13, 2024
1 parent 6f45dde commit 480e277
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
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.
/// Initialize ALBA with `Setup`.
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)
}
}

0 comments on commit 480e277

Please sign in to comment.