-
Notifications
You must be signed in to change notification settings - Fork 4
/
smt_proofs_test.go
405 lines (362 loc) · 14.6 KB
/
smt_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
package smt
import (
"crypto/sha256"
"crypto/sha512"
"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 TestSMT_Proof_Operations(t *testing.T) {
var smn, smv kvstore.MapStore
var smt *SMTWithStorage
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)
smt = NewSMTWithStorage(smn, smv, sha256.New())
base := smt.Spec()
// Generate and verify a proof on an empty key.
proof, err = smt.Prove([]byte("testKey3"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifyProof(proof, base.th.placeholder(), []byte("testKey3"), defaultEmptyValue, base)
require.NoError(t, err)
require.True(t, result)
result, err = VerifyProof(proof, root, []byte("testKey3"), []byte("badValue"), base)
require.NoError(t, err)
require.False(t, result)
// Add a key, generate and verify a Merkle proof.
err = smt.Update([]byte("testKey"), []byte("testValue"))
require.NoError(t, err)
root = smt.Root()
proof, err = smt.Prove([]byte("testKey"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifyProof(proof, root, []byte("testKey"), []byte("testValue"), base)
require.NoError(t, err)
require.True(t, result)
result, err = VerifyProof(proof, root, []byte("testKey"), []byte("badValue"), base)
require.NoError(t, err)
require.False(t, result)
// Add a key, generate and verify both Merkle proofs.
err = smt.Update([]byte("testKey2"), []byte("testValue"))
require.NoError(t, err)
root = smt.Root()
proof, err = smt.Prove([]byte("testKey"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifyProof(proof, root, []byte("testKey"), []byte("testValue"), base)
require.NoError(t, err)
require.True(t, result)
result, err = VerifyProof(proof, root, []byte("testKey"), []byte("badValue"), base)
require.NoError(t, err)
require.False(t, result)
result, err = VerifyProof(randomizeProof(proof), root, []byte("testKey"), []byte("testValue"), base)
require.NoError(t, err)
require.False(t, result)
proof, err = smt.Prove([]byte("testKey2"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifyProof(proof, root, []byte("testKey2"), []byte("testValue"), base)
require.NoError(t, err)
require.True(t, result)
result, err = VerifyProof(proof, root, []byte("testKey2"), []byte("badValue"), base)
require.NoError(t, err)
require.False(t, result)
result, err = VerifyProof(randomizeProof(proof), root, []byte("testKey2"), []byte("testValue"), base)
require.NoError(t, err)
require.False(t, result)
// Try proving a default value for a non-default leaf.
_, leafData := base.th.digestLeafNode(base.ph.Path([]byte("testKey2")), base.valueHash([]byte("testValue")))
proof = &SparseMerkleProof{
SideNodes: proof.SideNodes,
NonMembershipLeafData: leafData,
}
result, err = VerifyProof(proof, root, []byte("testKey2"), defaultEmptyValue, base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
// Generate and verify a proof on an empty key.
proof, err = smt.Prove([]byte("testKey3"))
require.NoError(t, err)
checkCompactEquivalence(t, proof, base)
result, err = VerifyProof(proof, root, []byte("testKey3"), defaultEmptyValue, base)
require.NoError(t, err)
require.True(t, result)
result, err = VerifyProof(proof, root, []byte("testKey3"), []byte("badValue"), base)
require.NoError(t, err)
require.False(t, result)
result, err = VerifyProof(randomizeProof(proof), root, []byte("testKey3"), defaultEmptyValue, base)
require.NoError(t, err)
require.False(t, result)
}
// Test sanity check cases for non-compact proofs.
func TestSMT_Proof_ValidateBasic(t *testing.T) {
smn := simplemap.NewSimpleMap()
smv := simplemap.NewSimpleMap()
smt := NewSMTWithStorage(smn, smv, sha256.New())
base := smt.Spec()
err := smt.Update([]byte("testKey1"), []byte("testValue1"))
require.NoError(t, err)
err = smt.Update([]byte("testKey2"), []byte("testValue2"))
require.NoError(t, err)
err = smt.Update([]byte("testKey3"), []byte("testValue3"))
require.NoError(t, err)
err = smt.Update([]byte("testKey4"), []byte("testValue4"))
require.NoError(t, err)
root := smt.Root()
// Case: invalid number of sidenodes.
proof, _ := smt.Prove([]byte("testKey1"))
sideNodes := make([][]byte, smt.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 := VerifyProof(proof, root, []byte("testKey1"), []byte("testValue1"), base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
// Case: incorrect size for NonMembershipLeafData.
proof, _ = smt.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 = VerifyProof(proof, root, []byte("testKey1"), []byte("testValue1"), base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
// Case: unexpected sidenode size.
proof, _ = smt.Prove([]byte("testKey1"))
proof.SideNodes[0] = make([]byte, 1)
require.EqualError(t, proof.validateBasic(base), "invalid side node size: got 1 but want 32")
result, err = VerifyProof(proof, root, []byte("testKey1"), []byte("testValue1"), 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, _ = smt.Prove([]byte("testKey1"))
proof.SiblingData = base.th.digestData(proof.SiblingData)
require.EqualError(
t,
proof.validateBasic(base),
"invalid sibling data hash: got 187864587bac133246face60f98b8214407aa314f37dfc9ce8e1f5c80284a866 but want 101cb41e8679c5376da9fb4c1e5ad4772876affb74045574cc7c12e4c38975f9",
)
result, err = VerifyProof(proof, root, []byte("testKey1"), []byte("testValue1"), base)
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactProof(proof, base)
require.Error(t, err)
}
func TestSMT_ClosestProof_ValidateBasic(t *testing.T) {
smn := simplemap.NewSimpleMap()
smt := NewSparseMerkleTrie(smn, sha256.New())
base := smt.Spec()
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
// insert some unrelated values to populate the trie
require.NoError(t, smt.Update([]byte("foo"), []byte("oof")))
require.NoError(t, smt.Update([]byte("bar"), []byte("rab")))
require.NoError(t, smt.Update([]byte("baz"), []byte("zab")))
require.NoError(t, smt.Update([]byte("bin"), []byte("nib")))
require.NoError(t, smt.Update([]byte("fiz"), []byte("zif")))
require.NoError(t, smt.Update([]byte("fob"), []byte("bof")))
require.NoError(t, smt.Update([]byte("testKey"), []byte("testValue")))
require.NoError(t, smt.Update([]byte("testKey2"), []byte("testValue2")))
require.NoError(t, smt.Update([]byte("testKey3"), []byte("testValue3")))
require.NoError(t, smt.Update([]byte("testKey4"), []byte("testValue4")))
root := smt.Root()
proof, err := smt.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, smt.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, smt.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
proof, err = smt.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, smt.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, smt.Spec())
require.ErrorIs(t, err, ErrBadProof)
require.False(t, result)
_, err = CompactClosestProof(proof, base)
require.Error(t, err)
proof, err = smt.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, smt.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/2c2ea530-a2e8-49d7-89c2-ca9c615b0c79
func TestSMT_ProveClosest(t *testing.T) {
var smn kvstore.MapStore
var smt *SMT
var proof *SparseMerkleClosestProof
var result bool
var root []byte
var err error
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smt = NewSparseMerkleTrie(smn, sha256.New(), WithValueHasher(nil))
// insert some unrelated values to populate the trie
require.NoError(t, smt.Update([]byte("foo"), []byte("oof")))
require.NoError(t, smt.Update([]byte("bar"), []byte("rab")))
require.NoError(t, smt.Update([]byte("baz"), []byte("zab")))
require.NoError(t, smt.Update([]byte("bin"), []byte("nib")))
require.NoError(t, smt.Update([]byte("fiz"), []byte("zif")))
require.NoError(t, smt.Update([]byte("fob"), []byte("bof")))
require.NoError(t, smt.Update([]byte("testKey"), []byte("testValue")))
require.NoError(t, smt.Update([]byte("testKey2"), []byte("testValue2")))
require.NoError(t, smt.Update([]byte("testKey3"), []byte("testValue3")))
require.NoError(t, smt.Update([]byte("testKey4"), []byte("testValue4")))
root = smt.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 = smt.ProveClosest(path[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smt.Spec())
require.NotEqual(t, proof, &SparseMerkleClosestProof{})
result, err = VerifyClosestProof(proof, root, smt.Spec())
require.NoError(t, err)
require.True(t, result)
closestPath := sha256.Sum256([]byte("testKey2"))
require.Equal(t, closestPath[:], proof.ClosestPath)
require.Equal(t, []byte("testValue2"), proof.ClosestValueHash)
// 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 = smt.ProveClosest(path2[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smt.Spec())
require.NotEqual(t, proof, &SparseMerkleClosestProof{})
result, err = VerifyClosestProof(proof, root, smt.Spec())
require.NoError(t, err)
require.True(t, result)
closestPath = sha256.Sum256([]byte("testKey4"))
require.Equal(t, closestPath[:], proof.ClosestPath)
require.Equal(t, []byte("testValue4"), proof.ClosestValueHash)
}
func TestSMT_ProveClosest_Empty(t *testing.T) {
var smn kvstore.MapStore
var smt *SMT
var proof *SparseMerkleClosestProof
var err error
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smt = NewSparseMerkleTrie(smn, sha256.New(), WithValueHasher(nil))
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
proof, err = smt.ProveClosest(path[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smt.Spec())
require.Equal(t, proof, &SparseMerkleClosestProof{
Path: path[:],
FlippedBits: []int{0},
Depth: 0,
ClosestPath: smt.placeholder(),
ClosestProof: &SparseMerkleProof{},
})
result, err := VerifyClosestProof(proof, smt.Root(), smt.Spec())
require.NoError(t, err)
require.True(t, result)
}
func TestSMT_ProveClosest_OneNode(t *testing.T) {
var smn kvstore.MapStore
var smt *SMT
var proof *SparseMerkleClosestProof
var err error
smn = simplemap.NewSimpleMap()
smt = NewSparseMerkleTrie(smn, sha256.New(), WithValueHasher(nil))
require.NoError(t, smt.Update([]byte("foo"), []byte("bar")))
path := sha256.Sum256([]byte("testKey2"))
flipPathBit(path[:], 3)
flipPathBit(path[:], 6)
proof, err = smt.ProveClosest(path[:])
require.NoError(t, err)
checkClosestCompactEquivalence(t, proof, smt.Spec())
closestPath := sha256.Sum256([]byte("foo"))
require.Equal(t, proof, &SparseMerkleClosestProof{
Path: path[:],
FlippedBits: []int{},
Depth: 0,
ClosestPath: closestPath[:],
ClosestValueHash: []byte("bar"),
ClosestProof: &SparseMerkleProof{},
})
result, err := VerifyClosestProof(proof, smt.Root(), smt.Spec())
require.NoError(t, err)
require.True(t, result)
}
func TestSMT_ProveClosest_Proof(t *testing.T) {
var smn kvstore.MapStore
var smt256 *SMT
var smt512 *SMT
var proof256 *SparseMerkleClosestProof
var proof512 *SparseMerkleClosestProof
var err error
// setup trie (256+512 path hasher) and nodestore
smn = simplemap.NewSimpleMap()
require.NoError(t, err)
smt256 = NewSparseMerkleTrie(smn, sha256.New())
smt512 = NewSparseMerkleTrie(smn, sha512.New())
// insert 100 key-value-sum triples
for i := 0; i < 100; i++ {
s := strconv.Itoa(i)
require.NoError(t, smt256.Update([]byte(s), []byte(s)))
require.NoError(t, smt512.Update([]byte(s), []byte(s)))
// generate proofs for each key in the trie
path256 := sha256.Sum256([]byte(s))
path512 := sha512.Sum512([]byte(s))
proof256, err = smt256.ProveClosest(path256[:])
require.NoError(t, err)
proof512, err = smt512.ProveClosest(path512[:])
require.NoError(t, err)
// ensure proof is same after compression and decompression
checkClosestCompactEquivalence(t, proof256, smt256.Spec())
checkClosestCompactEquivalence(t, proof512, smt512.Spec())
}
}