merkle

package
v2.0.0-...-8ee05d3 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Verify

func Verify(dataBlock DataBlock, proof *Proof, root []byte, hashFunc HashFuncType) (bool, error)

Verify verifies the data block with the Merkle Tree proof and Merkle root hash

Types

type Config

type Config struct {
	// Customizable hash function used for tree generation.
	HashFunc HashFuncType
	// 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 ModeType
	// If true, generate a dummy node with random hash value.
	// Otherwise, then the odd node situation is handled by duplicating the previous node.
	NoDuplicates bool
	// IMPORTANT: To match the behavior of merkletreejs, sort leaves before building tree
	SortLeaves bool
	// IMPORTANT: To match the behavior of merkletreejs, sort pairs before hashing them
	SortPairs bool
}

Config is the configuration of Merkle Tree.

type DataBlock

type DataBlock interface {
	Serialize() ([]byte, error)
}

DataBlock is the interface of input data blocks to generate the Merkle Tree.

type HashFuncType

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

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

type MerkleTree

type MerkleTree struct {
	*Config

	// Root is the Merkle root hash
	Root []byte
	// Leaves are Merkle Tree leaves, i.e. the hashes of the data blocks for tree generation
	Leaves [][]byte
	// Proofs are proofs to the data blocks generated during the tree building process
	Proofs []*Proof
	// Depth is the Merkle Tree depth
	Depth uint32
	// contains filtered or unexported fields
}

MerkleTree implements the Merkle Tree structure

func New

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

New generates a new Merkle Tree with specified configuration.

func (*MerkleTree) GenerateProof

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

GenerateProof generates the Merkle proof for a data block with the Merkle Tree structure generated beforehand. The 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) (bool, error)

Verify verifies the data block with the Merkle Tree proof

type ModeType

type ModeType int

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

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

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 implements the Merkle Tree proof.

Jump to

Keyboard shortcuts

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