Skip to content

Commit

Permalink
code reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Mar 29, 2024
1 parent 462db29 commit 7eafc42
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 40 deletions.
12 changes: 6 additions & 6 deletions ssz-rs/src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl HashTreeRoot for bool {
}
}

impl GeneralizedIndexable for bool {
fn item_length() -> usize {
Self::size_hint()
}
}

impl Prove for bool {
fn prove(&mut self, index: GeneralizedIndex) -> Result<ProofAndWitness, MerkleizationError> {
prove_primitive(self, index)
Expand All @@ -64,12 +70,6 @@ impl Prove for bool {

impl SimpleSerialize for bool {}

impl GeneralizedIndexable for bool {
fn item_length() -> usize {
Self::size_hint()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions ssz-rs/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ where
}
}

impl<T, const N: usize> SimpleSerialize for List<T, N> where T: SimpleSerialize {}

impl<T, const N: usize> GeneralizedIndexable for List<T, N>
where
T: SimpleSerialize + GeneralizedIndexable,
Expand Down Expand Up @@ -292,6 +290,8 @@ where
}
}

impl<T, const N: usize> SimpleSerialize for List<T, N> where T: SimpleSerialize {}

#[cfg(feature = "serde")]
struct ListVisitor<T: Serializable>(PhantomData<Vec<T>>);

Expand Down
36 changes: 18 additions & 18 deletions ssz-rs/src/merkleization/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,29 @@ use crate::{
};
use sha2::{Digest, Sha256};

pub type ProofAndWitness = (Proof, Node);

pub fn get_subtree_index(i: GeneralizedIndex) -> Result<usize, Error> {
let i_log2 = log_2(i).ok_or(Error::InvalidGeneralizedIndex)?;
Ok(i % 2usize.pow(i_log2))
}

/// Types that can produce Merkle proofs against themselves given a `GeneralizedIndex`.
pub trait Prove {
/// Provide a Merkle proof of the node in this type's merkle tree corresponding to the `index`.
fn prove(&mut self, index: GeneralizedIndex) -> Result<ProofAndWitness, Error>;
}

/// Produce a Merkle proof (and corresponding witness) for the type `T` at the given `path` relative
/// to `T`.
pub fn prove<T: GeneralizedIndexable + Prove>(
data: &mut T,
path: Path,
) -> Result<ProofAndWitness, Error> {
let index = T::generalized_index(path)?;
data.prove(index)
}

/// Contains data necessary to verify `leaf` was included under some witness "root" node
/// at the generalized position `index`.
#[derive(Debug)]
Expand All @@ -31,8 +49,6 @@ impl Proof {
}
}

pub type ProofAndWitness = (Proof, Node);

pub fn prove_primitive<T: HashTreeRoot + ?Sized>(
data: &mut T,
index: GeneralizedIndex,
Expand All @@ -46,22 +62,6 @@ pub fn prove_primitive<T: HashTreeRoot + ?Sized>(
Ok((proof, root))
}

/// Types that can produce Merkle proofs against themselves given a `GeneralizedIndex`.
pub trait Prove {
/// Provide a Merkle proof of the node in this type's merkle tree corresponding to the `index`.
fn prove(&mut self, index: GeneralizedIndex) -> Result<ProofAndWitness, Error>;
}

/// Produce a Merkle proof (and corresponding witness) for the type `T` at the given `path` relative
/// to `T`.
pub fn prove<T: GeneralizedIndexable + Prove>(
data: &mut T,
path: Path,
) -> Result<ProofAndWitness, Error> {
let index = T::generalized_index(path)?;
data.prove(index)
}

pub fn is_valid_merkle_branch_for_generalized_index<T: AsRef<[u8]>>(
leaf: Node,
branch: &[T],
Expand Down
24 changes: 12 additions & 12 deletions ssz-rs/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ macro_rules! define_uint {
}
}

impl GeneralizedIndexable for $uint {
fn item_length() -> usize {
Self::size_hint()
}
}

impl Prove for $uint {
fn prove(
&mut self,
Expand All @@ -79,12 +85,6 @@ macro_rules! define_uint {
}

impl SimpleSerialize for $uint {}

impl GeneralizedIndexable for $uint {
fn item_length() -> usize {
Self::size_hint()
}
}
};
}

Expand Down Expand Up @@ -149,6 +149,12 @@ impl HashTreeRoot for U256 {
}
}

impl GeneralizedIndexable for U256 {
fn item_length() -> usize {
Self::size_hint()
}
}

impl Prove for U256 {
fn prove(&mut self, index: GeneralizedIndex) -> Result<ProofAndWitness, MerkleizationError> {
prove_primitive(self, index)
Expand All @@ -157,12 +163,6 @@ impl Prove for U256 {

impl SimpleSerialize for U256 {}

impl GeneralizedIndexable for U256 {
fn item_length() -> usize {
Self::size_hint()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
13 changes: 11 additions & 2 deletions ssz-rs/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ where
}
}

impl<T, const N: usize> SimpleSerialize for Vector<T, N> where T: SimpleSerialize {}

impl<T, const N: usize> GeneralizedIndexable for Vector<T, N>
where
T: SimpleSerialize + GeneralizedIndexable,
Expand Down Expand Up @@ -278,6 +276,17 @@ where
}
}

impl<T, const N: usize> Prove for Vector<T, N>
where
T: SimpleSerialize + GeneralizedIndexable + Prove,
{
fn prove(&mut self, index: GeneralizedIndex) -> Result<ProofAndWitness, MerkleizationError> {
todo!()
}
}

impl<T, const N: usize> SimpleSerialize for Vector<T, N> where T: SimpleSerialize {}

#[cfg(feature = "serde")]
struct VectorVisitor<T: Serializable>(PhantomData<Vec<T>>);

Expand Down

0 comments on commit 7eafc42

Please sign in to comment.