-
Notifications
You must be signed in to change notification settings - Fork 3
/
merkletree.go
305 lines (265 loc) · 6.29 KB
/
merkletree.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package merkletree
import (
"bytes"
"errors"
"golang.org/x/crypto/sha3"
"hash"
"sort"
)
// Content represents the data that is stored and verified by the tree. A type that
// implements this interface can be used as an item in the tree.
type Content interface {
CalculateHash() ([]byte, error)
Equals(other Content) (bool, error)
}
// MerkleTree is the container for the tree. It holds a pointer to the root of the tree,
// a list of pointers to the leaf nodes, and the merkle root.
type MerkleTree struct {
Root *Node
merkleRoot []byte
Leafs []*Node
hashStrategy func() hash.Hash
}
type Node struct {
Tree *MerkleTree
Parent *Node
Left *Node
Right *Node
leaf bool
single bool
Hash []byte
C Content
}
func (n *Node) verifyNode() ([]byte, error) {
if n.leaf {
return n.C.CalculateHash()
}
rightBytes, err := n.Right.verifyNode()
if err != nil {
return nil, err
}
leftBytes, err := n.Left.verifyNode()
if err != nil {
return nil, err
}
// if have only one child
if n.Left == n.Right && n.Left.single && n.Right.single {
return n.Hash, nil
}
h := n.Tree.hashStrategy()
if _, err := h.Write(combineTwoHash(leftBytes, rightBytes)); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
func (n *Node) calculateNodeHash() ([]byte, error) {
if n.leaf {
return n.C.CalculateHash()
}
// if n is single or n's child is single
if n.single || (n.Left == n.Right && n.Left.single && n.Right.single) {
return n.Hash, nil
}
h := n.Tree.hashStrategy()
if _, err := h.Write(combineTwoHash(n.Left.Hash, n.Right.Hash)); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
func NewTree(cs []Content) (*MerkleTree, error) {
// default hash is keccak256
return NewTreeWithHashStrategy(cs, sha3.NewLegacyKeccak256)
}
func NewTreeWithHashStrategy(cs []Content, hashStrategy func() hash.Hash) (*MerkleTree, error) {
t := &MerkleTree{
hashStrategy: hashStrategy,
}
root, leafs, err := buildWithContent(cs, t)
if err != nil {
return nil, err
}
t.Root = root
t.Leafs = leafs
t.merkleRoot = root.Hash
return t, nil
}
func (m *MerkleTree) GetMerklePath(content Content) ([][]byte, []int64, error) {
for _, current := range m.Leafs {
ok, err := current.C.Equals(content)
if err != nil {
return nil, nil, err
}
if ok {
currentParent := current.Parent
var merklePath [][]byte
var index []int64
for currentParent != nil {
if !current.single {
if bytes.Equal(currentParent.Left.Hash, current.Hash) {
merklePath = append(merklePath, currentParent.Right.Hash)
index = append(index, 1) // right leaf
} else {
merklePath = append(merklePath, currentParent.Left.Hash)
index = append(index, 0) // left leaf
}
}
current = currentParent
currentParent = currentParent.Parent
}
return merklePath, index, nil
}
}
return nil, nil, nil
}
func buildWithContent(cs []Content, t *MerkleTree) (*Node, []*Node, error) {
if len(cs) == 0 {
return nil, nil, errors.New("error: cannot construct tree with no content")
}
var leafs []*Node
for _, c := range cs {
hashBz, err := c.CalculateHash()
if err != nil {
return nil, nil, err
}
leafs = append(leafs, &Node{
Hash: hashBz,
C: c,
leaf: true,
Tree: t,
})
}
leafs = sortLeafs(leafs)
root, err := buildIntermediate(leafs, t)
if err != nil {
return nil, nil, err
}
return root, leafs, nil
}
func sortLeafs(leafs []*Node) []*Node {
sort.Slice(leafs, func(i, j int) bool {
return bytes.Compare(leafs[i].Hash, leafs[j].Hash) < 0
})
return leafs
}
func buildIntermediate(nl []*Node, t *MerkleTree) (*Node, error) {
var nodes []*Node
for i := 0; i < len(nl); i += 2 {
h := t.hashStrategy()
var left, right = i, i + 1
if i+1 == len(nl) {
right = i
}
var nextHash []byte
if left != right {
// appear in pairs
// compare their child hashes when doing combine
if _, err := h.Write(combineTwoHash(nl[left].Hash, nl[right].Hash)); err != nil {
return nil, err
}
nextHash = h.Sum(nil)
} else {
// single node
// don't compute new hash
nextHash = nl[right].Hash
nl[right].single = true
}
n := &Node{
Left: nl[left],
Right: nl[right],
Hash: nextHash,
Tree: t,
}
nodes = append(nodes, n)
nl[left].Parent = n
nl[right].Parent = n
if len(nl) == 2 || len(nl) == 1 {
// n is root
return n, nil
}
}
return buildIntermediate(nodes, t)
}
func (m *MerkleTree) MerkleRoot() []byte {
return m.merkleRoot
}
func (m *MerkleTree) RebuildTree() error {
var cs []Content
for _, c := range m.Leafs {
cs = append(cs, c.C)
}
root, leafs, err := buildWithContent(cs, m)
if err != nil {
return err
}
m.Root = root
m.Leafs = leafs
m.merkleRoot = root.Hash
return nil
}
func (m *MerkleTree) RebuildTreeWith(cs []Content) error {
root, leafs, err := buildWithContent(cs, m)
if err != nil {
return err
}
m.Root = root
m.Leafs = leafs
m.merkleRoot = root.Hash
return nil
}
func (m *MerkleTree) VerifyContent(content Content) (bool, error) {
for _, current := range m.Leafs {
ok, err := current.C.Equals(content)
if err != nil {
return false, err
}
if ok {
currentParent := current.Parent
for currentParent != nil {
if !current.single {
h := m.hashStrategy()
rightHash, err := currentParent.Right.calculateNodeHash()
if err != nil {
return false, err
}
leftHash, err := currentParent.Left.calculateNodeHash()
if err != nil {
return false, err
}
if _, err := h.Write(combineTwoHash(leftHash, rightHash)); err != nil {
return false, err
}
calHash := h.Sum(nil)
if bytes.Compare(calHash, currentParent.Hash) != 0 {
return false, nil
}
}
current = currentParent
currentParent = currentParent.Parent
}
return true, nil
}
}
return false, nil
}
func (m *MerkleTree) VerifyTree() (bool, error) {
calculatedMerkleRoot, err := m.Root.verifyNode()
if err != nil {
return false, err
}
if bytes.Compare(m.merkleRoot, calculatedMerkleRoot) == 0 {
return true, nil
}
return false, nil
}
// ----------------------------------------------------------------------------
func combineTwoHash(a, b []byte) []byte {
bf := bytes.NewBuffer(nil)
if bytes.Compare(a, b) < 0 {
bf.Write(a)
bf.Write(b)
return bf.Bytes()
}
bf.Write(b)
bf.Write(a)
return bf.Bytes()
}