mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
Merkle tree math helpers (#3571)
This commit is contained in:
parent
0e329fc115
commit
e64287773c
@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"merkle_trie.go",
|
||||
"sparse_merkle.go",
|
||||
"zerohashes.go",
|
||||
],
|
||||
@ -17,7 +18,10 @@ go_library(
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
size = "small",
|
||||
srcs = ["sparse_merkle_test.go"],
|
||||
srcs = [
|
||||
"merkle_trie_test.go",
|
||||
"sparse_merkle_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//contracts/deposit-contract:go_default_library",
|
||||
|
37
shared/trieutil/merkle_trie.go
Normal file
37
shared/trieutil/merkle_trie.go
Normal file
@ -0,0 +1,37 @@
|
||||
package trieutil
|
||||
|
||||
// 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 NextPowerOf2(n int) int {
|
||||
if n <= 2 {
|
||||
return n
|
||||
}
|
||||
return 2 * NextPowerOf2((n+1)/2)
|
||||
}
|
||||
|
||||
// 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)
|
||||
func PrevPowerOf2(n int) int {
|
||||
if n <= 2 {
|
||||
return n
|
||||
}
|
||||
return 2 * PrevPowerOf2(n/2)
|
||||
}
|
45
shared/trieutil/merkle_trie_test.go
Normal file
45
shared/trieutil/merkle_trie_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
package trieutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNextPowerOf2(t *testing.T) {
|
||||
tests := []struct {
|
||||
input int
|
||||
want int
|
||||
}{
|
||||
{input: 0, want: 0},
|
||||
{input: 1, want: 1},
|
||||
{input: 2, want: 2},
|
||||
{input: 3, want: 4},
|
||||
{input: 5, want: 8},
|
||||
{input: 9, want: 16},
|
||||
{input: 20, want: 32},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := NextPowerOf2(tt.input); got != tt.want {
|
||||
t.Errorf("NextPowerOf2() = %v, want %v", got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrevPowerOf2(t *testing.T) {
|
||||
tests := []struct {
|
||||
input int
|
||||
want int
|
||||
}{
|
||||
{input: 0, want: 0},
|
||||
{input: 1, want: 1},
|
||||
{input: 2, want: 2},
|
||||
{input: 3, want: 2},
|
||||
{input: 5, want: 4},
|
||||
{input: 9, want: 8},
|
||||
{input: 20, want: 16},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := PrevPowerOf2(tt.input); got != tt.want {
|
||||
t.Errorf("PrevPowerOf2() = %v, want %v", got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user