Skip to content

Commit

Permalink
Check elements are distinct in simple lottery. (#111)
Browse files Browse the repository at this point in the history
## Content

Found another issue with the simple lottery. We need to check that
elements in the proof are distinct. I guess the simple lottery is not so
simple. :)

## 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)
  • Loading branch information
tolikzinovyev authored Jan 8, 2025
1 parent 727bd6b commit 94c7624
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
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;

0 comments on commit 94c7624

Please sign in to comment.