-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 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
1 parent
6f45dde
commit 480e277
Showing
7 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |