fMerkleTree

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GobDecode

func GobDecode(data []byte, e any) error

func GobEncode

func GobEncode(e any) ([]byte, error)

func IndexOfElement

func IndexOfElement(elements []Element, element Element, fromIndex int, comparator ComparatorFunction) int

* * Find an element in the tree * @param elements elements of tree * @param element An element to find * @param comparator A function that checks leaf value equality * @param fromIndex The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned * @returns {number} Index if element is found, otherwise -1

func MIMC7 added in v1.2.1

func MIMC7(left Element, right Element) []byte

func Poseidon added in v1.1.0

func Poseidon(left Element, right Element) []byte

func Poseidon2 added in v1.1.0

func Poseidon2(left Element, right Element) []byte

func SHA256Hash added in v1.1.0

func SHA256Hash(left Element, right Element) []byte

Types

type BaseTree

type BaseTree struct {
	// contains filtered or unexported fields
}

func (*BaseTree) BulkInsert

func (bt *BaseTree) BulkInsert(elements []Element) error

func (BaseTree) Capacity

func (bt BaseTree) Capacity() int

func (BaseTree) Elements

func (bt BaseTree) Elements() []Element

func (*BaseTree) Insert

func (bt *BaseTree) Insert(element Element) error

* * Insert new element into the tree * @param element Element to insert

func (BaseTree) Layers

func (bt BaseTree) Layers() [][]Element

func (*BaseTree) Path

func (bt *BaseTree) Path(index int) (ProofPath, error)

* * Get merkle path to a leaf * @param {number} index Leaf index to generate path for * @returns {{pathElements: Object[], pathIndex: number[]}} An object containing adjacent elements and left-right index

func (BaseTree) Root

func (bt BaseTree) Root() Element

func (*BaseTree) SetLayer

func (bt *BaseTree) SetLayer(i, j int, val Element)

func (*BaseTree) Update

func (bt *BaseTree) Update(index int, element Element) error

* * Change an element in the tree * @param {number} index Index of element to change * @param element Updated element value

func (*BaseTree) VerifyProof added in v1.2.0

func (bt *BaseTree) VerifyProof(elem Element, proof ProofPath) error

* VerifyProof *

func (BaseTree) Zeros

func (bt BaseTree) Zeros() []Element

type ComparatorFunction

type ComparatorFunction func(left Element, right Element) bool

type Element

type Element []byte

func (Element) BigInt added in v1.2.1

func (e Element) BigInt() *big.Int

func (Element) Cmp added in v1.2.1

func (e Element) Cmp(x Element) bool

func (Element) Hex

func (e Element) Hex() string

type HashFunction

type HashFunction func(left Element, right Element) []byte

type MerkleTree

type MerkleTree struct {
	*BaseTree
}

func DeserializeMerkleTree

func DeserializeMerkleTree(data SerializedTreeState, hashFn HashFunction) (*MerkleTree, error)

func NewMerkleTree

func NewMerkleTree(levels int, elements []Element, zeroElement Element, hashFn HashFunction) (*MerkleTree, error)

func (*MerkleTree) BulkInsert

func (mt *MerkleTree) BulkInsert(elements []Element) error

* * Insert multiple elements into the tree. * @param elements Elements to insert

func (MerkleTree) GetTreeSlices

func (mt MerkleTree) GetTreeSlices(count int) ([]TreeSlice, error)

func (MerkleTree) IndexOf

func (mt MerkleTree) IndexOf(element Element) int

func (MerkleTree) Proof

func (mt MerkleTree) Proof(element Element) (ProofPath, error)

func (MerkleTree) Serialize

func (mt MerkleTree) Serialize() (SerializedTreeState, error)

* * Serialize entire tree state including intermediate layers into a plain object * Deserializing it back will not require to recompute any hashes * Elements are not converted to a plain type, this is responsibility of the caller

type ProofPath

type ProofPath struct {
	PathElements  []Element `json:"pathElements"`
	PathIndices   []int     `json:"pathIndices"`
	PathPositions []int     `json:"pathPositions"`
	PathRoot      Element   `json:"pathRoot"`
}

type SerializedTreeState

type SerializedTreeState interface {
	GetLevels() int
	GetRoot() Element
	GetLayers() ([][]Element, error)
	GetZeros() ([]Element, error)
}

func NewSerializedTreeState

func NewSerializedTreeState(tree *MerkleTree) (SerializedTreeState, error)

type TreeEdge

type TreeEdge struct {
	EdgeElement       Element   `json:"edgeElement"`
	EdgePath          ProofPath `json:"edgePath"`
	EdgeIndex         int       `json:"edgeIndex"`
	EdgeElementsCount int       `json:"edgeElementsCount"`
}

type TreeSlice

type TreeSlice struct {
	Edge     TreeEdge  `json:"edge"`
	Elements []Element `json:"elements"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL