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

feat: add simple sparse merkle tree #27

Merged
merged 1 commit into from
Dec 14, 2022
Merged

feat: add simple sparse merkle tree #27

merged 1 commit into from
Dec 14, 2022

Conversation

vlopes11
Copy link
Contributor

@vlopes11 vlopes11 commented Dec 8, 2022

This commit moves the previous implementation of SparseMerkleTree from
miden-core to this crate.

It also include a couple of new tests, a bench suite, and a couple of
minor fixes. The original API was preserved to maintain compatibility
with AdviceTape.

closes #21

@vlopes11 vlopes11 self-assigned this Dec 8, 2022
@vlopes11 vlopes11 force-pushed the add-simple-smt branch 3 times, most recently from 8e68b7d to 463e90b Compare December 8, 2022 16:46
src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
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 great! Thank you! I left a few small comments inline and have a few general comments as well:

  1. I am not sure we need a generic version of SimpleSmt. Under the merkle module we place structures which rely on Rpo256 as the hash function. Maybe in the future we'll also need other hash functions, but I would rather keep things simple for now and not add features which we might not need to use for a while.
  2. I'd prefer to keep public interfaces as consistent with interfaces for MerkleTree and MerklePathSet as possible. These are all just different variants of advice sets and the more consistent they are, the better. So, ideally, the following methods should look the same across all 3:
    a. root()
    b. depth()
    c. get_node()
    d. get_path()
    e. get_path()
    f. update_leaf()
    I like the idea of using MerklePath struct - but then we should use it across all 3 consistently.
  3. It is difficult for me to understand performance implications of this structure vs. the one in the original implementation. So, I am thinking we should put together some benchmarks, both for new structure and for the old one and see how they perform. We probably want to measure tree construction times for various fill factors as well as get_merkle_path() and update_leaf() functions.

src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/path.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/path.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/storage.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/storage.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/storage.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@Al-Kindi-0 Al-Kindi-0 left a comment

Choose a reason for hiding this comment

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

Looks great!
In addition to the other comments, I have added just one, a nit in fact, to those.

src/merkle/simple_smt/storage.rs Outdated Show resolved Hide resolved
@vlopes11
Copy link
Contributor Author

Understood! It is indeed not desirable to have a whole new implementation when we will lose compatibility with the AdviceTape interface.

I restored the original API and implementation from miden-core and reused the new added tests/bench to not waste the effort. It was actually beneficial because I picked a couple of small nits with the proptest.

I'm resolving the old conversations because the PR changed entirely.

@vlopes11 vlopes11 force-pushed the add-simple-smt branch 2 times, most recently from 5c2928c to 253bb23 Compare December 12, 2022 19:42
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 a few small comments inline.

Also, one comment about benchmarks: it might be good to benchmark the get_leaf_path() and update_leaf() operations for a couple of different scenarios. Specifically:

  • For a tree which is 50% full.
  • For a tree which is 1% full.

Also, for tree depth - could we use 20 - or is that too slow?

src/merkle/simple_smt/mod.rs Show resolved Hide resolved
src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
benches/smt.rs Outdated Show resolved Hide resolved
benches/smt.rs Outdated Show resolved Hide resolved
@vlopes11
Copy link
Contributor Author

@bobbinth as for the benches, 20 depth is indeed considerably slow. As for the tree contents, we shouldn't have difference of performance between empty, partially filled and full trees, except for the memory overhead of fetching the nodes/leaves from the internal BTreeMap. If we have relevant difference, then it is likely we have a implementation problem.

I'll add these as sanity check.

@bobbinth
Copy link
Contributor

the benches, 20 depth is indeed considerably slow. As for the tree contents, we shouldn't have difference of performance between empty

Let's keep it at depth 16 then. Agreed that it shouldn't make a difference - but I think a sanity check in this case wouldn't hurt.

@vlopes11
Copy link
Contributor Author

The cost for inclusion was independent of the inserted count, as expected

image

But, there was a difference for path 🤔

image

Likely it's because of the empty precalculated nodes for |leaves| = 1

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! Just a couple more small nit comments inline.

src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 137 to 139
if !self.store.check_leaf_node_exists(key) {
return Err(MerkleError::InvalidIndex(self.depth, key));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we already do this check in get_path(), we can probably get rid of it here, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, should have been removed in the last commit. Fixed!

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!

@vlopes11
Copy link
Contributor Author

@bobbinth I pushed another commit to make the bench check for a range of depths and leaves, so I'm requesting your review again

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!

Copy link
Contributor

@grjte grjte left a comment

Choose a reason for hiding this comment

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

Looks great! I left one nit inline for clarity

src/merkle/simple_smt/mod.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@Al-Kindi-0 Al-Kindi-0 left a comment

Choose a reason for hiding this comment

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

Looks great, thank you! I left one small nit which can be discarded.

@vlopes11
Copy link
Contributor Author

Hey @Al-Kindi-0 , I don't see your comment. Is it for the previous storage file?

This commit moves the previous implementation of `SparseMerkleTree` from
miden-core to this crate.

It also include a couple of new tests, a bench suite, and a couple of
minor fixes. The original API was preserved to maintain compatibility
with `AdviceTape`.

closes #21
@Al-Kindi-0
Copy link
Collaborator

I think it was for some days ago. I see know that it was taken into account so thank you!

@vlopes11 vlopes11 merged commit aa12215 into next Dec 14, 2022
@vlopes11 vlopes11 deleted the add-simple-smt branch December 14, 2022 13:32
@bobbinth bobbinth mentioned this pull request Dec 14, 2022
2 tasks
@grjte grjte mentioned this pull request Feb 6, 2023
2 tasks
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.

4 participants