diff --git a/primitives/src/merkle_tree/examples.rs b/primitives/src/merkle_tree/examples.rs index 09544bb32..ad8af32bb 100644 --- a/primitives/src/merkle_tree/examples.rs +++ b/primitives/src/merkle_tree/examples.rs @@ -7,18 +7,12 @@ //! Provides sample instantiations of merkle tree. //! E.g. Sparse merkle tree with BigUInt index. -use super::{append_only::MerkleTree, prelude::RescueHash, DigestAlgorithm, Element, Index}; +use super::{append_only::MerkleTree, prelude::RescueHash, DigestAlgorithm}; use crate::{ errors::PrimitivesError, rescue::{sponge::RescueCRHF, RescueParameter}, }; use ark_ff::Field; -use ark_serialize::{ - CanonicalDeserialize, CanonicalSerialize, Compress, Read, SerializationError, Valid, Validate, - Write, -}; -use ark_std::vec::Vec; -use sha3::{Digest, Sha3_256}; use typenum::U3; /// Element type for interval merkle tree @@ -40,70 +34,3 @@ impl DigestAlgorithm, u64, F> for RescueHash /// Interval merkle tree instantiation for interval merkle tree using Rescue /// hash function. pub type IntervalMerkleTree = MerkleTree, RescueHash, u64, U3, F>; - -/// Update the array length here -#[derive(Default, Eq, PartialEq, Clone, Copy, Debug, Ord, PartialOrd, Hash)] -pub struct Sha3Node(pub(crate) [u8; 32]); - -impl AsRef<[u8]> for Sha3Node { - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl CanonicalSerialize for Sha3Node { - fn serialize_with_mode( - &self, - mut writer: W, - _compress: Compress, - ) -> Result<(), SerializationError> { - writer.write_all(&self.0)?; - Ok(()) - } - - fn serialized_size(&self, _compress: Compress) -> usize { - 32 - } -} -impl CanonicalDeserialize for Sha3Node { - fn deserialize_with_mode( - mut reader: R, - _compress: Compress, - _validate: Validate, - ) -> Result { - let mut ret = [0u8; 32]; - reader.read_exact(&mut ret)?; - Ok(Sha3Node(ret)) - } -} - -impl Valid for Sha3Node { - fn check(&self) -> Result<(), SerializationError> { - Ok(()) - } -} - -/// Wrapper for SHA3_512 hash function -#[derive(Clone, Debug, Hash, Eq, PartialEq)] -pub struct Sha3Digest(); - -impl DigestAlgorithm for Sha3Digest { - fn digest(data: &[Sha3Node]) -> Result { - let mut hasher = Sha3_256::new(); - for value in data { - hasher.update(value); - } - Ok(Sha3Node(hasher.finalize().into())) - } - - fn digest_leaf(_pos: &I, elem: &E) -> Result { - let mut writer = Vec::new(); - elem.serialize_compressed(&mut writer).unwrap(); - let mut hasher = Sha3_256::new(); - hasher.update(writer); - Ok(Sha3Node(hasher.finalize().into())) - } -} - -/// Merkle tree using SHA3 hash -pub type SHA3MerkleTree = MerkleTree; diff --git a/primitives/src/merkle_tree/namespaced_merkle_tree/hash.rs b/primitives/src/merkle_tree/namespaced_merkle_tree/hash.rs index 1ab4956e4..1ef19bc89 100644 --- a/primitives/src/merkle_tree/namespaced_merkle_tree/hash.rs +++ b/primitives/src/merkle_tree/namespaced_merkle_tree/hash.rs @@ -7,7 +7,7 @@ use sha3::Sha3_256; use crate::{ errors::PrimitivesError, - merkle_tree::examples::{Sha3Digest, Sha3Node}, + merkle_tree::prelude::{Sha3Digest, Sha3Node}, }; use super::{BindNamespace, DigestAlgorithm, Element, Index, Namespace, Namespaced, NodeValue}; diff --git a/primitives/src/merkle_tree/namespaced_merkle_tree/mod.rs b/primitives/src/merkle_tree/namespaced_merkle_tree/mod.rs index 938c08036..9742aa376 100644 --- a/primitives/src/merkle_tree/namespaced_merkle_tree/mod.rs +++ b/primitives/src/merkle_tree/namespaced_merkle_tree/mod.rs @@ -365,7 +365,7 @@ mod nmt_tests { use typenum::U2; use super::*; - use crate::merkle_tree::examples::{Sha3Digest, Sha3Node}; + use crate::merkle_tree::prelude::{Sha3Digest, Sha3Node}; type NamespaceId = u64; type Hasher = NamespacedHasher; diff --git a/primitives/src/merkle_tree/prelude.rs b/primitives/src/merkle_tree/prelude.rs index 81601ffbc..25b33e1a0 100644 --- a/primitives/src/merkle_tree/prelude.rs +++ b/primitives/src/merkle_tree/prelude.rs @@ -4,7 +4,7 @@ // You should have received a copy of the MIT License // along with the Jellyfish library. If not, see . -//! Prelude +//! Prelude. Also provides sample instantiations of merkle trees. pub use crate::{ impl_to_traversal_path_biguint, impl_to_traversal_path_primitives, @@ -16,16 +16,20 @@ pub use crate::{ }, }; +use super::light_weight::LightWeightMerkleTree; use crate::{ errors::PrimitivesError, rescue::{sponge::RescueCRHF, RescueParameter}, }; -use ark_std::marker::PhantomData; +use ark_serialize::{ + CanonicalDeserialize, CanonicalSerialize, Compress, Read, SerializationError, Valid, Validate, + Write, +}; +use ark_std::{marker::PhantomData, vec::Vec}; use num_bigint::BigUint; +use sha3::{Digest, Sha3_256}; use typenum::U3; -use super::light_weight::LightWeightMerkleTree; - /// Wrapper for rescue hash function #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct RescueHash { @@ -73,3 +77,72 @@ impl DigestAlgorithm for RescueHash { /// Example instantiation of a SparseMerkleTree indexed by I pub type RescueSparseMerkleTree = UniversalMerkleTree, I, U3, F>; + +/// Update the array length here +#[derive(Default, Eq, PartialEq, Clone, Copy, Debug, Ord, PartialOrd, Hash)] +pub struct Sha3Node(pub(crate) [u8; 32]); + +impl AsRef<[u8]> for Sha3Node { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl CanonicalSerialize for Sha3Node { + fn serialize_with_mode( + &self, + mut writer: W, + _compress: Compress, + ) -> Result<(), SerializationError> { + writer.write_all(&self.0)?; + Ok(()) + } + + fn serialized_size(&self, _compress: Compress) -> usize { + 32 + } +} +impl CanonicalDeserialize for Sha3Node { + fn deserialize_with_mode( + mut reader: R, + _compress: Compress, + _validate: Validate, + ) -> Result { + let mut ret = [0u8; 32]; + reader.read_exact(&mut ret)?; + Ok(Sha3Node(ret)) + } +} + +impl Valid for Sha3Node { + fn check(&self) -> Result<(), SerializationError> { + Ok(()) + } +} + +/// Wrapper for SHA3_512 hash function +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +pub struct Sha3Digest(); + +impl DigestAlgorithm for Sha3Digest { + fn digest(data: &[Sha3Node]) -> Result { + let mut hasher = Sha3_256::new(); + for value in data { + hasher.update(value); + } + Ok(Sha3Node(hasher.finalize().into())) + } + + fn digest_leaf(_pos: &I, elem: &E) -> Result { + let mut writer = Vec::new(); + elem.serialize_compressed(&mut writer).unwrap(); + let mut hasher = Sha3_256::new(); + hasher.update(writer); + Ok(Sha3Node(hasher.finalize().into())) + } +} + +/// Merkle tree using SHA3 hash +pub type SHA3MerkleTree = MerkleTree; +/// Light weight merkle tree using SHA3 hash +pub type LightWeightSHA3MerkleTree = LightWeightMerkleTree;