forked from celestiaorg/smt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
51 lines (46 loc) · 1.21 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package smt
// getPathBit gets the bit at an offset from the most significant bit
func getPathBit(data []byte, position int) int {
if int(data[position/8])&(1<<(8-1-uint(position)%8)) > 0 {
return 1
}
return 0
}
// setPathBit sets the bit at an offset from the most significant bit
func setPathBit(data []byte, position int) {
n := int(data[position/8])
n |= 1 << (8 - 1 - uint(position)%8)
data[position/8] = byte(n)
}
func countSetBits(data []byte) int {
count := 0
for i := 0; i < len(data)*8; i++ {
if getPathBit(data, i) == 1 {
count++
}
}
return count
}
// counts common bits in each path, starting from some position
func countCommonPrefix(data1, data2 []byte, from int) int {
count := 0
for i := from; i < len(data1)*8; i++ {
if getPathBit(data1, i) == getPathBit(data2, i) {
count++
} else {
break
}
}
return count + from
}
// Used for verification of serialized proof data
func hashSerialization(smt *BaseSMT, data []byte) []byte {
if isExtension(data) {
pathBounds, path, childHash := parseExtension(data, smt.ph)
ext := extensionNode{path: path, child: &lazyNode{childHash}}
copy(ext.pathBounds[:], pathBounds)
return smt.hashNode(&ext)
} else {
return smt.th.digest(data)
}
}