merkle

package
v0.0.0-...-7dbb591 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: GPL-3.0, Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Copyright © 2018, 2019 Weald Technology Trading Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2018, 2019 Weald Technology Trading Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeProof

func EncodeProof(p *Proof) []byte

func VerifyProof

func VerifyProof(data []byte, proof *Proof, root []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, proof *Proof, root []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 BLAKE3

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

func NewBLAKE3

func NewBLAKE3() *BLAKE3

New creates a new SHA256 hashing method

func (*BLAKE3) Hash

func (h *BLAKE3) Hash(a, b []byte) []byte

Hash generates a BLAKE3 hash from input byte arrays

func (*BLAKE3) HashLength

func (h *BLAKE3) HashLength() int

HashLength returns the length of hashes generated by Hash() in bytes

type HashType

type HashType interface {
	// Hash calculates the hash of a given input
	Hash([]byte, []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 {
	// contains filtered or unexported fields
}

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. data must contain at least one element for it to be valid.

func NewUsing

func NewUsing(data [][]byte, hash HashType) (*MerkleTree, error)

NewUsing creates a new Merkle tree using the provided raw data and supplied hash type. data must contain at least one element for it to be valid.

func (*MerkleTree) EncodedProofLength

func (t *MerkleTree) EncodedProofLength() int

EncodedProofLength returns the byte length of the proof for a piece of data. 4 bytes are for how many hashes are in the path, 8 bytes for embedding the index in the tree (see proof.go for details).

func (*MerkleTree) GenerateProof

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

GenerateProof generates the proof for a piece of data. 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) Root

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

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

type Proof

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

Proof is a proof of a Merkle tree

func DecodeProof

func DecodeProof(p []byte) *Proof

Jump to

Keyboard shortcuts

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