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-11-22 12:24:35 +00:00
|
|
|
value = Sha256(append(branch[i][:], value[:]...))
|
2022-12-09 18:19:01 +00:00
|
|
|
} else {
|
2023-11-22 12:24:35 +00:00
|
|
|
value = Sha256(append(value[:], branch[i][:]...))
|
2022-12-09 18:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return value == root
|
|
|
|
}
|
2023-02-10 00:09:05 +00:00
|
|
|
|
|
|
|
func PreparateRootsForHashing(roots []libcommon.Hash) [][32]byte {
|
|
|
|
ret := make([][32]byte, len(roots))
|
|
|
|
for i := range roots {
|
|
|
|
copy(ret[i][:], roots[i][:])
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|