-
Notifications
You must be signed in to change notification settings - Fork 4
/
proofs_test.go
211 lines (184 loc) · 6.42 KB
/
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
package smt
import (
"crypto/rand"
"crypto/sha256"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt/kvstore/simplemap"
)
func TestSparseMerkleProof_Marshal(t *testing.T) {
trie := setupTrie(t)
proof, err := trie.Prove([]byte("key"))
require.NoError(t, err)
bz, err := proof.Marshal()
require.NoError(t, err)
require.NotNil(t, bz)
require.Greater(t, len(bz), 0)
proof2, err := trie.Prove([]byte("key2"))
require.NoError(t, err)
bz2, err := proof2.Marshal()
require.NoError(t, err)
require.NotNil(t, bz2)
require.Greater(t, len(bz2), 0)
require.NotEqual(t, bz, bz2)
proof3 := randomizeProof(proof)
bz3, err := proof3.Marshal()
require.NoError(t, err)
require.NotNil(t, bz3)
require.Greater(t, len(bz3), 0)
require.NotEqual(t, bz, bz3)
}
func TestSparseMerkleProof_Unmarshal(t *testing.T) {
trie := setupTrie(t)
proof, err := trie.Prove([]byte("key"))
require.NoError(t, err)
bz, err := proof.Marshal()
require.NoError(t, err)
require.NotNil(t, bz)
require.Greater(t, len(bz), 0)
uproof := new(SparseMerkleProof)
require.NoError(t, uproof.Unmarshal(bz))
require.Equal(t, proof, uproof)
proof2, err := trie.Prove([]byte("key2"))
require.NoError(t, err)
bz2, err := proof2.Marshal()
require.NoError(t, err)
require.NotNil(t, bz2)
require.Greater(t, len(bz2), 0)
uproof2 := new(SparseMerkleProof)
require.NoError(t, uproof2.Unmarshal(bz2))
require.Equal(t, proof2, uproof2)
proof3 := randomizeProof(proof)
bz3, err := proof3.Marshal()
require.NoError(t, err)
require.NotNil(t, bz3)
require.Greater(t, len(bz3), 0)
uproof3 := new(SparseMerkleProof)
require.NoError(t, uproof3.Unmarshal(bz3))
require.Equal(t, proof3, uproof3)
}
func TestSparseCompactMerkleProof_Marshal(t *testing.T) {
trie := setupTrie(t)
proof, err := trie.Prove([]byte("key"))
require.NoError(t, err)
compactProof, err := CompactProof(proof, trie.Spec())
require.NoError(t, err)
bz, err := compactProof.Marshal()
require.NoError(t, err)
require.NotNil(t, bz)
require.Greater(t, len(bz), 0)
proof2, err := trie.Prove([]byte("key2"))
require.NoError(t, err)
compactProof2, err := CompactProof(proof2, trie.Spec())
require.NoError(t, err)
bz2, err := compactProof2.Marshal()
require.NoError(t, err)
require.NotNil(t, bz2)
require.Greater(t, len(bz2), 0)
require.NotEqual(t, bz, bz2)
proof3 := randomizeProof(proof)
compactProof3, err := CompactProof(proof3, trie.Spec())
require.NoError(t, err)
bz3, err := compactProof3.Marshal()
require.NoError(t, err)
require.NotNil(t, bz3)
require.Greater(t, len(bz3), 0)
require.NotEqual(t, bz, bz3)
}
func TestSparseCompactMerkleProof_Unmarshal(t *testing.T) {
trie := setupTrie(t)
proof, err := trie.Prove([]byte("key"))
require.NoError(t, err)
compactProof, err := CompactProof(proof, trie.Spec())
require.NoError(t, err)
bz, err := compactProof.Marshal()
require.NoError(t, err)
require.NotNil(t, bz)
require.Greater(t, len(bz), 0)
uCproof := new(SparseCompactMerkleProof)
require.NoError(t, uCproof.Unmarshal(bz))
require.Equal(t, compactProof, uCproof)
uproof, err := DecompactProof(uCproof, trie.Spec())
require.NoError(t, err)
require.Equal(t, proof, uproof)
proof2, err := trie.Prove([]byte("key2"))
require.NoError(t, err)
compactProof2, err := CompactProof(proof2, trie.Spec())
require.NoError(t, err)
bz2, err := compactProof2.Marshal()
require.NoError(t, err)
require.NotNil(t, bz2)
require.Greater(t, len(bz2), 0)
uCproof2 := new(SparseCompactMerkleProof)
require.NoError(t, uCproof2.Unmarshal(bz2))
require.Equal(t, compactProof2, uCproof2)
uproof2, err := DecompactProof(uCproof2, trie.Spec())
require.NoError(t, err)
require.Equal(t, proof2, uproof2)
proof3 := randomizeProof(proof)
compactProof3, err := CompactProof(proof3, trie.Spec())
require.NoError(t, err)
bz3, err := compactProof3.Marshal()
require.NoError(t, err)
require.NotNil(t, bz3)
require.Greater(t, len(bz3), 0)
uCproof3 := new(SparseCompactMerkleProof)
require.NoError(t, uCproof3.Unmarshal(bz3))
require.Equal(t, compactProof3, uCproof3)
uproof3, err := DecompactProof(uCproof3, trie.Spec())
require.NoError(t, err)
require.Equal(t, proof3, uproof3)
}
func setupTrie(t *testing.T) *SMT {
t.Helper()
db := simplemap.NewSimpleMap()
trie := NewSparseMerkleTrie(db, sha256.New())
require.NoError(t, trie.Update([]byte("key"), []byte("value")))
require.NoError(t, trie.Update([]byte("key2"), []byte("value2")))
require.NoError(t, trie.Update([]byte("key3"), []byte("value3")))
return trie
}
func randomizeProof(proof *SparseMerkleProof) *SparseMerkleProof {
sideNodes := make([][]byte, len(proof.SideNodes))
for i := range sideNodes {
sideNodes[i] = make([]byte, len(proof.SideNodes[i]))
rand.Read(sideNodes[i]) // nolint: errcheck
}
return &SparseMerkleProof{
SideNodes: sideNodes,
NonMembershipLeafData: proof.NonMembershipLeafData,
}
}
func randomizeSumProof(proof *SparseMerkleProof) *SparseMerkleProof {
sideNodes := make([][]byte, len(proof.SideNodes))
for i := range sideNodes {
data := proof.SideNodes[i]
firstSumByteIdx, firstCountByteIdx := getFirstMetaByteIdx(data)
sideNodes[i] = make([]byte, len(data)-sumSizeBytes-countSizeBytes)
rand.Read(sideNodes[i]) // nolint: errcheck
sideNodes[i] = append(sideNodes[i], data[firstSumByteIdx:firstCountByteIdx]...)
sideNodes[i] = append(sideNodes[i], data[firstCountByteIdx:]...)
}
return &SparseMerkleProof{
SideNodes: sideNodes,
NonMembershipLeafData: proof.NonMembershipLeafData,
}
}
// Check that a non-compact proof is equivalent to the proof returned when it is compacted and de-compacted.
func checkCompactEquivalence(t *testing.T, proof *SparseMerkleProof, base *TrieSpec) {
t.Helper()
compactedProof, err := CompactProof(proof, base)
require.NoErrorf(t, err, "failed to compact proof: %v", err)
decompactedProof, err := DecompactProof(compactedProof, base)
require.NoErrorf(t, err, "failed to decompact proof: %v", err)
require.Equal(t, proof, decompactedProof)
}
// Check that a non-compact proof is equivalent to the proof returned when it is compacted and de-compacted.
func checkClosestCompactEquivalence(t *testing.T, proof *SparseMerkleClosestProof, spec *TrieSpec) {
t.Helper()
compactedProof, err := CompactClosestProof(proof, spec)
require.NoErrorf(t, err, "failed to compact proof: %v", err)
decompactedProof, err := DecompactClosestProof(compactedProof, spec)
require.NoErrorf(t, err, "failed to decompact proof: %v", err)
require.Equal(t, proof, decompactedProof)
}