merkletree

package
v0.0.0-...-ad76458 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidNumOfDataBlocks is the error for an invalid number of data blocks.
	ErrInvalidNumOfDataBlocks = errors.New("the number of data blocks must be greater than 1")
	// ErrInvalidConfigMode is the error for an invalid configuration mode.
	ErrInvalidConfigMode = errors.New("invalid configuration mode")
	// ErrProofIsNil is the error for a nil proof.
	ErrProofIsNil = errors.New("proof is nil")
	// ErrDataBlockIsNil is the error for a nil data block.
	ErrDataBlockIsNil = errors.New("data block is nil")
	// ErrProofInvalidModeTreeNotBuilt is the error for an invalid mode in Proof() function.
	// Proof() function requires a built tree to generate the proof.
	ErrProofInvalidModeTreeNotBuilt = errors.New("merkle tree is not in built, could not generate proof by this method")
	// ErrProofInvalidDataBlock is the error for an invalid data block in Proof() function.
	ErrProofInvalidDataBlock = errors.New("data block is not a member of the merkle tree")
)

Functions

func DefaultHashFunc

func DefaultHashFunc(data []byte) ([]byte, error)

DefaultHashFunc is the default hash function used when no user-specified hash function is provided. It implements the SHA256 hash function and reuses sha256Digest to reduce memory allocations.

func DefaultHashFuncParallel

func DefaultHashFuncParallel(data []byte) ([]byte, error)

DefaultHashFuncParallel is the default hash function used by parallel algorithms when no user-specified hash function is provided. It implements the SHA256 hash function and creates a new hash digest for each call, ensuring that it is safe for concurrent use.

func Verify

func Verify(dataBlock DataBlock, proof *Proof, root []byte, config *Config) error

Verify checks if the data block is valid using the Merkle Tree proof and the provided Merkle root hash. It returns true if the data block is valid, false otherwise. An error is returned in case of any issues during the verification process.

Types

type Config

type Config struct {
	// Customizable hash function used for tree generation.
	HashFunc TypeHashFunc
	// Number of goroutines run in parallel.
	// If RunInParallel is true and NumRoutine is set to 0, use number of CPU as the number of goroutines.
	NumRoutines int
	// Mode of the Merkle Tree generation.
	Mode TypeConfigMode
	// If RunInParallel is true, the generation runs in parallel, otherwise runs without parallelization.
	// This increase the performance for the calculation of large number of data blocks, e.g. over 10,000 blocks.
	RunInParallel bool
	// SortSiblingPairs is the parameter for OpenZeppelin compatibility.
	// If set to `true`, the hashing sibling pairs are sorted.
	SortSiblingPairs bool
	// If true, the leaf nodes are NOT hashed before being added to the Merkle Tree.
	DisableLeafHashing bool
}

Config is the configuration of Merkle Tree.

type DataBlock

type DataBlock interface {
	// Serialize converts the data block into a byte slice.
	// It returns the serialized byte slice and an error, if any occurs during the serialization process.
	Serialize() ([]byte, error)
}

DataBlock is the interface for input data blocks used to generate the Merkle Tree. Implementations of DataBlock should provide a serialization method that converts the data block into a byte slice for hashing purposes.

type MerkleTree

type MerkleTree struct {
	Config

	// Root is the hash of the Merkle root node.
	Root []byte
	// Leaves are the hashes of the data blocks that form the Merkle Tree's leaves.
	// These hashes are used to generate the tree structure.
	// If the DisableLeafHashing configuration is set to true, the original data blocks are used as the leaves.
	Leaves [][]byte
	// Proofs are the proofs to the data blocks generated during the tree building process.
	Proofs []*Proof
	// Depth is the depth of the Merkle Tree.
	Depth int
	// NumLeaves is the number of leaves in the Merkle Tree.
	// This value is fixed once the tree is built.
	NumLeaves int
	// The sorted keys from the original map of data blocks for figuring out the index from the key
	Keys []string
	// contains filtered or unexported fields
}

MerkleTree implements the Merkle Tree data structure.

func New

func New(config *Config, blocks map[string]DataBlock) (m *MerkleTree, err error)

New generates a new Merkle Tree with the specified configuration and data blocks.

func (*MerkleTree) GetIndexForKey

func (t *MerkleTree) GetIndexForKey(key string) (int, bool)

Retrieve the index for the given key in the stored sorted key array

func (*MerkleTree) Proof

func (m *MerkleTree) Proof(dataBlock DataBlock) (*Proof, error)

Proof generates the Merkle proof for a data block using the previously generated Merkle Tree structure. This method is only available when the configuration mode is ModeTreeBuild or ModeProofGenAndTreeBuild. In ModeProofGen, proofs for all the data blocks are already generated, and the Merkle Tree structure is not cached.

func (*MerkleTree) Verify

func (m *MerkleTree) Verify(dataBlock DataBlock, proof *Proof) error

Verify checks if the data block is valid using the Merkle Tree proof and the cached Merkle root hash.

type Proof

type Proof struct {
	Siblings [][]byte // Sibling nodes to the Merkle Tree path of the data block.
	Path     uint32   // Path variable indicating whether the neighbor is on the left or right.
}

Proof represents a Merkle Tree proof.

type TypeConfigMode

type TypeConfigMode int

TypeConfigMode is the type in the Merkle Tree configuration indicating what operations are performed.

const (
	// ModeProofGen is the proof generation configuration mode.
	ModeProofGen TypeConfigMode = iota
	// ModeTreeBuild is the tree building configuration mode.
	ModeTreeBuild
	// ModeProofGenAndTreeBuild is the proof generation and tree building configuration mode.
	ModeProofGenAndTreeBuild
)

type TypeHashFunc

type TypeHashFunc func([]byte) ([]byte, error)

TypeHashFunc is the signature of the hash functions used for Merkle Tree generation.

Jump to

Keyboard shortcuts

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