{"id":16044437,"url":"https://github.com/nervosnetwork/merkle-mountain-range","last_synced_at":"2025-04-05T16:09:13.481Z","repository":{"id":35124148,"uuid":"209726839","full_name":"nervosnetwork/merkle-mountain-range","owner":"nervosnetwork","description":"A generalized merkle mountain range implementation.","archived":false,"fork":false,"pushed_at":"2023-08-06T19:08:28.000Z","size":76,"stargazers_count":48,"open_issues_count":4,"forks_count":29,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-04-24T16:18:47.880Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nervosnetwork.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-20T07:02:21.000Z","updated_at":"2024-06-19T01:30:50.992Z","dependencies_parsed_at":"2024-06-19T01:30:48.063Z","dependency_job_id":"fa3e5165-93a0-4b78-ba93-aa5e069aa30a","html_url":"https://github.com/nervosnetwork/merkle-mountain-range","commit_stats":{"total_commits":43,"total_committers":9,"mean_commits":4.777777777777778,"dds":0.5348837209302326,"last_synced_commit":"9e77d3ef81ddfdd9b7dd9583762582e859849dde"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fmerkle-mountain-range","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fmerkle-mountain-range/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fmerkle-mountain-range/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nervosnetwork%2Fmerkle-mountain-range/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nervosnetwork","download_url":"https://codeload.github.com/nervosnetwork/merkle-mountain-range/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361691,"owners_count":20926643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T00:03:40.077Z","updated_at":"2025-04-05T16:09:13.439Z","avatar_url":"https://github.com/nervosnetwork.png","language":"Rust","funding_links":[],"categories":["Researches"],"sub_categories":["Merkle Mountain Ranges"],"readme":"# Merkle mountain range\n[![Crates.io](https://img.shields.io/crates/v/ckb-merkle-mountain-range.svg)](https://crates.io/crates/ckb-merkle-mountain-range)\n\nA generalized merkle mountain range implementation.\n\n## Features\n\n* Leaves accumulation\n* Multi leaves merkle proof\n* Accumulate from last leaf's merkle proof\n\n## Construct\n\n``` txt\n# An 11 leaves MMR\n\n          14\n       /       \\\n     6          13\n   /   \\       /   \\\n  2     5     9     12     17\n / \\   /  \\  / \\   /  \\   /  \\\n0   1 3   4 7   8 10  11 15  16 18\n```\n\nIn MMR, we use the insertion order to reference leaves and nodes.\n\nWe insert a new leaf to MMR by the following:\n\n1. insert leaf or node to next position.\n2. if the current position has a left sibling, we merge the left and right nodes to produce a new parent node, then go back to step 1 to insert the node.\n\nFor example, we insert a leaf to the example MMR:\n\n1. insert leaf to next position: `19`.\n2. now check the left sibling `18` and calculate parent node: `merge(mmr[18], mmr[19])`.\n3. insert parent node to position `20`.\n4. since the node `20` also has a left sibling `17`, calculate parent node: `merge(mmr[17], mmr[20])`.\n5. insert new node to next position `21`.\n6. since the node `21` have no left sibling, complete the insertion.\n\nExample MMR after insertion of a new leaf:\n\n``` txt\n          14\n       /       \\\n     6          13            21\n   /   \\       /   \\         /   \\\n  2     5     9     12     17     20\n / \\   /  \\  / \\   /  \\   /  \\   /  \\\n0   1 3   4 7   8 10  11 15  16 18  19\n```\n\n## Merkle root\n\nAn MMR is constructed by one or more sub merkle trees (or mountains). Each sub merkle tree's root is a peak in MMR, we calculate the MMR root by bagging these peaks from right to left.\n\nFor example, in the 11 leaf MMR we have 3 peaks: `14, 17, 18`, we bag these peaks from right to left to get the root: `merge(mmr[14], merge(mmr[17], mmr[18]))`.\n\n## Merkle proof\n\nThe merkle proof is an array of hashes constructed with the following parts:\n\n1. A merkle proof from the leaf's sibling to the peak that contains the leaf.\n2. A hash that bags all right-hand side peaks, skip this part if no right-hand peaks.\n3. Hashes of all left-hand peaks from right to left, skip this part if no left-hand peaks.\n\nWe can reconstruct the merkle root from the proofs. Pre-calculating the peak positions from the size of MMR may help us do the bagging.\n\n## References\n\n* [Merkle mountain range](https://github.com/opentimestamps/opentimestamps-server/blob/master/doc/merkle-mountain-range.md)\n* [Grin Doc](https://github.com/mimblewimble/grin/blob/master/doc/mmr.md#structure)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnervosnetwork%2Fmerkle-mountain-range","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnervosnetwork%2Fmerkle-mountain-range","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnervosnetwork%2Fmerkle-mountain-range/lists"}