-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle.go
410 lines (378 loc) · 12.6 KB
/
merkle.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2018, Christos Katsakioris
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// Package merkle implements a very simple, immutable, in-memory, generic,
// "hash function-agnostic" merkle tree.
package merkle
import (
"bytes"
"crypto"
"hash"
"sort"
)
// Datum is the interface that any piece of data has to implement so as to be
// able to be contained in the leaves of the merkle tree.
type Datum interface {
// Serialize must return a serialized representation of the Datum.
Serialize() []byte
}
type (
// ErrHashUnavailable signifies that the requested hash function has
// not been linked into the binary.
ErrHashUnavailable struct{}
// ErrNoData signifies that the piece of data requested is either nil
// or not present in the merkle tree.
ErrNoData struct{}
)
func (ErrHashUnavailable) Error() string {
return "Hash Algorithm Unavailable"
}
func (ErrNoData) Error() string {
return "Nonexistent Data"
}
type (
// Tree is the exported struct to interact with the merkle tree.
Tree struct {
hash crypto.Hash
mns [][][]byte
tls []treeLeaf
}
treeLeaf struct {
digest []byte
datum []byte
orderedID uint
}
)
// Height returns the height of the merkle tree, including both its leaves and
// the merkle nodes.
func (t *Tree) Height() int {
return len(t.mns) + 1
}
// Size returns the total number of nodes in the merkle tree, including both
// its leaves and the merkle nodes.
func (t *Tree) Size() int {
return t.MerkleSize() + t.NumLeaves()
}
// MerkleSize returns the number of merkle nodes in the merkle trees, i.e. the
// total number of nodes in the merkle tree, excluding its leaves.
func (t *Tree) MerkleSize() (merkleSize int) {
for i := range t.mns {
merkleSize += len(t.mns[i])
}
return
}
// NumLeaves returns the number of leaves in the merkle tree.
func (t *Tree) NumLeaves() (numLeaves int) {
return len(t.tls)
}
// MerkleRoot returns the hash digest of the root of the merkle tree.
func (t *Tree) MerkleRoot() []byte {
return t.mns[0][0]
}
// NewTree creates a new merkle tree given one of the available (i.e. linked
// into the binary) hash functions and a bunch of data.
//
// It returns a non-nil error either if the requested hash function has not
// been linked into the binary, or if data are not given at all.
func NewTree(hash crypto.Hash, data ...Datum) (*Tree, error) {
if !hash.Available() {
return nil, ErrHashUnavailable{}
}
h := hash.New()
if len(data) == 0 {
return nil, ErrNoData{}
}
// Create the leaves...
tls := appendTreeLeaves(h, nil, data)
// ...and construct the merkle nodes above them.
mns := constructMerkleNodes(h, tls)
return &Tree{
hash: hash,
mns: mns,
tls: tls,
}, nil
}
// AppendAndReconstruct appends the given data as new tree leaves, and
// reconstructs the merkle tree to take them into account as well.
//
// This obviously modifies the merkle root of the tree.
func (t *Tree) AppendAndReconstruct(data ...Datum) {
if len(data) == 0 {
return
}
h := t.hash.New()
// Append the new leaves...
t.tls = appendTreeLeaves(h, t.tls, data)
// ...and reconstruct the merkle nodes above them.
t.mns = constructMerkleNodes(h, t.tls)
}
// DeleteAndReconstruct deletes the given data from the tree leaves, and
// reconstructs the merkle tree on the new (reduced) number of leaves.
//
// This obviously modifies the merkle root of the tree.
func (t *Tree) DeleteAndReconstruct(data ...Datum) {
if len(data) == 0 {
return
}
// Delete the appropriate leaves...
t.tls = deleteTreeLeaves(t.tls, data)
// ...and reconstruct the merkle nodes above the remaining ones.
t.mns = constructMerkleNodes(t.hash.New(), t.tls)
}
// VerifyDigest verifies that the given (leaf) hash digest is present in the
// merkle tree, in which case it returns true and a nil error value.
//
// It requires O(L) search among the leaves and O(log2(L)) hash calculations.
//
// If the given hash digest cannot be verified, VerifyDigest returns false.
// If the given hash digest cannot be found in one of the merkle tree's leaves,
// VerifyDigest returns false and a non-nil error value.
func (t *Tree) VerifyDigest(digest []byte) (bool, error) {
for leafIndex := range t.tls {
if bytes.Compare(digest, t.tls[leafIndex].datum) == 0 {
return t.verify(leafIndex)
}
}
return false, ErrNoData{}
}
// VerifyOrderedID verifies that the Datum with the given ordered ID (based on
// the order that the leaves were initially given) is present in the merkle
// tree, in which case it returns true and a nil error value.
//
// It requires O(L) search among the leaves and O(log2(L)) hash calculations.
//
// If the given hash digest cannot be verified, VerifyOrderedID returns false.
// If the given hash digest cannot be found in one of the merkle tree's leaves,
// VerifyOrderedID returns false and a non-nil error value.
func (t *Tree) VerifyOrderedID(orderedID uint) (bool, error) {
for leafIndex := range t.tls {
if t.tls[leafIndex].orderedID == orderedID {
return t.verify(leafIndex)
}
}
return false, ErrNoData{}
}
// VerifySerializedDatum verifies that the given Datum (given in its serialized
// format) is present in the merkle tree, in which case it returns true and a
// nil error value.
//
// It requires O(log2(L)) search among the leaves and O(log2(L)) hash
// calculations.
//
// If the given hash digest cannot be verified, VerifySerializedDatum returns
// false.
// If the given hash digest cannot be found in one of the merkle tree's leaves,
// VerifySerializedDatum returns false and a non-nil error value.
func (t *Tree) VerifySerializedDatum(serializedDatum []byte) (bool, error) {
leafIndex := sort.Search(len(t.tls), func(i int) bool {
return bytes.Compare(t.tls[i].datum, serializedDatum) >= 0
})
if leafIndex < len(t.tls) && bytes.Compare(t.tls[leafIndex].datum, serializedDatum) == 0 {
return t.verify(leafIndex)
}
return false, ErrNoData{}
}
// VerifyDatum verifies that the given Datum is present in the merkle tree, in
// which case it returns true and a nil error value.
//
// It requires O(log2(L)) search among the leaves and O(log2(L)) hash
// calculations.
//
// If the given hash digest cannot be verified, VerifyDatum returns false.
// If the given hash digest cannot be found in one of the merkle tree's leaves,
// VerifyDatum returns false and a non-nil error value.
func (t *Tree) VerifyDatum(datum Datum) (bool, error) {
if datum == nil {
return false, ErrNoData{}
}
return t.VerifySerializedDatum(datum.Serialize())
}
func (t *Tree) verify(currentIndex int) (bool, error) {
h := t.hash.New()
h.Write(t.tls[currentIndex].datum)
currentDigest := h.Sum(nil)
var (
siblingDigest, parentDigest []byte
parentIndex int
first, second []byte
)
// Verify leaf.
if currentIndex%2 == 0 {
if currentIndex < len(t.tls)-1 {
siblingDigest = t.tls[currentIndex+1].digest
} else {
siblingDigest = []byte{}
}
parentIndex = currentIndex / 2
parentDigest = t.mns[len(t.mns)-1][parentIndex]
first, second = currentDigest, siblingDigest
} else {
siblingDigest = t.tls[currentIndex-1].digest
parentIndex = (currentIndex - 1) / 2
parentDigest = t.mns[len(t.mns)-1][parentIndex]
first, second = siblingDigest, currentDigest
}
h.Reset()
h.Write(first)
h.Write(second)
if bytes.Compare(parentDigest, h.Sum(nil)) != 0 {
return false, nil
}
// Verify merkle path.
for currentLevel := len(t.mns) - 1; currentLevel > 0; currentLevel-- {
currentIndex, currentDigest = parentIndex, parentDigest
if currentIndex%2 == 0 {
if currentIndex < len(t.mns[currentLevel])-1 {
siblingDigest = t.mns[currentLevel][currentIndex+1]
} else {
siblingDigest = []byte{}
}
parentIndex = currentIndex / 2
parentDigest = t.mns[currentLevel-1][parentIndex]
first, second = currentDigest, siblingDigest
} else {
siblingDigest = t.mns[currentLevel][currentIndex-1]
parentIndex = (currentIndex - 1) / 2
parentDigest = t.mns[currentLevel-1][parentIndex]
first, second = siblingDigest, currentDigest
}
h.Reset()
h.Write(first)
h.Write(second)
if bytes.Compare(parentDigest, h.Sum(nil)) != 0 {
return false, nil
}
}
return true, nil
}
// Leaves returns a slice of all pieces of Data stored in the merkle tree (in
// their serialized format) in the order that they were inserted by the user.
func (t *Tree) Leaves() [][]byte {
tls2 := make([]treeLeaf, len(t.tls))
copy(tls2, t.tls)
sort.Slice(tls2, func(i, j int) bool {
return tls2[i].orderedID < tls2[j].orderedID
})
ret := make([][]byte, len(tls2))
retSeq := make([]byte, 0)
currentIndex := 0
for i := range tls2 {
retSeq = append(retSeq, tls2[i].datum...)
ret[i] = retSeq[currentIndex : currentIndex+len(tls2[i].datum)]
currentIndex += len(tls2[i].datum)
}
return ret
}
func appendTreeLeaves(h hash.Hash, oldTreeLeaves []treeLeaf, newData []Datum) (newTreeLeaves []treeLeaf) {
newTreeLeaves = make([]treeLeaf, len(oldTreeLeaves), len(oldTreeLeaves)+len(newData))
copy(newTreeLeaves, oldTreeLeaves)
for i := range newData {
serializedDatum := newData[i].Serialize()
h.Reset()
h.Write(serializedDatum)
newTreeLeaves = append(newTreeLeaves, treeLeaf{
digest: h.Sum(nil),
datum: serializedDatum,
orderedID: uint(len(oldTreeLeaves) + i),
})
}
sort.Slice(newTreeLeaves, func(i, j int) bool {
return bytes.Compare(newTreeLeaves[i].datum, newTreeLeaves[j].datum) == -1
})
return
}
func deleteTreeLeaves(oldTreeLeaves []treeLeaf, delData []Datum) (newTreeLeaves []treeLeaf) {
// Serialize all data to be deleted.
delSerializedData := make([][]byte, 0, len(delData))
for i := range delData {
delSerializedData = append(delSerializedData, delData[i].Serialize())
}
// Create a copy of oldTreeLeaves to process it.
oldTls := make([]treeLeaf, len(oldTreeLeaves))
copy(oldTls, oldTreeLeaves)
// Find each of the serializedData to be deleted and remove them from the copy.
for i := range delSerializedData {
j := sort.Search(len(oldTls), func(k int) bool {
return bytes.Compare(oldTls[k].datum, delSerializedData[i]) >= 0
})
if j < len(oldTls) && bytes.Compare(oldTls[j].datum, delSerializedData[i]) == 0 {
oldTls = append(oldTls[:j], oldTls[j+1:]...)
}
}
// Sort oldTls by orderedID, and reset the orderedIDs.
sort.Slice(oldTls, func(i, j int) bool {
return oldTls[i].orderedID < oldTls[j].orderedID
})
for i := range oldTls {
oldTls[i].orderedID = uint(i)
}
// Copy oldTls to a new slice to avoid wasting capacity.
newTreeLeaves = make([]treeLeaf, len(oldTreeLeaves)-len(delData))
copy(newTreeLeaves, oldTls)
// Finally, sort newTreeLeaves by serializedDatum again.
sort.Slice(newTreeLeaves, func(i, j int) bool {
return bytes.Compare(newTreeLeaves[i].datum, newTreeLeaves[j].datum) == -1
})
return
}
// mns[0][0] --> ROOT
// mns[1][0] mns[1][1]
// mns[2][0] mns[2][1] mns[2][2] mns[2][3]
// mns[3][0] mns[3][1] mns[3][2] mns[3][3] mns[3][4] mns[3][5] mns[3][6] mns[3][7]
// . . .
func constructMerkleNodes(h hash.Hash, tls []treeLeaf) (mns [][][]byte) {
numMerkleNodes, rowSizes := calculateMerkleNumbers(len(tls))
mnsSeq := make([]byte, 0, h.Size()*numMerkleNodes)
mns = make([][][]byte, len(rowSizes))
mnCount := 0
for i := 0; i < len(rowSizes); i++ {
mns[i] = make([][]byte, rowSizes[len(rowSizes)-1-i])
for j := 0; j < rowSizes[len(rowSizes)-1-i]; j++ {
mns[i][j] = mnsSeq[mnCount*h.Size() : (mnCount+1)*h.Size()]
if i == len(rowSizes)-1 {
h.Reset()
h.Write(tls[2*j].digest)
if 2*j+1 < len(tls) {
h.Write(tls[2*j+1].digest)
}
digest := h.Sum(nil)
copy(mns[i][j], digest)
}
mnCount += 1
}
}
for i := len(rowSizes) - 2; i >= 0; i-- {
for j := 0; j < rowSizes[len(rowSizes)-1-i]; j++ {
h.Reset()
h.Write(mns[i+1][2*j])
if 2*j+1 < len(mns[i+1]) {
h.Write(mns[i+1][2*j+1])
}
digest := h.Sum(nil)
copy(mns[i][j], digest)
}
}
return
}
func calculateMerkleNumbers(numLeaves int) (numMerkleNodes int, mns []int) {
for numLeaves > 1 {
if numLeaves%2 == 1 {
numLeaves = (numLeaves / 2) + 1
} else {
numLeaves = numLeaves / 2
}
mns = append(mns, numLeaves)
numMerkleNodes += numLeaves
}
return
}