Documentation
¶
Overview ¶
Package arbo implements a Merkle Tree compatible with the circomlib implementation of the MerkleTree, following the specification from https://docs.iden3.io/publications/pdfs/Merkle-Tree.pdf and https://eprint.iacr.org/2018/955.
Allows to define which hash function to use. So for example, when working with zkSnarks the Poseidon hash function can be used, but when not, it can be used the Blake2b hash function, which has much faster computation time.
Package arbo > vt.go implements the Virtual Tree, which computes a tree without computing any hash. With the idea of once all the leafs are placed in their positions, the hashes can be computed, avoiding computing a node hash more than one time.
Index ¶
- Constants
- Variables
- func BigIntToBytes(blen int, bi *big.Int) []byte
- func BytesToBigInt(b []byte) *big.Int
- func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool, error)
- func PackSiblings(hashFunc HashFunction, siblings [][]byte) []byte
- func ReadIntermediateChilds(b []byte) ([]byte, []byte)
- func ReadLeafValue(b []byte) ([]byte, []byte)
- func SwapEndianness(b []byte) []byte
- func UnpackSiblings(hashFunc HashFunction, b []byte) ([][]byte, error)
- type HashBlake2b
- type HashFunction
- type HashPoseidon
- type HashSha256
- type Tree
- func (t *Tree) Add(k, v []byte) error
- func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error)
- func (t *Tree) Dump(rootKey []byte) ([]byte, error)
- func (t *Tree) GenProof(k []byte) ([]byte, []byte, error)
- func (t *Tree) Get(k []byte) ([]byte, []byte, error)
- func (t *Tree) GetNLeafs() (int, error)
- func (t *Tree) Graphviz(w io.Writer, rootKey []byte) error
- func (t *Tree) GraphvizFirstNLevels(w io.Writer, rootKey []byte, untilLvl int) error
- func (t *Tree) HashFunction() HashFunction
- func (t *Tree) ImportDump(b []byte) error
- func (t *Tree) Iterate(rootKey []byte, f func([]byte, []byte)) error
- func (t *Tree) IterateWithStop(rootKey []byte, f func(int, []byte, []byte) bool) error
- func (t *Tree) PrintGraphviz(rootKey []byte) error
- func (t *Tree) PrintGraphvizFirstNLevels(rootKey []byte, untilLvl int) error
- func (t *Tree) Root() []byte
- func (t *Tree) Update(k, v []byte) error
Constants ¶
const ( // PrefixValueLen defines the bytes-prefix length used for the Value // bytes representation stored in the db PrefixValueLen = 2 // PrefixValueEmpty is used for the first byte of a Value to indicate // that is an Empty value PrefixValueEmpty = 0 // PrefixValueLeaf is used for the first byte of a Value to indicate // that is a Leaf value PrefixValueLeaf = 1 // PrefixValueIntermediate is used for the first byte of a Value to // indicate that is a Intermediate value PrefixValueIntermediate = 2 )
Variables ¶
var ( // TypeHashSha256 represents the label for the HashFunction of Sha256 TypeHashSha256 = []byte("sha256") // TypeHashPoseidon represents the label for the HashFunction of // Poseidon TypeHashPoseidon = []byte("poseidon") // TypeHashBlake2b represents the label for the HashFunction of Blake2b TypeHashBlake2b = []byte("blake2b") // HashFunctionSha256 contains the HashSha256 struct which implements // the HashFunction interface HashFunctionSha256 HashSha256 // HashFunctionPoseidon contains the HashPoseidon struct which implements // the HashFunction interface HashFunctionPoseidon HashPoseidon // HashFunctionBlake2b contains the HashBlake2b struct which implements // the HashFunction interface HashFunctionBlake2b HashBlake2b )
var ( // ErrKeyNotFound is used when a key is not found in the db neither in // the current db Batch. ErrKeyNotFound = fmt.Errorf("key not found") // ErrKeyAlreadyExists is used when trying to add a key as leaf to the // tree that already exists. ErrKeyAlreadyExists = fmt.Errorf("key already exists") // ErrInvalidValuePrefix is used when going down into the tree, a value // is read from the db and has an unrecognized prefix. ErrInvalidValuePrefix = fmt.Errorf("invalid value prefix") // ErrDBNoTx is used when trying to use Tree.dbPut but Tree.dbBatch==nil ErrDBNoTx = fmt.Errorf("dbPut error: no db Batch") // ErrMaxLevel indicates when going down into the tree, the max level is // reached ErrMaxLevel = fmt.Errorf("max level reached") // ErrMaxVirtualLevel indicates when going down into the tree, the max // virtual level is reached ErrMaxVirtualLevel = fmt.Errorf("max virtual level reached") )
Functions ¶
func BigIntToBytes ¶
BigIntToBytes converts a *big.Int into a byte array in Little-Endian
func BytesToBigInt ¶
BytesToBigInt converts a byte array in Little-Endian representation into *big.Int
func CheckProof ¶
func CheckProof(hashFunc HashFunction, k, v, root, packedSiblings []byte) (bool, error)
CheckProof verifies the given proof. The proof verification depends on the HashFunction passed as parameter.
func PackSiblings ¶
func PackSiblings(hashFunc HashFunction, siblings [][]byte) []byte
PackSiblings packs the siblings into a byte array. [ 1 byte | L bytes | S * N bytes ] [ bitmap length (L) | bitmap | N non-zero siblings ] Where the bitmap indicates if the sibling is 0 or a value from the siblings array. And S is the size of the output of the hash function used for the Tree.
func ReadIntermediateChilds ¶
ReadIntermediateChilds reads from a byte array the two childs keys
func ReadLeafValue ¶
ReadLeafValue reads from a byte array the leaf key & value
func SwapEndianness ¶
SwapEndianness swaps the order of the bytes in the byte slice.
func UnpackSiblings ¶
func UnpackSiblings(hashFunc HashFunction, b []byte) ([][]byte, error)
UnpackSiblings unpacks the siblings from a byte array.
Types ¶
type HashBlake2b ¶
type HashBlake2b struct{}
HashBlake2b implements the HashFunction interface for the Blake2b hash
func (HashBlake2b) Hash ¶
func (f HashBlake2b) Hash(b ...[]byte) ([]byte, error)
Hash implements the hash method for the HashFunction HashBlake2b
func (HashBlake2b) Type ¶
func (f HashBlake2b) Type() []byte
Type returns the type of HashFunction for the HashBlake2b
type HashFunction ¶
HashFunction defines the interface that is expected for a hash function to be used in a generic way in the Tree.
type HashPoseidon ¶
type HashPoseidon struct{}
HashPoseidon implements the HashFunction interface for the Poseidon hash
func (HashPoseidon) Hash ¶
func (f HashPoseidon) Hash(b ...[]byte) ([]byte, error)
Hash implements the hash method for the HashFunction HashPoseidon
func (HashPoseidon) Type ¶
func (f HashPoseidon) Type() []byte
Type returns the type of HashFunction for the HashPoseidon
type HashSha256 ¶
type HashSha256 struct{}
HashSha256 implements the HashFunction interface for the Sha256 hash
func (HashSha256) Hash ¶
func (f HashSha256) Hash(b ...[]byte) ([]byte, error)
Hash implements the hash method for the HashFunction HashSha256
func (HashSha256) Type ¶
func (f HashSha256) Type() []byte
Type returns the type of HashFunction for the HashSha256
type Tree ¶
Tree defines the struct that implements the MerkleTree functionalities
func NewTree ¶
NewTree returns a new Tree, if there is a Tree still in the given database, it will load it.
func (*Tree) Add ¶
Add inserts the key-value into the Tree. If the inputs come from a *big.Int, is expected that are represented by a Little-Endian byte array (for circom compatibility).
func (*Tree) AddBatch ¶
AddBatch adds a batch of key-values to the Tree. Returns an array containing the indexes of the keys failed to add.
func (*Tree) Dump ¶
Dump exports all the Tree leafs in a byte array of length: [ N * (2+len(k+v)) ]. Where N is the number of key-values, and for each k+v: [ 1 byte | 1 byte | S bytes | len(v) bytes ] [ len(k) | len(v) | key | value ] Where S is the size of the output of the hash function used for the Tree.
func (*Tree) GenProof ¶
GenProof generates a MerkleTree proof for the given key. If the key exists in the Tree, the proof will be of existence, if the key does not exist in the tree, the proof will be of non-existence.
func (*Tree) Graphviz ¶
Graphviz iterates across the full tree to generate a string Graphviz representation of the tree and writes it to w
func (*Tree) GraphvizFirstNLevels ¶
GraphvizFirstNLevels iterates across the first NLevels of the tree to generate a string Graphviz representation of the first NLevels of the tree and writes it to w
func (*Tree) HashFunction ¶
func (t *Tree) HashFunction() HashFunction
HashFunction returns Tree.hashFunction
func (*Tree) ImportDump ¶
ImportDump imports the leafs (that have been exported with the ExportLeafs method) in the Tree.
func (*Tree) Iterate ¶
Iterate iterates through the full Tree, executing the given function on each node of the Tree.
func (*Tree) IterateWithStop ¶
IterateWithStop does the same than Iterate, but with int for the current level, and a boolean parameter used by the passed function, is to indicate to stop iterating on the branch when the method returns 'true'.
func (*Tree) PrintGraphviz ¶
PrintGraphviz prints the output of Tree.Graphviz
func (*Tree) PrintGraphvizFirstNLevels ¶
PrintGraphvizFirstNLevels prints the output of Tree.GraphvizFirstNLevels