Categorygithub.com/iden3/go-merkletree-sql/v2
modulepackage
2.0.6
Repository: https://github.com/iden3/go-merkletree-sql.git
Documentation: pkg.go.dev

# README

go-merkletree-sql GoDoc Go Report Card Test

MerkleTree compatible with version from circomlib.

Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8 with several changes and more functionalities.

Usage

More detailed examples can be found at the tests, and in the documentation.

import (
	"context"
	"math/big"
	"testing"

	"github.com/iden3/go-merkletree-sql/v2"
	"github.com/iden3/go-merkletree-sql/v2/db/memory"
	sql "github.com/iden3/go-merkletree-sql/v2/db/pgx"
	"github.com/stretchr/testify/require"
)

func TestMT(t *testing.T) {
	mtDepth := 40     // maximum depth of the tree
	mtId := uint64(1) // id of tree in sql database, you can have multiple trees with different ids
	ctx := context.Background()

	var treeStorage merkletree.Storage

	// setup pgxConn here
	treeStorage = sql.NewSqlStorage(pgxConn, mtId)
	// OR
	treeStorage = memory.NewMemoryStorage()

	mt, err := merkletree.NewMerkleTree(ctx, treeStorage, mtDepth)
	require.NoError(t, err)

	err = mt.Add(ctx, big.NewInt(1), big.NewInt(2))
	require.NoError(t, err)

	proof, _, err := mt.GenerateProof(ctx, big.NewInt(1), mt.Root())
	require.NoError(t, err)

	valid := merkletree.VerifyProof(mt.Root(), proof, big.NewInt(1), big.NewInt(2))
	require.True(t, valid)
}

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as below, without any additional terms or conditions.

License

Copyright 2023 0kims Association

This project is licensed under either of

at your option.

# Packages

No description provided by the author

# Functions

BytesToUint16 returns a uint16 from a byte array.
No description provided by the author
CircomSiblingsFromSiblings returns the full siblings compatible with circom.
Clone clones a byte array into a new byte array.
Concat concatenates arrays of bytes.
ElemBytesToBigInts serializes an array of ElemBytes to []byte.
ElemBytesToBytes serializes an array of ElemBytes to []byte.
HashElems performs a poseidon hash over the array of ElemBytes, currently we are using 2 elements.
HashElemsKey performs a poseidon hash over the array of ElemBytes, currently we are using 2 elements.
LeafKey computes the key of a leaf node given the hIndex and hValue of the entry of the leaf.
NewBigIntFromHashBytes returns a *big.Int from a byte array, swapping the endianness in the process.
No description provided by the author
No description provided by the author
NewHashFromBigInt returns a *Hash representation of the given *big.Int.
NewHashFromHex returns a *Hash representation of the given hex string.
NewHashFromString returns a *Hash representation of the given decimal string.
NewMerkleTree loads a new MerkleTree.
NewNodeEmpty creates a new empty node.
NewNodeFromBytes creates a new node by parsing the input []byte.
NewNodeLeaf creates a new leaf node.
NewNodeMiddle creates a new middle node.
NewProofFromBytes parses a byte array into a Proof.
NewProofFromData reconstructs proof from siblings and auxiliary node.
RootFromProof calculates the root that would correspond to a tree whose siblings are the ones in the proof with the leaf hashing to hIndex and hValue.
SetBitBigEndian sets the bit n in the bitmap to 1, in Big Endian.
SiblingsFromProof returns all the siblings of the proof.
SwapEndianness swaps the order of the bytes in the slice.
TestBit tests whether the bit n in bitmap is 1.
TestBitBigEndian tests whether the bit n in bitmap is 1, in Big Endian.
Uint16ToBytes returns a byte array from a uint16.
VerifyProof verifies the Merkle Proof for the entry and root.

# Constants

DataLen indicates how many elements are used for the data.
DBEntryTypeRoot indicates a type of DB entry that indicates the current Root of a MerkleTree.
ElemBytesLen is the length of the Hash byte array.
IndexLen indicates how many elements are used for the index.
NodeTypeEmpty indicates the type of an empty Node.
NodeTypeLeaf indicates the type of a leaf Node that contains a key & value.
NodeTypeMiddle indicates the type of middle Node that has children.

# Variables

ErrEntryIndexAlreadyExists is used when the entry index already exists in the tree.
ErrInvalidDBValue is used when a value in the key value DB is invalid (for example, it doen't contain a byte header and a []byte body of at least len=1.
ErrInvalidNodeFound is used when an invalid node is found and can't be parsed.
ErrInvalidProofBytes is used when a serialized proof is invalid.
ErrKeyNotFound is used when a key is not found in the MerkleTree.
ErrNodeBytesBadSize is used when the data of a node has an incorrect size and can't be parsed.
ErrNodeKeyAlreadyExists is used when a node key already exists.
ErrNotFound is used by the implementations of the interface db.Storage for when a key is not found in the storage.
ErrNotWritable is used when the MerkleTree is not writable and a write function is called.
ErrReachedMaxLevel is used when a traversal of the MT reaches the maximum level.
HashZero is used at Empty nodes.

# Structs

CircomProcessorProof defines the ProcessorProof compatible with circom.
CircomVerifierProof defines the VerifierProof compatible with circom.
Entry
Entry is the generic type that is stored in the MT.
KV contains a key (K) and a value (V).
MerkleTree is the struct with the main elements of the MerkleTree.
Node is the struct that represents a node in the MT.
NodeAux contains the auxiliary node used in a non-existence proof.
Proof defines the required elements for a MT proof of existence or non-existence.

# Interfaces

No description provided by the author
Storage is the interface that defines the methods for the storage used in the merkletree.

# Type aliases

Data is the type used to represent the data stored in an entry of the MT.
ElemBytes is the basic type used to store data in the MT.
Hash is the generic type stored in the MerkleTree.
KvMap is a key-value map between a sha256 byte array hash, and a KV struct.
NodeType defines the type of node in the MT.