-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkletree.go
204 lines (161 loc) · 3.9 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
package merkletree
import (
"bytes"
"errors"
"fmt"
"hash"
"sort"
)
type merkleTree struct {
leaves []*node
rootNode *node
opts *Options
}
func New(leaves [][]byte, opts *Options) (*merkleTree, error) {
if len(leaves) == 0 {
return nil, errors.New("no leaves to build the tree")
}
node, leafNodes, err := constructTree(leaves, opts)
if err != nil {
return nil, err
}
tree := &merkleTree{
leaves: leafNodes,
rootNode: node,
opts: opts,
}
return tree, err
}
func constructTree(leaves [][]byte, opts *Options) (root *node, leafNodes []*node, err error) {
for _, leaf := range leaves {
leafNodes = append(leafNodes, &node{Hash: leaf})
}
if opts.SortLeaves {
sort.Slice(leafNodes, func(i, j int) bool {
return bytes.Compare(leafNodes[i].Hash, leafNodes[j].Hash) < 0
})
}
nodesInLayer := append([]*node(nil), leafNodes...)
for len(nodesInLayer) > 1 {
layerSize := len(nodesInLayer)
newLayerSize := 0
for i := 0; i < layerSize; i += 2 {
var newNode *node
if i+1 == layerSize {
newNode = nodesInLayer[i]
} else {
newNode = &node{
Left: nodesInLayer[i],
Right: nodesInLayer[i+1],
}
if opts.SortPairs && bytes.Compare(newNode.Left.Hash, newNode.Right.Hash) > 0 {
newNode.Hash, err = calculateHashOfTwoBytes(
opts.HashObj,
newNode.Right.Hash,
newNode.Left.Hash)
} else {
newNode.Hash, err = calculateHashOfTwoBytes(
opts.HashObj,
newNode.Left.Hash,
newNode.Right.Hash)
}
if err != nil {
return nil, nil, err
}
newNode.Left.Parent = newNode
newNode.Right.Parent = newNode
}
nodesInLayer[newLayerSize] = newNode
newLayerSize++
}
nodesInLayer = nodesInLayer[:newLayerSize]
}
return nodesInLayer[0], leafNodes, nil
}
func (m *merkleTree) GetLeaves() [][]byte {
// Copy the leaves recursively
result := make([][]byte, 0, len(m.leaves))
for _, leaf := range m.leaves {
result = append(result, append([]byte(nil), leaf.Hash...))
}
return result
}
func (m *merkleTree) Root() []byte {
return append([]byte(nil), m.rootNode.Hash...)
}
func (m *merkleTree) HexRoot() string {
return hexEncode(m.rootNode.Hash)
}
func (m *merkleTree) GetProof(leaf []byte) [][]byte {
index := -1
var leafNode *node
for i, leafNode_ := range m.leaves {
if bytes.Equal(leaf, leafNode_.Hash) {
index = i
leafNode = leafNode_
break
}
}
if index == -1 {
return nil
}
var proof [][]byte
for leafNode.Parent != nil {
parent := leafNode.Parent
if leafNode == parent.Left {
proof = append(proof, parent.Right.Hash)
} else {
proof = append(proof, parent.Left.Hash)
}
leafNode = parent
}
return proof
}
func (m *merkleTree) GetHexProof(leaf []byte) []string {
data := m.GetProof(leaf)
if data == nil {
return nil
}
result := make([]string, len(data))
for i, h := range data {
result[i] = hexEncode(h)
}
return result
}
func (m *merkleTree) String() string {
return fmt.Sprintf("merkleTree(%s)", m.Root())
}
func (m *merkleTree) Inspect(indent string) string {
return inspect(m.rootNode, indent, "")
}
func inspect(root *node, indent string, inspection string) string {
inspection += fmt.Sprintf("%s%s\n", indent, hexEncode(root.Hash))
if root.Left != nil {
inspection = inspect(root.Left, indent+" ", inspection)
}
if root.Right != nil {
inspection = inspect(root.Right, indent+" ", inspection)
}
return inspection
}
func VerifySorted(root []byte, leaf []byte, proof [][]byte, hashObj hash.Hash) bool {
if root == nil || leaf == nil || proof == nil || hashObj == nil {
return false
}
var err error
calculatedRoot := leaf
for _, hash := range proof {
left := calculatedRoot
right := hash
if bytes.Compare(left, right) > 0 {
tmp := left
left = right
right = tmp
}
calculatedRoot, err = calculateHashOfTwoBytes(hashObj, left, right)
if err != nil {
return false
}
}
return bytes.Equal(calculatedRoot, root)
}