Skip to content

Commit

Permalink
open() takes a reference to key
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Jan 16, 2024
1 parent c7149d5 commit a9b53c0
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion benches/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn smt_rpo(c: &mut Criterion) {
&key,
|b, key| {
b.iter(|| {
tree.open(black_box(LeafIndex::<DEPTH>::new(*key).unwrap()));
tree.open(black_box(&LeafIndex::<DEPTH>::new(*key).unwrap()));
});
},
);
Expand Down
2 changes: 1 addition & 1 deletion benches/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn get_leaf_path_simplesmt(c: &mut Criterion) {
b.iter_batched(
|| random_index(size_u64, SMT_MAX_DEPTH),
|index| {
black_box(smt.open(LeafIndex::<SMT_MAX_DEPTH>::new(index.value()).unwrap()))
black_box(smt.open(&LeafIndex::<SMT_MAX_DEPTH>::new(index.value()).unwrap()))
},
BatchSize::SmallInput,
)
Expand Down
2 changes: 1 addition & 1 deletion src/merkle/simple_smt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<const DEPTH: u8> SimpleSmt<DEPTH> {
/// Returns a Merkle path from the leaf node specified by the key to the root.
///
/// The node itself is not included in the path.
pub fn open(&self, key: LeafIndex<DEPTH>) -> (MerklePath, Word) {
pub fn open(&self, key: &LeafIndex<DEPTH>) -> (MerklePath, Word) {
<Self as SparseMerkleTree<DEPTH>>::open(self, key)
}

Expand Down
14 changes: 7 additions & 7 deletions src/merkle/simple_smt/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()).0
);
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()).0
);
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()).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);
}

#[test]
Expand Down Expand Up @@ -213,7 +213,7 @@ 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/merkle/smt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Smt {
/// Returns a Merkle path from the leaf node specified by the key to the root.
///
/// The node itself is not included in the path.
pub fn open(&self, key: RpoDigest) -> (MerklePath, SmtLeaf) {
pub fn open(&self, key: &RpoDigest) -> (MerklePath, SmtLeaf) {
<Self as SparseMerkleTree<SMT_DEPTH>>::open(self, key)
}

Expand Down
4 changes: 2 additions & 2 deletions src/merkle/smt/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn test_smt_insert_multiple_values() {
assert_eq!(smt.root(), tree_root);

let expected_path = store.get_path(tree_root, key_index).unwrap();
assert_eq!(smt.open(key).0, expected_path.path);
assert_eq!(smt.open(&key).0, expected_path.path);
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ fn test_smt_path_to_keys_in_same_leaf_are_equal() {

let smt = Smt::with_entries([(key_1, value_1), (key_2, value_2)]).unwrap();

assert_eq!(smt.open(key_1), smt.open(key_2));
assert_eq!(smt.open(&key_1), smt.open(&key_2));
}

// HELPERS
Expand Down
6 changes: 3 additions & 3 deletions src/merkle/sparse_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
/// Returns a Merkle path from the leaf node specified by the key to the root.
///
/// The node itself is not included in the path.
fn open(&self, key: Self::Key) -> Self::Opening {
let leaf = self.get_leaf(&key);
fn open(&self, key: &Self::Key) -> Self::Opening {
let leaf = self.get_leaf(key);

let mut index: NodeIndex = {
let leaf_index: LeafIndex<DEPTH> = Self::key_to_leaf_index(&key);
let leaf_index: LeafIndex<DEPTH> = Self::key_to_leaf_index(key);
leaf_index.into()
};

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)?).0;
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()).0,
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()).0,
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()).0,
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()).0,
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()).0,
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()).0, value_path.path);
}

let d = 2;
Expand Down

0 comments on commit a9b53c0

Please sign in to comment.