Skip to content

Commit

Permalink
Replacing ToBytes with AsRef<[u8]>
Browse files Browse the repository at this point in the history
  • Loading branch information
rrtoledo committed Feb 10, 2025
1 parent e0d2819 commit 3fa62ae
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 68 deletions.
11 changes: 4 additions & 7 deletions src/centralized_telescope/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

use super::params::Params;
use super::round::Round;
use crate::utils::{
sample,
types::{Hash, ToBytes},
};
use crate::utils::{sample, types::Hash};
use blake2::{Blake2s256, Digest};

/// Centralized Telescope proof
#[derive(Debug, Clone)]
pub struct Proof<E: ToBytes + Clone + Sized + Ord> {
pub struct Proof<E: AsRef<[u8]> + Clone + Ord> {
/// Numbers of retries done to find the proof
pub retry_counter: u64,
/// Index of the searched subtree to find the proof
Expand All @@ -21,7 +18,7 @@ pub struct Proof<E: ToBytes + Clone + Sized + Ord> {
pub element_sequence: Vec<E>,
}

impl<E: ToBytes + Clone + Sized + Ord> Proof<E> {
impl<E: AsRef<[u8]> + Clone + Ord> Proof<E> {
/// Centralized Telescope's proving algorithm, based on a DFS algorithm.
/// Calls up to `params.max_retries` times the prove_index function and
/// returns a `Proof` if a suitable candidate tuple is found.
Expand Down Expand Up @@ -248,7 +245,7 @@ impl<E: ToBytes + Clone + Sized + Ord> Proof<E> {
let mut hasher = Blake2s256::new();
hasher.update(b"Telescope-bin_hash");
hasher.update(retry_bytes);
hasher.update(element.to_be_bytes());
hasher.update(element.as_ref());
let digest: Hash = hasher.finalize().into();
sample::sample_uniform(&digest, set_size)
}
Expand Down
11 changes: 4 additions & 7 deletions src/centralized_telescope/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
#![doc = include_str!("../../docs/rustdoc/centralized_telescope/round.md")]

use crate::utils::{
sample,
types::{Hash, ToBytes},
};
use crate::utils::{sample, types::Hash};
use blake2::{Blake2s256, Digest};

/// Round parameters
#[derive(Debug, Clone)]
pub struct Round<E: ToBytes + Clone + Sized + Ord> {
pub struct Round<E: AsRef<[u8]> + Clone + Ord> {
/// Numbers of retries done so far
pub retry_counter: u64,
/// Index of the current subtree being searched
Expand All @@ -25,7 +22,7 @@ pub struct Round<E: ToBytes + Clone + Sized + Ord> {
pub set_size: u64,
}

impl<E: ToBytes + Clone + Sized + Ord> Round<E> {
impl<E: AsRef<[u8]> + Clone + Ord> Round<E> {
/// Output a round from retry and search counters as well as set_size
/// Initilialises the hash with round_hash(retry_counter || search_bytes)
/// and random value as oracle(round_hash(retry_counter || search_bytes), set_size)
Expand All @@ -48,7 +45,7 @@ impl<E: ToBytes + Clone + Sized + Ord> Round<E> {
pub(super) fn update(r: &Self, element: &E) -> Option<Self> {
let mut element_sequence = r.element_sequence.clone();
element_sequence.push(element.clone());
let (hash, id_opt) = Self::round_hash(&r.hash, element.to_be_bytes().as_ref(), r.set_size);
let (hash, id_opt) = Self::round_hash(&r.hash, element.as_ref(), r.set_size);
id_opt.map(|id| Self {
retry_counter: r.retry_counter,
search_counter: r.search_counter,
Expand Down
5 changes: 2 additions & 3 deletions src/centralized_telescope/telescope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Customer facing Centralized Telescope structure
use super::params::Params;
use super::proof::Proof;
use crate::utils::types::ToBytes;

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

Expand Down Expand Up @@ -166,7 +165,7 @@ impl Telescope {
/// let proof = telescope.prove(&prover_set).unwrap();
/// assert!(telescope.verify(&proof));
/// ```
pub fn verify<E: ToBytes + Clone + Sized + Ord>(&self, proof: &Proof<E>) -> bool {
pub fn verify<E: AsRef<[u8]> + Clone + Ord>(&self, proof: &Proof<E>) -> bool {
proof.verify(self.set_size, &self.params)
}
}
5 changes: 2 additions & 3 deletions src/simple_lottery/lottery.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Customer facing Lottery structure
use super::params::Params;
use super::proof::Proof;
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 +106,7 @@ impl Lottery {
/// }
/// let proof = lottery.prove(&prover_set).unwrap();
/// ```
pub fn prove<E: ToBytes + Clone + Sized + Ord>(&self, prover_set: &[E]) -> Option<Proof<E>> {
pub fn prove<E: AsRef<[u8]> + Clone + Ord>(&self, prover_set: &[E]) -> Option<Proof<E>> {
Proof::new(&self.params, prover_set)
}

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

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

impl<E: ToBytes + Clone + Sized + Ord> Proof<E> {
impl<E: AsRef<[u8]> + Clone + Ord> Proof<E> {
/// Simple Lottery's proving algorithm, based on a DFS algorithm.
///
/// # Arguments
Expand Down Expand Up @@ -94,7 +91,7 @@ impl<E: ToBytes + Clone + Sized + Ord> Proof<E> {
/// otherwise
fn lottery_hash(lottery_probability: f64, element: &E) -> bool {
let mut hasher = Blake2s256::new();
hasher.update(element.to_be_bytes());
hasher.update(element.as_ref());
let digest: Hash = hasher.finalize().into();
sample::sample_bernoulli(&digest, lottery_probability)
}
Expand Down
4 changes: 1 addition & 3 deletions src/utils/misc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::types::ToBytes;

/// Returns true iff all elements in the slice are distinct.
pub(crate) fn check_distinct<E: ToBytes + Clone + Sized + Ord>(elements: &[E]) -> bool {
pub(crate) fn check_distinct<E: AsRef<[u8]> + Clone + Ord>(elements: &[E]) -> bool {
let mut elements = elements.to_vec();
elements.sort_unstable();
elements.is_sorted_by(|a, b| a < b)
Expand Down
38 changes: 0 additions & 38 deletions src/utils/types.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
//! Types and implementation
use std::mem;
/// Digest size for internal hashes
pub(crate) const DIGEST_SIZE: usize = 32;

/// Hash type for internal hashes
pub(crate) type Hash = [u8; DIGEST_SIZE];

/// Trait to represent input as bytes
pub trait ToBytes {
/// Reference on unsized array of u8
type ByteArray: AsRef<[u8]>;
/// Returns the byte representation of the element
fn to_be_bytes(&self) -> Self::ByteArray;
}

// Implementing ToBytes for integer types
macro_rules! impl_ToBytes {
(for $($t:ty),+) => {
$(impl ToBytes for $t {
type ByteArray = [u8; mem::size_of::<Self>()];

fn to_be_bytes(&self) -> Self::ByteArray {
return Self::to_be_bytes(*self);
}
})*
}
}
impl_ToBytes!(for u8, u16, u32, u64, u128);
impl_ToBytes!(for i8, i16, i32, i64, i128);

// Implementing ToBytes for byte arrays
macro_rules! impl_u8ToBytes {
(for $($t:ty),+) => {
$(impl ToBytes for $t {
type ByteArray = Self;

fn to_be_bytes(&self) -> Self::ByteArray {
*self
}
})*
}
}
impl_u8ToBytes!(for [u8;32], [u8;48], [u8;64], [u8;128], [u8;448]);

0 comments on commit 3fa62ae

Please sign in to comment.