-
Notifications
You must be signed in to change notification settings - Fork 7
/
merkletree.go
217 lines (172 loc) · 4.52 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
package merkletree
import (
"bytes"
"errors"
"math"
)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////
type Tree struct {
root Node
rows [][]Node
checksumFunc func(isLeaf bool, block []byte) []byte
}
type Node interface {
GetChecksum() []byte
ToString(checksumToStrFunc, int) string
}
type Branch struct {
checksum []byte
left Node
right Node
}
type Leaf struct {
checksum []byte
block []byte
}
type ProofPart struct {
isRight bool
checksum []byte
}
type Proof struct {
checksumFunc func(isLeaf bool, xs []byte) []byte
parts []*ProofPart
target []byte // checksum of some block
}
type checksumToStrFunc func([]byte) string
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
///////////////
func NewLeaf(sumFunc func(bool, []byte) []byte, block []byte) *Leaf {
return &Leaf{
checksum: sumFunc(true, block),
block: block,
}
}
func NewBranch(sumFunc func(bool, []byte) []byte, left Node, right Node) *Branch {
return &Branch{
checksum: sumFunc(false, append(left.GetChecksum(), right.GetChecksum()...)),
left: left,
right: right,
}
}
func NewTree(providedSumFunc func([]byte) []byte, blocks [][]byte) *Tree {
levels := int(math.Ceil(math.Log2(float64(len(blocks)+len(blocks)%2))) + 1)
sumFunc := func(isLeaf bool, xs []byte) []byte {
if isLeaf {
return providedSumFunc(append([]byte{0x00}, xs...))
}
return providedSumFunc(append([]byte{0x01}, xs...))
}
// represents each row in the tree, where rows[0] is the base and rows[len(rows)-1] is the root
rows := make([][]Node, levels)
// build our base of leaves
for i := 0; i < len(blocks); i++ {
rows[0] = append(rows[0], NewLeaf(sumFunc, blocks[i]))
}
// build upwards until we hit the root
for i := 1; i < levels; i++ {
prev := rows[i-1]
// each iteration creates a branch from a pair of values originating from the previous level
for j := 0; j < len(prev); j = j + 2 {
var l, r Node
// if we don't have enough to make a pair, duplicate the left
if j+1 >= len(prev) {
l = prev[j]
r = l
} else {
l = prev[j]
r = prev[j+1]
}
b := NewBranch(sumFunc, l, r)
rows[i] = append(rows[i], b)
}
}
return &Tree{
checksumFunc: sumFunc,
rows: rows,
root: rows[len(rows)-1][0],
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// METHODS
//////////
func (b *Branch) GetChecksum() []byte {
return b.checksum
}
func (l *Leaf) GetChecksum() []byte {
return l.checksum
}
func (t *Tree) VerifyProof(p *Proof) bool {
index := t.getLeafIdxByChecksum(p.target)
if index == -1 {
return false
}
z := p.target
for i := 0; i < len(t.rows)-1; i++ {
if p.parts[i].isRight {
z = t.checksumFunc(false, append(z, p.parts[i].checksum...))
} else {
z = t.checksumFunc(false, append(p.parts[i].checksum, z...))
}
index = int(float64(index / 2))
}
return bytes.Equal(t.root.GetChecksum(), z)
}
func (t *Tree) getLeafIdxByChecksum(checksum []byte) int {
index := -1
for i := 0; i < len(t.rows[0]); i++ {
if bytes.Equal(checksum, t.rows[0][i].GetChecksum()) {
return i
}
}
return index
}
func (t *Tree) CreateProof(leafChecksum []byte) (*Proof, error) {
var parts []*ProofPart
index := t.getLeafIdxByChecksum(leafChecksum)
if index == -1 {
return nil, errors.New("target not found in receiver")
}
for i := 0; i < len(t.rows)-1; i++ {
if index%2 == 1 {
// is right, so go back one to get left
parts = append(parts, &ProofPart{
isRight: false,
checksum: t.rows[i][index-1].GetChecksum(),
})
} else {
var checksum []byte
if (index + 1) < len(t.rows[i]) {
checksum = t.rows[i][index+1].GetChecksum()
} else {
checksum = t.rows[i][index].GetChecksum()
}
// is left, so go one forward to get hash pair
parts = append(parts, &ProofPart{
isRight: true,
checksum: checksum,
})
}
index = int(float64(index / 2))
}
return &Proof{
checksumFunc: t.checksumFunc,
parts: parts,
target: leafChecksum,
}, nil
}
func (p *Proof) Equals(o *Proof) bool {
if !bytes.Equal(p.target, o.target) {
return false
}
if len(p.parts) != len(o.parts) {
return false
}
ok := true
for i := 0; i < len(p.parts); i++ {
ok = ok && p.parts[i].isRight && o.parts[i].isRight && bytes.Equal(p.parts[i].checksum, o.parts[i].checksum)
}
return ok
}