diff --git a/ssz-rs/src/boolean.rs b/ssz-rs/src/boolean.rs index 49a5e2f2..a4a088ec 100644 --- a/ssz-rs/src/boolean.rs +++ b/ssz-rs/src/boolean.rs @@ -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 { prove_primitive(self, index) @@ -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::*; diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index aef05baf..eae37130 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -250,8 +250,6 @@ where } } -impl SimpleSerialize for List where T: SimpleSerialize {} - impl GeneralizedIndexable for List where T: SimpleSerialize + GeneralizedIndexable, @@ -292,6 +290,8 @@ where } } +impl SimpleSerialize for List where T: SimpleSerialize {} + #[cfg(feature = "serde")] struct ListVisitor(PhantomData>); diff --git a/ssz-rs/src/merkleization/proofs.rs b/ssz-rs/src/merkleization/proofs.rs index f7618410..c92dcfd5 100644 --- a/ssz-rs/src/merkleization/proofs.rs +++ b/ssz-rs/src/merkleization/proofs.rs @@ -8,11 +8,29 @@ use crate::{ }; use sha2::{Digest, Sha256}; +pub type ProofAndWitness = (Proof, Node); + pub fn get_subtree_index(i: GeneralizedIndex) -> Result { 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; +} + +/// Produce a Merkle proof (and corresponding witness) for the type `T` at the given `path` relative +/// to `T`. +pub fn prove( + data: &mut T, + path: Path, +) -> Result { + 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)] @@ -31,8 +49,6 @@ impl Proof { } } -pub type ProofAndWitness = (Proof, Node); - pub fn prove_primitive( data: &mut T, index: GeneralizedIndex, @@ -46,22 +62,6 @@ pub fn prove_primitive( 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; -} - -/// Produce a Merkle proof (and corresponding witness) for the type `T` at the given `path` relative -/// to `T`. -pub fn prove( - data: &mut T, - path: Path, -) -> Result { - let index = T::generalized_index(path)?; - data.prove(index) -} - pub fn is_valid_merkle_branch_for_generalized_index>( leaf: Node, branch: &[T], diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index a4fa8305..d74b1903 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -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, @@ -79,12 +85,6 @@ macro_rules! define_uint { } impl SimpleSerialize for $uint {} - - impl GeneralizedIndexable for $uint { - fn item_length() -> usize { - Self::size_hint() - } - } }; } @@ -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 { prove_primitive(self, index) @@ -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::*; diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 2fac0f93..49938cd7 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -245,8 +245,6 @@ where } } -impl SimpleSerialize for Vector where T: SimpleSerialize {} - impl GeneralizedIndexable for Vector where T: SimpleSerialize + GeneralizedIndexable, @@ -278,6 +276,17 @@ where } } +impl Prove for Vector +where + T: SimpleSerialize + GeneralizedIndexable + Prove, +{ + fn prove(&mut self, index: GeneralizedIndex) -> Result { + todo!() + } +} + +impl SimpleSerialize for Vector where T: SimpleSerialize {} + #[cfg(feature = "serde")] struct VectorVisitor(PhantomData>);