Merged leaves & optimistic rollup
Merged leaves is used for item append sequence validation for the merkle tree rollup of massive items.
Merged leaves concept is used for the sequence verification of the appended items when we want to prove Merkle tree updates through multiple transactions. Let us assume that what exactly we are going to prove is the following result.
[Original merkle tree]
- Root: 0xabcd...
- Index: m
[Items to add]
- [leaf_1, leaf_2, ..., leaf_n]
[Updated merkle tree]
- Root: 0xfdec...
- Index: m + nTo prove above, we will run the following steps:
To add a massive number of items, we split the transactions and record the intermediate proof result.
Every item will be merged sequentially into the
mergedLeavesvalue to ensure the sequence of the item appending.mergedLeaves = 0 mergedLeaves = hash(mergedLeaves, items[0]) mergedLeaves = hash(mergedLeaves, items[1]) ... mergedLeaves = hash(mergedLeaves, items[n])In the end, the result stored on the EVM will be
- start root - start index - result root - result index - mergedLeavesAnd then we can compare the Merkle tree update result.
Let us see a more detailed example.
startRootis0x0001234...startIndexis 38itemsToAddis[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09]we are now trying to prove
resultRootis0xabcd1234...when after adding all items in theitemsToAddlist.
We're going to add three items at once, so after two times of transactions, we can have a proof for the Merkle tree transition on EVM. Note that we have used random values for the hash calculation for this example.
Start
[stored proof on the EVM] startRoot = 0x0001234...; startIndex = 38; resultRoot = 0x0001234...; resultIndex = 38; mergedLeaves = 0x0Add the first item
[calculating...] startRoot = 0x0001234...; startIndex = 38; resultRoot = 0xAFCA1234...; resultIndex = 39; mergedLeaves = keccak256(0x0, 0x01) = 0xA0...Add the second item
[calculating...] startRoot = 0x0001234...; startIndex = 38; resultRoot = 0xBA891234...; resultIndex = 40; mergedLeaves = keccak256(0xA0..., 0x02); = 0xB1...Add the third item & record to the storage
[stored proof on the EVM] startRoot = 0x0001234...; startIndex = 38; resultRoot = 0xC9B31234...; resultIndex = 41; mergedLeaves = keccak256(0xB1..., 0x03); = 0xC3...To add the fourth item, we will retrive the result from the storage and keep going to append items.
As a result, we now have the result on Ethereum storage, proving the valid Merkle tree transition by the EVM calculation.
[stored proof on the EVM] startRoot = 0x0001234...; startIndex = 38; resultRoot = 0xF1F3A4B...; resultIndex = 47; mergedLeaves = 0xDEFEDFED...;Using the stored proof, we can validate the following information is valid or not. To validate the information, it computes the
mergedLeavesresult of theitemsToAddand compares it with the storedmergedLeaves.startRoot: 0x0001234..., startIndex: 38, itemsToAdd: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09], resultRoot: 0xF1F3A4B, resultIndex: 47Here is how it computes the
mergedLeavesofitemsToAddmerged = bytes32(0); for(uint i = 0; i < itemsToAdd.length; i++) { merged = keccak256(merged, itemsToAdd[i]); }Finally, if the result
mergedvalue equals to themergedLeavesvalue0xDEFEDFED...of the stored proof, it returnstrueor will be reverted.
Last updated
Was this helpful?