-
Notifications
You must be signed in to change notification settings - Fork 43
/
proof_test.go
1869 lines (1755 loc) · 54.4 KB
/
proof_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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nmt
import (
"bytes"
"crypto/sha256"
"fmt"
"hash"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/celestiaorg/nmt/namespace"
pb "github.com/celestiaorg/nmt/pb"
)
func TestJsonMarshal_Proof(t *testing.T) {
// create a tree with 4 leaves
nIDSize := 1
tree := exampleNMT(nIDSize, true, 1, 2, 3, 4)
// build a proof for an NID that is within the namespace range of the tree
nID := []byte{1}
proof, err := tree.ProveNamespace(nID)
require.NoError(t, err)
// marshal the proof to JSON
jsonProof, err := proof.MarshalJSON()
require.NoError(t, err)
// unmarshal the proof from JSON
var unmarshalledProof Proof
err = unmarshalledProof.UnmarshalJSON(jsonProof)
require.NoError(t, err)
// verify that the unmarshalled proof is equal to the original proof
assert.Equal(t, proof, unmarshalledProof)
}
// TestVerifyNamespace_EmptyProof tests the correct behaviour of VerifyNamespace for valid and invalid empty proofs.
func TestVerifyNamespace_EmptyProof(t *testing.T) {
// create a tree with 4 leaves
nIDSize := 1
tree := exampleNMT(nIDSize, true, 1, 2, 3, 4)
root, err := tree.Root()
require.NoError(t, err)
// build a proof for an NID that is outside the namespace range of the tree
// start = end = 0, nodes = empty, leafHash = empty
nID0 := []byte{0}
validEmptyProof, err := tree.ProveNamespace(nID0)
require.NoError(t, err)
// build a proof for an NID that is within the namespace range of the tree, then corrupt it to have a zero range
// start = end = 0, nodes = non-empty, leafHash = empty
nID1 := []byte{1}
invalidEmptyProof, err := tree.ProveNamespace(nID1)
require.NoError(t, err)
// modify the proof to contain a zero range
invalidEmptyProof.start = 0
invalidEmptyProof.end = 0
// root of an empty tree
hasher := sha256.New()
emptyRoot := tree.treeHasher.EmptyRoot()
type args struct {
proof Proof
hasher hash.Hash
nID namespace.ID
leaves [][]byte
root []byte
}
tests := []struct {
name string
args args
want bool
isValidEmptyProof bool
}{
// in the following tests, proof should always contain an empty range
// test cases for a non-empty tree hence non-empty root
{"valid empty proof & empty leaves & nID not in range", args{validEmptyProof, hasher, nID0, [][]byte{}, root}, true, true},
{"invalid empty proof & empty leaves & nID in range", args{invalidEmptyProof, hasher, nID1, [][]byte{}, root}, false, false},
{"valid empty proof & non-empty leaves & nID not in range", args{validEmptyProof, hasher, nID0, [][]byte{{1}}, root}, false, true},
{"valid empty proof & empty leaves & nID in range", args{validEmptyProof, hasher, nID1, [][]byte{}, root}, false, true},
// test cases for an empty tree hence empty root
{"valid empty proof & empty leaves & nID not in range ", args{validEmptyProof, hasher, nID0, [][]byte{}, emptyRoot}, true, true},
{"invalid empty proof & empty leaves & nID in range", args{invalidEmptyProof, hasher, nID1, [][]byte{}, emptyRoot}, false, false},
{"valid empty proof & non-empty leaves & nID not in range", args{validEmptyProof, hasher, nID0, [][]byte{{1}}, emptyRoot}, false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.True(t, tt.args.proof.IsEmptyProof() == tt.isValidEmptyProof)
if got := tt.args.proof.VerifyNamespace(tt.args.hasher, tt.args.nID, tt.args.leaves, tt.args.root); got != tt.want {
t.Errorf("VerifyNamespace() = %v, want %v", got, tt.want)
}
})
}
}
func TestProof_VerifyNamespace_False(t *testing.T) {
const testNidLen = 3
n := New(sha256.New(), NamespaceIDSize(testNidLen))
data := append(append([]namespaceDataPair{
newNamespaceDataPair([]byte{0, 0, 0}, []byte("first leaf")),
},
generateLeafData(testNidLen, 0, 9, []byte("data"))...,
), newNamespaceDataPair([]byte{0, 0, 8}, []byte("last leaf")))
for _, d := range data {
err := n.Push(namespace.PrefixedData(append(d.ID, d.Data...)))
if err != nil {
t.Fatalf("invalid test setup: error on Push(): %v", err)
}
}
validProof, err := n.ProveNamespace([]byte{0, 0, 0})
if err != nil {
t.Fatalf("invalid test setup: error on ProveNamespace(): %v", err)
}
// inclusion proof of the leaf index 0
incProof0, err := n.buildRangeProof(0, 1)
require.NoError(t, err)
incompleteFirstNs := NewInclusionProof(0, 1, incProof0, false)
type args struct {
nID namespace.ID
data [][]byte
root []byte
}
pushedZeroNs := n.Get([]byte{0, 0, 0})
pushedLastNs := n.Get([]byte{0, 0, 8})
// an invalid absence proof for an existing namespace ID (2) in the constructed tree
leafIndex := 3
inclusionProofOfLeafIndex, err := n.buildRangeProof(leafIndex, leafIndex+1)
require.NoError(t, err)
leafHash := n.leafHashes[leafIndex] // the only data item with namespace ID = 2 in the constructed tree is at index 3
invalidAbsenceProof := NewAbsenceProof(leafIndex, leafIndex+1, inclusionProofOfLeafIndex, leafHash, false)
// inclusion proof of the leaf index 10
incProof10, err := n.buildRangeProof(10, 11)
require.NoError(t, err)
// root
root, err := n.Root()
require.NoError(t, err)
tests := []struct {
name string
proof Proof
args args
want bool
}{
{
"invalid nid (too long)", validProof,
args{[]byte{0, 0, 0, 0}, pushedZeroNs, root},
false,
},
{
"invalid leaf data (too short)", validProof,
args{[]byte{0, 0, 0}, [][]byte{{0, 1}}, root},
false,
},
{
"mismatching IDs in data", validProof,
args{[]byte{0, 0, 0}, append(append([][]byte(nil), pushedZeroNs...), []byte{1, 1, 1}), root},
false,
},
{
"added another leaf", validProof,
args{[]byte{0, 0, 0}, append(append([][]byte(nil), pushedZeroNs...), []byte{0, 0, 0}), root},
false,
},
{
"remove one leaf, errors", validProof,
args{[]byte{0, 0, 0}, pushedZeroNs[:len(pushedZeroNs)-1], root},
false,
},
{
"remove one leaf & update proof range, errors", NewInclusionProof(validProof.Start(), validProof.End()-1, validProof.Nodes(), false),
args{[]byte{0, 0, 0}, pushedZeroNs[:len(pushedZeroNs)-1], root},
false,
},
{
"incomplete namespace proof (right)", incompleteFirstNs,
args{[]byte{0, 0, 0}, pushedZeroNs[:len(pushedZeroNs)-1], root},
false,
},
{
"incomplete namespace proof (left)", NewInclusionProof(10, 11, incProof10, false),
args{[]byte{0, 0, 8}, pushedLastNs[1:], root},
false,
},
{
"remove all leaves, errors", validProof,
args{[]byte{0, 0, 0}, pushedZeroNs[:len(pushedZeroNs)-2], root},
false,
},
{
"invalid absence proof of an existing nid", invalidAbsenceProof,
args{[]byte{0, 0, 2}, [][]byte{}, root},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// make copy of nodes for mutation check
duplicateNodes := make([][]byte, len(tt.proof.nodes))
for i := range tt.proof.nodes {
duplicateNodes[i] = make([]byte, len(tt.proof.nodes[i]))
copy(duplicateNodes[i], tt.proof.nodes[i])
}
got := tt.proof.VerifyNamespace(sha256.New(), tt.args.nID, tt.args.data, tt.args.root)
if got != tt.want {
t.Errorf("VerifyNamespace() got = %v, want %v", got, tt.want)
}
// check if proof was mutated during verification
for i := range tt.proof.nodes {
if !bytes.Equal(duplicateNodes[i], tt.proof.nodes[i]) {
t.Errorf("VerifyNameSpace() proof got mutated during verification")
}
}
})
}
}
func TestProof_MultipleLeaves(t *testing.T) {
n := New(sha256.New())
ns := []byte{1, 2, 3, 4, 5, 6, 7, 8}
rawData := [][]byte{
bytes.Repeat([]byte{1}, 100),
bytes.Repeat([]byte{2}, 100),
bytes.Repeat([]byte{3}, 100),
bytes.Repeat([]byte{4}, 100),
bytes.Repeat([]byte{5}, 100),
bytes.Repeat([]byte{6}, 100),
bytes.Repeat([]byte{7}, 100),
bytes.Repeat([]byte{8}, 100),
}
for _, d := range rawData {
err := n.Push(safeAppend(ns, d))
if err != nil {
t.Fatal(err)
}
}
root, err := n.Root()
require.NoError(t, err)
type args struct {
start, end int
root []byte
}
tests := []struct {
name string
args args
want bool
}{
{
"3rd through 5th leaf", args{2, 4, root}, true,
},
{
"single leaf", args{2, 3, root}, true,
},
{
"first leaf", args{0, 1, root}, true,
},
{
"most leaves", args{0, 7, root}, true,
},
{
"most leaves", args{0, 7, bytes.Repeat([]byte{1}, 48)}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proof, err := n.ProveRange(tt.args.start, tt.args.end)
if err != nil {
t.Fatal(err)
}
got := proof.VerifyInclusion(sha256.New(), ns, rawData[tt.args.start:tt.args.end], tt.args.root)
if got != tt.want {
t.Errorf("VerifyInclusion() got = %v, want %v", got, tt.want)
}
})
}
}
func safeAppend(id, data []byte) []byte {
return append(append(make([]byte, 0, len(id)+len(data)), id...), data...)
}
func TestVerifyLeafHashes_Err(t *testing.T) {
// create a sample tree
nameIDSize := 2
nmt := exampleNMT(nameIDSize, true, 1, 2, 3, 4, 5, 6, 7, 8)
nmthasher := nmt.treeHasher
hasher := nmthasher.(*NmtHasher)
root, err := nmt.Root()
require.NoError(t, err)
// shrink the size of the root so that the root hash is invalid.
corruptRoot := root[:len(root)-1]
// create an NMT proof
nID5 := namespace.ID{5, 5}
proof5, err := nmt.ProveNamespace(nID5)
require.NoError(t, err)
// corrupt the leafHash so that the proof verification fails during the root computation.
// note that the leaf at index 4 has the namespace ID of 5.
leafHash5 := nmt.leafHashes[4]
corruptLeafHash5 := leafHash5[:nmt.NamespaceSize()]
// corrupt the leafHash: replace its namespace ID with a different one.
nID3 := createByteSlice(nameIDSize, 3)
leafHash5SmallerNID := concat(nID3, nID3, nmt.leafHashes[4][2*nmt.NamespaceSize():])
require.NoError(t, hasher.ValidateNodeFormat(leafHash5SmallerNID))
nID6 := createByteSlice(nameIDSize, 7)
leafHash5BiggerNID := concat(nID6, nID6, nmt.leafHashes[4][2*nmt.NamespaceSize():])
require.NoError(t, hasher.ValidateNodeFormat(leafHash5BiggerNID))
// create nmt proof for namespace ID 4
nID4 := namespace.ID{4, 4}
proof4InvalidNodes, err := nmt.ProveNamespace(nID4)
require.NoError(t, err)
// corrupt the last node in the proof4.nodes, it resides on the right side of the proof4.end index.
// this test scenario makes the proof verification fail when constructing the tree root from the
// computed subtree root and the proof.nodes on the right side of the proof.end index.
proof4InvalidNodes.nodes[2] = proof4InvalidNodes.nodes[2][:nmt.NamespaceSize()-1]
leafHash4 := nmt.leafHashes[3]
// create a proof with invalid range: start = end = 0
proof4InvalidRangeSEE, err := nmt.ProveNamespace(nID4)
require.NoError(t, err)
proof4InvalidRangeSEE.end = 0
proof4InvalidRangeSEE.start = 0
// create a proof with invalid range: start > end
proof4InvalidRangeSBE, err := nmt.ProveNamespace(nID4)
require.NoError(t, err)
proof4InvalidRangeSBE.start = proof4InvalidRangeSBE.end + 1
// create a proof with invalid range: start < 0
proof4InvalidRangeSLZ, err := nmt.ProveNamespace(nID4)
require.NoError(t, err)
proof4InvalidRangeSLZ.start = -1
tests := []struct {
name string
proof Proof
Hasher *NmtHasher
verifyCompleteness bool
nID namespace.ID
leafHashes [][]byte
root []byte
wantErr bool
}{
{"corrupt root", proof5, hasher, true, nID5, [][]byte{leafHash5}, corruptRoot, true},
{"wrong leafHash: not namespaced", proof5, hasher, true, nID5, [][]byte{corruptLeafHash5}, root, true},
{"wrong leafHash: smaller namespace", proof5, hasher, true, nID5, [][]byte{leafHash5SmallerNID}, root, true},
{"wong leafHash: bigger namespace", proof5, hasher, true, nID5, [][]byte{leafHash5BiggerNID}, root, true},
{"wrong proof.nodes: the last node has an incorrect format", proof4InvalidNodes, hasher, false, nID4, [][]byte{leafHash4}, root, true},
// the verifyCompleteness parameter in the verifyProof function should be set to false in order to bypass nodes correctness check during the completeness verification (otherwise it panics).
{"wrong proof range: start = end", proof4InvalidRangeSEE, hasher, true, nID4, [][]byte{leafHash4}, root, true},
{"wrong proof range: start > end", proof4InvalidRangeSBE, hasher, true, nID4, [][]byte{leafHash4}, root, true},
{"wrong proof range: start < 0", proof4InvalidRangeSLZ, hasher, true, nID4, [][]byte{leafHash4}, root, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.proof.VerifyLeafHashes(tt.Hasher, tt.verifyCompleteness, tt.nID, tt.leafHashes, tt.root)
assert.Equal(t, tt.wantErr, err != nil)
})
}
}
func TestVerifyInclusion_False(t *testing.T) {
hasher := sha256.New()
// create a sample tree with namespace ID size of 1
nmt1 := exampleNMT(1, true, 1, 2, 3, 4, 5, 6, 7, 8)
root1, err := nmt1.Root()
require.NoError(t, err)
nid4_1 := namespace.ID{4}
proof4_1, err := nmt1.ProveRange(3, 4) // leaf at index 3 has namespace ID 4
require.NoError(t, err)
leaf4_1 := nmt1.leaves[3][nmt1.NamespaceSize():]
// create a sample tree with namespace ID size of 2
nmt2 := exampleNMT(2, true, 1, 2, 3, 4, 5, 6, 7, 8)
root2, err := nmt2.Root()
require.NoError(t, err)
nid4_2 := namespace.ID{4, 4}
proof4_2, err := nmt2.ProveRange(3, 4) // leaf at index 3 has namespace ID 4
require.NoError(t, err)
leaf4_2 := nmt2.leaves[3][nmt2.NamespaceSize():]
require.Equal(t, leaf4_2, leaf4_1)
leaf := leaf4_1
type args struct {
hasher hash.Hash
nID namespace.ID
leavesWithoutNamespace [][]byte
root []byte
}
tests := []struct {
name string
proof Proof
args args
result bool
}{
{"nID size of proof < nID size of VerifyInclusion's nmt hasher", proof4_1, args{hasher, nid4_2, [][]byte{leaf}, root2}, false},
{"nID size of proof > nID size of VerifyInclusion's nmt hasher", proof4_2, args{hasher, nid4_1, [][]byte{leaf}, root1}, false},
{"nID size of root < nID size of VerifyInclusion's nmt hasher", proof4_2, args{hasher, nid4_2, [][]byte{leaf}, root1}, false},
{"nID size of root > nID size of VerifyInclusion's nmt hasher", proof4_1, args{hasher, nid4_1, [][]byte{leaf}, root2}, false},
{"nID size of proof and root < nID size of VerifyInclusion's nmt hasher", proof4_1, args{hasher, nid4_2, [][]byte{leaf}, root1}, false},
{"nID size of proof and root > nID size of VerifyInclusion's nmt hasher", proof4_2, args{hasher, nid4_1, [][]byte{leaf}, root2}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.proof.VerifyInclusion(tt.args.hasher, tt.args.nID, tt.args.leavesWithoutNamespace, tt.args.root)
assert.Equal(t, tt.result, got)
})
}
}
// TestVerifyInclusion_EmptyProofs tests the correct behaviour of VerifyInclusion in response to valid and invalid empty proofs.
func TestVerifyInclusion_EmptyProofs(t *testing.T) {
hasher := sha256.New()
// create a tree
nIDSize := 1
tree := exampleNMT(nIDSize, true, 1, 2, 3, 4, 5, 6, 7, 8)
root, err := tree.Root()
require.NoError(t, err)
sampleLeafWithoutNID := tree.leaves[3][tree.NamespaceSize():] // does not matter which leaf we choose, just a leaf that belongs to the tree
sampleNID := tree.leaves[3][:tree.NamespaceSize()] // the NID of the leaf we chose
sampleNode := tree.leafHashes[7] // does not matter which node we choose, just a node that belongs to the tree
// create an empty proof
emptyProof := Proof{}
// verify that the proof is a valid empty proof
// this check is to ensure that we stay consistent with the definition of empty proofs
require.True(t, emptyProof.IsEmptyProof())
// create a non-empty proof
nonEmptyProof := Proof{nodes: [][]byte{sampleNode}}
type args struct {
hasher hash.Hash
nID namespace.ID
leavesWithoutNamespace [][]byte
root []byte
}
tests := []struct {
name string
proof Proof
args args
result bool
}{
{"valid empty proof and leaves == empty", emptyProof, args{hasher, sampleNID, [][]byte{}, root}, true},
{"valid empty proof and leaves == non-empty", emptyProof, args{hasher, sampleNID, [][]byte{sampleLeafWithoutNID}, root}, false},
{"invalid empty proof and leaves == empty", nonEmptyProof, args{hasher, sampleNID, [][]byte{}, root}, false},
{"invalid empty proof and leaves != empty", nonEmptyProof, args{hasher, sampleNID, [][]byte{sampleLeafWithoutNID}, root}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.proof.VerifyInclusion(tt.args.hasher, tt.args.nID, tt.args.leavesWithoutNamespace, tt.args.root)
assert.Equal(t, tt.result, got)
})
}
}
func TestVerifyNamespace_False(t *testing.T) {
nIDs := []byte{1, 2, 3, 4, 5, 6, 7, 8, 11}
// create a sample tree with namespace ID size of 1
nmt1 := exampleNMT(1, true, nIDs...)
root1, err := nmt1.Root()
require.NoError(t, err)
nid4_1 := namespace.ID{4}
proof4_1, err := nmt1.ProveNamespace(nid4_1) // leaf at index 3 has namespace ID 4
require.NoError(t, err)
// create a sample tree with namespace ID size of 2
nmt2 := exampleNMT(2, true, nIDs...)
root2, err := nmt2.Root()
require.NoError(t, err)
nid4_2 := namespace.ID{4, 4}
proof4_2, err := nmt2.ProveNamespace(nid4_2) // leaf at index 3 has namespace ID 4
require.NoError(t, err)
leaf := nmt1.leaves[3]
// create an absence proof with namespace ID size of 1
nid9_1 := namespace.ID{9}
absenceProof9_1, err := nmt1.ProveNamespace(nid9_1)
require.NoError(t, err)
require.True(t, absenceProof9_1.IsOfAbsence())
// create an absence proof with namespace ID size of 2
nid9_2 := namespace.ID{9, 9}
absenceProof9_2, err := nmt2.ProveNamespace(nid9_2)
require.NoError(t, err)
require.True(t, absenceProof9_2.IsOfAbsence())
// swap leafHashes of the absence proofs
buffer := absenceProof9_2.leafHash
absenceProof9_2.leafHash = absenceProof9_1.leafHash
absenceProof9_1.leafHash = buffer
hasher := sha256.New()
type args struct {
hasher hash.Hash
nID namespace.ID
leaves [][]byte
root []byte
}
tests := []struct {
name string
proof Proof
args args
result bool
}{
{"nID size of proof.nodes < nID size of VerifyNamespace's nmt hasher", proof4_1, args{hasher, nid4_2, [][]byte{leaf}, root2}, false},
{"nID size of proof.nodes > nID size of VerifyNamespace's nmt hasher", proof4_2, args{hasher, nid4_1, [][]byte{leaf}, root1}, false},
{"nID size of root < nID size of VerifyNamespace's nmt hasher", proof4_2, args{hasher, nid4_2, [][]byte{leaf}, root1}, false},
{"nID size of root > nID size of VerifyNamespace's nmt hasher", proof4_1, args{hasher, nid4_1, [][]byte{leaf}, root2}, false},
{"nID size of proof.leafHash < nID size of VerifyNamespace's nmt hasher", absenceProof9_2, args{hasher, nid9_2, [][]byte{}, root2}, false},
{"nID size of proof.leafHash > nID size of VerifyNamespace's nmt hasher", absenceProof9_1, args{hasher, nid9_1, [][]byte{}, root1}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.proof.VerifyNamespace(tt.args.hasher, tt.args.nID, tt.args.leaves, tt.args.root)
assert.Equal(t, tt.result, got)
})
}
}
func TestVerifyInclusion_MismatchingRange(t *testing.T) {
nIDs := []byte{1, 2, 3, 4, 6, 6, 6, 9}
nmt := exampleNMT(1, true, nIDs...)
root, err := nmt.Root()
require.NoError(t, err)
nid6 := namespace.ID{6}
// node at index 5 has namespace ID 6
incProof6, err := nmt.ProveNamespace(nid6)
require.NoError(t, err)
// leaves with namespace ID 6
leaf4 := nmt.leaves[4][nmt.NamespaceSize():]
leaf5 := nmt.leaves[5][nmt.NamespaceSize():]
leaf6 := nmt.leaves[6][nmt.NamespaceSize():]
type args struct {
nIDSize namespace.IDSize
nID namespace.ID
leavesWithoutNamespace [][]byte
root []byte
}
tests := []struct {
name string
proof Proof
args args
result bool
}{
{
"inclusion proof: size of proof's range = size of leavesWithoutNamespace",
incProof6,
args{1, nid6, [][]byte{leaf4, leaf5, leaf6}, root},
true,
},
{
"inclusion proof: size of proof's range > size of" +
" a non-empty leavesWithoutNamespace",
incProof6,
args{1, nid6, [][]byte{leaf4, leaf5}, root},
false,
},
{
"inclusion proof: size of proof's range > size of" +
" an empty leavesWithoutNamespace",
incProof6,
args{1, nid6, [][]byte{}, root},
false,
},
{
"inclusion proof: size of proof's range < size of" +
" leavesWithoutNamespace",
incProof6,
args{1, nid6, [][]byte{leaf4, leaf5, leaf6, leaf6}, root},
false,
},
{
// in this testcase the nameID does not really matter since the
// leaves are empty
"empty proof: size of proof's range = size of leavesWithoutNamespace",
Proof{start: 1, end: 1},
args{1, nid6, [][]byte{}, root},
true,
},
{
"empty proof: size of proof's range < size of" +
" leavesWithoutNamespace",
Proof{start: 1, end: 1},
args{1, nid6, [][]byte{leaf4, leaf5, leaf6}, root},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hasher := sha256.New()
got := tt.proof.VerifyInclusion(hasher, tt.args.nID,
tt.args.leavesWithoutNamespace, tt.args.root)
assert.Equal(t, tt.result, got)
})
}
}
func TestVerifyLeafHashes_MismatchingRange(t *testing.T) {
nIDs := []byte{1, 2, 3, 4, 6, 6, 6, 9}
nmt := exampleNMT(1, true, nIDs...)
root, err := nmt.Root()
require.NoError(t, err)
nid5 := namespace.ID{5}
// namespace 5 does not exist in the tree, hence the proof is an absence proof
absenceProof5, err := nmt.ProveNamespace(nid5)
require.NoError(t, err)
leafHash5 := nmt.leafHashes[4]
nid6 := namespace.ID{6}
// node at index 5 has namespace ID 6
incProof6, err := nmt.Prove(5)
require.NoError(t, err)
leafHash6 := nmt.leafHashes[5]
type args struct {
nIDSize namespace.IDSize
nID namespace.ID
leafHashes [][]byte
root []byte
}
tests := []struct {
name string
proof Proof
args args
result bool
err error
}{
{
"absence proof: size of proof's range = size of leafHashes",
absenceProof5,
args{1, namespace.ID{5}, [][]byte{leafHash5}, root},
true, nil,
},
{
"absence proof: size of proof's range > size of leafHashes",
absenceProof5,
args{1, nid5, [][]byte{}, root},
false, ErrWrongLeafHashesSize,
},
{
"absence proof: size of proof's range < size of leafHashes",
absenceProof5,
args{1, nid5, [][]byte{leafHash5, leafHash5}, root},
false, ErrWrongLeafHashesSize,
},
{
"inclusion proof: size of proof's range = size of leafHashes",
incProof6,
args{1, nid6, [][]byte{leafHash6}, root},
true, nil,
},
{
"inclusion proof: size of proof's range > size of leafHashes",
incProof6,
args{1, nid6, [][]byte{}, root},
false, ErrWrongLeafHashesSize,
},
{
"inclusion proof: size of proof's range < size of leafHashes",
incProof6,
args{1, nid6, [][]byte{leafHash6, leafHash6}, root},
false,
ErrWrongLeafHashesSize,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hasher := NewNmtHasher(sha256.New(), tt.args.nIDSize, true)
got, err := tt.proof.VerifyLeafHashes(hasher, false, tt.args.nID,
tt.args.leafHashes, tt.args.root)
assert.Equal(t, tt.result, got)
if tt.err != nil {
assert.ErrorAs(t, err, &tt.err)
}
})
}
}
func TestVerifyLeafHashes_False(t *testing.T) {
nIDs := []byte{1, 2, 3, 4, 6, 7, 8, 9}
// create a sample tree with namespace ID size of 1
nmt1 := exampleNMT(1, true, nIDs...)
root1, err := nmt1.Root()
require.NoError(t, err)
nid4_1 := namespace.ID{4}
proof4_1, err := nmt1.ProveNamespace(nid4_1) // leaf at index 3 has namespace ID 4
require.NoError(t, err)
leafHash1 := nmt1.leafHashes[3]
// corrupt the namespace of the leafHash
leafHash1Corrupted := make([]byte, len(leafHash1))
copy(leafHash1Corrupted, leafHash1)
leafHash1Corrupted[0] = 0 // change the min namespace
leafHash1Corrupted[1] = 0 // change the max namespace
// create an absence proof with namespace ID size of 1
nid5_1 := namespace.ID{5}
absenceProof5_1, err := nmt1.ProveNamespace(nid5_1)
require.NoError(t, err)
leafHash6_1 := nmt1.leafHashes[4]
assert.Equal(t, leafHash6_1, absenceProof5_1.leafHash)
// create a sample tree with namespace ID size of 2
nmt2 := exampleNMT(2, true, nIDs...)
root2, err := nmt2.Root()
require.NoError(t, err)
nid4_2 := namespace.ID{4, 4}
proof4_2, err := nmt2.ProveNamespace(nid4_2) // leaf at index 3 has namespace ID 4
require.NoError(t, err)
leafHash2 := nmt2.leafHashes[3]
type args struct {
nIDSize namespace.IDSize
nID namespace.ID
leaves [][]byte
root []byte
}
tests := []struct {
name string
proof Proof
args args
result bool
}{
{
"nID size of proof < nID size of VerifyLeafHashes' nmt hasher",
proof4_1,
args{2, nid4_2, [][]byte{leafHash2}, root2},
false,
},
{"nID size of proof > nID size of VerifyLeafHashes' nmt hasher", proof4_2, args{1, nid4_1, [][]byte{leafHash1}, root1}, false},
{"nID size of root < nID size of VerifyLeafHashes' nmt hasher", proof4_2, args{2, nid4_2, [][]byte{leafHash2}, root1}, false},
{"nID size of root > nID size of VerifyLeafHashes' nmt hasher", proof4_1, args{1, nid4_1, [][]byte{leafHash1}, root2}, false},
{"size of queried nID > nID size of VerifyLeafHashes' nmt hasher", proof4_1, args{1, nid4_2, [][]byte{leafHash1}, root1}, false},
{"size of queried nID < nID size of VerifyLeafHashes' nmt hasher", proof4_2, args{2, nid4_1, [][]byte{leafHash2}, root2}, false},
{"nID size of leafHash < nID size of VerifyLeafHashes' nmt hasher", proof4_2, args{2, nid4_2, [][]byte{leafHash1}, root2}, false},
{"nID size of leafHash > nID size of VerifyLeafHashes' nmt hasher", proof4_1, args{1, nid4_1, [][]byte{leafHash2}, root1}, false},
{"nID of leafHashes do not match the queried nID", proof4_1, args{1, nid4_1, [][]byte{leafHash1Corrupted}, root1}, false},
{"absence proof: nID of leafHashes do not match the queried nID, which is a valid case", absenceProof5_1, args{1, nid5_1, [][]byte{leafHash6_1}, root1}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hasher := NewNmtHasher(sha256.New(), tt.args.nIDSize, true)
got, _ := tt.proof.VerifyLeafHashes(hasher, true, tt.args.nID, tt.args.leaves, tt.args.root)
assert.Equal(t, tt.result, got)
})
}
}
func TestIsEmptyProof(t *testing.T) {
tests := []struct {
name string
proof Proof
expected bool
}{
{
name: "valid empty proof",
proof: Proof{
leafHash: nil,
nodes: nil,
start: 1,
end: 1,
},
expected: true,
},
{
name: "invalid empty proof - start != end",
proof: Proof{
leafHash: nil,
nodes: nil,
start: 0,
end: 1,
},
expected: false,
},
{
name: "invalid empty proof - non-empty nodes",
proof: Proof{
leafHash: nil,
nodes: [][]byte{{0x01}},
start: 1,
end: 1,
},
expected: false,
},
{
name: "invalid absence proof - non-empty leafHash",
proof: Proof{
leafHash: []byte{0x01},
nodes: nil,
start: 1,
end: 1,
},
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.proof.IsEmptyProof()
assert.Equal(t, test.expected, result)
})
}
}
// TestIsEmptyProofOverlapAbsenceProof ensures there is no overlap between empty proofs and absence proofs.
func TestIsEmptyProofOverlapAbsenceProof(t *testing.T) {
tests := []struct {
name string
proof Proof
}{
{
name: "valid empty proof",
proof: Proof{
leafHash: nil,
nodes: nil,
start: 1,
end: 1,
},
},
{
name: "valid absence proof",
proof: Proof{
leafHash: []byte{0x01, 0x02, 0x03},
nodes: nil,
start: 1,
end: 1,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.proof.IsEmptyProof()
absenceResult := test.proof.IsOfAbsence()
if result {
assert.False(t, absenceResult)
}
if absenceResult {
assert.False(t, result)
}
})
}
}
// TestVerifyNamespace_ShortAbsenceProof_Valid checks whether VerifyNamespace
// can correctly verify short namespace absence proofs
func TestVerifyNamespace_ShortAbsenceProof_Valid(t *testing.T) {
// create a Merkle tree with 8 leaves
tree := exampleNMT(1, true, 1, 2, 3, 4, 6, 7, 8, 9)
qNS := []byte{5} // does not belong to the tree
root, err := tree.Root()
assert.NoError(t, err)
// In the following illustration, nodes are suffixed with the range
// of leaves they cover, with the upper bound being non-inclusive.
// For example, Node3_4 denotes a node that covers the 3rd leaf (excluding the 4th leaf),
// while Node4_6 represents the node that covers the 4th and 5th leaves.
//
// Node0_8 Tree Root
// / \
// / \
// Node0_4 Node4_8 Non-Leaf Node
// / \ / \
// / \ / \
// Node0_2 Node2_4 Node4_6 Node6_8 Non-Leaf Node
// / \ / \ / \ / \
// Node0_1 Node1_2 Node2_3 Node3_4 Node4_5 Node5_6 Node6_7 Node7_8 Leaf Hash
// 1 2 3 4 6 7 8 9 Leaf namespace
// 0 1 2 3 4 5 6 7 Leaf index
// nodes needed for the full absence proof of qNS
Node4_5 := tree.leafHashes[4]
Node5_6 := tree.leafHashes[5]
Node6_8, err := tree.computeRoot(6, 8)
assert.NoError(t, err)
Node0_4, err := tree.computeRoot(0, 4)
assert.NoError(t, err)
// nodes needed for the short absence proof of qNS; the proof of inclusion
// of the parent of Node4_5
Node4_6, err := tree.computeRoot(4, 6)
assert.NoError(t, err)
// nodes needed for another short absence parent of qNS; the proof of
// inclusion of the grandparent of Node4_5
Node4_8, err := tree.computeRoot(4, 8)
assert.NoError(t, err)
tests := []struct {
name string
qNID []byte
leafHash []byte
nodes [][]byte
start int
end int
}{
{
name: "valid full absence proof",
qNID: qNS,
leafHash: Node4_5,
nodes: [][]byte{Node0_4, Node5_6, Node6_8},
start: 4, // the index position of leafHash at its respective level
end: 5,
},
{
name: "valid short absence proof: one level higher",
qNID: qNS,
leafHash: Node4_6,
nodes: [][]byte{Node0_4, Node6_8},
start: 2, // the index position of leafHash at its respective level
end: 3,
},
{
name: "valid short absence proof: two levels higher",
qNID: qNS,
leafHash: Node4_8,
nodes: [][]byte{Node0_4},
start: 1, // the index position of leafHash at its respective level
end: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proof := Proof{
leafHash: tt.leafHash,
nodes: tt.nodes,
start: tt.start,
end: tt.end,
}
res := proof.VerifyNamespace(sha256.New(), qNS, nil, root)
assert.True(t, res)
})
}
}
// TestVerifyNamespace_ShortAbsenceProof_Invalid checks whether VerifyNamespace rejects invalid short absence proofs.
func TestVerifyNamespace_ShortAbsenceProof_Invalid(t *testing.T) {
// create a Merkle tree with 8 leaves
tree := exampleNMT(1, true, 1, 2, 3, 4, 6, 8, 8, 8)
qNS := []byte{7} // does not belong to the tree
root, err := tree.Root()
assert.NoError(t, err)
// In the following illustration, nodes are suffixed with the range
// of leaves they cover, with the upper bound being non-inclusive.
// For example, Node3_4 denotes a node that covers the 3rd leaf (excluding the 4th leaf),
// while Node4_6 represents the node that covers the 4th and 5th leaves.
//
// Node0_8 Tree Root
// / \
// / \
// Node0_4 Node4_8 Non-Leaf Node
// / \ / \
// / \ / \
// Node0_2 Node2_4 Node4_6 Node6_8 Non-Leaf Node
// / \ / \ / \ / \
// Node0_1 Node1_2 Node2_3 Node3_4 Node4_5 Node5_6 Node6_7 Node7_8 Leaf Hash
// 1 2 3 4 6 8 8 8 Leaf namespace
// 0 1 2 3 4 5 6 7 Leaf index
// nodes needed for the full absence proof of qNS
Node5_6 := tree.leafHashes[5]
Node4_5 := tree.leafHashes[4]
Node6_8, err := tree.computeRoot(6, 8)
assert.NoError(t, err)