Skip to content

Commit

Permalink
exporting sha3 merkle tree instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrain committed Jan 10, 2024
1 parent 88b4a72 commit 7532d20
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 80 deletions.
75 changes: 1 addition & 74 deletions primitives/src/merkle_tree/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,70 +34,3 @@ impl<F: RescueParameter> DigestAlgorithm<Interval<F>, u64, F> for RescueHash<F>
/// Interval merkle tree instantiation for interval merkle tree using Rescue
/// hash function.
pub type IntervalMerkleTree<F> = MerkleTree<Interval<F>, RescueHash<F>, 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<W: Write>(
&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<R: Read>(
mut reader: R,
_compress: Compress,
_validate: Validate,
) -> Result<Self, SerializationError> {
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<E: Element + CanonicalSerialize, I: Index> DigestAlgorithm<E, I, Sha3Node> for Sha3Digest {
fn digest(data: &[Sha3Node]) -> Result<Sha3Node, PrimitivesError> {
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<Sha3Node, PrimitivesError> {
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<E> = MerkleTree<E, Sha3Digest, u64, U3, Sha3Node>;
2 changes: 1 addition & 1 deletion primitives/src/merkle_tree/namespaced_merkle_tree/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/merkle_tree/namespaced_merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sha3Digest, Leaf, NamespaceId, Sha3Node, u64>;
Expand Down
81 changes: 77 additions & 4 deletions primitives/src/merkle_tree/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// You should have received a copy of the MIT License
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

//! Prelude
//! Prelude. Also provides sample instantiations of merkle trees.
pub use crate::{
impl_to_traversal_path_biguint, impl_to_traversal_path_primitives,
Expand All @@ -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<F: RescueParameter> {
Expand Down Expand Up @@ -73,3 +77,72 @@ impl<F: RescueParameter> DigestAlgorithm<F, F, F> for RescueHash<F> {

/// Example instantiation of a SparseMerkleTree indexed by I
pub type RescueSparseMerkleTree<I, F> = UniversalMerkleTree<F, RescueHash<F>, 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<W: Write>(
&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<R: Read>(
mut reader: R,
_compress: Compress,
_validate: Validate,
) -> Result<Self, SerializationError> {
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<E: Element + CanonicalSerialize, I: Index> DigestAlgorithm<E, I, Sha3Node> for Sha3Digest {
fn digest(data: &[Sha3Node]) -> Result<Sha3Node, PrimitivesError> {
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<Sha3Node, PrimitivesError> {
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<E> = MerkleTree<E, Sha3Digest, u64, U3, Sha3Node>;
/// Light weight merkle tree using SHA3 hash
pub type LightWeightSHA3MerkleTree<E> = LightWeightMerkleTree<E, Sha3Digest, u64, U3, Sha3Node>;

0 comments on commit 7532d20

Please sign in to comment.