Documentation ¶
Overview ¶
Package merkletree implements a high-performance Merkle Tree in Go. It supports parallel execution for enhanced performance and offers compatibility with OpenZeppelin through sorted sibling pairs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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") )
var FLAG bool
Functions ¶
func DefaultHashFunc ¶
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 ¶
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.
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 // contains filtered or unexported fields }
MerkleTree implements the Merkle Tree data structure.
func New ¶
func New(config *Config, blocks []DataBlock) (m *MerkleTree, err error)
New generates a new Merkle Tree with the specified configuration and data blocks.
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) String ¶
func (m *MerkleTree) String()
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 ¶
TypeHashFunc is the signature of the hash functions used for Merkle Tree generation.