trie2

package
v0.15.13 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PathScheme dbScheme = iota + 1
	HashScheme
)

Variables

View Source
var (
	ErrCommitted  = errors.New("trie is committed")
	ErrEmptyRange = errors.New("empty range")
)

Functions

func NewEmptyNodeReader

func NewEmptyNodeReader() nodeReader

func RunOnTempTriePedersen

func RunOnTempTriePedersen(height uint8, do func(*Trie) error) error

func RunOnTempTriePoseidon

func RunOnTempTriePoseidon(height uint8, do func(*Trie) error) error

func VerifyProof

func VerifyProof(root, key *felt.Felt, proof *ProofNodeSet, hash crypto.HashFn) (felt.Felt, error)

VerifyProof verifies that a proof path is valid for a given key in a binary trie. It walks through the proof nodes, verifying each step matches the expected path to reach the key.

The proof is considered invalid if:

  • Any proof node is missing from the node set
  • Any node's computed hash doesn't match its expected hash
  • The path bits don't match the key bits
  • The proof ends before processing all key bits

func VerifyRangeProof

func VerifyRangeProof(rootHash, first *felt.Felt, keys, values []*felt.Felt, proof *ProofNodeSet) (bool, error)

Types

type MissingNodeError

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

func (*MissingNodeError) Error

func (e *MissingNodeError) Error() string

type Path

type Path = trieutils.Path

type ProofNodeSet

type ProofNodeSet = utils.OrderedSet[felt.Felt, trienode.Node]

func NewProofNodeSet

func NewProofNodeSet() *ProofNodeSet

type TestNodeDatabase

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

func NewTestNodeDatabase

func NewTestNodeDatabase(disk db.KeyValueStore, scheme dbScheme) TestNodeDatabase

func (*TestNodeDatabase) NodeReader

func (*TestNodeDatabase) Update

func (d *TestNodeDatabase) Update(root, parent *felt.Felt, nodes *trienode.MergeNodeSet) error

type Trie

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

func New

func New(
	id trieutils.TrieID,
	height uint8,
	hashFn crypto.HashFn,
	nodeDB database.NodeDatabase,
) (*Trie, error)

Creates a new trie, with the arguments: - id: the trie id - height: the height of the trie, 251 for contract and class trie - hashFn: the hash function to use - nodeDB: database interface, which provides methods to read and write trie nodes

func NewEmpty

func NewEmpty(height uint8, hashFn crypto.HashFn) *Trie

Creates an empty trie, only used for temporary trie construction

func NewEmptyPedersen

func NewEmptyPedersen() (*Trie, error)

func NewEmptyPoseidon

func NewEmptyPoseidon() (*Trie, error)

func NewFromRootHash added in v0.14.6

func NewFromRootHash(
	id trieutils.TrieID,
	height uint8,
	hashFn crypto.HashFn,
	nodeDB database.NodeDatabase,
	rootHash *felt.Felt,
) (*Trie, error)

Similar to New, creates a new trie, but additionally takes one more argument: - rootHash: the root hash of the trie (provided only for hash scheme of trieDB, needed to properly recreate the trie)

func (*Trie) Commit

func (t *Trie) Commit() (felt.Felt, *trienode.NodeSet)

Collapses the trie into a single hash node and flush the node changes to the database.

func (*Trie) Copy

func (t *Trie) Copy() *Trie

func (*Trie) Delete

func (t *Trie) Delete(key *felt.Felt) error

Removes the given key from the trie.

func (*Trie) FeltToPath

func (t *Trie) FeltToPath(f *felt.Felt) Path

Converts a Felt value into a Path representation suitable to use as a trie key with the specified height.

func (*Trie) Get

func (t *Trie) Get(key *felt.Felt) (felt.Felt, error)

Retrieves the value associated with the given key. Returns felt.Zero if the key doesn't exist. May update the trie's internal structure if nodes need to be resolved.

func (*Trie) GetRangeProof

func (t *Trie) GetRangeProof(leftKey, rightKey *felt.Felt, proofSet *ProofNodeSet) error

GetRangeProof generates a range proof for the given range of keys. The proof contains the proof nodes on the path from the root to the closest ancestor of the left and right keys.

func (*Trie) Hash

func (t *Trie) Hash() (felt.Felt, error)

Returns the root hash of the trie. Calling this method will also cache the hash of each node in the trie.

func (*Trie) HashFn

func (t *Trie) HashFn() crypto.HashFn

func (*Trie) Prove

func (t *Trie) Prove(key *felt.Felt, proof *ProofNodeSet) error

Prove generates a Merkle proof for a given key in the trie. The result contains the proof nodes on the path from the root to the leaf. The value is included in the proof if the key is present in the trie. If the key is not present, the proof will contain the nodes on the path to the closest ancestor.

func (*Trie) String

func (t *Trie) String() string

func (*Trie) Update

func (t *Trie) Update(key, value *felt.Felt) error

Modifies or inserts a key-value pair in the trie. If value is zero, the key is deleted from the trie.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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