Skip to content
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

Introduce SmtProof #270

Merged
merged 37 commits into from
Feb 6, 2024
Merged

Introduce SmtProof #270

merged 37 commits into from
Feb 6, 2024

Conversation

plafer
Copy link
Contributor

@plafer plafer commented Feb 5, 2024

Closes #251

This PR required a change in SmtLeaf. Specifically, for SmtProof::get(key) to know if it contains a value for key, SmtLeaf needs to know its index in the Smt.

This means that constructing a leaf from entries (Vec<RpoDigest, Word>) is no longer possible, because in the case where no entries are provided (leading to an empty leaf), we have no way to know the leaf index. This lead to adding multiple constructors (which some are able to constructor an SmtLeaf without error).

Also note that SmtProof::new() doesn't return an error (contrary to TieredSmtProof). This is because the only possible error would be if MerklePath doesn't have length 64. However, SparseMerkleTree::Opening requires From<(MerklePath, SmtLeaf)> - that is, we already have the assumption there that the MerklePath is of proper length. I kept that assumption in SmtProof. We could potentially add a debug_assert, or expect() it away in open(), as is done with TieredSmtProof.

This change would also impact the node's NullifierProof: it would need to be changed to also include the leaf_index. However, I don't see a way around it, unless I'm missing something.

@plafer plafer requested a review from bobbinth February 5, 2024 15:34
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good! I left some comments inline - most are pretty small.

@plafer plafer requested a review from bobbinth February 5, 2024 22:47
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left some more comment inline. Most are nits, but there is one about leaf serialization which I think might have a bug.

Copy link

sonarqubecloud bot commented Feb 6, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@plafer plafer requested a review from bobbinth February 6, 2024 18:18
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good! Thank you! I think merging this shouldn't cause any issues downstream as changes to SimpleSmt are non-breaking, right?

Also, I left one comment for further discussion. Let's create an issue for it.

Comment on lines +127 to +136
/// Returns the number of entries stored in the leaf
pub fn num_entries(&self) -> u64 {
match self {
SmtLeaf::Empty(_) => 0,
SmtLeaf::Single(_) => 1,
SmtLeaf::Multiple(entries) => {
entries.len().try_into().expect("shouldn't have more than 2^64 entries")
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realistically, it is unlikely that the number of entries in a leaf would be more than $2^{32}$ - and even then, if this happens, the tree is kind of broken. I wonder if we should explicitly limit the number of entries per leaf to $2^{32}$ or maybe even $2^{16}$. But this probably deserves a separate issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plafer plafer merged commit 1f6c479 into next Feb 6, 2024
10 checks passed
@plafer plafer deleted the plafer-issue-251 branch February 6, 2024 18:36
bobbinth pushed a commit that referenced this pull request Feb 14, 2024
* add conversion for `SmtLeaf`

* introduce `SmtProof` scaffolding

* implement `verify_membership()`

* SmtLeaf: knows its index

* `SmtLeaf::index`

* `SmtLeaf::get_value()` returns an Option

* fix `verify_membership()`

* impl `SmtProof::get`

* impl `into_parts()`

* `SmtProof::compute_root`

* use `SmtProof` in `Smt::open`

* `SmtLeaf` constructors

* Vec

* impl `Error` for `SmtLeafError`

* fix std Error

* move Word/Digest conversions to LeafIndex

* `SmtProof::new()` returns an error

* `SparseMerkleTree::path_and_leaf_to_opening`

* `SmtLeaf`: serializable/deserializable

* `SmtProof`: serializable/deserializable

* add tests for SmtLeaf serialization

* move `SmtLeaf` to submodule

* use constructors internally

* fix docs

* Add `Vec`

* add `Vec` to tests

* no_std use statements

* fmt

* `Errors`: make heading

* use `SMT_DEPTH`

* SmtLeaf single case: check leaf index

* Multiple case: check consistency with leaf index

* use `pub(super)` instead of `pub(crate)`

* use `pub(super)`

* `SmtLeaf`: add `num_entries()` accessor

* Fix `SmtLeaf` serialization

* improve leaf serialization tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants