Coder Social home page Coder Social logo

nmt's Introduction

Namespaced Merkle Tree (NMT)

Go version API Reference golangci-lint Go codecov.io

A Namespaced Merkle Tree is

[...] an ordered Merkle tree that uses a modified hash function so that each node in the tree includes the range of namespaces of the messages in all of the descendants of each node. The leafs in the tree are ordered by the namespace identifiers of the messages. In a namespaced Merkle tree, each non-leaf node in the tree contains the lowest and highest namespace identifiers found in all the leaf nodes that are descendants of the non-leaf node, in addition to the hash of the concatenation of the children of the node. This enables Merkle inclusion proofs to be created that prove to a verifier that all the elements of the tree for a specific namespace have been included in a Merkle inclusion proof.

The concept was first introduced by @musalbas in the LazyLedger academic paper.

Example

package main

import (
    "crypto/sha256"
    "fmt"

    "github.com/lazyledger/nmt"
    "github.com/lazyledger/nmt/namespace"
)

func main() {
    // the tree will use this namespace size
    nidSize := 1
    // the leaves that will be pushed
    data := []namespace.PrefixedData{
        namespace.PrefixedDataFrom(namespace.ID{0}, []byte("leaf_0")),
        namespace.PrefixedDataFrom(namespace.ID{0}, []byte("leaf_1")),
        namespace.PrefixedDataFrom(namespace.ID{1}, []byte("leaf_2")),
        namespace.PrefixedDataFrom(namespace.ID{1}, []byte("leaf_3"))}

    // Init a tree with the namespace size as well as
    // the underlying hash function:
    tree := New(sha256.New(), namespace.Size(nidSize))
    for _, d := range data {
        if err := tree.Push(d); err != nil {
            panic("unexpected error")
        }
    }
    
    // compute the root
    root := tree.Root()
    // the root's min/max namespace is the min and max namespace of all leaves:
    if root.Min().Equal(namespace.ID{0}) {
        fmt.Printf("Min namespace: %x\n", root.Min())
    }
    if root.Max().Equal(namespace.ID{1}) {
        fmt.Printf("Max namespace: %x\n", root.Max())
    }
    
    // compute proof for namespace 0:
    proof, err := tree.ProveNamespace(namespace.ID{0}); if err != nil {
        panic("unexpected err")
    }
    
    // verify proof using the root and the leaves of namespace 0:
    leafs := []namespace.PrefixedData{
        namespace.PrefixedDataFrom(namespace.ID{0}, []byte("leaf_0")),
        namespace.PrefixedDataFrom(namespace.ID{0}, []byte("leaf_1"))}
    
    if proof.VerifyNamespace(nmtHasher, namespace.ID{0}, leafs, root) {
        fmt.Printf("Successfully verified namespace: %x\n", namespace.ID{0})
    }
    
    if proof.VerifyNamespace(nmtHasher, namespace.ID{2}, leafs, root) {
        panic(fmt.Sprintf("Proof for namespace %x, passed for namespace: %x\n", namespace.ID{0}, namespace.ID{2}))
    }
}

The above will create a Namespaced merkle tree with four leafs which looks like this:

example

Where nid_0 = nid_1 = 0 and nid_2 = nid_3 = 1 and data_i = "leaf_i" for i = 0,...,3.

Related

This implementation (currently) uses NebulousLabs' merkletree implementation and was heavily inspired by the initial implementation in the LazyLedger prototype.

nmt's People

Contributors

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