merkletree

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func VerifyProof

func VerifyProof(data []byte, salt bool, proof *Proof, pollard [][]byte) (bool, error)

VerifyProof verifies a Merkle tree proof for a piece of data using the default hash type. The proof and path are as per Merkle tree's GenerateProof(), and root is the root hash of the tree against which the proof is to be verified. Note that this does not require the Merkle tree to verify the proof, only its root; this allows for checking against historical trees without having to instantiate them.

This returns true if the proof is verified, otherwise false.

func VerifyProofUsing

func VerifyProofUsing(data []byte, salt bool, proof *Proof, pollard [][]byte, hashType HashType) (bool, error)

VerifyProofUsing verifies a Merkle tree proof for a piece of data using the provided hash type. The proof and is as per Merkle tree's GenerateProof(), and root is the root hash of the tree against which the proof is to be verified. Note that this does not require the Merkle tree to verify the proof, only its root; this allows for checking against historical trees without having to instantiate them.

This returns true if the proof is verified, otherwise false.

Types

type HashFunc

type HashFunc func(...[]byte) []byte

HashFunc is a hashing function

type HashType

type HashType interface {
	// Hash calculates the hash of a given input
	Hash(...[]byte) []byte

	// HashLength provides the length of the hash
	HashLength() int
}

HashType defines the interface that must be supplied by hash functions

type MerkleTree

type MerkleTree struct {
	// data is the data from which the Merkle tree is created
	Data [][]byte
	// nodes are the leaf and branch nodes of the Merkle tree
	Nodes [][]byte
}

MerkleTree is the structure for the Merkle tree.

func New

func New(data [][]byte) (*MerkleTree, error)

New creates a new Merkle tree using the provided raw data and default hash type. Salting is not used. data must contain at least one element for it to be valid.

func (*MerkleTree) Datas

func (t *MerkleTree) Datas() [][]byte

Datas Root returns the Merkle date of the tree.

func (*MerkleTree) GenerateProof

func (t *MerkleTree) GenerateProof(data []byte, height int) (*Proof, error)

GenerateProof generates the proof for a piece of data. Height is the height of the pollard to verify the proof. If using the Merkle root to verify this should be 0. If the data is not present in the tree this will return an error. If the data is present in the tree this will return the hashes for each level in the tree and the index of the value in the tree

func (*MerkleTree) Height

func (t *MerkleTree) Height() int

Height returns the height of the Merkle tree.

func (*MerkleTree) Leaf

func (t *MerkleTree) Leaf() [][]byte

Leaf Root returns the Merkle leaf of the tree.

func (*MerkleTree) LeafDiff

func (t *MerkleTree) LeafDiff(other *MerkleTree) (lenEqual, equal bool, index int64)

LeafDiff 与另一个默克尔树对比,查找不同的 index lenEqual 两个默克尔树数据长度相同 equal 表示相同 idx 的数据相同,无论数据长度是否相同 例如:[1,2,3,4,5] [1,2,3,4] 这两个 equal 为 true [1,2,3,2,5] [1,2,3,4] equal 为 false index 为不相等时的索引

func (*MerkleTree) Pollard

func (t *MerkleTree) Pollard(height int) [][]byte

Pollard returns the Merkle root plus branches to a certain height. Height 0 will return just the root, height 1 the root plus the two branches directly above it, height 2 the root, two branches directly above it and four branches directly above them, etc.

func (*MerkleTree) Root

func (t *MerkleTree) Root() []byte

Root returns the Merkle root (hash of the root node) of the tree.

func (*MerkleTree) String

func (t *MerkleTree) String() string

String implements the stringer interface

type Proof

type Proof struct {
	Hashes [][]byte
	Index  uint64
}

Proof is a proof of a Merkle tree

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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