Skip to content

Zecrey-Labs/zecrey-smt

 
 

Repository files navigation

zkbnb-smt

zkbnb-smt is an implementation code library based on the concept of SparseMerkleTree, which implements the concepts of data persistence and data compression.

For an overview, see the design.

Installation

go get github.com/zecrey-labs/zecrey-smt@latest

Quickstart

package main

import (
	"crypto/sha256"
	"fmt"

	bsmt "github.com/zecrey-labs/zecrey-smt"
	"github.com/zecrey-labs/zecrey-smt/database/memory"
)

func main() {
	db := memory.NewMemoryDB()
	hasher := bsmt.NewHasher(sha256.New())
	nilHash := hasher.Hash([]byte("nilHash"))
	maxDepth := uint8(8)

	smt, err := bsmt.NewBNBSparseMerkleTree(hasher, db, maxDepth, nilHash)
	if err != nil {
		fmt.Println(err)
		return
	}

	key1 := uint64(1)
	key2 := uint64(2)
	key3 := uint64(23)
	val1 := hasher.Hash([]byte("test1"))
	val2 := hasher.Hash([]byte("test2"))
	val3 := hasher.Hash([]byte("test3"))
	smt.Set(key1, val1)
	smt.Set(key2, val2)
	smt.Set(key3, val3)

	version1, err := smt.Commit(nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	_, err = smt.Get(key1, &version1)
	if err != nil {
		fmt.Println(err)
		return
	}

	_, err = smt.Get(key2, &version1)
	if err != nil {
		fmt.Println(err)
		return
	}
	_, err = smt.Get(key3, &version1)
	if err != nil {
		fmt.Println(err)
		return
	}

	proof, err := smt.GetProof(key1)
	if err != nil {
		fmt.Println(err)
		return
	}
	if !smt.VerifyProof(key1, proof) {
		fmt.Println("verify proof failed")
		return
	}
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%