Code Monkey home page Code Monkey logo

mmr-c's Introduction

Merkle mountain range

Merkle mountain range in C.

You can check unit tests to learn the usage.

Construct

# An 11 leaves MMR

          14
       /       \
     6          13
   /   \       /   \
  2     5     9     12     17
 / \   /  \  / \   /  \   /  \
0   1 3   4 7   8 10  11 15  16 18

In MMR, we use the insertion order to reference leaves and nodes. we inserting a new leaf to MMR by the following:

  1. insert leaf or node to next position.
  2. 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.

For example, we insert a leaf to the example MMR:

  1. insert leaf to next position: 19.
  2. now check the left sibling 18 and calculate parent node: merge(mmr[18], mmr[19]).
  3. insert parent node to position 20.
  4. the node 20 also has a left sibling 17, calculate parent node: merge(mmr[17], mmr[20]).
  5. insert new node to next position 21.
  6. the node 20 have no left sibling, complete the insertion.

Example MMR after insertion of a new leaf:

          14
       /       \
     6          13            21
   /   \       /   \         /   \
  2     5     9     12     17     20
 / \   /  \  / \   /  \   /  \   /  \
0   1 3   4 7   8 10  11 15  16 18  19

Merkle root

An 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.

For example, we have a MMR with 3 peaks: 14, 17, 18, we bagging thses peaks from right to left to get the root: merge(merge(mmr[18], mmr[17]), mmr[14]).

Merkle proof

The merkle proof is an array of hashes constructed by the follows parts:

  1. A merkle proof from the leaf's sibling to the peak that contains the leaf.
  2. A hash that bagging all right-hand side peaks, skip this part if no right-hand peaks.
  3. Hashes of all left-hand peaks, the from right to left, skip this part if no left-hand peaks.

We can reconstruct the merkle root from the proofs. Pre calculate the peaks positions from the size of MMR may help us do the bagging.

References

LICENSE

MIT

AUTHOR

Jiang Jinyang [email protected]

mmr-c's People

Contributors

jjyr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.