Documentation
¶
Overview ¶
Package proof provides helper functions for merkle tree operations. # Merkle Trees and Proof Generation
We demonstrate the Merkle tree functionality in the context of the beacon state. There are two key concepts for understanding how fields are accessed in the tree:
**Field Index vs Generalized Index:**
Field Index: Position of a field in the BeaconState struct (0-based)
BeaconState struct { GenesisTime [0] GenesisValidatorsRoot [1] <-- Field index is 1 Slot [2] Fork [3] ... }
Generalized Index: Position in the binary Merkle tree
Root [1] / \ [2] [3] / \ / \ [4] [5] [6] [7] / \ / \ / \ / \ [8] [9] [10][11] [12][13] [14][15] <-- Leaf level | | | | | | | | GT GVR S F ... ... ... ... <-- Fields
For example, GenesisValidatorsRoot (field index 1):
depth = ceil(log2(num_fields)) // = 4 for this example general_index = 2^depth + field_index = 9
**Proof Generation:**
A Merkle proof consists of the sibling hashes needed to reconstruct the root:
Root [R]
/ \
[A] [B]
/ \ / \
[C] [D] [E] [F]
/ \ / \ / \ / \
[G] [H] [J] [K] [L] [M] [N] [O]
^
GenesisValidatorsRoot
To prove GenesisValidatorsRoot exists: 1. Start at H (GenesisValidatorsRoot) 2. Collect siblings: [G, D, B] 3. To verify:
- Hash(G,H) = C
- Hash(C,D) = A
- Hash(A,B) = R
Since the proof process can be applied to any leaf (A-O), we need to always use the generalized index for proof generation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FieldGeneralizedIndex ¶
FieldGeneralizedIndex obtains the generalized index of a field leaf using the field name.
func FieldIndex ¶
FieldIndex returns the index of a field in a struct. The index represents the field's position in the struct's memory layout.
func LeafGeneralizedIndex ¶
LeafGeneralizedIndex calculates the generalized index of a leaf in a binary Merkle tree. The generalized index is the absolute position of the leaf in the tree's array representation.
Types ¶
This section is empty.