-
Notifications
You must be signed in to change notification settings - Fork 4
/
smst_proofs_test.go
511 lines (451 loc) · 18.2 KB
/
smst_proofs_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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package smt
import (
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt/kvstore"
"github.com/pokt-network/smt/kvstore/simplemap"
)
// Test base case Merkle proof operations.
func TestSMST_Proof_Operations(t *testing.T) {
var smn, smv kvstore.MapStore
var smst *SMSTWithStorage
var proof *SparseMerkleProof
var result bool
var root []byte
var err error
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smv = simplemap.NewSimpleMap()
require.NoError(t, err)
smst = NewSMSTWithStorage(smn, smv, sha256.New())
base := smst.Spec()
// Generate and verify a proof on an empty key.
proof, err = smst.Prove([]byte("testKey3"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifySumProof(proof, base.placeholder(), []byte("testKey3"), defaultEmptyValue, 0, 0, base)
require.NoError(t, err)
require.True(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey3"), []byte("badValue"), 5, 1, base)
require.NoError(t, err)
require.False(t, result)
// Add a key, generate and verify a Merkle proof.
err = smst.Update([]byte("testKey"), []byte("testValue"), 5)
require.NoError(t, err)
root = smst.Root()
proof, err = smst.Prove([]byte("testKey"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("testValue"), 5, 1, base) // valid
require.NoError(t, err)
require.True(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("testValue"), 5, 2, base) // wrong count
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("badValue"), 5, 1, base) // wrong value
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("testValue"), 10, 1, base) // wrong sum
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("badValue"), 10, 1, base) // wrong value and sum
require.NoError(t, err)
require.False(t, result)
// Add a key, generate and verify both Merkle proofs.
err = smst.Update([]byte("testKey2"), []byte("testValue"), 5)
require.NoError(t, err)
root = smst.Root()
proof, err = smst.Prove([]byte("testKey"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("testValue"), 5, 1, base) // valid
require.NoError(t, err)
require.True(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("testValue"), 5, 2, base) // wrong count
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("badValue"), 5, 1, base) // wrong value
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("testValue"), 10, 1, base) // wrong sum
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey"), []byte("badValue"), 10, 1, base) // wrong value and sum
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(
randomizeSumProof(proof),
root,
[]byte("testKey"),
[]byte("testValue"),
5,
1,
base,
) // invalid proof
require.NoError(t, err)
require.False(t, result)
proof, err = smst.Prove([]byte("testKey2"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifySumProof(proof, root, []byte("testKey2"), []byte("testValue"), 5, 1, base) // valid
require.NoError(t, err)
require.True(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey2"), []byte("badValue"), 5, 1, base) // wrong value
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey2"), []byte("testValue"), 10, 1, base) // wrong sum
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey2"), []byte("badValue"), 10, 1, base) // wrong value and sum
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(
randomizeSumProof(proof),
root,
[]byte("testKey2"),
[]byte("testValue"),
5,
1,
base,
) // invalid proof
require.NoError(t, err)
require.False(t, result)
// Try (and fail) proving a default value for a non-default leaf.
var sum [sumSizeBytes]byte
binary.BigEndian.PutUint64(sum[:], 5)
testVal := base.valueHash([]byte("testValue"))
testVal = append(testVal, sum[:]...)
_, leafData := base.th.digestSumLeafNode(base.ph.Path([]byte("testKey2")), testVal)
proof = &SparseMerkleProof{
SideNodes: proof.SideNodes,
NonMembershipLeafData: leafData,
}
result, err = VerifySumProof(proof, root, []byte("testKey2"), defaultEmptyValue, 0, 0, base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
// Generate and verify a proof on an empty key.
proof, err = smst.Prove([]byte("testKey3"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifySumProof(proof, root, []byte("testKey3"), defaultEmptyValue, 0, 0, base) // valid
require.NoError(t, err)
require.True(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey3"), []byte("badValue"), 0, 0, base) // wrong value
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(proof, root, []byte("testKey3"), defaultEmptyValue, 5, 0, base) // wrong sum
require.NoError(t, err)
require.False(t, result)
result, err = VerifySumProof(
randomizeSumProof(proof),
root,
[]byte("testKey3"),
defaultEmptyValue,
0,
0,
base,
) // invalid proof
require.NoError(t, err)
require.False(t, result)
}
// Test sanity check cases for non-compact proofs.
func TestSMST_Proof_ValidateBasic(t *testing.T) {
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
smst := NewSMSTWithStorage(smn, smv, sha256.New())
base := smst.Spec()
err := smst.Update([]byte("testKey1"), []byte("testValue1"), 1)
require.NoError(t, err)
err = smst.Update([]byte("testKey2"), []byte("testValue2"), 2)
require.NoError(t, err)
err = smst.Update([]byte("testKey3"), []byte("testValue3"), 3)
require.NoError(t, err)
err = smst.Update([]byte("testKey4"), []byte("testValue4"), 4)
require.NoError(t, err)
root := smst.Root()
// Case: invalid number of sidenodes.
proof, _ := smst.Prove([]byte("testKey1"))
sideNodes := make([][]byte, smst.Spec().depth()+1)
for i := range sideNodes {
sideNodes[i] = proof.SideNodes[0]
}
proof.SideNodes = sideNodes
require.EqualError(t, proof.validateBasic(base), "too many side nodes: got 257 but max is 256")
result, err := VerifySumProof(proof, root, []byte("testKey1"), []byte("testValue1"), 1, 1, base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
// Case: incorrect size for NonMembershipLeafData.
proof, _ = smst.Prove([]byte("testKey1"))
proof.NonMembershipLeafData = make([]byte, 1)
require.EqualError(t, proof.validateBasic(base), "invalid non-membership leaf data size: got 1 but min is 33")
result, err = VerifySumProof(proof, root, []byte("testKey1"), []byte("testValue1"), 1, 1, base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
// Case: unexpected side node size.
proof, _ = smst.Prove([]byte("testKey1"))
proof.SideNodes[0] = make([]byte, 1)
require.EqualError(t, proof.validateBasic(base), "invalid side node size: got 1 but want 48")
result, err = VerifySumProof(proof, root, []byte("testKey1"), []byte("testValue1"), 1, 1, base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
// Case: incorrect non-nil sibling data
proof, _ = smst.Prove([]byte("testKey1"))
proof.SiblingData = base.th.digestData(proof.SiblingData)
require.EqualError(
t,
proof.validateBasic(base),
"invalid sibling data hash: got 30ecfc36781633f6765088e69165733d7192483b4468ca53ef21794bb035f72f799a6026c448b3eacafa2b82ca1ff7f2 but want 3c54b08cc0074a44a12cb0ea0486d29d799a6026c448b3eacafa2b82ca1ff7f200000000000000090000000000000003",
)
result, err = VerifySumProof(proof, root, []byte("testKey1"), []byte("testValue1"), 1, 1, base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
}
func TestSMST_ClosestProof_ValidateBasic(t *testing.T) {
smn := simplemap.NewSimpleMap()
smst := NewSparseMerkleSumTrie(smn, sha256.New())
base := smst.Spec()
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
// insert some unrelated values to populate the trie
require.NoError(t, smst.Update([]byte("foo"), []byte("oof"), 3))
require.NoError(t, smst.Update([]byte("bar"), []byte("rab"), 6))
require.NoError(t, smst.Update([]byte("baz"), []byte("zab"), 9))
require.NoError(t, smst.Update([]byte("bin"), []byte("nib"), 12))
require.NoError(t, smst.Update([]byte("fiz"), []byte("zif"), 15))
require.NoError(t, smst.Update([]byte("fob"), []byte("bof"), 18))
require.NoError(t, smst.Update([]byte("testKey"), []byte("testValue"), 21))
require.NoError(t, smst.Update([]byte("testKey2"), []byte("testValue2"), 24))
require.NoError(t, smst.Update([]byte("testKey3"), []byte("testValue3"), 27))
require.NoError(t, smst.Update([]byte("testKey4"), []byte("testValue4"), 30))
root := smst.Root()
proof, err := smst.ProveClosest(path[:])
require.NoError(t, err)
proof.Depth = -1
require.EqualError(t, proof.validateBasic(base), "invalid depth: got -1, outside of [0, 256]")
result, err := VerifyClosestProof(proof, root, smst.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
proof.Depth = 257
require.EqualError(t, proof.validateBasic(base), "invalid depth: got 257, outside of [0, 256]")
result, err = VerifyClosestProof(proof, root, smst.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
proof, err = smst.ProveClosest(path[:])
require.NoError(t, err)
proof.FlippedBits[0] = -1
require.EqualError(t, proof.validateBasic(base), "invalid flipped bit index 0: got -1, outside of [0, 8]")
result, err = VerifyClosestProof(proof, root, smst.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
proof.FlippedBits[0] = 9
require.EqualError(t, proof.validateBasic(base), "invalid flipped bit index 0: got 9, outside of [0, 8]")
result, err = VerifyClosestProof(proof, root, smst.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
proof, err = smst.ProveClosest(path[:])
require.NoError(t, err)
flipPathBit(proof.Path, 3)
require.EqualError(
t,
proof.validateBasic(base),
"invalid closest path: 8d13809f932d0296b88c1913231ab4b403f05c88363575476204fef6930f22ae (not equal at bit: 3)",
)
result, err = VerifyClosestProof(proof, root, smst.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
}
// ProveClosest test against a visual representation of the trie
// See: https://github.com/pokt-network/smt/assets/53987565/2a2f33e0-f81f-41c5-bd76-af0cd1cd8f15
func TestSMST_ProveClosest(t *testing.T) {
var smn kvstore.MapStore
var smst *SMST
var proof *SparseMerkleClosestProof
var result bool
var root []byte
var err error
var sumBz [sumSizeBytes]byte
var countBz [countSizeBytes]byte
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smst = NewSparseMerkleSumTrie(smn, sha256.New(), WithValueHasher(nil))
// insert some unrelated values to populate the trie
require.NoError(t, smst.Update([]byte("foo"), []byte("oof"), 3))
require.NoError(t, smst.Update([]byte("bar"), []byte("rab"), 6))
require.NoError(t, smst.Update([]byte("baz"), []byte("zab"), 9))
require.NoError(t, smst.Update([]byte("bin"), []byte("nib"), 12))
require.NoError(t, smst.Update([]byte("fiz"), []byte("zif"), 15))
require.NoError(t, smst.Update([]byte("fob"), []byte("bof"), 18))
require.NoError(t, smst.Update([]byte("testKey"), []byte("testValue"), 21))
require.NoError(t, smst.Update([]byte("testKey2"), []byte("testValue2"), 24))
require.NoError(t, smst.Update([]byte("testKey3"), []byte("testValue3"), 27))
require.NoError(t, smst.Update([]byte("testKey4"), []byte("testValue4"), 30))
root = smst.Root()
// `testKey2` is the child of an inner node, which is the child of an
// extension node. The extension node has the path bounds of [3, 7]. This
// means any bits between 3-6 can be flipped, and the resulting path would
// still traverse through the same extension node and lead to testKey2 -
// the closest key. However, flipping bit 7 will lead to testKey4.
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
proof, err = smst.ProveClosest(path[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smst.Spec())
require.NotEqual(t, proof, &SparseMerkleClosestProof{})
closestPath := sha256.Sum256([]byte("testKey2"))
closestValueHash := []byte("testValue2")
binary.BigEndian.PutUint64(sumBz[:], 24)
closestValueHash = append(closestValueHash, sumBz[:]...)
binary.BigEndian.PutUint64(countBz[:], 1)
closestValueHash = append(closestValueHash, countBz[:]...)
require.Equal(t, proof, &SparseMerkleClosestProof{
Path: path[:],
FlippedBits: []int{3, 6},
Depth: 8,
ClosestPath: closestPath[:],
ClosestValueHash: closestValueHash,
ClosestProof: proof.ClosestProof, // copy of proof as we are checking equality of other fields
})
result, err = VerifyClosestProof(proof, root, smst.Spec())
require.NoError(t, err)
require.True(t, result)
// testKey4 is the neighbour of testKey2, by flipping the final bit of the
// extension node we change the longest common prefix to that of testKey4
path2 := sha256.Sum256([]byte("testKey2"))
flipPathBit(path2[:], 3)
flipPathBit(path2[:], 7)
proof, err = smst.ProveClosest(path2[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smst.Spec())
require.NotEqual(t, proof, &SparseMerkleClosestProof{})
closestPath = sha256.Sum256([]byte("testKey4"))
closestValueHash = []byte("testValue4")
binary.BigEndian.PutUint64(sumBz[:], 30)
closestValueHash = append(closestValueHash, sumBz[:]...)
closestValueHash = append(closestValueHash, countBz[:]...)
require.Equal(t, proof, &SparseMerkleClosestProof{
Path: path2[:],
FlippedBits: []int{3},
Depth: 8,
ClosestPath: closestPath[:],
ClosestValueHash: closestValueHash,
ClosestProof: proof.ClosestProof, // copy of proof as we are checking equality of other fields
})
result, err = VerifyClosestProof(proof, root, smst.Spec())
require.NoError(t, err)
require.True(t, result)
}
func TestSMST_ProveClosest_Empty(t *testing.T) {
var smn kvstore.MapStore
var smst *SMST
var proof *SparseMerkleClosestProof
var err error
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smst = NewSparseMerkleSumTrie(smn, sha256.New(), WithValueHasher(nil))
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
proof, err = smst.ProveClosest(path[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smst.Spec())
require.Equal(t, proof, &SparseMerkleClosestProof{
Path: path[:],
FlippedBits: []int{0},
Depth: 0,
ClosestPath: smst.placeholder(),
ClosestProof: &SparseMerkleProof{},
})
result, err := VerifyClosestProof(proof, smst.Root(), smst.Spec())
require.NoError(t, err)
require.True(t, result)
}
func TestSMST_ProveClosest_OneNode(t *testing.T) {
var smn kvstore.MapStore
var smst *SMST
var proof *SparseMerkleClosestProof
var err error
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smst = NewSparseMerkleSumTrie(smn, sha256.New(), WithValueHasher(nil))
require.NoError(t, smst.Update([]byte("foo"), []byte("bar"), 5))
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
proof, err = smst.ProveClosest(path[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smst.Spec())
closestPath := sha256.Sum256([]byte("foo"))
closestValueHash := []byte("bar")
// Manually insert the sum, which is the weight of the single node in the trie
var sumBz [sumSizeBytes]byte
binary.BigEndian.PutUint64(sumBz[:], 5)
closestValueHash = append(closestValueHash, sumBz[:]...)
// Manually insert the count, which is 1 for a single leaf
var countBz [countSizeBytes]byte
binary.BigEndian.PutUint64(countBz[:], 1)
closestValueHash = append(closestValueHash, countBz[:]...)
require.Equal(t, proof, &SparseMerkleClosestProof{
Path: path[:],
FlippedBits: []int{},
Depth: 0,
ClosestPath: closestPath[:],
ClosestValueHash: closestValueHash,
ClosestProof: &SparseMerkleProof{},
})
result, err := VerifyClosestProof(proof, smst.Root(), smst.Spec())
require.NoError(t, err)
require.True(t, result)
}
func TestSMST_ProveClosest_Proof(t *testing.T) {
var smn kvstore.MapStore
var smst256 *SMST
var smst512 *SMST
var proof256 *SparseMerkleClosestProof
var proof512 *SparseMerkleClosestProof
var err error
// setup trie (256+512 path hasher) and nodestore
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smst256 = NewSparseMerkleSumTrie(smn, sha256.New())
smst512 = NewSparseMerkleSumTrie(smn, sha512.New())
// insert 100 key-value-sum triples
for i := 0; i < 100; i++ {
s := strconv.Itoa(i)
require.NoError(t, smst256.Update([]byte(s), []byte(s), uint64(i)))
require.NoError(t, smst512.Update([]byte(s), []byte(s), uint64(i)))
// generate proofs for each key in the trie
path256 := sha256.Sum256([]byte(s))
path512 := sha512.Sum512([]byte(s))
proof256, err = smst256.ProveClosest(path256[:])
require.NoError(t, err)
proof512, err = smst512.ProveClosest(path512[:])
require.NoError(t, err)
// ensure proof is same after compression and decompression
checkClosestCompactEquivalence(t, proof256, smst256.Spec())
checkClosestCompactEquivalence(t, proof512, smst512.Spec())
}
}