mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-11 16:41:29 +01:00
fix: add validation to NodeIndex constructor and remove BitIterator
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use super::mmr::{Mmr, MmrPeaks};
|
||||
use super::{
|
||||
BTreeMap, BTreeSet, EmptySubtreeRoots, MerkleError, MerklePath, MerklePathSet, MerkleTree,
|
||||
NodeIndex, RootPath, Rpo256, RpoDigest, SimpleSmt, ValuePath, Vec, Word,
|
||||
BTreeMap, EmptySubtreeRoots, MerkleError, MerklePath, MerklePathSet, MerkleTree, NodeIndex,
|
||||
RootPath, Rpo256, RpoDigest, SimpleSmt, ValuePath, Vec, Word,
|
||||
};
|
||||
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
|
||||
|
||||
@@ -53,15 +53,19 @@ pub struct Node {
|
||||
///
|
||||
/// // every leaf except the last are the same
|
||||
/// for i in 0..7 {
|
||||
/// let d0 = store.get_node(ROOT0, NodeIndex::new(3, i)).unwrap();
|
||||
/// let d1 = store.get_node(ROOT1, NodeIndex::new(3, i)).unwrap();
|
||||
/// let idx0 = NodeIndex::new(3, i).unwrap();
|
||||
/// let d0 = store.get_node(ROOT0, idx0).unwrap();
|
||||
/// let idx1 = NodeIndex::new(3, i).unwrap();
|
||||
/// let d1 = store.get_node(ROOT1, idx1).unwrap();
|
||||
/// assert_eq!(d0, d1, "Both trees have the same leaf at pos {i}");
|
||||
/// }
|
||||
///
|
||||
/// // The leafs A-B-C-D are the same for both trees, so are their 2 immediate parents
|
||||
/// for i in 0..4 {
|
||||
/// let d0 = store.get_path(ROOT0, NodeIndex::new(3, i)).unwrap();
|
||||
/// let d1 = store.get_path(ROOT1, NodeIndex::new(3, i)).unwrap();
|
||||
/// let idx0 = NodeIndex::new(3, i).unwrap();
|
||||
/// let d0 = store.get_path(ROOT0, idx0).unwrap();
|
||||
/// let idx1 = NodeIndex::new(3, i).unwrap();
|
||||
/// let d1 = store.get_path(ROOT1, idx1).unwrap();
|
||||
/// assert_eq!(d0.path[0..2], d1.path[0..2], "Both sub-trees are equal up to two levels");
|
||||
/// }
|
||||
///
|
||||
@@ -184,12 +188,14 @@ impl MerkleStore {
|
||||
.get(&hash)
|
||||
.ok_or(MerkleError::RootNotInStore(hash.into()))?;
|
||||
|
||||
for bit in index.bit_iterator().rev() {
|
||||
for i in (0..index.depth()).rev() {
|
||||
let node = self
|
||||
.nodes
|
||||
.get(&hash)
|
||||
.ok_or(MerkleError::NodeNotInStore(hash.into(), index))?;
|
||||
hash = if bit { node.right } else { node.left }
|
||||
|
||||
let bit = (index.value() >> i) & 1;
|
||||
hash = if bit == 0 { node.left } else { node.right }
|
||||
}
|
||||
|
||||
Ok(hash.into())
|
||||
@@ -213,18 +219,19 @@ impl MerkleStore {
|
||||
.get(&hash)
|
||||
.ok_or(MerkleError::RootNotInStore(hash.into()))?;
|
||||
|
||||
for bit in index.bit_iterator().rev() {
|
||||
for i in (0..index.depth()).rev() {
|
||||
let node = self
|
||||
.nodes
|
||||
.get(&hash)
|
||||
.ok_or(MerkleError::NodeNotInStore(hash.into(), index))?;
|
||||
|
||||
hash = if bit {
|
||||
path.push(node.left.into());
|
||||
node.right
|
||||
} else {
|
||||
let bit = (index.value() >> i) & 1;
|
||||
hash = if bit == 0 {
|
||||
path.push(node.right.into());
|
||||
node.left
|
||||
} else {
|
||||
path.push(node.left.into());
|
||||
node.right
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,7 +322,7 @@ impl MerkleStore {
|
||||
mut node: Word,
|
||||
path: MerklePath,
|
||||
) -> Result<Word, MerkleError> {
|
||||
let mut index = NodeIndex::new(self.nodes.len() as u8, index_value);
|
||||
let mut index = NodeIndex::new(path.len() as u8, index_value)?;
|
||||
|
||||
for sibling in path {
|
||||
let (left, right) = match index.is_value_odd() {
|
||||
@@ -344,33 +351,14 @@ impl MerkleStore {
|
||||
/// into the store.
|
||||
///
|
||||
/// For further reference, check [MerkleStore::add_merkle_path].
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Every path must resolve to the same root, otherwise this will return an `ConflictingRoots`
|
||||
/// error.
|
||||
pub fn add_merkle_paths<I>(&mut self, paths: I) -> Result<Word, MerkleError>
|
||||
pub fn add_merkle_paths<I>(&mut self, paths: I) -> Result<(), MerkleError>
|
||||
where
|
||||
I: IntoIterator<Item = (u64, Word, MerklePath)>,
|
||||
{
|
||||
let paths: Vec<(u64, Word, MerklePath)> = paths.into_iter().collect();
|
||||
|
||||
let roots: BTreeSet<RpoDigest> = paths
|
||||
.iter()
|
||||
.map(|(index, node, path)| path.compute_root(*index, *node).into())
|
||||
.collect();
|
||||
|
||||
if roots.len() != 1 {
|
||||
return Err(MerkleError::ConflictingRoots(
|
||||
roots.iter().map(|v| Word::from(*v)).collect(),
|
||||
));
|
||||
}
|
||||
|
||||
for (index_value, node, path) in paths {
|
||||
for (index_value, node, path) in paths.into_iter() {
|
||||
self.add_merkle_path(index_value, node, path)?;
|
||||
}
|
||||
|
||||
Ok(roots.iter().next().unwrap().into())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Appends the provided [MerklePathSet] into the store.
|
||||
@@ -432,15 +420,9 @@ impl MerkleStore {
|
||||
let root2: RpoDigest = root2.into();
|
||||
|
||||
if !self.nodes.contains_key(&root1) {
|
||||
Err(MerkleError::NodeNotInStore(
|
||||
root1.into(),
|
||||
NodeIndex::new(0, 0),
|
||||
))
|
||||
Err(MerkleError::NodeNotInStore(root1.into(), NodeIndex::root()))
|
||||
} else if !self.nodes.contains_key(&root1) {
|
||||
Err(MerkleError::NodeNotInStore(
|
||||
root2.into(),
|
||||
NodeIndex::new(0, 0),
|
||||
))
|
||||
Err(MerkleError::NodeNotInStore(root2.into(), NodeIndex::root()))
|
||||
} else {
|
||||
let parent: Word = Rpo256::merge(&[root1, root2]).into();
|
||||
self.nodes.insert(
|
||||
|
||||
@@ -22,12 +22,12 @@ fn test_root_not_in_store() -> Result<(), MerkleError> {
|
||||
let mtree = MerkleTree::new(LEAVES4.to_vec())?;
|
||||
let store = MerkleStore::default().with_merkle_tree(LEAVES4)?;
|
||||
assert_eq!(
|
||||
store.get_node(LEAVES4[0], NodeIndex::new(mtree.depth(), 0)),
|
||||
store.get_node(LEAVES4[0], NodeIndex::make(mtree.depth(), 0)),
|
||||
Err(MerkleError::RootNotInStore(LEAVES4[0])),
|
||||
"Leaf 0 is not a root"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_path(LEAVES4[0], NodeIndex::new(mtree.depth(), 0)),
|
||||
store.get_path(LEAVES4[0], NodeIndex::make(mtree.depth(), 0)),
|
||||
Err(MerkleError::RootNotInStore(LEAVES4[0])),
|
||||
"Leaf 0 is not a root"
|
||||
);
|
||||
@@ -45,22 +45,22 @@ fn test_merkle_tree() -> Result<(), MerkleError> {
|
||||
// STORE LEAVES ARE CORRECT ==============================================================
|
||||
// checks the leaves in the store corresponds to the expected values
|
||||
assert_eq!(
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 0)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 0)),
|
||||
Ok(LEAVES4[0]),
|
||||
"node 0 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 1)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 1)),
|
||||
Ok(LEAVES4[1]),
|
||||
"node 1 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 2)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 2)),
|
||||
Ok(LEAVES4[2]),
|
||||
"node 2 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 3)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 3)),
|
||||
Ok(LEAVES4[3]),
|
||||
"node 3 must be in the tree"
|
||||
);
|
||||
@@ -68,76 +68,76 @@ fn test_merkle_tree() -> Result<(), MerkleError> {
|
||||
// STORE LEAVES MATCH TREE ===============================================================
|
||||
// sanity check the values returned by the store and the tree
|
||||
assert_eq!(
|
||||
mtree.get_node(NodeIndex::new(mtree.depth(), 0)),
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 0)),
|
||||
mtree.get_node(NodeIndex::make(mtree.depth(), 0)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 0)),
|
||||
"node 0 must be the same for both MerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_node(NodeIndex::new(mtree.depth(), 1)),
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 1)),
|
||||
mtree.get_node(NodeIndex::make(mtree.depth(), 1)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 1)),
|
||||
"node 1 must be the same for both MerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_node(NodeIndex::new(mtree.depth(), 2)),
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 2)),
|
||||
mtree.get_node(NodeIndex::make(mtree.depth(), 2)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 2)),
|
||||
"node 2 must be the same for both MerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_node(NodeIndex::new(mtree.depth(), 3)),
|
||||
store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 3)),
|
||||
mtree.get_node(NodeIndex::make(mtree.depth(), 3)),
|
||||
store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 3)),
|
||||
"node 3 must be the same for both MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
// STORE MERKLE PATH MATCHS ==============================================================
|
||||
// assert the merkle path returned by the store is the same as the one in the tree
|
||||
let result = store
|
||||
.get_path(mtree.root(), NodeIndex::new(mtree.depth(), 0))
|
||||
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 0))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[0], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_path(NodeIndex::new(mtree.depth(), 0)),
|
||||
mtree.get_path(NodeIndex::make(mtree.depth(), 0)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(mtree.root(), NodeIndex::new(mtree.depth(), 1))
|
||||
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 1))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[1], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_path(NodeIndex::new(mtree.depth(), 1)),
|
||||
mtree.get_path(NodeIndex::make(mtree.depth(), 1)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(mtree.root(), NodeIndex::new(mtree.depth(), 2))
|
||||
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 2))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[2], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_path(NodeIndex::new(mtree.depth(), 2)),
|
||||
mtree.get_path(NodeIndex::make(mtree.depth(), 2)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(mtree.root(), NodeIndex::new(mtree.depth(), 3))
|
||||
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 3))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[3], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
mtree.get_path(NodeIndex::new(mtree.depth(), 3)),
|
||||
mtree.get_path(NodeIndex::make(mtree.depth(), 3)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
@@ -153,7 +153,7 @@ fn test_empty_roots() {
|
||||
for depth in 0..255 {
|
||||
root = Rpo256::merge(&[root; 2]);
|
||||
assert!(
|
||||
store.get_node(root.into(), NodeIndex::new(0, 0)).is_ok(),
|
||||
store.get_node(root.into(), NodeIndex::make(0, 0)).is_ok(),
|
||||
"The root of the empty tree of depth {depth} must be registered"
|
||||
);
|
||||
}
|
||||
@@ -169,7 +169,7 @@ fn test_leaf_paths_for_empty_trees() -> Result<(), MerkleError> {
|
||||
for depth in 1..64 {
|
||||
let smt = SimpleSmt::new(depth)?;
|
||||
|
||||
let index = NodeIndex::new(depth, 0);
|
||||
let index = NodeIndex::make(depth, 0);
|
||||
let store_path = store.get_path(smt.root(), index)?;
|
||||
let smt_path = smt.get_path(index)?;
|
||||
assert_eq!(
|
||||
@@ -181,7 +181,7 @@ fn test_leaf_paths_for_empty_trees() -> Result<(), MerkleError> {
|
||||
"the returned merkle path does not match the computed values"
|
||||
);
|
||||
assert_eq!(
|
||||
store_path.path.compute_root(depth.into(), EMPTY),
|
||||
store_path.path.compute_root(depth.into(), EMPTY).unwrap(),
|
||||
smt.root(),
|
||||
"computed root from the path must match the empty tree root"
|
||||
);
|
||||
@@ -197,7 +197,7 @@ fn test_get_invalid_node() {
|
||||
store
|
||||
.add_merkle_tree(LEAVES4.to_vec())
|
||||
.expect("adding a merkle tree to the store must work");
|
||||
let _ = store.get_node(mtree.root(), NodeIndex::new(mtree.depth(), 3));
|
||||
let _ = store.get_node(mtree.root(), NodeIndex::make(mtree.depth(), 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -211,18 +211,18 @@ fn test_add_sparse_merkle_tree_one_level() -> Result<(), MerkleError> {
|
||||
.with_leaves(keys2.into_iter().zip(leaves2.into_iter()))
|
||||
.unwrap();
|
||||
|
||||
let idx = NodeIndex::new(1, 0);
|
||||
assert_eq!(smt.get_node(&idx).unwrap(), leaves2[0]);
|
||||
let idx = NodeIndex::make(1, 0);
|
||||
assert_eq!(smt.get_node(idx).unwrap(), leaves2[0]);
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), idx).unwrap(),
|
||||
smt.get_node(&idx).unwrap()
|
||||
smt.get_node(idx).unwrap()
|
||||
);
|
||||
|
||||
let idx = NodeIndex::new(1, 1);
|
||||
assert_eq!(smt.get_node(&idx).unwrap(), leaves2[1]);
|
||||
let idx = NodeIndex::make(1, 1);
|
||||
assert_eq!(smt.get_node(idx).unwrap(), leaves2[1]);
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), idx).unwrap(),
|
||||
smt.get_node(&idx).unwrap()
|
||||
smt.get_node(idx).unwrap()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
@@ -244,27 +244,27 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
|
||||
// STORE LEAVES ARE CORRECT ==============================================================
|
||||
// checks the leaves in the store corresponds to the expected values
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 0)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 0)),
|
||||
Ok(LEAVES4[0]),
|
||||
"node 0 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 1)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 1)),
|
||||
Ok(LEAVES4[1]),
|
||||
"node 1 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 2)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 2)),
|
||||
Ok(LEAVES4[2]),
|
||||
"node 2 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 3)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 3)),
|
||||
Ok(LEAVES4[3]),
|
||||
"node 3 must be in the tree"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 4)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 4)),
|
||||
Ok(EMPTY),
|
||||
"unmodified node 4 must be ZERO"
|
||||
);
|
||||
@@ -272,94 +272,94 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
|
||||
// STORE LEAVES MATCH TREE ===============================================================
|
||||
// sanity check the values returned by the store and the tree
|
||||
assert_eq!(
|
||||
smt.get_node(&NodeIndex::new(smt.depth(), 0)),
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 0)),
|
||||
smt.get_node(NodeIndex::make(smt.depth(), 0)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 0)),
|
||||
"node 0 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_node(&NodeIndex::new(smt.depth(), 1)),
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 1)),
|
||||
smt.get_node(NodeIndex::make(smt.depth(), 1)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 1)),
|
||||
"node 1 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_node(&NodeIndex::new(smt.depth(), 2)),
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 2)),
|
||||
smt.get_node(NodeIndex::make(smt.depth(), 2)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 2)),
|
||||
"node 2 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_node(&NodeIndex::new(smt.depth(), 3)),
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 3)),
|
||||
smt.get_node(NodeIndex::make(smt.depth(), 3)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 3)),
|
||||
"node 3 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_node(&NodeIndex::new(smt.depth(), 4)),
|
||||
store.get_node(smt.root(), NodeIndex::new(smt.depth(), 4)),
|
||||
smt.get_node(NodeIndex::make(smt.depth(), 4)),
|
||||
store.get_node(smt.root(), NodeIndex::make(smt.depth(), 4)),
|
||||
"node 4 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
// STORE MERKLE PATH MATCHS ==============================================================
|
||||
// assert the merkle path returned by the store is the same as the one in the tree
|
||||
let result = store
|
||||
.get_path(smt.root(), NodeIndex::new(smt.depth(), 0))
|
||||
.get_path(smt.root(), NodeIndex::make(smt.depth(), 0))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[0], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_path(NodeIndex::new(smt.depth(), 0)),
|
||||
smt.get_path(NodeIndex::make(smt.depth(), 0)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(smt.root(), NodeIndex::new(smt.depth(), 1))
|
||||
.get_path(smt.root(), NodeIndex::make(smt.depth(), 1))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[1], result.value,
|
||||
"Value for merkle path at index 1 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_path(NodeIndex::new(smt.depth(), 1)),
|
||||
smt.get_path(NodeIndex::make(smt.depth(), 1)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(smt.root(), NodeIndex::new(smt.depth(), 2))
|
||||
.get_path(smt.root(), NodeIndex::make(smt.depth(), 2))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[2], result.value,
|
||||
"Value for merkle path at index 2 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_path(NodeIndex::new(smt.depth(), 2)),
|
||||
smt.get_path(NodeIndex::make(smt.depth(), 2)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 2 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(smt.root(), NodeIndex::new(smt.depth(), 3))
|
||||
.get_path(smt.root(), NodeIndex::make(smt.depth(), 3))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[3], result.value,
|
||||
"Value for merkle path at index 3 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_path(NodeIndex::new(smt.depth(), 3)),
|
||||
smt.get_path(NodeIndex::make(smt.depth(), 3)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 3 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(smt.root(), NodeIndex::new(smt.depth(), 4))
|
||||
.get_path(smt.root(), NodeIndex::make(smt.depth(), 4))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
EMPTY, result.value,
|
||||
"Value for merkle path at index 4 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
smt.get_path(NodeIndex::new(smt.depth(), 4)),
|
||||
smt.get_path(NodeIndex::make(smt.depth(), 4)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 4 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
@@ -372,16 +372,16 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> {
|
||||
let mtree = MerkleTree::new(LEAVES4.to_vec())?;
|
||||
|
||||
let i0 = 0;
|
||||
let p0 = mtree.get_path(NodeIndex::new(2, i0)).unwrap();
|
||||
let p0 = mtree.get_path(NodeIndex::make(2, i0)).unwrap();
|
||||
|
||||
let i1 = 1;
|
||||
let p1 = mtree.get_path(NodeIndex::new(2, i1)).unwrap();
|
||||
let p1 = mtree.get_path(NodeIndex::make(2, i1)).unwrap();
|
||||
|
||||
let i2 = 2;
|
||||
let p2 = mtree.get_path(NodeIndex::new(2, i2)).unwrap();
|
||||
let p2 = mtree.get_path(NodeIndex::make(2, i2)).unwrap();
|
||||
|
||||
let i3 = 3;
|
||||
let p3 = mtree.get_path(NodeIndex::new(2, i3)).unwrap();
|
||||
let p3 = mtree.get_path(NodeIndex::make(2, i3)).unwrap();
|
||||
|
||||
let paths = [
|
||||
(i0, LEAVES4[i0 as usize], p0),
|
||||
@@ -401,22 +401,22 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> {
|
||||
// STORE LEAVES ARE CORRECT ==============================================================
|
||||
// checks the leaves in the store corresponds to the expected values
|
||||
assert_eq!(
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 0)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 0)),
|
||||
Ok(LEAVES4[0]),
|
||||
"node 0 must be in the set"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 1)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 1)),
|
||||
Ok(LEAVES4[1]),
|
||||
"node 1 must be in the set"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 2)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 2)),
|
||||
Ok(LEAVES4[2]),
|
||||
"node 2 must be in the set"
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 3)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 3)),
|
||||
Ok(LEAVES4[3]),
|
||||
"node 3 must be in the set"
|
||||
);
|
||||
@@ -424,76 +424,76 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> {
|
||||
// STORE LEAVES MATCH SET ================================================================
|
||||
// sanity check the values returned by the store and the set
|
||||
assert_eq!(
|
||||
set.get_node(NodeIndex::new(set.depth(), 0)),
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 0)),
|
||||
set.get_node(NodeIndex::make(set.depth(), 0)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 0)),
|
||||
"node 0 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_node(NodeIndex::new(set.depth(), 1)),
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 1)),
|
||||
set.get_node(NodeIndex::make(set.depth(), 1)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 1)),
|
||||
"node 1 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_node(NodeIndex::new(set.depth(), 2)),
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 2)),
|
||||
set.get_node(NodeIndex::make(set.depth(), 2)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 2)),
|
||||
"node 2 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_node(NodeIndex::new(set.depth(), 3)),
|
||||
store.get_node(set.root(), NodeIndex::new(set.depth(), 3)),
|
||||
set.get_node(NodeIndex::make(set.depth(), 3)),
|
||||
store.get_node(set.root(), NodeIndex::make(set.depth(), 3)),
|
||||
"node 3 must be the same for both SparseMerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
// STORE MERKLE PATH MATCHS ==============================================================
|
||||
// assert the merkle path returned by the store is the same as the one in the set
|
||||
let result = store
|
||||
.get_path(set.root(), NodeIndex::new(set.depth(), 0))
|
||||
.get_path(set.root(), NodeIndex::make(set.depth(), 0))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[0], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_path(NodeIndex::new(set.depth(), 0)),
|
||||
set.get_path(NodeIndex::make(set.depth(), 0)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(set.root(), NodeIndex::new(set.depth(), 1))
|
||||
.get_path(set.root(), NodeIndex::make(set.depth(), 1))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[1], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_path(NodeIndex::new(set.depth(), 1)),
|
||||
set.get_path(NodeIndex::make(set.depth(), 1)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(set.root(), NodeIndex::new(set.depth(), 2))
|
||||
.get_path(set.root(), NodeIndex::make(set.depth(), 2))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[2], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_path(NodeIndex::new(set.depth(), 2)),
|
||||
set.get_path(NodeIndex::make(set.depth(), 2)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
|
||||
let result = store
|
||||
.get_path(set.root(), NodeIndex::new(set.depth(), 3))
|
||||
.get_path(set.root(), NodeIndex::make(set.depth(), 3))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
LEAVES4[3], result.value,
|
||||
"Value for merkle path at index 0 must match leaf value"
|
||||
);
|
||||
assert_eq!(
|
||||
set.get_path(NodeIndex::new(set.depth(), 3)),
|
||||
set.get_path(NodeIndex::make(set.depth(), 3)),
|
||||
Ok(result.path),
|
||||
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
||||
);
|
||||
@@ -550,7 +550,7 @@ fn store_path_opens_from_leaf() {
|
||||
.with_merkle_tree([a, b, c, d, e, f, g, h])
|
||||
.unwrap();
|
||||
let path = store
|
||||
.get_path(root.into(), NodeIndex::new(3, 1))
|
||||
.get_path(root.into(), NodeIndex::make(3, 1))
|
||||
.unwrap()
|
||||
.path;
|
||||
|
||||
@@ -563,7 +563,7 @@ fn test_set_node() -> Result<(), MerkleError> {
|
||||
let mtree = MerkleTree::new(LEAVES4.to_vec())?;
|
||||
let mut store = MerkleStore::default().with_merkle_tree(LEAVES4)?;
|
||||
let value = int_to_node(42);
|
||||
let index = NodeIndex::new(mtree.depth(), 0);
|
||||
let index = NodeIndex::make(mtree.depth(), 0);
|
||||
let new_root = store.set_node(mtree.root(), index, value)?.root;
|
||||
assert_eq!(
|
||||
store.get_node(new_root, index),
|
||||
@@ -582,7 +582,7 @@ fn test_constructors() -> Result<(), MerkleError> {
|
||||
let depth = mtree.depth();
|
||||
let leaves = 2u64.pow(depth.into());
|
||||
for index in 0..leaves {
|
||||
let index = NodeIndex::new(depth, index);
|
||||
let index = NodeIndex::make(depth, index);
|
||||
let value_path = store.get_path(mtree.root(), index)?;
|
||||
assert_eq!(mtree.get_path(index)?, value_path.path);
|
||||
}
|
||||
@@ -597,34 +597,50 @@ fn test_constructors() -> Result<(), MerkleError> {
|
||||
let depth = smt.depth();
|
||||
|
||||
for key in KEYS4 {
|
||||
let index = NodeIndex::new(depth, key);
|
||||
let index = NodeIndex::make(depth, key);
|
||||
let value_path = store.get_path(smt.root(), index)?;
|
||||
assert_eq!(smt.get_path(index)?, value_path.path);
|
||||
}
|
||||
|
||||
let d = 2;
|
||||
let paths = [
|
||||
(0, LEAVES4[0], mtree.get_path(NodeIndex::new(d, 0)).unwrap()),
|
||||
(1, LEAVES4[1], mtree.get_path(NodeIndex::new(d, 1)).unwrap()),
|
||||
(2, LEAVES4[2], mtree.get_path(NodeIndex::new(d, 2)).unwrap()),
|
||||
(3, LEAVES4[3], mtree.get_path(NodeIndex::new(d, 3)).unwrap()),
|
||||
(
|
||||
0,
|
||||
LEAVES4[0],
|
||||
mtree.get_path(NodeIndex::make(d, 0)).unwrap(),
|
||||
),
|
||||
(
|
||||
1,
|
||||
LEAVES4[1],
|
||||
mtree.get_path(NodeIndex::make(d, 1)).unwrap(),
|
||||
),
|
||||
(
|
||||
2,
|
||||
LEAVES4[2],
|
||||
mtree.get_path(NodeIndex::make(d, 2)).unwrap(),
|
||||
),
|
||||
(
|
||||
3,
|
||||
LEAVES4[3],
|
||||
mtree.get_path(NodeIndex::make(d, 3)).unwrap(),
|
||||
),
|
||||
];
|
||||
|
||||
let store1 = MerkleStore::default().with_merkle_paths(paths.clone())?;
|
||||
let store2 = MerkleStore::default()
|
||||
.with_merkle_path(0, LEAVES4[0], mtree.get_path(NodeIndex::new(d, 0))?)?
|
||||
.with_merkle_path(1, LEAVES4[1], mtree.get_path(NodeIndex::new(d, 1))?)?
|
||||
.with_merkle_path(2, LEAVES4[2], mtree.get_path(NodeIndex::new(d, 2))?)?
|
||||
.with_merkle_path(3, LEAVES4[3], mtree.get_path(NodeIndex::new(d, 3))?)?;
|
||||
.with_merkle_path(0, LEAVES4[0], mtree.get_path(NodeIndex::make(d, 0))?)?
|
||||
.with_merkle_path(1, LEAVES4[1], mtree.get_path(NodeIndex::make(d, 1))?)?
|
||||
.with_merkle_path(2, LEAVES4[2], mtree.get_path(NodeIndex::make(d, 2))?)?
|
||||
.with_merkle_path(3, LEAVES4[3], mtree.get_path(NodeIndex::make(d, 3))?)?;
|
||||
let set = MerklePathSet::new(d).with_paths(paths).unwrap();
|
||||
|
||||
for key in [0, 1, 2, 3] {
|
||||
let index = NodeIndex::new(d, key);
|
||||
let index = NodeIndex::make(d, key);
|
||||
let value_path1 = store1.get_path(set.root(), index)?;
|
||||
let value_path2 = store2.get_path(set.root(), index)?;
|
||||
assert_eq!(value_path1, value_path2);
|
||||
|
||||
let index = NodeIndex::new(d, key);
|
||||
let index = NodeIndex::make(d, key);
|
||||
assert_eq!(set.get_path(index)?, value_path1.path);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user