-1

I'm currently implementing a Merkle Tree in Rust. One of the problems I'm facing is the complexity of generating the proof. Meaning the vector I give to the verifier in order to check if an element is present. Many people say that I should transverse the tree to find the path for the element (if the element is part of the data set), but I don't understand how that would work if the Merkle Tree is not sorted. Am I missing something? From my understanding the best way to do this is find the leaf (if it exists) and find your way up the tree. In that case the complexity will be O(N) to generated.

Thanks in advance!

Tito
  • 1
  • 1

1 Answers1

0

From my understanding the best way to do this is find the leaf (if it exists) and find your way up the tree. In that case the complexity will be O(N) to generated.

There are two steps here, and it's not clear which you think might take $O(N)$ time.

The first step is finding the leaf; yes, if you take the approach of examining each element individually, this will obviously take $O(N)$ time if there are $O(N)$ elements. On the other hand, this isn't necessarily the only possible algorithm. If we have our $N$ elements, then we can build a data structure (say, a hash table) to look up the elements quickly, if faster lookup is deemed necessary.

The second step is generating the authentication path; yes, generating the entire Merkle tree (and computing the root) will take $O(N)$ time, and there's no way to get around that. On the other hand, once we have constructed the tree, we can often save the values of intermediate nodes. In the most straight-forward approach, we can just save all the intermediate nodes; this requires $O(N)$ memory; on the other hand, constructing the authentication path just involves copying the appropriate node values, and that can be done in $O(\log N)$ time.

poncho
  • 147,019
  • 11
  • 229
  • 360
  • How is that it takes O(log N) time to construct the path? From what I understood if you start from the root you have no way to know where the leaf you want is. Or should I build the path from the leaf to the root? In that case I need to change the way I built the tree. – Tito Feb 05 '23 at 19:27
  • @Tito: if you know which leaf it is, you know the path to it from the root (and hence the authentication path, which is the sibling node for every entry on the path). It doesn't matter which direction you build the path; it is easy enough to compute which nodes you need to reveal. – poncho Feb 05 '23 at 22:35