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

Check elements are distinct in simple lottery. #111

Merged
merged 4 commits into from
Jan 8, 2025
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
2 changes: 2 additions & 0 deletions src/centralized_telescope/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use blake2::{Blake2s256, Digest};
/// Calls up to setup.max_retries times the prove_index function and returns an empty
/// proof if no suitable candidate is found.
pub(super) fn prove(setup: &Setup, prover_set: &[Element]) -> Option<Proof> {
debug_assert!(crate::utils::misc::check_distinct(prover_set));

// Run prove_index up to max_retries times
(0..setup.max_retries).find_map(|retry_counter| prove_index(setup, prover_set, retry_counter).1)
}
Expand Down
4 changes: 4 additions & 0 deletions src/simple_lottery/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ use crate::utils::types::Element;
use blake2::{Blake2s256, Digest};

pub(super) fn prove(setup: &Setup, prover_set: &[Element]) -> Option<Proof> {
debug_assert!(crate::utils::misc::check_distinct(prover_set));

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 {
element_sequence.sort_unstable();
return Some(Proof { element_sequence });
}
}
Expand All @@ -23,6 +26,7 @@ pub(super) fn prove(setup: &Setup, prover_set: &[Element]) -> Option<Proof> {

pub(super) fn verify(setup: &Setup, proof: &Proof) -> bool {
(proof.element_sequence.len() as u64 == setup.proof_size)
&& proof.element_sequence.is_sorted_by(|a, b| a < b)
&& proof
.element_sequence
.iter()
Expand Down
8 changes: 8 additions & 0 deletions src/utils/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::types::Element;

/// Returns true iff all elements in the slice are distinct.
pub(crate) fn check_distinct(elements: &[Element]) -> bool {
let mut elements = elements.to_vec();
elements.sort_unstable();
elements.is_sorted_by(|a, b| a < b)
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub(crate) mod sample;

pub(crate) mod types;

pub(crate) mod misc;
Loading