Skip to content

Commit

Permalink
Use ValuePath in simple smt
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Jan 17, 2024
1 parent 7e9afc9 commit 88797c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/merkle/path.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Word;

use super::{vec, InnerNodeInfo, MerkleError, NodeIndex, Rpo256, RpoDigest, Vec};
use core::ops::{Deref, DerefMut};
use winter_utils::{ByteReader, Deserializable, DeserializationError, Serializable};
Expand Down Expand Up @@ -179,6 +181,12 @@ impl ValuePath {
}
}

impl From<(MerklePath, Word)> for ValuePath {
fn from((path, value): (MerklePath, Word)) -> Self {
ValuePath::new(value.into(), path)
}
}

/// A container for a [MerklePath] and its [Word] root.
///
/// This structure does not provide any guarantees regarding the correctness of the path to the
Expand Down
10 changes: 5 additions & 5 deletions src/merkle/smt/simple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
merkle::{EmptySubtreeRoots, InnerNodeInfo, MerkleTreeDelta, StoreNode},
merkle::{EmptySubtreeRoots, InnerNodeInfo, MerkleTreeDelta, StoreNode, ValuePath},
utils::collections::TryApplyDiff,
EMPTY_WORD,
};

use super::{
InnerNode, LeafIndex, MerkleError, MerklePath, NodeIndex, RpoDigest, SparseMerkleTree, Word,
SMT_MAX_DEPTH, SMT_MIN_DEPTH,
InnerNode, LeafIndex, MerkleError, NodeIndex, RpoDigest, SparseMerkleTree, Word, SMT_MAX_DEPTH,
SMT_MIN_DEPTH,
};
use crate::utils::collections::{BTreeMap, BTreeSet};

