Documentation ¶
Index ¶
- Constants
- Variables
- func CheckEntryInField(e Entry) bool
- func ElemBytesToBigInts(elems ...ElemBytes) []*big.Int
- func ElemBytesToPoseidonInput(elems ...ElemBytes) ([poseidon.T]*big.Int, error)
- func ElemsBytesToBytes(es []ElemBytes) []byte
- func VerifyProof(rootKey *Hash, proof *Proof, hIndex, hValue *Hash) bool
- type Data
- type ElemBytes
- type Entrier
- type Entry
- func (e *Entry) Bytes() []byte
- func (e *Entry) Clone() *Entry
- func (e1 *Entry) Equal(e2 *Entry) bool
- func (e *Entry) HIndex() (*Hash, error)
- func (e *Entry) HValue() (*Hash, error)
- func (e *Entry) HiHv() (*Hash, *Hash, error)
- func (e *Entry) Index() []ElemBytes
- func (e Entry) MarshalText() ([]byte, error)
- func (e *Entry) UnmarshalText(text []byte) error
- func (e *Entry) Value() []ElemBytes
- type Hash
- func HashElems(elems ...ElemBytes) (*Hash, error)
- func HashElemsKey(key *big.Int, elems ...ElemBytes) (*Hash, error)
- func HexStringToHash(s string) Hash
- func LeafKey(hIndex, hValue *Hash) (*Hash, error)
- func NewHashFromBigInt(e *big.Int) *Hash
- func RootFromProof(proof *Proof, hIndex, hValue *Hash) (*Hash, error)
- func SiblingsFromProof(proof *Proof) []*Hash
- type Index
- type MerkleTree
- func (mt *MerkleTree) AddClaim(e Entrier) error
- func (mt *MerkleTree) AddEntry(e *Entry) error
- func (mt *MerkleTree) DumpClaims(rootKey *Hash) ([]string, error)
- func (mt *MerkleTree) DumpClaimsIoWriter(w io.Writer, rootKey *Hash) error
- func (mt *MerkleTree) DumpTree(w io.Writer, rootKey *Hash) error
- func (mt *MerkleTree) EntryExists(entry *Entry, rootKey *Hash) error
- func (mt *MerkleTree) GenerateProof(hIndex *Hash, rootKey *Hash) (*Proof, error)
- func (mt *MerkleTree) GetDataByIndex(hIndex *Hash) (*Data, error)
- func (mt *MerkleTree) GetNode(key *Hash) (*Node, error)
- func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error
- func (mt *MerkleTree) ImportDumpedClaims(dumpedClaims []string) error
- func (mt *MerkleTree) ImportTree(i io.Reader) error
- func (mt *MerkleTree) MaxLevels() int
- func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error
- func (mt *MerkleTree) RootKey() *Hash
- func (mt *MerkleTree) Snapshot(rootKey *Hash) (*MerkleTree, error)
- func (mt *MerkleTree) Storage() db.Storage
- func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error
- type Node
- type NodeAux
- type NodeType
- type Proof
Constants ¶
const ( // ElemBytesLen is the length in bytes of each element used for storing // data and hashing. ElemBytesLen = 32 // IndexLen indicates how many elements are used for the index. IndexLen = 4 // DataLen indicates how many elements are used for the data. DataLen = 8 )
Variables ¶
var ( // ErrNodeKeyAlreadyExists is used when a node key already exists. ErrNodeKeyAlreadyExists = errors.New("node already exists") // ErrEntryIndexNotFound is used when no entry is found for an index. ErrEntryIndexNotFound = errors.New("node index not found in the DB") // ErrNodeDataBadSize is used when the data of a node has an incorrect // size and can't be parsed. ErrNodeDataBadSize = errors.New("node data has incorrect size in the DB") // ErrReachedMaxLevel is used when a traversal of the MT reaches the // maximum level. ErrReachedMaxLevel = errors.New("reached maximum level of the merkle tree") // ErrInvalidNodeFound is used when an invalid node is found and can't // be parsed. ErrInvalidNodeFound = errors.New("found an invalid node in the DB") // ErrInvalidProofBytes is used when a serialized proof is invalid. ErrInvalidProofBytes = errors.New("the serialized proof is invalid") // 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. ErrInvalidDBValue = errors.New("the value in the DB is invalid") // ErrEntryIndexAlreadyExists is used when the entry index already // exists in the tree. ErrEntryIndexAlreadyExists = errors.New("the entry index already exists in the tree") // ErrNotWritable is used when the MerkleTree is not writable and a write function is called ErrNotWritable = errors.New("Merkle Tree not writable") // ErrEntryDataNotMatch is used when the entry data doesn't match the expected one. ErrEntryDataNotMatch = errors.New("Entry data doesn't match the expected one") // HashZero is a hash value of zeros, and is the key of an empty node. HashZero = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // ElemBytesOne is a constant element used as a prefix to compute leaf node keys. ElemBytesOne = ElemBytes{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} )
Functions ¶
func CheckEntryInField ¶
func ElemBytesToBigInts ¶
func ElemBytesToPoseidonInput ¶ added in v0.0.8
func ElemsBytesToBytes ¶
ElemsBytesToBytes serializes an array of ElemBytes to []byte.
Types ¶
type Data ¶
Data is the type used to represent the data stored in an entry of the MT. It consists of 8 elements: e0, e1, e2, e3, ...; where v = [e0,e1], index = [e2,e3].
func BigIntsToData ¶
func HexsToData ¶
func IntArrayToData ¶
func IntsToData ¶
func NewDataFromBytes ¶
func NewDataFromBytes(b [ElemBytesLen * DataLen]byte) *Data
func (Data) MarshalText ¶
func (*Data) UnmarshalText ¶
type ElemBytes ¶
type ElemBytes [ElemBytesLen]byte
ElemBytes is the basic type used to store data in the MT. ElemBytes corresponds to the serialization of an element from mimc7.
func NewElemBytesFromBigInt ¶ added in v0.0.8
type Entry ¶
type Entry struct { Data Data // contains filtered or unexported fields }
Entry is the generic type that is stored in the MT. The entry should not be modified after creating because the cached hIndex and hValue won't be updated.
func NewEntryFromBytes ¶
func NewEntryFromHexs ¶
func NewEntryFromIntArray ¶
func NewEntryFromInts ¶
func (*Entry) HIndex ¶
HIndex calculates the hash of the Index of the Entry, used to find the path from the root to the leaf in the MT.
func (Entry) MarshalText ¶
func (*Entry) UnmarshalText ¶
type Hash ¶
type Hash ElemBytes
Hash is the type used to represent a hash used in the MT.
func HashElems ¶
HashElems performs a poseidon hash over the array of ElemBytes. Uses poseidon.PoseidonHash to be compatible with the circom circuits implementations. The maxim slice input size is poseidon.T
func HashElemsKey ¶
HashElemsKey performs a poseidon hash over the array of ElemBytes.
func HexStringToHash ¶
HexStringToHash decodes a hex string into a Hash.
func LeafKey ¶
LeafKey computes the key of a leaf node given the hIndex and hValue of the entry of the leaf.
func NewHashFromBigInt ¶ added in v0.0.8
func RootFromProof ¶
RootFromProof calculates the root that would correspond to a tree whose siblings are the ones in the proof with the claim hashing to hIndex and hValue.
func SiblingsFromProof ¶ added in v0.0.8
SiblingsFromProof returns all the siblings of the proof. This function is used to generate the siblings input for the circom circuits.
func (Hash) MarshalText ¶
func (*Hash) UnmarshalText ¶
type Index ¶
Index is the type used to represent the index of an entry in the MT, used to find the path from the root to the leaf that contains such entry.
type MerkleTree ¶
MerkleTree is the struct with the main elements of the Merkle Tree
func NewMerkleTree ¶
func NewMerkleTree(storage db.Storage, maxLevels int) (*MerkleTree, error)
NewMerkleTree generates a new Merkle Tree
func (*MerkleTree) AddClaim ¶
func (mt *MerkleTree) AddClaim(e Entrier) error
AddClaim adds the Claim that fullfills the Entrier interface to the MerkleTree
func (*MerkleTree) AddEntry ¶
func (mt *MerkleTree) AddEntry(e *Entry) error
AddEntry adds the Entry to the MerkleTree
func (*MerkleTree) DumpClaims ¶
func (mt *MerkleTree) DumpClaims(rootKey *Hash) ([]string, error)
DumpClaims outputs a list of all the claims in hex.
func (*MerkleTree) DumpClaimsIoWriter ¶
func (mt *MerkleTree) DumpClaimsIoWriter(w io.Writer, rootKey *Hash) error
DumpClaimsIoWriter uses Walk function to get all the Claims of the tree and write them to w. The output is JSON encoded with claims in hex.
func (*MerkleTree) DumpTree ¶
func (mt *MerkleTree) DumpTree(w io.Writer, rootKey *Hash) error
DumpTree outputs a list of all the key value in hex. Notice that this will output the full tree, which is not needed to reconstruct the Tree. To reconstruct the tree can be done from the output of DumpClaims funtion. The difference between DumpTree and DumpClaims is that with DumpTree the size of the output will be almost the double (in raw bytes, but with the current implementation DumpTree output is even smaller than DumpClaims output size, but because DumpTree stores in binary while DumpClaims stores in hex) but to recover the tree will not need to compute the Tree, while with DumpClaims will require to compute the Tree (with the computational cost of each hash)
func (*MerkleTree) EntryExists ¶
func (mt *MerkleTree) EntryExists(entry *Entry, rootKey *Hash) error
EntryExists checks if a given entry is in the merkle tree starting from the rootKey. If rootKey is nil, the current merkle tree root is used.
func (*MerkleTree) GenerateProof ¶
func (mt *MerkleTree) GenerateProof(hIndex *Hash, rootKey *Hash) (*Proof, error)
GenerateProof generates the proof of existence (or non-existence) of an Entry's hash Index for a Merkle Tree given the root. If the rootKey is nil, the current merkletree root is used
func (*MerkleTree) GetDataByIndex ¶
func (mt *MerkleTree) GetDataByIndex(hIndex *Hash) (*Data, error)
GetDataByIndex returns the data from the MT in the position of the hash of the index (hIndex)
func (*MerkleTree) GetNode ¶
func (mt *MerkleTree) GetNode(key *Hash) (*Node, error)
GetNode gets a node by key from the MT. Empty nodes are not stored in the tree; they are all the same and assumed to always exist.
func (*MerkleTree) GraphViz ¶
func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error
GraphViz uses Walk function to generate a string GraphViz representation of the tree and writes it to w
func (*MerkleTree) ImportDumpedClaims ¶
func (mt *MerkleTree) ImportDumpedClaims(dumpedClaims []string) error
ImportClaims parses and adds the dumped list of claims in hex from the DumpClaims function.
func (*MerkleTree) ImportTree ¶
func (mt *MerkleTree) ImportTree(i io.Reader) error
ImportTree imports the tree from the output from the DumpTree function
func (*MerkleTree) MaxLevels ¶
func (mt *MerkleTree) MaxLevels() int
MaxLevels returns the MT maximum level
func (*MerkleTree) PrintGraphViz ¶ added in v0.0.8
func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error
PrintGraphViz prints directly the GraphViz() output
func (*MerkleTree) RootKey ¶
func (mt *MerkleTree) RootKey() *Hash
RootKey returns the MT root node key
func (*MerkleTree) Snapshot ¶
func (mt *MerkleTree) Snapshot(rootKey *Hash) (*MerkleTree, error)
func (*MerkleTree) Storage ¶
func (mt *MerkleTree) Storage() db.Storage
Storage returns the MT storage
func (*MerkleTree) Walk ¶
func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error
Walk iterates over all the branches of a MerkleTree with the given rootKey if rootKey is nil, it will get the current RootKey of the current state of the MerkleTree. For each node, it calls the f function given in the parameters. See some examples of the Walk function usage in the merkletree_test.go test functions: TestMTWalk, TestMTWalkGraphViz, TestMTWalkDumpClaims
type Node ¶
type Node struct { // Type is the type of node in the tree. Type NodeType // ChildL is the left child of a middle node. ChildL *Hash // ChildR is the right child of a middle node. ChildR *Hash // Entry is the data stored in a leaf node. Entry *Entry // contains filtered or unexported fields }
Node is the struct that represents a node in the MT. The node should not be modified after creation because the cached key won't be updated.
func NewNodeFromBytes ¶
NewNodeFromBytes creates a new node by parsing the input []byte.
func NewNodeMiddle ¶
NewNodeMiddle creates a new middle node.
func (*Node) Key ¶
Key computes the key of the node by hashing the content in a specific way for each type of node. This key is used as the hash of the merklee tree for each node.
type NodeType ¶
type NodeType byte
NodeType defines the type of node in the MT.
const ( // NodeTypeMiddle indicates the type of middle Node that has children. NodeTypeMiddle NodeType = 0 // NodeTypeLeaf indicates the type of a leaf Node that contains a claim. NodeTypeLeaf NodeType = 1 // NodeTypeEmpty indicates the type of an empty Node. NodeTypeEmpty NodeType = 2 // DBEntryTypeRoot indicates the type of a DB entry that indicates the current Root of a MerkleTree DBEntryTypeRoot NodeType = 3 )
type Proof ¶
type Proof struct { // existence indicates wether this is a proof of existence or non-existence. Existence bool // Siblings is a list of non-empty sibling keys. Siblings []*Hash NodeAux *NodeAux // contains filtered or unexported fields }
Proof defines the required elements for a MT proof of existence or non-existence.
func NewProofFromBytes ¶
NewProofFromBytes parses a byte array into a Proof.