gomerk

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 9 Imported by: 0

README

gomerk

Go Merkle Tree.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyTree         = errors.New("expected non-zero number of leaves")
	ErrInvalidNodeLength = errors.New("expected 32 bytes")
	ErrNotALeaf          = errors.New("index is not a leaf")
	ErrLeafNotInTree     = errors.New("leaf is not in tree")
	ErrDuplicatedIndex   = errors.New("cannot prove duplicated index")
	ErrIndexOutOfBounds  = errors.New("index out of bounds")
	ErrInvalidFormat     = errors.New("invalid tree format")
	ErrInvariant         = errors.New("invariant violation")
	ErrInvalidHex        = errors.New("invalid hex string")
	ErrAbiEncode         = errors.New("abi encoding error")
	ErrUnsupportedType   = errors.New("unsupported type")
	ErrMismatchedCount   = errors.New("mismatched leaf encoding count")
)

Functions

func ConcatSorted added in v0.2.0

func ConcatSorted(a, b Bytes32) []byte

func GetProof added in v0.2.0

func GetProof(tree []string, index int) ([]string, error)

GetProof returns a single proof for a leaf at index.

func IsValidTree added in v0.2.0

func IsValidTree(tree []string) bool

IsValidTree checks if tree is a valid Merkle tree.

func MakeTree added in v0.2.0

func MakeTree(leaves []Bytes32) ([]string, error)

MakeTree builds a Merkle tree from leaves.

func ProcessMultiProof added in v0.2.0

func ProcessMultiProof(mp *MultiProof) (string, error)

ProcessMultiProof computes the root from a MultiProof.

func ProcessProof added in v0.2.0

func ProcessProof(leaf Bytes32, proof []string) (string, error)

ProcessProof computes the root from a leaf and proof.

func RenderTree added in v0.2.0

func RenderTree(tree []string) (string, error)

RenderTree returns a string representation of the tree.

func TreeLeaves added in v0.2.0

func TreeLeaves(tree []string) iter.Seq2[int, string]

TreeLeaves returns an iterator over leaf indices.

func TreeNodes added in v0.2.0

func TreeNodes(tree []string) iter.Seq2[int, string]

TreeNodes returns an iterator over tree node indices.

func VerifySimple added in v0.2.0

func VerifySimple(root string, leaf Bytes32, proof []string) (bool, error)

VerifySimple is a static verification function.

func VerifyStandard added in v0.2.0

func VerifyStandard(root string, leafEncoding []string, leaf []any, proof []string) (bool, error)

VerifyStandard is a static verification function.

Types

type Bytes32 added in v0.2.0

type Bytes32 [32]byte

func HashLeaf

func HashLeaf(data []byte) Bytes32

func HashNode added in v0.2.0

func HashNode(a, b Bytes32) Bytes32

func HexToBytes32 added in v0.2.0

func HexToBytes32(s string) (b Bytes32, err error)

func Keccak256 added in v0.2.0

func Keccak256(data []byte) (h Bytes32)

func MustHexToBytes32 added in v0.2.0

func MustHexToBytes32(s string) Bytes32

func (Bytes32) Compare added in v0.2.0

func (a Bytes32) Compare(b Bytes32) int

func (Bytes32) Hex added in v0.2.0

func (b Bytes32) Hex() string

func (Bytes32) IsZero added in v0.2.0

func (b Bytes32) IsZero() bool

func (Bytes32) Less added in v0.2.0

func (a Bytes32) Less(b Bytes32) bool

func (Bytes32) String added in v0.2.0

func (b Bytes32) String() string

type MultiProof added in v0.2.0

type MultiProof struct {
	Leaves     []string `json:"leaves"`
	Proof      []string `json:"proof"`
	ProofFlags []bool   `json:"proofFlags"`
}

MultiProof represents a proof for multiple leaves.

func GetMultiProof added in v0.2.0

func GetMultiProof(tree []string, indices []int) (*MultiProof, error)

GetMultiProof generates a proof for multiple leaf indices.

type SimpleMerkleTree added in v0.2.0

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

SimpleMerkleTree is a Merkle tree for Bytes32 values.

func LoadSimpleMerkleTree added in v0.2.0

func LoadSimpleMerkleTree(data SimpleTreeData) (*SimpleMerkleTree, error)

LoadSimpleMerkleTree loads a tree from serialized data.

func NewSimpleMerkleTree added in v0.2.0

func NewSimpleMerkleTree(values []Bytes32, sortLeaves bool) (*SimpleMerkleTree, error)

NewSimpleMerkleTree creates a new SimpleMerkleTree from values.

func (*SimpleMerkleTree) All added in v0.2.0

func (t *SimpleMerkleTree) All() iter.Seq2[int, string]

All returns an iterator over all (index, value) pairs.

func (*SimpleMerkleTree) At added in v0.2.0

func (t *SimpleMerkleTree) At(i int) (string, bool)

func (*SimpleMerkleTree) Dump added in v0.2.0

func (t *SimpleMerkleTree) Dump() SimpleTreeData

Dump serializes the tree.

func (*SimpleMerkleTree) GetMultiProof added in v0.2.0

func (t *SimpleMerkleTree) GetMultiProof(leaves []Bytes32) (*MultiProof, error)

GetMultiProof returns a proof for multiple leaves.

func (*SimpleMerkleTree) GetMultiProofByIndices added in v0.2.0

func (t *SimpleMerkleTree) GetMultiProofByIndices(indices []int) (*MultiProof, error)

