Skip to content

Commit

Permalink
chore: miscellaneous fixes requested. (#454)
Browse files Browse the repository at this point in the history
* pp for signaturescheme should be clonable & clippy

* exporting sha3 merkle tree instantiation
  • Loading branch information
mrain authored Jan 10, 2024
1 parent e33a739 commit 20f6929
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 81 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>;
2 changes: 1 addition & 1 deletion primitives/src/signatures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait SignatureScheme: Clone + Send + Sync + 'static {
+ Eq;

/// Public Parameter
type PublicParameter: Debug + PartialEq + for<'a> Deserialize<'a> + Serialize;
type PublicParameter: Debug + Clone + PartialEq + for<'a> Deserialize<'a> + Serialize;

/// Signature
type Signature: Debug
Expand Down
5 changes: 5 additions & 0 deletions relation/src/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ mod cmp;
mod emulated;
mod logic;
mod range;
#[allow(unused_imports)]
pub use arithmetic::*;
#[allow(unused_imports)]
pub use cmp::*;
#[allow(unused_imports)]
pub use emulated::*;
#[allow(unused_imports)]
pub use logic::*;
#[allow(unused_imports)]
pub use range::*;

// Helper functions
Expand Down
1 change: 1 addition & 0 deletions utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use ark_std::{
};

pub use conversion::*;
#[allow(unused_imports)]
pub use macros::*;
pub use multi_pairing::*;
pub use serialize::*;
Expand Down

0 comments on commit 20f6929

Please sign in to comment.