merkletree

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = MerkleTreeOptions{
	SortLeaves: true,
}

DefaultOptions rappresenta la configurazione predefinita per un Merkle Tree

Functions

func Assert

func Assert(condition bool, message string)

Assert verifica una condizione e fornisce un log di errore senza crash immediato

func CheckLeafNode

func CheckLeafNode(tree []BytesLike, i int)

CheckLeafNode verifica se un indice è una foglia nell'albero di Merkle

func CheckValidMerkleNode

func CheckValidMerkleNode(node BytesLike)

func Compare

func Compare(a BytesLike, b BytesLike) (int, error)

Compare confronta due BytesLike e restituisce -1, 0, 1 (equivalente alla funzione compare in TypeScript)

func Concat

func Concat(values ...BytesLike) ([]byte, error)

Concat concatena più BytesLike in un unico array di byte (equivalente a concatBytes)

func Invariant

func Invariant(condition bool, message string)

Invariant verifica una condizione e causa un panic se la condizione è falsa

func InvariantWithDebug

func InvariantWithDebug(condition bool, message string, debugInfo interface{})

InvariantWithDebug permette di fornire messaggi più dettagliati per il debug

func IsInternalNode

func IsInternalNode(tree []BytesLike, i int) bool

IsInternalNode verifica se l'indice `i` è un nodo interno dell'albero di Merkle

func IsLeafNode

func IsLeafNode(tree []BytesLike, i int) bool

IsLeafNode verifica se un indice `i` è una foglia nell'albero di Merkle

func IsTreeNode

func IsTreeNode(tree []BytesLike, i int) bool

IsTreeNode verifica se l'indice `i` è un nodo valido nell'albero

func IsValidMerkleNode

func IsValidMerkleNode(node BytesLike) bool

func IsValidMerkleTree

func IsValidMerkleTree(tree []HexString, nodeHash NodeHash) bool

IsValidMerkleTree verifica se un Merkle Tree è valido

func LeftChildIndex

func LeftChildIndex(i int) int

func ParentIndex

func ParentIndex(i int) int

Funzioni di supporto per gli indici degli alberi di Merkle ParentIndex restituisce l'indice del nodo genitore per un nodo dato

func RightChildIndex

func RightChildIndex(i int) int

func SafePanic

func SafePanic(message string)

SafePanic permette di catturare gli errori e fornire messaggi dettagliati senza crash immediato

func SiblingIndex

func SiblingIndex(i int) int

SiblingIndex restituisce l'indice del nodo fratello per un nodo dato

func ToBytes

func ToBytes(value BytesLike) ([]byte, error)

ToBytes converte un BytesLike in un array di byte (equivalente a hexToBytes in TypeScript)

func ValidateArgument

func ValidateArgument(condition bool, message string)

ValidateArgument verifica una condizione e causa un panic se la condizione è falsa

func VerifySimpleMerkleTree

func VerifySimpleMerkleTree(root BytesLike, leaf BytesLike, proof []BytesLike, nodeHash NodeHash) bool

VerifySimpleMerkleTree verifica una proof di Merkle per un valore specifico

func VerifyStandardMerkleTree

func VerifyStandardMerkleTree[T any](root BytesLike, leaf T, proof []BytesLike) bool

Verify verifica una proof di Merkle per un valore specifico

Types

type BytesLike

type BytesLike interface{}

BytesLike rappresenta i tipi di dati compatibili con le operazioni di hashing

type HexString

type HexString string

HexString rappresenta una stringa esadecimale

func FormatLeaf

func FormatLeaf(value BytesLike) HexString

FormatLeaf converte un valore in un formato hashato per l'inserimento nel Merkle Tree

func GetProof

func GetProof(tree []BytesLike, index int) []HexString

GetProof restituisce la proof di Merkle per un nodo specifico

func MakeMerkleTree

func MakeMerkleTree(hashes []BytesLike, nodeHash NodeHash) []HexString

MakeMerkleTree costruisce un albero di Merkle a partire da una lista di hash delle foglie

func PrepareMerkleTree

func PrepareMerkleTree[T any](values []T, options MerkleTreeOptions, leafHash func(T) HexString, nodeHash NodeHash) ([]HexString, []struct {
	Value     T
	TreeIndex int
})

PrepareMerkleTree costruisce l'albero di Merkle e assegna gli indici corretti alle foglie

func ProcessMultiProof

func ProcessMultiProof(multiproof MultiProof, nodeHash NodeHash) HexString

ProcessMultiProof verifica una proof multipla e calcola la root risultante

func ProcessProof

func ProcessProof(leaf BytesLike, proof []BytesLike, nodeHash NodeHash) HexString

ProcessProof verifica la proof data e calcola la root risultante

func StandardLeafHash

func StandardLeafHash[T any](value T) HexString

StandardLeafHash calcola l'hash standard di una foglia, utilizzando l'encoding ABI come su Ethereum