GetMultiProofByIndices returns a proof for leaves at the given indices.

func (*SimpleMerkleTree) GetProof added in v0.2.0

func (t *SimpleMerkleTree) GetProof(leaf Bytes32) ([]string, error)

GetProof returns a proof for the given leaf.

func (*SimpleMerkleTree) GetProofByIndex added in v0.2.0

func (t *SimpleMerkleTree) GetProofByIndex(i int) ([]string, error)

GetProofByIndex returns a proof for the leaf at index.

func (*SimpleMerkleTree) Len added in v0.2.0

func (t *SimpleMerkleTree) Len() int

func (*SimpleMerkleTree) Render added in v0.2.0

func (t *SimpleMerkleTree) Render() (string, error)

Render returns a string representation.

func (*SimpleMerkleTree) Root added in v0.2.0

func (t *SimpleMerkleTree) Root() string

func (*SimpleMerkleTree) Validate added in v0.2.0

func (t *SimpleMerkleTree) Validate() error

Validate checks tree integrity.

func (*SimpleMerkleTree) Verify added in v0.2.0

func (t *SimpleMerkleTree) Verify(leaf Bytes32, proof []string) (bool, error)

Verify checks if a leaf is in the tree using the given proof.

func (*SimpleMerkleTree) VerifyMultiProof added in v0.2.0

func (t *SimpleMerkleTree) VerifyMultiProof(mp *MultiProof) (bool, error)

VerifyMultiProof checks a multi-proof.

type SimpleTreeData added in v0.2.0

type SimpleTreeData struct {
	Format string        `json:"format"`
	Tree   []string      `json:"tree"`
	Values []SimpleValue `json:"values"`
}

SimpleTreeData is the serialization format for SimpleMerkleTree.

type SimpleValue added in v0.2.0

type SimpleValue struct {
	Value     string `json:"value"`
	TreeIndex int    `json:"treeIndex"`
}

SimpleValue holds a leaf value and its tree index.

type StandardMerkleTree added in v0.2.0

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

StandardMerkleTree is a Merkle tree for ABI-encoded structured data.

func LoadStandardMerkleTree added in v0.2.0

func LoadStandardMerkleTree(data StandardTreeData) (*StandardMerkleTree, error)

LoadStandardMerkleTree loads a tree from serialized data.

func NewStandardMerkleTree added in v0.2.0

func NewStandardMerkleTree(values [][]any, leafEncoding []string, sortLeaves bool) (*StandardMerkleTree, error)

NewStandardMerkleTree creates a new StandardMerkleTree.

func (*StandardMerkleTree) All added in v0.2.0

func (t *StandardMerkleTree) All() iter.Seq2[int, []any]

All returns an iterator over all (index, value) pairs.

func (*StandardMerkleTree) At added in v0.2.0

func (t *StandardMerkleTree) At(i int) ([]any, bool)

func (*StandardMerkleTree) Dump added in v0.2.0

Dump serializes the tree.

func (*StandardMerkleTree) GetMultiProofByIndices added in v0.2.0

func (t *StandardMerkleTree) GetMultiProofByIndices(indices []int) (*MultiProof, error)

GetMultiProofByIndices returns a proof for leaves at the given indices.

func (*StandardMerkleTree) GetProof added in v0.2.0

func (t *StandardMerkleTree) GetProof(leaf []any) ([]string, error)

GetProof returns a proof for the given leaf.

func (*StandardMerkleTree) GetProofByIndex added in v0.2.0

func (t *StandardMerkleTree) GetProofByIndex(i int) ([]string, error)

GetProofByIndex returns a proof for the leaf at index.

func (*StandardMerkleTree) LeafEncoding added in v0.2.0

func (t *StandardMerkleTree) LeafEncoding() []string

func (*StandardMerkleTree) Len added in v0.2.0

func (t *StandardMerkleTree) Len() int

func (*StandardMerkleTree) Render added in v0.2.0

func (t *StandardMerkleTree) Render() (string, error)

Render returns a string representation.

func (*StandardMerkleTree) Root added in v0.2.0

func (t *StandardMerkleTree) Root() string

func (*StandardMerkleTree) Validate added in v0.2.0

func (t *StandardMerkleTree) Validate() error

Validate checks tree integrity.

func (*StandardMerkleTree) Verify added in v0.2.0

func (t *StandardMerkleTree) Verify(leaf []any, proof []string) (bool, error)

Verify checks if a leaf is in the tree using the given proof.

func (*StandardMerkleTree) VerifyMultiProof added in v0.2.0

func (t *StandardMerkleTree) VerifyMultiProof(mp *MultiProof) (bool, error)

VerifyMultiProof checks a multi-proof.

type StandardTreeData added in v0.2.0

type StandardTreeData struct {
	Format       string          `json:"format"`
	LeafEncoding []string        `json:"leafEncoding"`
	Tree         []string        `json:"tree"`
	Values       []StandardValue `json:"values"`
}

StandardTreeData is the serialization format for StandardMerkleTree.

type StandardValue added in v0.2.0

type StandardValue struct {
	Value     []any `json:"value"`
	TreeIndex int   `json:"treeIndex"`
}

StandardValue holds a leaf value and its tree index.

Directories

Path Synopsis
example
simple command
Simple Merkle Tree Example
Simple Merkle Tree Example
standard command
Standard Merkle Tree Example
Standard Merkle Tree Example

Jump to

Keyboard shortcuts

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