Expand Down Expand Up @@ -147,7 +147,7 @@ impl<const DEPTH: u8> SimpleSmt<DEPTH> {

/// Returns an opening of the leaf associated with `key`. Conceptually, an opening is a Merkle
/// path to the leaf, as well as the leaf itself.
pub fn open(&self, key: &LeafIndex<DEPTH>) -> (MerklePath, Word) {
pub fn open(&self, key: &LeafIndex<DEPTH>) -> ValuePath {
<Self as SparseMerkleTree<DEPTH>>::open(self, key)
}

Expand Down Expand Up @@ -246,7 +246,7 @@ impl<const DEPTH: u8> SparseMerkleTree<DEPTH> for SimpleSmt<DEPTH> {
type Key = LeafIndex<DEPTH>;
type Value = Word;
type Leaf = Word;
type Opening = (MerklePath, Word);
type Opening = ValuePath;

const EMPTY_VALUE: Self::Value = EMPTY_WORD;

Expand Down
16 changes: 8 additions & 8 deletions src/merkle/smt/simple/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn build_sparse_tree() {
assert_eq!(mt2.root(), smt.root());
assert_eq!(
mt2.get_path(NodeIndex::make(3, 6)).unwrap(),
smt.open(&LeafIndex::<3>::new(6).unwrap()).0
smt.open(&LeafIndex::<3>::new(6).unwrap()).path
);
assert_eq!(old_value, EMPTY_WORD);

Expand All @@ -72,7 +72,7 @@ fn build_sparse_tree() {
assert_eq!(mt3.root(), smt.root());
assert_eq!(
mt3.get_path(NodeIndex::make(3, 2)).unwrap(),
smt.open(&LeafIndex::<3>::new(2).unwrap()).0
smt.open(&LeafIndex::<3>::new(2).unwrap()).path
);
assert_eq!(old_value, EMPTY_WORD);
}
Expand Down Expand Up @@ -108,10 +108,10 @@ fn test_depth2_tree() {
assert_eq!(VALUES4[3], tree.get_node(NodeIndex::make(2, 3)).unwrap());

// check get_path(): depth 2
assert_eq!(vec![VALUES4[1], node3], *tree.open(&LeafIndex::<2>::new(0).unwrap()).0);
assert_eq!(vec![VALUES4[0], node3], *tree.open(&LeafIndex::<2>::new(1).unwrap()).0);
assert_eq!(vec![VALUES4[3], node2], *tree.open(&LeafIndex::<2>::new(2).unwrap()).0);
assert_eq!(vec![VALUES4[2], node2], *tree.open(&LeafIndex::<2>::new(3).unwrap()).0);
assert_eq!(vec![VALUES4[1], node3], *tree.open(&LeafIndex::<2>::new(0).unwrap()).path);
assert_eq!(vec![VALUES4[0], node3], *tree.open(&LeafIndex::<2>::new(1).unwrap()).path);
assert_eq!(vec![VALUES4[3], node2], *tree.open(&LeafIndex::<2>::new(2).unwrap()).path);
assert_eq!(vec![VALUES4[2], node2], *tree.open(&LeafIndex::<2>::new(3).unwrap()).path);
}

#[test]
Expand Down Expand Up @@ -213,9 +213,9 @@ fn small_tree_opening_is_consistent() {
];

for (key, path) in cases {
let (opening, _) = tree.open(&LeafIndex::<3>::new(key).unwrap());
let opening = tree.open(&LeafIndex::<3>::new(key).unwrap());

assert_eq!(path, *opening);
assert_eq!(path, *opening.path);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/merkle/store/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn test_leaf_paths_for_empty_trees() -> Result<(), MerkleError> {

let index = NodeIndex::make(DEPTH, 0);
let store_path = store.get_path(smt.root(), index)?;
let smt_path = smt.open(&LeafIndex::<DEPTH>::new(0)?).0;
let smt_path = smt.open(&LeafIndex::<DEPTH>::new(0)?).path;
assert_eq!(
store_path.value,
RpoDigest::default(),
Expand Down Expand Up @@ -301,7 +301,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
"Value for merkle path at index 0 must match leaf value"
);
assert_eq!(
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(0).unwrap()).0,
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(0).unwrap()).path,
result.path,
"merkle path for index 0 must be the same for the MerkleTree and MerkleStore"
);
Expand All @@ -312,7 +312,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
"Value for merkle path at index 1 must match leaf value"
);
assert_eq!(
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(1).unwrap()).0,
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(1).unwrap()).path,
result.path,
"merkle path for index 1 must be the same for the MerkleTree and MerkleStore"
);
Expand All @@ -323,7 +323,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
"Value for merkle path at index 2 must match leaf value"
);
assert_eq!(
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(2).unwrap()).0,
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(2).unwrap()).path,
result.path,
"merkle path for index 2 must be the same for the MerkleTree and MerkleStore"
);
Expand All @@ -334,7 +334,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
"Value for merkle path at index 3 must match leaf value"
);
assert_eq!(
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(3).unwrap()).0,
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(3).unwrap()).path,
result.path,
"merkle path for index 3 must be the same for the MerkleTree and MerkleStore"
);
Expand All @@ -346,7 +346,7 @@ fn test_sparse_merkle_tree() -> Result<(), MerkleError> {
"Value for merkle path at index 4 must match leaf value"
);
assert_eq!(
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(4).unwrap()).0,
smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(4).unwrap()).path,
result.path,
"merkle path for index 4 must be the same for the MerkleTree and MerkleStore"
);
Expand Down Expand Up @@ -563,7 +563,7 @@ fn test_constructors() -> Result<(), MerkleError> {
for key in KEYS4 {
let index = NodeIndex::make(DEPTH, key);
let value_path = store.get_path(smt.root(), index)?;
assert_eq!(smt.open(&LeafIndex::<DEPTH>::new(key).unwrap()).0, value_path.path);
assert_eq!(smt.open(&LeafIndex::<DEPTH>::new(key).unwrap()).path, value_path.path);
}

let d = 2;
Expand Down

0 comments on commit 88797c3

Please sign in to comment.