diff --git a/crypto-primitives/src/lib.rs b/crypto-primitives/src/lib.rs index b987ef9..b33aef6 100644 --- a/crypto-primitives/src/lib.rs +++ b/crypto-primitives/src/lib.rs @@ -6,7 +6,7 @@ extern crate derivative; pub mod commitment; pub mod crh; -pub mod mht; +pub mod merkle_tree; pub mod nizk; pub mod prf; pub mod signature; @@ -14,7 +14,7 @@ pub mod signature; pub use self::{ commitment::CommitmentScheme, crh::FixedLengthCRH, - mht::{HashMembershipProof, MerkleHashTree}, + merkle_tree::{MerkleTreePath, MerkleHashTree}, nizk::NIZK, prf::PRF, signature::SignatureScheme, @@ -24,7 +24,7 @@ pub use self::{ pub use self::{ commitment::CommitmentGadget, crh::FixedLengthCRHGadget, - mht::constraints::{MerklePath, MerklePathVerifierGadget}, + merkle_tree::constraints::MerkleTreePathGadget, nizk::NIZKVerifierGadget, prf::PRFGadget, signature::SigRandomizePkGadget, diff --git a/crypto-primitives/src/mht/constraints.rs b/crypto-primitives/src/merkle_tree/constraints.rs similarity index 85% rename from crypto-primitives/src/mht/constraints.rs rename to crypto-primitives/src/merkle_tree/constraints.rs index 3d6a4ed..16e40aa 100644 --- a/crypto-primitives/src/mht/constraints.rs +++ b/crypto-primitives/src/merkle_tree/constraints.rs @@ -3,63 +3,51 @@ use r1cs_core::{ConstraintSystem, SynthesisError}; use r1cs_std::prelude::*; use r1cs_std::boolean::AllocatedBit; -use crate::mht::*; +use crate::merkle_tree::*; use crate::crh::{FixedLengthCRH, FixedLengthCRHGadget}; -use std::{borrow::Borrow, marker::PhantomData}; +use std::borrow::Borrow; -pub struct MerklePath
+pub struct MerkleTreePathGadget
where
- P: MHTParameters,
+ P: MerkleTreeConfig,
HGadget: FixedLengthCRHGadget
+impl MerkleTreePathGadget
where
- P: MHTParameters,
- ConstraintF: Field,
- CRHGadget: FixedLengthCRHGadget ,
- _f: PhantomData MerklePathVerifierGadget
-where
- P: MHTParameters,
+ P: MerkleTreeConfig,
ConstraintF: Field,
CRHGadget: FixedLengthCRHGadget ,
) -> Result<(), SynthesisError> {
- Self::conditionally_check_membership(
+ self.conditionally_check_membership(
cs,
parameters,
root,
leaf,
- witness,
&Boolean::Constant(true),
)
}
pub fn conditionally_check_membership ,
should_enforce: &Boolean,
) -> Result<(), SynthesisError> {
- assert_eq!(witness.path.len(), P::HEIGHT - 1);
+ assert_eq!(self.path.len(), P::HEIGHT - 1);
// Check that the hash of the given leaf matches the leaf hash in the membership
// proof.
let leaf_bits = leaf.to_bytes(&mut cs.ns(|| "leaf_to_bytes"))?;
@@ -71,21 +59,21 @@ where
// Check if leaf is one of the bottom-most siblings.
let leaf_is_left = AllocatedBit::alloc(&mut cs.ns(|| "leaf_is_left"), || {
- Ok(leaf_hash == witness.path[0].0)
+ Ok(leaf_hash == self.path[0].0)
})?
.into();
CRHGadget::OutputGadget::conditional_enforce_equal_or(
&mut cs.ns(|| "check_leaf_is_left"),
&leaf_is_left,
&leaf_hash,
- &witness.path[0].0,
- &witness.path[0].1,
+ &self.path[0].0,
+ &self.path[0].1,
should_enforce,
)?;
// Check levels between leaf level and root.
let mut previous_hash = leaf_hash;
- for (i, &(ref left_hash, ref right_hash)) in witness.path.iter().enumerate() {
+ for (i, &(ref left_hash, ref right_hash)) in self.path.iter().enumerate() {
// Check if the previous_hash matches the correct current hash.
let previous_is_left =
AllocatedBit::alloc(&mut cs.ns(|| format!("previous_is_left_{}", i)), || {
@@ -138,10 +126,10 @@ where
HG::check_evaluation_gadget(cs, parameters, &bytes)
}
-impl AllocGadget
+impl AllocGadget
where
- P: MHTParameters,
+ P: MerkleTreeConfig,
HGadget: FixedLengthCRHGadget {
+pub type MerkleTreeParams = < ::H as FixedLengthCRH>::Parameters;
+pub type MerkleTreeDigest = < ::H as FixedLengthCRH>::Output;
+
+
+impl {
fn default() -> Self {
let mut path = Vec::with_capacity(P::HEIGHT as usize);
for _i in 1..P::HEIGHT as usize {
@@ -35,7 +39,7 @@ impl {
}
}
-impl {
+impl {
pub fn verify {
}
}
-pub struct MerkleHashTree {
+impl {
pub const HEIGHT: u8 = P::HEIGHT as u8;
pub fn blank(parameters: Rc< {
}
pub fn new {
&self,
index: usize,
leaf: &L,
- ) -> Result {
// Check that the given index corresponds to the correct leaf.
if leaf_hash != self.tree[tree_index] {
- Err(MHTError::IncorrectLeafIndex(tree_index))?
+ Err(MerkleTreeError::IncorrectLeafIndex(tree_index))?
}
// Iterate from the leaf up to the root, storing all intermediate hash values.
@@ -219,9 +223,9 @@ impl {
}
end_timer!(prove_time);
if path.len() != (Self::HEIGHT - 1) as usize {
- Err(MHTError::IncorrectPathLength(path.len()))?
+ Err(MerkleTreeError::IncorrectPathLength(path.len()))?
} else {
- Ok(HashMembershipProof {
+ Ok(MerkleTreePath {
path,
})
}
@@ -229,22 +233,22 @@ impl {
}
#[derive(Debug)]
-pub enum MHTError {
+pub enum MerkleTreeError {
IncorrectLeafIndex(usize),
IncorrectPathLength(usize),
}
-impl std::fmt::Display for MHTError {
+impl std::fmt::Display for MerkleTreeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let msg = match self {
- MHTError::IncorrectLeafIndex(index) => format!("incorrect leaf index: {}", index),
- MHTError::IncorrectPathLength(len) => format!("incorrect path length: {}", len),
+ MerkleTreeError::IncorrectLeafIndex(index) => format!("incorrect leaf index: {}", index),
+ MerkleTreeError::IncorrectPathLength(len) => format!("incorrect path length: {}", len),
};
write!(f, "{}", msg)
}
}
-impl std::error::Error for MHTError {
+impl std::error::Error for MerkleTreeError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
@@ -357,7 +361,7 @@ pub(crate) fn hash_empty