2022-12-09 18:19:01 +00:00
|
|
|
package utils
|
|
|
|
|
2023-01-17 01:22:05 +00:00
|
|
|
import (
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
)
|
|
|
|
|
2022-12-09 18:19:01 +00:00
|
|
|
// Check if leaf at index verifies against the Merkle root and branch
|
2023-01-17 01:22:05 +00:00
|
|
|
func IsValidMerkleBranch(leaf libcommon.Hash, branch []libcommon.Hash, depth uint64, index uint64, root [32]byte) bool {
|
2022-12-09 18:19:01 +00:00
|
|
|
value := leaf
|
|
|
|
for i := uint64(0); i < depth; i++ {
|
|
|
|
if (index / PowerOf2(i) % 2) == 1 {
|
2023-01-17 01:22:05 +00:00
|
|
|
value = Keccak256(append(branch[i][:], value[:]...))
|
2022-12-09 18:19:01 +00:00
|
|
|
} else {
|
2023-01-17 01:22:05 +00:00
|
|
|
value = Keccak256(append(value[:], branch[i][:]...))
|
2022-12-09 18:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return value == root
|
|
|
|
}
|