Documentation ¶
Index ¶
- func ConcatGeneralizedIndices(indices []int) int
- func GeneralizedIndexBit(index uint64, pos uint64) bool
- func GeneralizedIndexChild(index int, rightSide bool) int
- func GeneralizedIndexLength(index int) int
- func GeneralizedIndexParent(index int) int
- func GeneralizedIndexSibling(index int) int
- func MerkleTree(leaves [][]byte) [][]byte
- func NextPowerOf2(n int) int
- func PrevPowerOf2(n int) int
- func VerifyMerkleProof(root []byte, item []byte, merkleIndex int, proof [][]byte) bool
- type SparseMerkleTrie
- func (m *SparseMerkleTrie) HashTreeRoot() [32]byte
- func (m *SparseMerkleTrie) Insert(item []byte, index int)
- func (m *SparseMerkleTrie) Items() [][]byte
- func (m *SparseMerkleTrie) MerkleProof(index int) ([][]byte, error)
- func (m *SparseMerkleTrie) Root() [32]byte
- func (m *SparseMerkleTrie) ToProto() *protodb.SparseMerkleTrie
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConcatGeneralizedIndices ¶
ConcatGeneralizedIndices concats the generalized indices together.
Spec pseudocode definition:
def concat_generalized_indices(*indices: GeneralizedIndex) -> GeneralizedIndex: """ Given generalized indices i1 for A -> B, i2 for B -> C .... i_n for Y -> Z, returns the generalized index for A -> Z. """ o = GeneralizedIndex(1) for i in indices: o = GeneralizedIndex(o * get_previous_power_of_two(i) + (i - get_previous_power_of_two(i))) return o
func GeneralizedIndexBit ¶
GeneralizedIndexBit returns the given bit of a generalized index.
Spec pseudocode definition:
def get_generalized_index_bit(index: GeneralizedIndex, position: int) -> bool: """ Return the given bit of a generalized index. """ return (index & (1 << position)) > 0
func GeneralizedIndexChild ¶
GeneralizedIndexChild returns the child of a generalized index.
Spec pseudocode definition:
def generalized_index_child(index: GeneralizedIndex, right_side: bool) -> GeneralizedIndex: return GeneralizedIndex(index * 2 + right_side)
func GeneralizedIndexLength ¶
GeneralizedIndexLength returns the generalized index length from a given index.
Spec pseudocode definition:
def get_generalized_index_length(index: GeneralizedIndex) -> int: """ Return the length of a path represented by a generalized index. """ return int(log2(index))
func GeneralizedIndexParent ¶
GeneralizedIndexParent returns the parent of a generalized index.
Spec pseudocode definition:
def generalized_index_parent(index: GeneralizedIndex) -> GeneralizedIndex: return GeneralizedIndex(index // 2)
func GeneralizedIndexSibling ¶
GeneralizedIndexSibling returns the sibling of a generalized index.
Spec pseudocode definition:
def generalized_index_sibling(index: GeneralizedIndex) -> GeneralizedIndex: return GeneralizedIndex(index ^ 1)
func MerkleTree ¶
MerkleTree returns all the nodes in a merkle tree from inputting merkle leaves.
Spec pseudocode definition:
def merkle_tree(leaves: Sequence[Hash]) -> Sequence[Hash]: padded_length = get_next_power_of_two(len(leaves)) o = [Hash()] * padded_length + list(leaves) + [Hash()] * (padded_length - len(leaves)) for i in range(padded_length - 1, 0, -1): o[i] = hash(o[i * 2] + o[i * 2 + 1]) return o
func NextPowerOf2 ¶
NextPowerOf2 returns the next power of 2 >= the input
Spec pseudocode definition:
def get_next_power_of_two(x: int) -> int: """ Get next power of 2 >= the input. """ if x <= 2: return x else: return 2 * get_next_power_of_two((x + 1) // 2)
func PrevPowerOf2 ¶
PrevPowerOf2 returns the previous power of 2 >= the input
Spec pseudocode definition:
def get_previous_power_of_two(x: int) -> int: """ Get the previous power of 2 >= the input. """ if x <= 2: return x else: return 2 * get_previous_power_of_two(x // 2)
Types ¶
type SparseMerkleTrie ¶ added in v0.3.0
type SparseMerkleTrie struct {
// contains filtered or unexported fields
}
SparseMerkleTrie implements a sparse, general purpose Merkle trie to be used across ETH2.0 Phase 0 functionality.
func CreateTrieFromProto ¶ added in v0.3.0
func CreateTrieFromProto(trieObj *protodb.SparseMerkleTrie) *SparseMerkleTrie
CreateTrieFromProto creates a Sparse Merkle Trie from its corresponding merkle trie.
func GenerateTrieFromItems ¶
func GenerateTrieFromItems(items [][]byte, depth int) (*SparseMerkleTrie, error)
GenerateTrieFromItems constructs a Merkle trie from a sequence of byte slices.
func NewTrie ¶
func NewTrie(depth int) (*SparseMerkleTrie, error)
NewTrie returns a new merkle trie filled with zerohashes to use.
func (*SparseMerkleTrie) HashTreeRoot ¶ added in v0.3.0
func (m *SparseMerkleTrie) HashTreeRoot() [32]byte
HashTreeRoot of the Merkle trie as defined in the deposit contract.
Spec Definition: sha256(concat(node, self.to_little_endian_64(self.deposit_count), slice(zero_bytes32, start=0, len=24)))
func (*SparseMerkleTrie) Insert ¶ added in v0.3.0
func (m *SparseMerkleTrie) Insert(item []byte, index int)
Insert an item into the trie.
func (*SparseMerkleTrie) Items ¶ added in v0.3.0
func (m *SparseMerkleTrie) Items() [][]byte
Items returns the original items passed in when creating the Merkle trie.
func (*SparseMerkleTrie) MerkleProof ¶ added in v0.3.0
func (m *SparseMerkleTrie) MerkleProof(index int) ([][]byte, error)
MerkleProof computes a proof from a trie's branches using a Merkle index.
func (*SparseMerkleTrie) Root ¶ added in v0.3.0
func (m *SparseMerkleTrie) Root() [32]byte
Root returns the top-most, Merkle root of the trie.
func (*SparseMerkleTrie) ToProto ¶ added in v0.3.0
func (m *SparseMerkleTrie) ToProto() *protodb.SparseMerkleTrie
ToProto converts the underlying trie into its corresponding proto object