|
@ -9,12 +9,7 @@ use crate::{ |
|
|
use std::error::Error;
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
|
const KEYS4: [u64; 4] = [0, 1, 2, 3];
|
|
|
const KEYS4: [u64; 4] = [0, 1, 2, 3];
|
|
|
const LEAVES4: [Word; 4] = [
|
|
|
|
|
|
int_to_node(1),
|
|
|
|
|
|
int_to_node(2),
|
|
|
|
|
|
int_to_node(3),
|
|
|
|
|
|
int_to_node(4),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
const LEAVES4: [Word; 4] = [int_to_node(1), int_to_node(2), int_to_node(3), int_to_node(4)];
|
|
|
const EMPTY: Word = [ZERO; WORD_SIZE];
|
|
|
const EMPTY: Word = [ZERO; WORD_SIZE];
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
#[test]
|
|
@ -90,9 +85,7 @@ fn test_merkle_tree() -> Result<(), MerkleError> { |
|
|
|
|
|
|
|
|
// STORE MERKLE PATH MATCHS ==============================================================
|
|
|
// STORE MERKLE PATH MATCHS ==============================================================
|
|
|
// assert the merkle path returned by the store is the same as the one in the tree
|
|
|
// 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::make(mtree.depth(), 0))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 0)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[0], result.value,
|
|
|
LEAVES4[0], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -103,9 +96,7 @@ fn test_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 1))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 1)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[1], result.value,
|
|
|
LEAVES4[1], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -116,9 +107,7 @@ fn test_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 2))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 2)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[2], result.value,
|
|
|
LEAVES4[2], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -129,9 +118,7 @@ fn test_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 3))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(mtree.root(), NodeIndex::make(mtree.depth(), 3)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[3], result.value,
|
|
|
LEAVES4[3], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -172,10 +159,7 @@ fn test_leaf_paths_for_empty_trees() -> Result<(), MerkleError> { |
|
|
let index = NodeIndex::make(depth, 0);
|
|
|
let index = NodeIndex::make(depth, 0);
|
|
|
let store_path = store.get_path(smt.root(), index)?;
|
|
|
let store_path = store.get_path(smt.root(), index)?;
|
|
|
let smt_path = smt.get_path(index)?;
|
|
|
let smt_path = smt.get_path(index)?;
|
|
|
assert_eq!(
|
|
|
|
|
|
store_path.value, EMPTY,
|
|
|
|
|
|
"the leaf of an empty tree is always ZERO"
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
assert_eq!(store_path.value, EMPTY, "the leaf of an empty tree is always ZERO");
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
store_path.path, smt_path,
|
|
|
store_path.path, smt_path,
|
|
|
"the returned merkle path does not match the computed values"
|
|
|
"the returned merkle path does not match the computed values"
|
|
@ -213,17 +197,11 @@ fn test_add_sparse_merkle_tree_one_level() -> Result<(), MerkleError> { |
|
|
|
|
|
|
|
|
let idx = NodeIndex::make(1, 0);
|
|
|
let idx = NodeIndex::make(1, 0);
|
|
|
assert_eq!(smt.get_node(idx).unwrap(), leaves2[0]);
|
|
|
assert_eq!(smt.get_node(idx).unwrap(), leaves2[0]);
|
|
|
assert_eq!(
|
|
|
|
|
|
store.get_node(smt.root(), idx).unwrap(),
|
|
|
|
|
|
smt.get_node(idx).unwrap()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
assert_eq!(store.get_node(smt.root(), idx).unwrap(), smt.get_node(idx).unwrap());
|
|
|
|
|
|
|
|
|
let idx = NodeIndex::make(1, 1);
|
|
|
let idx = NodeIndex::make(1, 1);
|
|
|
assert_eq!(smt.get_node(idx).unwrap(), leaves2[1]);
|
|
|
assert_eq!(smt.get_node(idx).unwrap(), leaves2[1]);
|
|
|
assert_eq!(
|
|
|
|
|
|
store.get_node(smt.root(), idx).unwrap(),
|
|
|
|
|
|
smt.get_node(idx).unwrap()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
assert_eq!(store.get_node(smt.root(), idx).unwrap(), smt.get_node(idx).unwrap());
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
@ -231,10 +209,8 @@ fn test_add_sparse_merkle_tree_one_level() -> Result<(), MerkleError> { |
|
|
#[test]
|
|
|
#[test]
|
|
|
fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
|
|
|
fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
|
|
|
let mut store = MerkleStore::default();
|
|
|
let mut store = MerkleStore::default();
|
|
|
store.add_sparse_merkle_tree(
|
|
|
|
|
|
SimpleSmt::MAX_DEPTH,
|
|
|
|
|
|
KEYS4.into_iter().zip(LEAVES4.into_iter()),
|
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
store
|
|
|
|
|
|
.add_sparse_merkle_tree(SimpleSmt::MAX_DEPTH, KEYS4.into_iter().zip(LEAVES4.into_iter()))?;
|
|
|
|
|
|
|
|
|
let smt = SimpleSmt::new(SimpleSmt::MAX_DEPTH)
|
|
|
let smt = SimpleSmt::new(SimpleSmt::MAX_DEPTH)
|
|
|
.unwrap()
|
|
|
.unwrap()
|
|
@ -299,9 +275,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> { |
|
|
|
|
|
|
|
|
// STORE MERKLE PATH MATCHS ==============================================================
|
|
|
// STORE MERKLE PATH MATCHS ==============================================================
|
|
|
// assert the merkle path returned by the store is the same as the one in the tree
|
|
|
// 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::make(smt.depth(), 0))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 0)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[0], result.value,
|
|
|
LEAVES4[0], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -312,9 +286,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(smt.root(), NodeIndex::make(smt.depth(), 1))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 1)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[1], result.value,
|
|
|
LEAVES4[1], result.value,
|
|
|
"Value for merkle path at index 1 must match leaf value"
|
|
|
"Value for merkle path at index 1 must match leaf value"
|
|
@ -325,9 +297,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(smt.root(), NodeIndex::make(smt.depth(), 2))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 2)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[2], result.value,
|
|
|
LEAVES4[2], result.value,
|
|
|
"Value for merkle path at index 2 must match leaf value"
|
|
|
"Value for merkle path at index 2 must match leaf value"
|
|
@ -338,9 +308,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 2 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 2 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(smt.root(), NodeIndex::make(smt.depth(), 3))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(smt.root(), NodeIndex::make(smt.depth(), 3)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[3], result.value,
|
|
|
LEAVES4[3], result.value,
|
|
|
"Value for merkle path at index 3 must match leaf value"
|
|
|
"Value for merkle path at index 3 must match leaf value"
|
|
@ -351,13 +319,8 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> { |
|
|
"merkle path for index 3 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 3 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.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"
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store.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!(
|
|
|
assert_eq!(
|
|
|
smt.get_path(NodeIndex::make(smt.depth(), 4)),
|
|
|
smt.get_path(NodeIndex::make(smt.depth(), 4)),
|
|
|
Ok(result.path),
|
|
|
Ok(result.path),
|
|
@ -391,9 +354,7 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> { |
|
|
];
|
|
|
];
|
|
|
|
|
|
|
|
|
let mut store = MerkleStore::default();
|
|
|
let mut store = MerkleStore::default();
|
|
|
store
|
|
|
|
|
|
.add_merkle_paths(paths.clone())
|
|
|
|
|
|
.expect("the valid paths must work");
|
|
|
|
|
|
|
|
|
store.add_merkle_paths(paths.clone()).expect("the valid paths must work");
|
|
|
|
|
|
|
|
|
let depth = 2;
|
|
|
let depth = 2;
|
|
|
let set = MerklePathSet::new(depth).with_paths(paths).unwrap();
|
|
|
let set = MerklePathSet::new(depth).with_paths(paths).unwrap();
|
|
@ -446,9 +407,7 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> { |
|
|
|
|
|
|
|
|
// STORE MERKLE PATH MATCHS ==============================================================
|
|
|
// STORE MERKLE PATH MATCHS ==============================================================
|
|
|
// assert the merkle path returned by the store is the same as the one in the set
|
|
|
// 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::make(set.depth(), 0))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 0)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[0], result.value,
|
|
|
LEAVES4[0], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -459,9 +418,7 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> { |
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(set.root(), NodeIndex::make(set.depth(), 1))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 1)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[1], result.value,
|
|
|
LEAVES4[1], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -472,9 +429,7 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> { |
|
|
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(set.root(), NodeIndex::make(set.depth(), 2))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 2)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[2], result.value,
|
|
|
LEAVES4[2], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -485,9 +440,7 @@ fn test_add_merkle_paths() -> Result<(), MerkleError> { |
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
|
|
|
);
|
|
|
);
|
|
|
|
|
|
|
|
|
let result = store
|
|
|
|
|
|
.get_path(set.root(), NodeIndex::make(set.depth(), 3))
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
let result = store.get_path(set.root(), NodeIndex::make(set.depth(), 3)).unwrap();
|
|
|
assert_eq!(
|
|
|
assert_eq!(
|
|
|
LEAVES4[3], result.value,
|
|
|
LEAVES4[3], result.value,
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
|
"Value for merkle path at index 0 must match leaf value"
|
|
@ -546,13 +499,8 @@ fn store_path_opens_from_leaf() { |
|
|
|
|
|
|
|
|
let root = Rpo256::merge(&[m.into(), n.into()]);
|
|
|
let root = Rpo256::merge(&[m.into(), n.into()]);
|
|
|
|
|
|
|
|
|
let store = MerkleStore::default()
|
|
|
|
|
|
.with_merkle_tree([a, b, c, d, e, f, g, h])
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
let path = store
|
|
|
|
|
|
.get_path(root.into(), NodeIndex::make(3, 1))
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.path;
|
|
|
|
|
|
|
|
|
let store = MerkleStore::default().with_merkle_tree([a, b, c, d, e, f, g, h]).unwrap();
|
|
|
|
|
|
let path = store.get_path(root.into(), NodeIndex::make(3, 1)).unwrap().path;
|
|
|
|
|
|
|
|
|
let expected = MerklePath::new([a.into(), j.into(), n.into()].to_vec());
|
|
|
let expected = MerklePath::new([a.into(), j.into(), n.into()].to_vec());
|
|
|
assert_eq!(path, expected);
|
|
|
assert_eq!(path, expected);
|
|
@ -565,11 +513,7 @@ fn test_set_node() -> Result<(), MerkleError> { |
|
|
let value = int_to_node(42);
|
|
|
let value = int_to_node(42);
|
|
|
let index = NodeIndex::make(mtree.depth(), 0);
|
|
|
let index = NodeIndex::make(mtree.depth(), 0);
|
|
|
let new_root = store.set_node(mtree.root(), index, value)?.root;
|
|
|
let new_root = store.set_node(mtree.root(), index, value)?.root;
|
|
|
assert_eq!(
|
|
|
|
|
|
store.get_node(new_root, index),
|
|
|
|
|
|
Ok(value),
|
|
|
|
|
|
"Value must have changed"
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
assert_eq!(store.get_node(new_root, index), Ok(value), "Value must have changed");
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
@ -604,26 +548,10 @@ fn test_constructors() -> Result<(), MerkleError> { |
|
|
|
|
|
|
|
|
let d = 2;
|
|
|
let d = 2;
|
|
|
let paths = [
|
|
|
let paths = [
|
|
|
(
|
|
|
|
|
|
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(),
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
(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 store1 = MerkleStore::default().with_merkle_paths(paths.clone())?;
|
|
@ -792,10 +720,7 @@ fn get_leaf_depth_works_with_depth_8() { |
|
|
// duplicate the tree on `a` and assert the depth is short-circuited by such sub-tree
|
|
|
// duplicate the tree on `a` and assert the depth is short-circuited by such sub-tree
|
|
|
let index = NodeIndex::new(8, a).unwrap();
|
|
|
let index = NodeIndex::new(8, a).unwrap();
|
|
|
root = store.set_node(root, index, root).unwrap().root;
|
|
|
root = store.set_node(root, index, root).unwrap().root;
|
|
|
assert_eq!(
|
|
|
|
|
|
Err(MerkleError::DepthTooBig(9)),
|
|
|
|
|
|
store.get_leaf_depth(root, 8, a)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
assert_eq!(Err(MerkleError::DepthTooBig(9)), store.get_leaf_depth(root, 8, a));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[cfg(std)]
|
|
|
#[cfg(std)]
|
|
|