merkle

package
v1.14.4 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: LGPL-3.0 Imports: 11 Imported by: 8

Documentation

Index

Constants

View Source
const (
	LEFT byte = iota
	RIGHT
	MAX_SIZE = 1024 * 1024
)

Variables

View Source
var EMPTY_HASH = common.Uint256{}

Functions

func HashChildren added in v1.9.0

func HashChildren(left, right common.Uint256) common.Uint256

func HashLeaf added in v1.9.0

func HashLeaf(data []byte) common.Uint256

func MerkleHashes added in v1.9.0

func MerkleHashes(preLeaves []common.Uint256, depth int) [][]common.Uint256

func MerkleLeafPath added in v1.9.0

func MerkleLeafPath(data []byte, hashes []common.Uint256) ([]byte, error)

func MerkleProve added in v1.9.0

func MerkleProve(path []byte, root common.Uint256) ([]byte, error)

Types

type CompactMerkleTree

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

CompactMerkleTree calculate merkle tree with compact hash store in HashStore

func NewTree

func NewTree(tree_size uint32, hashes []common.Uint256, store HashStore) *CompactMerkleTree

NewTree returns a CompactMerkleTree instance

func (*CompactMerkleTree) Append

func (self *CompactMerkleTree) Append(leafv []byte) []common.Uint256

Append appends a leaf to the merkle tree and returns the audit path

func (*CompactMerkleTree) AppendHash

func (self *CompactMerkleTree) AppendHash(leaf common.Uint256) []common.Uint256

AppendHash appends a leaf hash to the merkle tree and returns the audit path

func (*CompactMerkleTree) ConsistencyProof

func (self *CompactMerkleTree) ConsistencyProof(m, n uint32) []common.Uint256

ConsistencyProof returns consistency proof

func (*CompactMerkleTree) DumpStatus

func (self *CompactMerkleTree) DumpStatus()

func (*CompactMerkleTree) GetRootWithNewLeaf

func (self *CompactMerkleTree) GetRootWithNewLeaf(newLeaf common.Uint256) common.Uint256

GetRootWithNewLeaf returns the new root hash if newLeaf is appended to the merkle tree

func (*CompactMerkleTree) GetRootWithNewLeaves added in v1.6.0

func (self *CompactMerkleTree) GetRootWithNewLeaves(newLeaf []common.Uint256) common.Uint256

func (*CompactMerkleTree) Hashes

func (self *CompactMerkleTree) Hashes() []common.Uint256

func (*CompactMerkleTree) InclusionProof

func (self *CompactMerkleTree) InclusionProof(m, n uint32) ([]common.Uint256, error)

InclusionProof returns the proof d[m] in D[0:n] m zero based index, n size 1-based

func (*CompactMerkleTree) Marshal

func (self *CompactMerkleTree) Marshal() ([]byte, error)

func (*CompactMerkleTree) Root

func (self *CompactMerkleTree) Root() common.Uint256

Root returns root hash of merkle tree

func (*CompactMerkleTree) TreeSize

func (self *CompactMerkleTree) TreeSize() uint32

func (*CompactMerkleTree) UnMarshal

func (self *CompactMerkleTree) UnMarshal(buf []byte) error

type HashStore

type HashStore interface {
	Append(hash []common.Uint256) error
	Flush() error
	Close()
	GetHash(pos uint32) (common.Uint256, error)
}

HashStore is an interface for persist hash

func NewFileHashStore

func NewFileHashStore(name string, tree_size uint32) (HashStore, error)

NewFileHashStore returns a HashStore implement in file

func NewMemHashStore

func NewMemHashStore() HashStore

NewMemHashStore returns a HashStore implement in memory

type MerkleVerifier

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

MerkleVerifier verify inclusion and consist proof

func NewMerkleVerifier

func NewMerkleVerifier() *MerkleVerifier

func (*MerkleVerifier) VerifyConsistency

func (self *MerkleVerifier) VerifyConsistency(old_tree_size,
	new_tree_size uint32, old_root, new_root common.Uint256, proof []common.Uint256) error

Verify the consistency between two root hashes.

old_tree_size must be <= new_tree_size.

Args:
    old_tree_size: size of the older tree.
    new_tree_size: size of the newer_tree.
    old_root: the root hash of the older tree.
    new_root: the root hash of the newer tree.
    proof: the consistency proof.

Returns:
    True. The return value is enforced by a decorator and need not be
        checked by the caller.

Raises:
    ConsistencyError: the proof indicates an inconsistency
        (this is usually really serious!).
    ProofError: the proof is invalid.
    ValueError: supplied tree sizes are invalid.

func (*MerkleVerifier) VerifyLeafHashInclusion

func (self *MerkleVerifier) VerifyLeafHashInclusion(leaf_hash common.Uint256,
	leaf_index uint32, proof []common.Uint256, root_hash common.Uint256, tree_size uint32) error

Verify a Merkle Audit PATH.

leaf_hash: The hash of the leaf for which the proof was provided. leaf_index: Index of the leaf in the tree. proof: A list of SHA-256 hashes representing the Merkle audit path. tree_size: The size of the tree root_hash: The root hash of the tree

Returns:

nil when the proof is valid

func (*MerkleVerifier) VerifyLeafInclusion

func (self *MerkleVerifier) VerifyLeafInclusion(leaf []byte,
	leaf_index uint32, proof []common.Uint256, root_hash common.Uint256, tree_size uint32) error

Verify a Merkle Audit PATH.

leaf: The leaf for which the proof is provided leaf_index: Index of the leaf in the tree. proof: A list of SHA-256 hashes representing the Merkle audit path. tree_size: The size of the tree root_hash: The root hash of the tree

Returns:

nil when the proof is valid

type TreeHasher

type TreeHasher struct {
}

func (TreeHasher) HashFullTree

func (self TreeHasher) HashFullTree(leaves [][]byte) common.Uint256

func (TreeHasher) HashFullTreeWithLeafHash

func (self TreeHasher) HashFullTreeWithLeafHash(leaves []common.Uint256) common.Uint256

Jump to

Keyboard shortcuts

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