func StandardNodeHash

func StandardNodeHash(a BytesLike, b BytesLike) HexString

StandardNodeHash calcola l'hash standard di due nodi

func ToHex

func ToHex(value BytesLike) (HexString, error)

ToHex converte un BytesLike in un HexString (equivalente a bytesToHex in TypeScript)

type LeafHash

type LeafHash[T any] func(leaf T) HexString

LeafHash rappresenta una funzione che calcola l'hash di una foglia

type MerkleTreeImpl

type MerkleTreeImpl[T any] struct {
	Tree   []HexString
	Values []struct {
		Value     T
		TreeIndex int
	}
	LeafHash   func(T) HexString
	NodeHash   NodeHash
	HashLookup map[HexString]int
}

MerkleTreeImpl è la struttura base del Merkle Tree

func (*MerkleTreeImpl[T]) GetProof

func (m *MerkleTreeImpl[T]) GetProof(leaf interface{}) []HexString

GetProof genera una proof per un valore specifico

func (*MerkleTreeImpl[T]) LeafHashFromInput

func (m *MerkleTreeImpl[T]) LeafHashFromInput(leaf interface{}) HexString

LeafHashFromInput calcola l'hash della foglia, assicurando coerenza con la costruzione

func (*MerkleTreeImpl[T]) Root

func (m *MerkleTreeImpl[T]) Root() HexString

Root restituisce la root dell'albero di Merkle

func (*MerkleTreeImpl[T]) Validate

func (m *MerkleTreeImpl[T]) Validate()

Validate verifica se l'albero è strutturalmente valido

func (*MerkleTreeImpl[T]) Verify

func (m *MerkleTreeImpl[T]) Verify(leaf interface{}, proof []HexString) bool

Verify verifica se una proof è valida

type MerkleTreeOptions

type MerkleTreeOptions struct {
	SortLeaves bool `json:"sortLeaves"` // Se true, le foglie vengono ordinate per facilitare le multiproof
}

MerkleTreeOptions definisce le opzioni di configurazione per la costruzione dell'albero di Merkle.

func NewMerkleTreeOptions

func NewMerkleTreeOptions(options *MerkleTreeOptions) MerkleTreeOptions

NewMerkleTreeOptions crea un oggetto `MerkleTreeOptions` con valori predefiniti se non specificati

type MultiProof

type MultiProof struct {
	Leaves     []HexString // Hash delle foglie incluse nella proof
	Proof      []HexString // Lista dei nodi necessari per il calcolo della root
	ProofFlags []bool      // Indica quali nodi devono essere combinati
}

func GetMultiProof

func GetMultiProof(tree []BytesLike, indices []int) MultiProof

GetMultiProof genera una proof multipla per un insieme di foglie

type NodeHash

type NodeHash func(left BytesLike, right BytesLike) HexString

NodeHash rappresenta una funzione che calcola l'hash di un nodo

type SimpleMerkleTree

type SimpleMerkleTree struct {
	MerkleTreeImpl[BytesLike]
}

SimpleMerkleTree rappresenta un Merkle Tree con hashing standard

func NewSimpleMerkleTree

func NewSimpleMerkleTree(values []BytesLike, options SimpleMerkleTreeOptions) *SimpleMerkleTree

NewSimpleMerkleTree crea un nuovo SimpleMerkleTree con i valori dati

func (*SimpleMerkleTree) Dump

Dump esporta i dati dell'albero per debugging o archiviazione

type SimpleMerkleTreeData

type SimpleMerkleTreeData struct {
	Format string
	Tree   []HexString
	Values []struct {
		Value     BytesLike
		TreeIndex int
	}
	Hash string
}

SimpleMerkleTreeData rappresenta i dati di un Simple Merkle Tree

type SimpleMerkleTreeOptions

type SimpleMerkleTreeOptions struct {
	MerkleTreeOptions // Includiamo le opzioni base del Merkle Tree
	NodeHash          NodeHash
}

SimpleMerkleTreeOptions rappresenta le opzioni per il Simple Merkle Tree

type StandardMerkleTree

type StandardMerkleTree[T any] struct {
	MerkleTreeImpl[T]
}

StandardMerkleTree rappresenta un Merkle Tree con encoding standard

func NewStandardMerkleTree

func NewStandardMerkleTree[T any](values []T, options MerkleTreeOptions) *StandardMerkleTree[T]

NewStandardMerkleTree crea un nuovo StandardMerkleTree con i valori dati

func (*StandardMerkleTree[T]) Dump

Dump esporta i dati dell'albero per debugging o archiviazione

type StandardMerkleTreeData

type StandardMerkleTreeData[T any] struct {
	Format string
	Tree   []HexString
	Values []struct {
		Value     T
		TreeIndex int
	}
}

StandardMerkleTreeData rappresenta i dati esportabili di un Standard Merkle Tree

Jump to

Keyboard shortcuts

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