Documentation ¶
Index ¶
- Constants
- Variables
- func NewBigIntFromHashBytes(b []byte) (*big.Int, error)
- func SetBitBigEndian(bitmap []byte, n uint)
- func SwapEndianness(b []byte) []byte
- func TestBit(bitmap []byte, n uint) bool
- func TestBitBigEndian(bitmap []byte, n uint) bool
- func VerifyProof(rootKey *Hash, proof *Proof, k, v *big.Int) bool
- type CircomProcessorProof
- type CircomVerifierProof
- type Hash
- func CircomSiblingsFromSiblings(siblings []*Hash, levels int) []*Hash
- func HashElems(elems ...*big.Int) (*Hash, error)
- func HashElemsKey(key *big.Int, elems ...*big.Int) (*Hash, error)
- func LeafKey(k, v *Hash) (*Hash, error)
- func NewHashFromBigInt(b *big.Int) *Hash
- func NewHashFromBytes(b []byte) (*Hash, error)
- func NewHashFromHex(h string) (*Hash, error)
- func NewHashFromString(s string) (*Hash, error)
- func RootFromProof(proof *Proof, k, v *big.Int) (*Hash, error)
- func SiblingsFromProof(proof *Proof) []*Hash
- type MerkleTree
- func (mt *MerkleTree) Add(k, v *big.Int) error
- func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof, error)
- func (mt *MerkleTree) DB() db.Storage
- func (mt *MerkleTree) Delete(k *big.Int) error
- func (mt *MerkleTree) DumpLeafs(rootKey *Hash) ([]byte, error)
- func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error)
- func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, *big.Int, error)
- func (mt *MerkleTree) GenerateSCVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error)
- func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error)
- func (mt *MerkleTree) GetNode(key *Hash) (*Node, error)
- func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error
- func (mt *MerkleTree) ImportDumpedLeafs(b []byte) error
- func (mt *MerkleTree) MaxLevels() int
- func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error
- func (mt *MerkleTree) Root() *Hash
- func (mt *MerkleTree) Snapshot(rootKey *Hash) (*MerkleTree, error)
- func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error)
- func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error
- type Node
- type NodeAux
- type NodeType
- type Proof
Constants ¶
const (
// ElemBytesLen is the length of the Hash byte array
ElemBytesLen = 32
)
Variables ¶
var ( // ErrNodeKeyAlreadyExists is used when a node key already exists. ErrNodeKeyAlreadyExists = errors.New("key already exists") // ErrKeyNotFound is used when a key is not found in the MerkleTree. ErrKeyNotFound = errors.New("Key not found in the MerkleTree") // ErrNodeBytesBadSize is used when the data of a node has an incorrect // size and can't be parsed. ErrNodeBytesBadSize = 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") // HashZero is used at Empty nodes 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} )
Functions ¶
func NewBigIntFromHashBytes ¶
NewBigIntFromHashBytes returns a *big.Int from a byte array, swapping the endianness in the process. This is the intended method to get a *big.Int from a byte array that previously has ben generated by the Hash.Bytes() method.
func SetBitBigEndian ¶
SetBitBigEndian sets the bit n in the bitmap to 1, in Big Endian.
func SwapEndianness ¶
SwapEndianness swaps the order of the bytes in the slice.
func TestBitBigEndian ¶
TestBitBigEndian tests whether the bit n in bitmap is 1, in Big Endian.
Types ¶
type CircomProcessorProof ¶
type CircomProcessorProof struct { OldRoot *Hash `json:"oldRoot"` NewRoot *Hash `json:"newRoot"` Siblings []*Hash `json:"siblings"` OldKey *Hash `json:"oldKey"` OldValue *Hash `json:"oldValue"` NewKey *Hash `json:"newKey"` NewValue *Hash `json:"newValue"` IsOld0 bool `json:"isOld0"` // 0: NOP, 1: Update, 2: Insert, 3: Delete Fnc int `json:"fnc"` }
CircomProcessorProof defines the ProcessorProof compatible with circom. Is the data of the proof between the transition from one state to another.
func (CircomProcessorProof) String ¶
func (p CircomProcessorProof) String() string
String returns a human readable string representation of the CircomProcessorProof
type CircomVerifierProof ¶
type CircomVerifierProof struct { Root *Hash `json:"root"` Siblings []*Hash `json:"siblings"` OldKey *Hash `json:"oldKey"` OldValue *Hash `json:"oldValue"` IsOld0 bool `json:"isOld0"` Key *Hash `json:"key"` Value *Hash `json:"value"` Fnc int `json:"fnc"` // 0: inclusion, 1: non inclusion }
CircomVerifierProof defines the VerifierProof compatible with circom. Is the data of the proof that a certain leaf exists in the MerkleTree.
type Hash ¶
type Hash [32]byte
Hash is the generic type stored in the MerkleTree
func CircomSiblingsFromSiblings ¶
CircomSiblingsFromSiblings returns the full siblings compatible with circom
func HashElems ¶
HashElems performs a poseidon hash over the array of ElemBytes, currently we are using 2 elements. Uses poseidon.Hash to be compatible with the circom circuits implementations.
func HashElemsKey ¶
HashElemsKey performs a poseidon hash over the array of ElemBytes, currently we are using 2 elements.
func LeafKey ¶
LeafKey computes the key of a leaf node given the hIndex and hValue of the entry of the leaf.
func NewHashFromBigInt ¶
NewHashFromBigInt returns a *Hash representation of the given *big.Int
func NewHashFromBytes ¶
NewHashFromBytes returns a *Hash from a byte array, swapping the endianness in the process. This is the intended method to get a *Hash from a byte array that previously has ben generated by the Hash.Bytes() method.
func NewHashFromHex ¶
NewHashFromHex returns a *Hash representation of the given hex string
func NewHashFromString ¶
NewHashFromString returns a *Hash representation of the given decimal string
func RootFromProof ¶
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.
func SiblingsFromProof ¶
SiblingsFromProof returns all the siblings of the proof.
func (*Hash) Bytes ¶
Bytes returns the []byte representation of the *Hash, which always is 32 bytes length.
func (Hash) MarshalText ¶
MarshalText implements the marshaler for the Hash type
func (*Hash) UnmarshalText ¶
UnmarshalText implements the unmarshaler for the Hash type
type MerkleTree ¶
MerkleTree is the struct with the main elements of the MerkleTree
func NewMerkleTree ¶
func NewMerkleTree(storage db.Storage, maxLevels int) (*MerkleTree, error)
NewMerkleTree loads a new Merkletree. If in the sotrage already exists one will open that one, if not, will create a new one.
func (*MerkleTree) Add ¶
func (mt *MerkleTree) Add(k, v *big.Int) error
Add adds a Key & Value into the MerkleTree. Where the `k` determines the path from the Root to the Leaf.
func (*MerkleTree) AddAndGetCircomProof ¶
func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof, error)
AddAndGetCircomProof does an Add, and returns a CircomProcessorProof
func (*MerkleTree) Delete ¶
func (mt *MerkleTree) Delete(k *big.Int) error
Delete removes the specified Key from the MerkleTree and updates the path from the deleted key to the Root with the new values. This method removes the key from the MerkleTree, but does not remove the old nodes from the key-value database; this means that if the tree is accessed by an old Root where the key was not deleted yet, the key will still exist. If is desired to remove the key-values from the database that are not under the current Root, an option could be to dump all the leafs (using mt.DumpLeafs) and import them in a new MerkleTree in a new database (using mt.ImportDumpedLeafs), but this will loose all the Root history of the MerkleTree
func (*MerkleTree) DumpLeafs ¶
func (mt *MerkleTree) DumpLeafs(rootKey *Hash) ([]byte, error)
DumpLeafs returns all the Leafs that exist under the given Root. If no Root is given (nil), it uses the current Root of the MerkleTree.
func (*MerkleTree) GenerateCircomVerifierProof ¶
func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error)
GenerateCircomVerifierProof returns the CircomVerifierProof for a certain key in the MerkleTree. If the rootKey is nil, the current merkletree root is used.
func (*MerkleTree) GenerateProof ¶
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) GenerateSCVerifierProof ¶
func (mt *MerkleTree) GenerateSCVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error)
GenerateSCVerifierProof returns the CircomVerifierProof for a certain key in the MerkleTree with the Siblings without the extra 0 needed at the circom circuits, which makes it straight forward to verifiy inside a Smart Contract. If the rootKey is nil, the current merkletree root is used.
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) ImportDumpedLeafs ¶
func (mt *MerkleTree) ImportDumpedLeafs(b []byte) error
ImportDumpedLeafs parses and adds to the MerkleTree the dumped list of leafs from the DumpLeafs function.
func (*MerkleTree) MaxLevels ¶
func (mt *MerkleTree) MaxLevels() int
MaxLevels returns the MT maximum level
func (*MerkleTree) PrintGraphViz ¶
func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error
PrintGraphViz prints directly the GraphViz() output
func (*MerkleTree) Snapshot ¶
func (mt *MerkleTree) Snapshot(rootKey *Hash) (*MerkleTree, error)
Snapshot returns a read-only copy of the MerkleTree
func (*MerkleTree) Update ¶
func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error)
Update updates the value of a specified key in the MerkleTree, and updates the path from the leaf to the Root with the new values. Returns the CircomProcessorProof.
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.go and merkletree_test.go
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 [2]*Hash // 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 key & // value. 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.
func (*Proof) AllSiblings ¶
AllSiblings returns all the siblings of the proof.