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

SimpleSmt: distinguish between depth of tree and depth of insertions #220

Closed
plafer opened this issue Nov 16, 2023 · 3 comments
Closed

SimpleSmt: distinguish between depth of tree and depth of insertions #220

plafer opened this issue Nov 16, 2023 · 3 comments

Comments

@plafer
Copy link
Contributor

plafer commented Nov 16, 2023

I propose we add the ability to instantiate a tree which has depth D, but where all insertions are done at depth d, where d < D.

Background

The "created notes" tree is currently constructed as:

  • Each batch has a tree of depth 12 of all created notes in all transactions (i.e. up to $2^{12}$ created notes per batch)
  • A block creates a tree of depth 8, containing all the created note roots from the batches it contains (i.e. up to $2^{20}$ created notes per block)

The natural way to think of the "notes created" tree in a block is a tree of depth 20, which contains the $2^8$ subtrees of depth 12 (one per batch, where the tree is padded with empty trees if there are not enough batches). A tree where no notes were created would have root E20 (i.e. the root of the empty tree of depth 20).

To allow us to create such a tree, we need to be able to create a tree of depth 20, where all leaves are inserted at depth 8 (the leaves are the roots of the batches' created notes trees). Note that the current MASM mtree_set already supports this behavior; this issue is about allowing Rust code to easily build such a tree.

In comparison, the current API forces us to create a tree of depth 8. There are a few drawbacks:

  1. A transaction batch with no created notes (i.e. with root E12) modifies the root of the block created notes tree
  2. Transaction batches with no created notes incur a cost in the block kernel (derives from 1.)
  3. Unintuitive: the root of an empty block's created notes tree is not E20. In fact, it isn't even E8, because each transaction batch modifies its root.
@bobbinth
Copy link
Contributor

I wonder if we could achieve the same goal by adding set_subtree() method to the current SimpleSmt. This method could look like so:

pub fn set_subtree(&mut self, index: u64, subtree: SimpleSmt) -> Result<Word, MerkleError>

This method would compute the insertion depth based on depth difference between the subtree and the SMT being updated.

Then, we can use this method to manipulate the note tree as follows:

  • We instantiate an empty SimpleSmt of depth 21.
  • Then, we set subtrees of depth 13, which will be inserted at depth 8.

@plafer
Copy link
Contributor Author

plafer commented Nov 24, 2023

Great idea, this results in a much cleaner API.

@plafer
Copy link
Contributor Author

plafer commented Dec 11, 2023

Closed with #232

@plafer plafer closed this as completed Dec 11, 2023
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

No branches or pull requests

2 participants