arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
feat: allow merging of leaves

Consider the case of a MMR with one entry, and a new entry is being
added. Both of these values are quite unique, they are at the same time
the root and only leaf of their corresponding tree. Currently this
representation is not supported by the [MerkleStore], so the leaves are
not in it. Once the two values are merged, they both become leaves of a
new tree under the new parent, and the existing validation didn't permit
that promotion from happening.

This lifts the validation, and changes the method to clarify that not
only `root` are being merged, by arbitrary nodes of a tree (leafs,
internal, or roots), with arbitrary mixing of each.
Augusto F. Hack hack.augusto@gmail.com
Fri, 14 Apr 2023 20:35:08 +0200
commit

3996374a8bb647053af056563a38ad148be5183d

parent

7fa03c79673d659ee6228da4c61cc4c99d89a923

1 files changed, 12 insertions(+), 15 deletions(-)

jump to
M src/merkle/store/mod.rs → src/merkle/store/mod.rs

@@ -478,26 +478,23 @@

Ok(RootPath { root, path }) } + /// Merges two elements and adds the resulting node into the store. + /// + /// Merges arbitrary values. They may be leafs, nodes, or a mixture of both. pub fn merge_roots(&mut self, root1: Word, root2: Word) -> Result<Word, MerkleError> { let root1: RpoDigest = root1.into(); let root2: RpoDigest = root2.into(); - if !self.nodes.contains_key(&root1) { - Err(MerkleError::NodeNotInStore(root1.into(), NodeIndex::root())) - } else if !self.nodes.contains_key(&root1) { - Err(MerkleError::NodeNotInStore(root2.into(), NodeIndex::root())) - } else { - let parent: Word = Rpo256::merge(&[root1, root2]).into(); - self.nodes.insert( - parent.into(), - Node { - left: root1, - right: root2, - }, - ); + let parent: Word = Rpo256::merge(&[root1, root2]).into(); + self.nodes.insert( + parent.into(), + Node { + left: root1, + right: root2, + }, + ); - Ok(parent) - } + Ok(parent) } }