forked from spacemeshos/merkle-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle_test.go
442 lines (384 loc) · 12.5 KB
/
merkle_test.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package merkle
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/stretchr/testify/require"
"io"
"testing"
"time"
)
/*
8-leaf tree (1st 2 bytes of each node):
+--------------------------------------------------+
| 89a0 |
| ba94 633b |
| cb59 0094 bd50 fa67 |
| 0000 0100 0200 0300 0400 0500 0600 0700 |
+--------------------------------------------------+
*/
func TestNewTree(t *testing.T) {
r := require.New(t)
tree := NewTree(GetSha256Parent)
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("89a0f1577268cc19b0a39c7a69f804fd140640c699585eb635ebb03c06154cce")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func concatLeaves(lChild, rChild []byte) []byte {
if len(lChild) == NodeSize {
lChild = lChild[:1]
}
if len(rChild) == NodeSize {
rChild = rChild[:1]
}
return append(lChild, rChild...)
}
func TestNewTreeWithMinHeightEqual(t *testing.T) {
r := require.New(t)
tree := NewTreeBuilder(concatLeaves).WithMinHeight(3).Build()
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("0001020304050607")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func TestNewTreeWithMinHeightGreater(t *testing.T) {
r := require.New(t)
tree := NewTreeBuilder(concatLeaves).WithMinHeight(4).Build()
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
// An 8-leaf tree is 3 layers high, so setting a minHeight of 4 means we need to add a "padding node" to the root.
expectedRoot, _ := NewNodeFromHex("000102030405060700")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func TestNewTreeWithMinHeightGreater2(t *testing.T) {
r := require.New(t)
tree := NewTreeBuilder(concatLeaves).WithMinHeight(5).Build()
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
// An 8-leaf tree is 3 layers high, so setting a minHeight of 5 means we need to add two "padding nodes" to the root.
expectedRoot, _ := NewNodeFromHex("00010203040506070000")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func TestNewTreeUnbalanced(t *testing.T) {
r := require.New(t)
tree := NewTree(GetSha256Parent)
for i := uint64(0); i < 9; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("cb71c80ee780788eedb819ec125a41e0cde57bd0955cdd3157ca363193ab5ff1")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func TestNewTreeUnbalanced2(t *testing.T) {
r := require.New(t)
tree := NewTree(GetSha256Parent)
for i := uint64(0); i < 10; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("59f32a43534fe4c4c0966421aef624267cdf65bd11f74998c60f27c7caccb12d")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func TestNewTreeUnbalanced3(t *testing.T) {
r := require.New(t)
tree := NewTree(GetSha256Parent)
for i := uint64(0); i < 15; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("b9746fb884ed07041c5cbb3bb5526e1383928e832a8385e08db995966889b5a8")
root := tree.Root()
r.Equal(expectedRoot, root)
}
func TestNewTreeUnbalancedProof(t *testing.T) {
r := require.New(t)
leavesToProve := []uint64{0, 4, 7}
sliceWriters := make(map[uint]*sliceReadWriter)
for i := uint(0); i < 5; i++ {
sliceWriters[i] = &sliceReadWriter{}
}
tree := NewTreeBuilder(GetSha256Parent).
WithLeavesToProve(leavesToProve).
WithCache(WritersFromSliceReadWriters(sliceWriters)).
Build()
for i := uint64(0); i < 10; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("59f32a43534fe4c4c0966421aef624267cdf65bd11f74998c60f27c7caccb12d")
root := tree.Root()
r.Equal(expectedRoot, root)
r.Len(sliceWriters[0].slice, 10)
r.Len(sliceWriters[1].slice, 5)
r.Len(sliceWriters[2].slice, 2)
r.Len(sliceWriters[3].slice, 1)
r.NotEqual(sliceWriters[3].slice[0], expectedRoot)
expectedProof := make([][]byte, 5)
expectedProof[0], _ = NewNodeFromHex("0100000000000000000000000000000000000000000000000000000000000000")
expectedProof[1], _ = NewNodeFromHex("0094579cfc7b716038d416a311465309bea202baa922b224a7b08f01599642fb")
expectedProof[2], _ = NewNodeFromHex("0500000000000000000000000000000000000000000000000000000000000000")
expectedProof[3], _ = NewNodeFromHex("0600000000000000000000000000000000000000000000000000000000000000")
expectedProof[4], _ = NewNodeFromHex("bc68417a8495de6e22d95b980fca5a1183f29eff0e2a9b7ddde91ed5bcbea952")
var proof nodes
proof = tree.Proof()
r.EqualValues(expectedProof, proof)
}
func BenchmarkNewTree(b *testing.B) {
var size uint64 = 1 << 28
tree := NewTree(GetSha256Parent)
for i := uint64(0); i < size; i++ {
_ = tree.AddLeaf(NewNodeFromUint64(i))
}
/*
goos: darwin
goarch: amd64
pkg: github.com/spacemeshos/merkle-tree
BenchmarkNewTree-8 1 125055682774 ns/op
PASS
*/
}
func BenchmarkNewTreeSmall(b *testing.B) {
var size uint64 = 1 << 23
start := time.Now()
tree := NewTree(GetSha256Parent)
for i := uint64(0); i < size; i++ {
_ = tree.AddLeaf(NewNodeFromUint64(i))
}
b.Log(time.Since(start))
/*
merkle_test.go:72: 3.700763631s
*/
}
func BenchmarkNewTreeNoHashing(b *testing.B) {
var size uint64 = 1 << 28
tree := NewTree(func(leftChild, rightChild []byte) []byte {
arr := [32]byte{}
return arr[:]
})
for i := uint64(0); i < size; i++ {
_ = tree.AddLeaf(NewNodeFromUint64(i))
}
/*
goos: darwin
goarch: amd64
pkg: github.com/spacemeshos/merkle-tree
BenchmarkNewTreeNoHashing-8 1 14668889972 ns/op
BenchmarkNewTreeNoHashing-8 1 15791579912 ns/op
PASS
*/
}
/*
28 layer tree takes 125 seconds to construct. Overhead (no hashing) is 15.5 seconds. Net: 109.5 seconds.
(8.5GB @ 32b leaves) => x30 256GB => 55 minutes for hashing, 8 minutes overhead.
Reading 256GB from a magnetic disk should take ~30 minutes.
*/
// Proving tree tests
func TestNewProvingTree(t *testing.T) {
r := require.New(t)
tree := NewProvingTree(GetSha256Parent, []uint64{4})
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("89a0f1577268cc19b0a39c7a69f804fd140640c699585eb635ebb03c06154cce")
root := tree.Root()
r.Equal(expectedRoot, root)
expectedProof := make([][]byte, 3)
expectedProof[0], _ = NewNodeFromHex("0500000000000000000000000000000000000000000000000000000000000000")
expectedProof[1], _ = NewNodeFromHex("fa670379e5c2212ed93ff09769622f81f98a91e1ec8fb114d607dd25220b9088")
expectedProof[2], _ = NewNodeFromHex("ba94ffe7edabf26ef12736f8eb5ce74d15bedb6af61444ae2906e926b1a95084")
proof := tree.Proof()
r.EqualValues(expectedProof, proof)
/***************************************************
| 89a0 |
| .ba94. 633b |
| cb59 0094 bd50 .fa67. |
| 0000 0100 0200 0300 =0400=.0500. 0600 0700 |
***************************************************/
}
func TestNewProvingTreeMultiProof(t *testing.T) {
r := require.New(t)
tree := NewProvingTree(GetSha256Parent, []uint64{1, 4})
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("89a0f1577268cc19b0a39c7a69f804fd140640c699585eb635ebb03c06154cce")
root := tree.Root()
r.Equal(expectedRoot, root)
expectedProof := make([][]byte, 4)
expectedProof[0], _ = NewNodeFromHex("0000000000000000000000000000000000000000000000000000000000000000")
expectedProof[1], _ = NewNodeFromHex("0094579cfc7b716038d416a311465309bea202baa922b224a7b08f01599642fb")
expectedProof[2], _ = NewNodeFromHex("0500000000000000000000000000000000000000000000000000000000000000")
expectedProof[3], _ = NewNodeFromHex("fa670379e5c2212ed93ff09769622f81f98a91e1ec8fb114d607dd25220b9088")
proof := tree.Proof()
r.EqualValues(expectedProof, proof)
/***************************************************
| 89a0 |
| ba94 633b |
| cb59 .0094. bd50 .fa67. |
| .0000.=0100= 0200 0300 =0400=.0500. 0600 0700 |
***************************************************/
}
func TestNewProvingTreeMultiProof2(t *testing.T) {
r := require.New(t)
tree := NewProvingTree(GetSha256Parent, []uint64{0, 1, 4})
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("89a0f1577268cc19b0a39c7a69f804fd140640c699585eb635ebb03c06154cce")
root := tree.Root()
r.Equal(expectedRoot, root)
expectedProof := make([][]byte, 3)
expectedProof[0], _ = NewNodeFromHex("0094579cfc7b716038d416a311465309bea202baa922b224a7b08f01599642fb")
expectedProof[1], _ = NewNodeFromHex("0500000000000000000000000000000000000000000000000000000000000000")
expectedProof[2], _ = NewNodeFromHex("fa670379e5c2212ed93ff09769622f81f98a91e1ec8fb114d607dd25220b9088")
proof := tree.Proof()
r.EqualValues(expectedProof, proof)
/***************************************************
| 89a0 |
| ba94 633b |
| cb59 .0094. bd50 .fa67. |
| =0000==0100= 0200 0300 =0400=.0500. 0600 0700 |
***************************************************/
}
func NewNodeFromUint64(i uint64) []byte {
b := make([]byte, NodeSize)
binary.LittleEndian.PutUint64(b, i)
return b
}
func NewNodeFromHex(s string) ([]byte, error) {
return hex.DecodeString(s)
}
// Caching tests:
type sliceReadWriter struct {
slice [][]byte
position uint64
}
func (s *sliceReadWriter) Width() uint64 {
return uint64(len(s.slice))
}
func (s *sliceReadWriter) Seek(index uint64) error {
if index >= uint64(len(s.slice)) {
return io.EOF
}
s.position = index
return nil
}
func (s *sliceReadWriter) ReadNext() ([]byte, error) {
if s.position >= uint64(len(s.slice)) {
return nil, io.EOF
}
value := make([]byte, NodeSize)
copy(value, s.slice[s.position])
s.position++
return value, nil
}
func (s *sliceReadWriter) Write(p []byte) (n int, err error) {
s.slice = append(s.slice, p)
return len(p), nil
}
func TestNewCachingTree(t *testing.T) {
r := require.New(t)
sliceWriters := make(map[uint]*sliceReadWriter)
for i := uint(0); i < 4; i++ {
sliceWriters[i] = &sliceReadWriter{}
}
tree := NewCachingTree(GetSha256Parent, WritersFromSliceReadWriters(sliceWriters))
for i := uint64(0); i < 8; i++ {
err := tree.AddLeaf(NewNodeFromUint64(i))
r.NoError(err)
}
expectedRoot, _ := NewNodeFromHex("89a0f1577268cc19b0a39c7a69f804fd140640c699585eb635ebb03c06154cce")
root := tree.Root()
r.Equal(expectedRoot, root)
r.Len(sliceWriters[0].slice, 8)
r.Len(sliceWriters[1].slice, 4)
r.Len(sliceWriters[2].slice, 2)
r.Len(sliceWriters[3].slice, 1)
r.Equal(sliceWriters[3].slice[0], expectedRoot)
// printCache(0, 3, sliceWriters)
}
func printCache(bottom, top int, sliceWriters map[uint]*sliceReadWriter) {
for i := top; i >= bottom; i-- {
print("| ")
for _, n := range sliceWriters[uint(i)].slice {
printSpaces(numSpaces(i))
fmt.Print(hex.EncodeToString(n[:2]))
printSpaces(numSpaces(i))
}
println(" |")
}
}
func numSpaces(n int) int {
res := 1
for i := 0; i < n; i++ {
res += 3 * (1 << uint(i))
}
return res
}
func printSpaces(n int) {
for i := 0; i < n; i++ {
print(" ")
}
}
func BenchmarkNewCachingTreeSmall(b *testing.B) {
var size uint64 = 1 << 23
cache := make(map[uint]io.Writer)
for i := uint(7); i < 23; i++ {
cache[i] = &sliceReadWriter{}
}
start := time.Now()
tree := NewCachingTree(GetSha256Parent, cache)
for i := uint64(0); i < size; i++ {
_ = tree.AddLeaf(NewNodeFromUint64(i))
}
b.Log(time.Since(start))
/*
merkle_test.go:242: 3.054842184s
*/
}
func TestSparseBoolStack(t *testing.T) {
r := require.New(t)
allFalse := newSparseBoolStack([]uint64{})
for i := 0; i < 1000; i++ {
r.False(allFalse.Pop())
}
allTrue := newSparseBoolStack([]uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
for i := 0; i < 10; i++ {
r.True(allTrue.Pop())
}
rounds := make([]uint64, 0, 100)
for i := 0; i < 1000; i += 10 {
rounds = append(rounds, uint64(i))
}
roundsTrue := newSparseBoolStack(rounds)
for i := 0; i < 1000; i++ {
if i%10 == 0 {
r.True(roundsTrue.Pop())
} else {
r.False(roundsTrue.Pop())
}
}
}
func TestEmptyNode(t *testing.T) {
r := require.New(t)
r.True(emptyNode.IsEmpty())
r.False(emptyNode.onProvenPath)
}