forked from cosmos/ics23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectors_test.go
111 lines (105 loc) · 2.93 KB
/
vectors_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
package ics23
import (
"bytes"
"fmt"
"testing"
)
func TestVectors(t *testing.T) {
cases := VectorsTestData()
for _, tc := range cases {
tc := tc
name := fmt.Sprintf("%s/%s", tc.Dir, tc.Filename)
t.Run(name, func(t *testing.T) {
proof, ref := LoadFile(t, tc.Dir, tc.Filename)
// Test Calculate method
calculatedRoot, err := proof.Calculate()
if err != nil {
t.Fatal("proof.Calculate() returned error")
}
if !bytes.Equal(ref.RootHash, calculatedRoot) {
t.Fatalf("calculated root: %X did not match expected root: %X", calculatedRoot, ref.RootHash)
}
// Test Verify method
if ref.Value == nil {
// non-existence
valid := VerifyNonMembership(tc.Spec, ref.RootHash, proof, ref.Key)
if !valid {
t.Fatal("Invalid proof")
}
} else {
valid := VerifyMembership(tc.Spec, ref.RootHash, proof, ref.Key, ref.Value)
if !valid {
t.Fatal("Invalid proof")
}
}
})
}
}
func TestBatchVectors(t *testing.T) {
cases := BatchVectorsTestData(t)
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
// try one proof
if tc.Ref.Value == nil {
// non-existence
valid := VerifyNonMembership(tc.Spec, tc.Ref.RootHash, tc.Proof, tc.Ref.Key)
if valid == tc.Invalid {
t.Logf("name: %+v", name)
t.Logf("ref: %+v", tc.Ref)
t.Logf("spec: %+v", tc.Spec)
t.Errorf("Expected proof validity: %t", !tc.Invalid)
}
keys := [][]byte{tc.Ref.Key}
valid = BatchVerifyNonMembership(tc.Spec, tc.Ref.RootHash, tc.Proof, keys)
if valid == tc.Invalid {
t.Errorf("Expected batch proof validity: %t", !tc.Invalid)
}
} else {
valid := VerifyMembership(tc.Spec, tc.Ref.RootHash, tc.Proof, tc.Ref.Key, tc.Ref.Value)
if valid == tc.Invalid {
t.Errorf("Expected proof validity: %t", !tc.Invalid)
}
items := make(map[string][]byte)
items[string(tc.Ref.Key)] = tc.Ref.Value
valid = BatchVerifyMembership(tc.Spec, tc.Ref.RootHash, tc.Proof, items)
if valid == tc.Invalid {
t.Errorf("Expected batch proof validity: %t", !tc.Invalid)
}
}
})
}
}
func TestDecompressBatchVectors(t *testing.T) {
cases := DecompressBatchVectorsTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
small, err := tc.Marshal()
if err != nil {
t.Fatalf("Marshal batch %v", err)
}
decomp := Decompress(tc)
if decomp == tc {
t.Fatalf("Decompression is a no-op")
}
big, err := decomp.Marshal()
if err != nil {
t.Fatalf("Marshal batch %v", err)
}
if len(small) >= len(big) {
t.Fatalf("Compression doesn't reduce size")
}
restore := Compress(tc)
resmall, err := restore.Marshal()
if err != nil {
t.Fatalf("Marshal batch %v", err)
}
if len(resmall) != len(small) {
t.Fatalf("Decompressed len %d, original len %d", len(resmall), len(small))
}
if !bytes.Equal(resmall, small) {
t.Fatal("Decompressed batch proof differs from original")
}
})
}
}