From 480e277f7333e55246b5b18b4830b9c2128c9f4e Mon Sep 17 00:00:00 2001 From: Tolik Zinovyev Date: Fri, 13 Dec 2024 04:40:11 -0500 Subject: [PATCH 1/2] Implement simple lottery. (#97) ## 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 --- src/centralized_telescope/proof.rs | 6 ++--- src/centralized_telescope/wrapper.rs | 2 +- src/simple_lottery/algorithm.rs | 37 ++++++++++++++++++++++++++++ src/simple_lottery/mod.rs | 11 +++++++++ src/simple_lottery/proof.rs | 10 ++++++++ src/simple_lottery/types.rs | 7 ++++++ src/simple_lottery/wrapper.rs | 36 +++++++++++++++++++++++++++ 7 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/simple_lottery/algorithm.rs create mode 100644 src/simple_lottery/proof.rs create mode 100644 src/simple_lottery/types.rs create mode 100644 src/simple_lottery/wrapper.rs diff --git a/src/centralized_telescope/proof.rs b/src/centralized_telescope/proof.rs index b97ad20a..d2c77390 100644 --- a/src/centralized_telescope/proof.rs +++ b/src/centralized_telescope/proof.rs @@ -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, } diff --git a/src/centralized_telescope/wrapper.rs b/src/centralized_telescope/wrapper.rs index 5c8799b3..d017eb8c 100644 --- a/src/centralized_telescope/wrapper.rs +++ b/src/centralized_telescope/wrapper.rs @@ -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, diff --git a/src/simple_lottery/algorithm.rs b/src/simple_lottery/algorithm.rs new file mode 100644 index 00000000..cbe5c95f --- /dev/null +++ b/src/simple_lottery/algorithm.rs @@ -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 { + 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) +} diff --git a/src/simple_lottery/mod.rs b/src/simple_lottery/mod.rs index f92ec50a..25d1c635 100644 --- a/src/simple_lottery/mod.rs +++ b/src/simple_lottery/mod.rs @@ -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; diff --git a/src/simple_lottery/proof.rs b/src/simple_lottery/proof.rs new file mode 100644 index 00000000..5b58ab67 --- /dev/null +++ b/src/simple_lottery/proof.rs @@ -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, +} diff --git a/src/simple_lottery/types.rs b/src/simple_lottery/types.rs new file mode 100644 index 00000000..cafcdac7 --- /dev/null +++ b/src/simple_lottery/types.rs @@ -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]; diff --git a/src/simple_lottery/wrapper.rs b/src/simple_lottery/wrapper.rs new file mode 100644 index 00000000..d09d9dd2 --- /dev/null +++ b/src/simple_lottery/wrapper.rs @@ -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 { + 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) + } +} From cb8e08f2ed6c5d5db0172f36d77f36954ea00243 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 13 Dec 2024 15:15:47 +0000 Subject: [PATCH 2/2] fixup indent (#106) ## Content This PR includes... ## Pre-submit checklist - Branch - [ ] Tests are provided (if possible) - [ ] Commit sequence broadly makes sense - [ ] Key commits have useful messages - PR - [ ] No clippy warnings in the CI - [ ] Self-reviewed the diff - [ ] Useful pull request description - [ ] Reviewer requested - Documentation - [ ] Update README file (if relevant) - [ ] Update documentation website (if relevant) ## Comments ## Issue(s) Relates to #YYY or Closes #YYY --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5c82d7d4..3990a912 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ ALBA is an ideal choice for applications that require: - *Fast proof generation and verification*, such as in blockchain systems or multisignature schemes. - *Efficient decentralized collaboration*, enabling multiple participants to jointly prove knowledge. - *Flexibility in tradeoffs*, balancing proof size and communication overhead. + Whether it is for multisignatures, proof-of-stake systems, or secure voting protocols, ALBA provides a robust, scalable, and efficient solution for proving knowledge across diverse use cases. ## Implementation