Merkletools-go is a library used by the Chainpoint project to efficiently generate and interact with Merkle trees. It possesses a number of options for creating blockchain-compatible trees and generating proofs of inclusion.
This package uses Go modules for dependency management.
go get github.com/chainpoint/merkletools-go
package main
import (
"fmt"
"encoding/hex"
"time"
merkletools "github.com/chainpoint/merkletools-go"
)
func main() {
mt := merkletools.MerkleTree{}
var hash, _ = hex.DecodeString("cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c")
leafCount := 100000
for i := 0; i < leafCount; i++ {
mt.AddLeaf(hash)
}
mt.MakeTree()
for i := 0; i < leafCount; i++ {
proof := mt.GetProof(i)
isValid := merkletools.VerifyProof(proof, mt.GetLeaf(i).Hash, mt.GetMerkleRoot())
if !isValid {
panic("Bad Proof!")
}
}
}