Skip to content

Commit

Permalink
Lottery/*: Replacing Element type by ToBytes trait
Browse files Browse the repository at this point in the history
  • Loading branch information
rrtoledo committed Jan 29, 2025
1 parent 892188c commit 4dfcff7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/simple_lottery/lottery.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Customer facing Lottery structure
use super::params::Params;
use super::proof::Proof;
use crate::utils::types::Element;
use crate::utils::types::ToBytes;

/// The main simple lottery struct with prove and verify functions.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Lottery {
/// }
/// let proof = lottery.prove(&prover_set).unwrap();
/// ```
pub fn prove(&self, prover_set: &[Element]) -> Option<Proof> {
pub fn prove<E: ToBytes + Clone + Sized + Ord>(&self, prover_set: &[E]) -> Option<Proof<E>> {
Proof::new(&self.params, prover_set)
}

Expand Down Expand Up @@ -135,7 +135,7 @@ impl Lottery {
/// let proof = lottery.prove(&prover_set).unwrap();
/// assert!(lottery.verify(&proof));
/// ```
pub fn verify(&self, proof: &Proof) -> bool {
pub fn verify<E: ToBytes + Clone + Sized + Ord>(&self, proof: &Proof<E>) -> bool {
proof.verify(&self.params)
}
}
22 changes: 11 additions & 11 deletions src/simple_lottery/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
use super::params::Params;
use crate::utils::{
sample,
types::{Element, Hash},
types::{Hash, ToBytes},
};
use blake2::{Blake2s256, Digest};

/// Simple lottery proof
#[derive(Debug, Clone)]
pub struct Proof {
pub struct Proof<E: ToBytes + Clone + Sized + Ord> {
/// Sequence of elements from prover's set
pub element_sequence: Vec<Element>,
pub element_sequence: Vec<E>,
}

impl Proof {
impl<E: ToBytes + Clone + Sized + Ord> Proof<E> {
/// Simple Lottery's proving algorithm, based on a DFS algorithm.
///
/// # Arguments
Expand All @@ -38,13 +38,13 @@ impl Proof {
/// }
/// let proof = Proof::new(&params, &prover_set).unwrap();
/// ```
pub fn new(params: &Params, prover_set: &[Element]) -> Option<Self> {
pub fn new(params: &Params, prover_set: &[E]) -> Option<Self> {
debug_assert!(crate::utils::misc::check_distinct(prover_set));

let mut element_sequence = Vec::with_capacity(params.proof_size as usize);
for &element in prover_set {
if Proof::lottery_hash(params.lottery_probability, element) {
element_sequence.push(element);
for element in prover_set {
if Proof::lottery_hash(params.lottery_probability, element.clone()) {
element_sequence.push(element.clone());
}
if element_sequence.len() as u64 >= params.proof_size {
element_sequence.sort_unstable();
Expand Down Expand Up @@ -87,14 +87,14 @@ impl Proof {
&& self
.element_sequence
.iter()
.all(|&element| Proof::lottery_hash(params.lottery_probability, element))
.all(|element| Self::lottery_hash(params.lottery_probability, element))
}

/// Oracle defined as Bernoulli(q) returning 1 with probability q and 0
/// otherwise
fn lottery_hash(lottery_probability: f64, element: Element) -> bool {
fn lottery_hash(lottery_probability: f64, element: &E) -> bool {
let mut hasher = Blake2s256::new();
hasher.update(element);
hasher.update(element.to_be_bytes());
let digest: Hash = hasher.finalize().into();
sample::sample_bernoulli(&digest, lottery_probability)
}
Expand Down

0 comments on commit 4dfcff7

Please sign in to comment.