forked from celestiaorg/smt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtreehasher.go
118 lines (96 loc) · 2.83 KB
/
treehasher.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package smt
import (
"bytes"
"hash"
)
var (
leafPrefix = []byte{0}
innerPrefix = []byte{1}
extPrefix = []byte{2}
)
var _ PathHasher = (*pathHasher)(nil)
var _ ValueHasher = (*treeHasher)(nil)
type PathHasher interface {
Path([]byte) []byte
PathSize() int
}
type ValueHasher interface {
digest([]byte) []byte
}
type treeHasher struct {
hasher hash.Hash
zeroValue []byte
}
type pathHasher struct {
treeHasher
}
func newTreeHasher(hasher hash.Hash) *treeHasher {
th := treeHasher{hasher: hasher}
th.zeroValue = make([]byte, th.hashSize())
return &th
}
func (ph *pathHasher) Path(key []byte) []byte {
return ph.digest(key)[:ph.PathSize()]
}
func (ph *pathHasher) PathSize() int {
return ph.hasher.Size()
}
func (th *treeHasher) digest(data []byte) []byte {
th.hasher.Write(data)
sum := th.hasher.Sum(nil)
th.hasher.Reset()
return sum
}
func (th *treeHasher) digestLeaf(path []byte, leafData []byte) ([]byte, []byte) {
value := encodeLeaf(path, leafData)
return th.digest(value), value
}
func (th *treeHasher) digestNode(leftData []byte, rightData []byte) ([]byte, []byte) {
value := encodeInner(leftData, rightData)
return th.digest(value), value
}
func (th *treeHasher) parseNode(data []byte) ([]byte, []byte) {
return data[len(innerPrefix) : th.hashSize()+len(innerPrefix)], data[len(innerPrefix)+th.hashSize():]
}
func (th *treeHasher) hashSize() int {
return th.hasher.Size()
}
func (th *treeHasher) placeholder() []byte {
return th.zeroValue
}
func isLeaf(data []byte) bool {
return bytes.Equal(data[:len(leafPrefix)], leafPrefix)
}
func isExtension(data []byte) bool {
return bytes.Equal(data[:len(extPrefix)], extPrefix)
}
func parseLeaf(data []byte, ph PathHasher) ([]byte, []byte) {
return data[len(leafPrefix) : ph.PathSize()+len(leafPrefix)], data[len(leafPrefix)+ph.PathSize():]
}
func parseExtension(data []byte, ph PathHasher) (pathBounds, path, childData []byte) {
return data[len(extPrefix) : len(extPrefix)+2],
data[len(extPrefix)+2 : len(extPrefix)+2+ph.PathSize()],
data[len(extPrefix)+2+ph.PathSize():]
}
func encodeLeaf(path []byte, leafData []byte) []byte {
value := make([]byte, 0, len(leafPrefix)+len(path)+len(leafData))
value = append(value, leafPrefix...)
value = append(value, path...)
value = append(value, leafData...)
return value
}
func encodeInner(leftData []byte, rightData []byte) []byte {
value := make([]byte, 0, len(innerPrefix)+len(leftData)+len(rightData))
value = append(value, innerPrefix...)
value = append(value, leftData...)
value = append(value, rightData...)
return value
}
func encodeExtension(pathBounds [2]byte, path []byte, childData []byte) []byte {
value := make([]byte, 0, len(extPrefix)+len(path)+2+len(childData))
value = append(value, extPrefix...)
value = append(value, pathBounds[:]...)
value = append(value, path...)
value = append(value, childData...)
return value
}