merkle

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

A merkle tree for lanyard.org. merkle uses keccak256 hashing for leaves and intermediary nodes and therefore is vulnerable to a second preimage attack. This package does not duplicate or pad leaves in the case of odd cardinality and therefore may be unsafe for certain use cases. If you are curious about this type of bug, see the following bitcoin issue.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Valid

func Valid(root []byte, proof [][]byte, target []byte) bool

Cumulatively hashes the list pairwise starting with (target, proof[0]). Finally, the cumulative hash is compared with the root.

Types

type Tree

type Tree [][][]byte

A the outer list represents levels in the tree. Each level is a list of nodes in the tree. Each node is a hash of its children.

Example
var addrStrs = []string{
	"0xE124F06277b5AC791bA45B92853BA9A0ea93327D",
	"0x07d048f78B7C093B3Ef27D478B78026a70D9734e",
	"0x38976611f5f7bEAd7e79E752f5B80AE72dD3eFa7",
	"0x1Ab00ffedD724B930080aD30269083F1453cF34E",
	"0x860a6bC426C3bb1186b2E11Ac486ABa000C209B4",
	"0x0B3eC21fc53AD8b17AF4A80723c1496541fCb35f",
	"0x2D13F6CEe6dA8b30a84ee7954594925bd5E47Ab7",
	"0x3C64Cd43331beb5B6fAb76dbAb85226955c5CC3A",
	"0x238dA873f984188b4F4c7efF03B5580C65a49dcB",
	"0xbAfC038aDfd8BcF6E632C797175A057714416d04",
}
var addrs [][]byte
for i := range addrStrs {
	addrs = append(addrs, common.HexToAddress(addrStrs[i]).Bytes())
}
var tr Tree
tr = New(addrs)
fmt.Println(common.Bytes2Hex(tr.Root()))
Output:

ed40d49077a2cd13601cf79a512e6b92c7fd0f952e7dc9f4758d7134f9712bc4

func New

func New(items [][]byte) Tree

Returns a complete Tree using items for the leaves. Intermediary nodes and items will be hashed using Keccak256.

func (Tree) Index added in v1.0.4

func (t Tree) Index(target []byte) int

Returns the index of the target leaf in the tree. If the target is not a leaf in the tree, returns -1.

func (Tree) LeafProofs added in v1.0.4

func (t Tree) LeafProofs() [][][]byte

Returns proofs for all leafs in the tree. For details on how an individual proof is calculated, see Tree.Proof.

func (Tree) Proof

func (t Tree) Proof(index int) [][]byte

Returns a list of hashes such that cumulatively hashing the list pairwise will yield the root hash of the tree. Example:

[abcde]
[abcd, e]
[ab, cd, e]
[a, b, c, d, e]

If the target is 'c' Proof returns: [d, ab, e]

If the target is 'e' Proof returns: [cd]

The result of this func will be used in Valid

func (Tree) Root

func (t Tree) Root() []byte

Jump to

Keyboard shortcuts

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