-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add missing methods to Smt
#268
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba1949f
add `get_value()`
plafer 26f8da7
test_get_value
plafer 3db31dd
`entries()` method
plafer aa0959c
Rename `SmtLeaf` functions to use `entries` nomenclature
plafer cc4602a
entries test
plafer bc1e2e4
fmt
plafer f88c2e0
remove potential allocations
plafer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,11 +109,18 @@ impl Smt { | |
<Self as SparseMerkleTree<SMT_DEPTH>>::root(self) | ||
} | ||
|
||
/// Returns the leaf at the specified index. | ||
/// Returns the leaf to which `key` maps | ||
pub fn get_leaf(&self, key: &RpoDigest) -> SmtLeaf { | ||
<Self as SparseMerkleTree<SMT_DEPTH>>::get_leaf(self, key) | ||
} | ||
|
||
/// Returns the value associated with `key` | ||
pub fn get_value(&self, key: &RpoDigest) -> Word { | ||
let leaf = self.get_leaf(key); | ||
|
||
leaf.get_value(key) | ||
} | ||
|
||
/// 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: &RpoDigest) -> (MerklePath, SmtLeaf) { | ||
|
@@ -130,6 +137,11 @@ impl Smt { | |
.map(|(leaf_index, leaf)| (LeafIndex::new_max_depth(*leaf_index), leaf)) | ||
} | ||
|
||
/// Returns an iterator over the key-value pairs of this [Smt]. | ||
pub fn entries(&self) -> impl Iterator<Item = &(RpoDigest, Word)> { | ||
self.leaves().flat_map(|(_, leaf)| leaf.entries()) | ||
} | ||
|
||
/// Returns an iterator over the inner nodes of this [Smt]. | ||
pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_ { | ||
self.inner_nodes.values().map(|e| InnerNodeInfo { | ||
|
@@ -270,11 +282,11 @@ impl SmtLeaf { | |
|
||
/// Converts a leaf to a list of field elements | ||
pub fn into_elements(self) -> Vec<Felt> { | ||
self.into_kv_pairs().into_iter().flat_map(kv_to_elements).collect() | ||
self.into_entries().into_iter().flat_map(kv_to_elements).collect() | ||
} | ||
|
||
/// Returns the key-value pairs in the leaf | ||
pub fn kv_pairs(&self) -> Vec<&(RpoDigest, Word)> { | ||
pub fn entries(&self) -> Vec<&(RpoDigest, Word)> { | ||
match self { | ||
SmtLeaf::Empty => Vec::new(), | ||
SmtLeaf::Single(kv_pair) => vec![kv_pair], | ||
|
@@ -283,7 +295,7 @@ impl SmtLeaf { | |
} | ||
|
||
/// Converts a leaf the key-value pairs in the leaf | ||
pub fn into_kv_pairs(self) -> Vec<(RpoDigest, Word)> { | ||
pub fn into_entries(self) -> Vec<(RpoDigest, Word)> { | ||
match self { | ||
SmtLeaf::Empty => Vec::new(), | ||
SmtLeaf::Single(kv_pair) => vec![kv_pair], | ||
|
@@ -306,6 +318,17 @@ impl SmtLeaf { | |
// HELPERS | ||
// --------------------------------------------------------------------------------------------- | ||
|
||
/// Returns the value associated with `key` in the leaf | ||
fn get_value(&self, key: &RpoDigest) -> Word { | ||
for (key_in_leaf, value_in_leaf) in self.entries() { | ||
if key == key_in_leaf { | ||
return *value_in_leaf; | ||
} | ||
} | ||
|
||
EMPTY_WORD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the compiler can optimize this away, but I'd prefer to handle it using a match statement to avoid potential unnecessary allocations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
} | ||
|
||
/// Inserts key-value pair into the leaf; returns the previous value associated with `key`, if | ||
/// any. | ||
fn insert(&mut self, key: RpoDigest, value: Word) -> Option<Word> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment as above: this may result in closing of the leaf first and then getting a value from it. May be better to make sure we don't rely on the compiler for optimizing away unnecessary cloning/allocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed