Skip to content

Commit

Permalink
address commments for verify_merkle_proof
Browse files Browse the repository at this point in the history
  • Loading branch information
mrain committed Oct 22, 2024
1 parent 0e10f6c commit 11e0f4d
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions merkle_tree/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,37 +167,39 @@ where
T: NodeValue,
H: DigestAlgorithm<E, I, T>,
{
if proof.len() == commitment.height() {
let init = if let Some(elem) = element {
H::digest_leaf(pos, elem)?
} else {
T::default()
};
let computed_root = pos
.to_traversal_path(commitment.height())
.iter()
.zip(proof.iter())
.try_fold(
init,
|val, (branch, values)| -> Result<T, MerkleTreeError> {
if values.len() == 0 {
Ok(T::default())
} else {
let mut data = values.clone();
data.insert(*branch, val);
H::digest(&data)
}
},
)?;
if computed_root == commitment.digest() {
Ok(Ok(()))
} else {
Ok(Err(()))
}
} else {
Err(MerkleTreeError::InconsistentStructureError(
if proof.len() != commitment.height() {
return Err(MerkleTreeError::InconsistentStructureError(
"Inconsistent Merkle tree height.".to_string(),
))
));
}

let init = if let Some(elem) = element {
H::digest_leaf(pos, elem)?
} else {
T::default()
};
let mut data = [T::default(); ARITY];
let computed_root = pos
.to_traversal_path(commitment.height())
.iter()
.zip(proof.iter())
.try_fold(
init,
|val, (branch, values)| -> Result<T, MerkleTreeError> {
if values.len() == 0 {
Ok(T::default())
} else {
data[..*branch].copy_from_slice(&values[..*branch]);
data[*branch] = val;
data[*branch + 1..].copy_from_slice(&values[*branch..]);
H::digest(&data)
}
},
)?;
if computed_root == commitment.digest() {
Ok(Ok(()))
} else {
Ok(Err(()))
}
}

Expand Down

0 comments on commit 11e0f4d

Please sign in to comment.