verify_test.gno
10.65 Kb · 320 lines
1package qrc
2
3import (
4 "crypto/merkle"
5 "encoding/hex"
6 "testing"
7)
8
9func TestSizes(t *testing.T) {
10 for _, tc := range []struct {
11 name string
12 got, want int
13 }{
14 {"HashSize", HashSize, 32},
15 {"DigestBits", DigestBits, 256},
16 {"PublicKeySize", PublicKeySize, 16384},
17 {"SignatureSize", SignatureSize, 16384},
18 {"MinHeight", MinHeight, 6},
19 {"MaxHeight", MaxHeight, 16},
20 {"Leaves(MinHeight)", Leaves(MinHeight), 64},
21 {"Leaves(MaxHeight)", Leaves(MaxHeight), 65536},
22 {"ProofSize(MinHeight)", ProofSize(MinHeight), 16576},
23 {"ProofSize(MaxHeight)", ProofSize(MaxHeight), 16896},
24 } {
25 if tc.got != tc.want {
26 t.Errorf("%s = %d, want %d", tc.name, tc.got, tc.want)
27 }
28 }
29}
30
31// TestHashes pins down which functions the two names stand for. Keccak256 is
32// the original Keccak with its 0x01 padding, not the SHA3-256 of FIPS 202,
33// whose digest of the empty string starts with a7ffc6f8.
34func TestHashes(t *testing.T) {
35 for _, tc := range []struct {
36 h Hash
37 name string
38 empty string
39 abc string
40 }{
41 {
42 SHA256, "sha256",
43 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
44 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
45 },
46 {
47 Keccak256, "keccak256",
48 "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
49 "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
50 },
51 } {
52 if !tc.h.Valid() {
53 t.Errorf("%s is not valid", tc.name)
54 }
55 if tc.h.String() != tc.name {
56 t.Errorf("String() = %q, want %q", tc.h.String(), tc.name)
57 }
58 empty := tc.h.Sum(nil)
59 if got := hex.EncodeToString(empty[:]); got != tc.empty {
60 t.Errorf("%s of nothing = %s", tc.name, got)
61 }
62 abc := tc.h.Sum([]byte("abc"))
63 if got := hex.EncodeToString(abc[:]); got != tc.abc {
64 t.Errorf("%s of abc = %s", tc.name, got)
65 }
66 }
67
68 for _, h := range []Hash{0, 3, 255} {
69 if h.Valid() {
70 t.Errorf("hash %d is valid", h)
71 }
72 if h.String() != "unknown" {
73 t.Errorf("hash %d is called %q", h, h.String())
74 }
75 }
76}
77
78func TestVerify(t *testing.T) {
79 msg := []byte("bind this key to my account")
80 for _, h := range bothHashes {
81 for _, tc := range []struct {
82 height int
83 leaves []int
84 }{
85 {6, []int{0, 1, 2, 31, 32, 37, 62, 63}},
86 {7, []int{0, 64, 127}},
87 {11, []int{1365, 682, 2047}},
88 {16, []int{0, 21845, 43690, 65535}},
89 } {
90 keys := map[int]testKey{}
91 for _, leaf := range tc.leaves {
92 keys[leaf] = testKey{h, byte(leaf)}
93 }
94 tree := newTestTree(h, tc.height, keys)
95 for _, leaf := range tc.leaves {
96 proof := append(keys[leaf].sign(msg), tree.path(leaf)...)
97 if len(proof) != ProofSize(tc.height) {
98 t.Fatalf("%s, height %d: proof of %d bytes", h, tc.height, len(proof))
99 }
100 if err := Verify(h, tree.root(), tc.height, leaf, msg, proof); err != nil {
101 t.Errorf("%s, height %d, leaf %d: %v", h, tc.height, leaf, err)
102 }
103 }
104 }
105 }
106}
107
108// TestVerifyMessages signs messages of very different sizes: the empty one,
109// single bytes and a long one.
110func TestVerifyMessages(t *testing.T) {
111 long := make([]byte, 70000)
112 for i := range long {
113 long[i] = byte(i * 7)
114 }
115 for _, h := range bothHashes {
116 for i, msg := range [][]byte{nil, {0}, {0xff}, []byte("a"), []byte("b"), long} {
117 root, proof := testProof(h, MinHeight, 5+i, msg)
118 if err := Verify(h, root, MinHeight, 5+i, msg, proof); err != nil {
119 t.Errorf("%s, message %d: %v", h, i, err)
120 }
121 }
122 }
123}
124
125func TestVerifyRejectsChangedProof(t *testing.T) {
126 const height, leaf = MinHeight, 37
127 msg := []byte("bind this key to my account")
128 for _, h := range bothHashes {
129 root, proof := testProof(h, height, leaf, msg)
130 if err := Verify(h, root, height, leaf, msg, proof); err != nil {
131 t.Fatalf("%s: %v", h, err)
132 }
133
134 // One flipped bit anywhere breaks the proof: in a secret, in the hash
135 // of a secret that stays private, in a sibling of the path.
136 positions := []int{
137 0, 31, 32, 63, 64, // first pair and the start of the second
138 SignatureSize - 64, SignatureSize - 33, SignatureSize - 32, SignatureSize - 1,
139 SignatureSize, SignatureSize + 31, SignatureSize + 32, // path
140 len(proof) - 32, len(proof) - 1,
141 }
142 for at := 101; at < len(proof); at += 211 {
143 positions = append(positions, at)
144 }
145 for _, at := range positions {
146 for _, mask := range []byte{0x01, 0x80} {
147 changed := append([]byte(nil), proof...)
148 changed[at] ^= mask
149 if err := Verify(h, root, height, leaf, msg, changed); err != ErrMismatch {
150 t.Fatalf("%s: byte %d changed, got %v", h, at, err)
151 }
152 }
153 }
154
155 // Two pairs swapped, two siblings swapped.
156 swapped := append([]byte(nil), proof...)
157 copy(swapped[0:64], proof[64:128])
158 copy(swapped[64:128], proof[0:64])
159 if err := Verify(h, root, height, leaf, msg, swapped); err != ErrMismatch {
160 t.Errorf("%s: pairs swapped, got %v", h, err)
161 }
162 swapped = append([]byte(nil), proof...)
163 copy(swapped[SignatureSize:SignatureSize+32], proof[SignatureSize+32:SignatureSize+64])
164 copy(swapped[SignatureSize+32:SignatureSize+64], proof[SignatureSize:SignatureSize+32])
165 if err := Verify(h, root, height, leaf, msg, swapped); err != ErrMismatch {
166 t.Errorf("%s: siblings swapped, got %v", h, err)
167 }
168
169 // The halves of a pair swapped: a hash shown where the secret goes.
170 swapped = append([]byte(nil), proof...)
171 copy(swapped[0:32], proof[32:64])
172 copy(swapped[32:64], proof[0:32])
173 if err := Verify(h, root, height, leaf, msg, swapped); err != ErrMismatch {
174 t.Errorf("%s: halves of a pair swapped, got %v", h, err)
175 }
176
177 // The public key itself is not a signature of anything.
178 key := testKey{h, byte(leaf)}
179 tree := newTestTree(h, height, map[int]testKey{leaf: key})
180 forged := append(key.public(), tree.path(leaf)...)
181 if err := Verify(h, root, height, leaf, msg, forged); err != ErrMismatch {
182 t.Errorf("%s: public key shown as a signature, got %v", h, err)
183 }
184 }
185}
186
187func TestVerifyRejectsChangedRequest(t *testing.T) {
188 const height, leaf = MinHeight, 37
189 msg := []byte("bind this key to my account")
190 for _, h := range bothHashes {
191 root, proof := testProof(h, height, leaf, msg)
192
193 // Another message, however close.
194 for _, other := range [][]byte{
195 nil,
196 []byte("bind this key to my accounu"),
197 []byte("bind this key to my account "),
198 []byte("bind this key to my accoun"),
199 []byte("Bind this key to my account"),
200 } {
201 if err := Verify(h, root, height, leaf, other, proof); err != ErrMismatch {
202 t.Errorf("%s: message %q, got %v", h, other, err)
203 }
204 }
205
206 // Another leaf: the path only leads to the root from where it started.
207 for other := 0; other < Leaves(height); other++ {
208 if other == leaf {
209 continue
210 }
211 if err := Verify(h, root, height, other, msg, proof); err != ErrMismatch {
212 t.Fatalf("%s: leaf %d, got %v", h, other, err)
213 }
214 }
215
216 // Another root.
217 for at := 0; at < HashSize; at++ {
218 other := root
219 other[at] ^= 0x10
220 if err := Verify(h, other, height, leaf, msg, proof); err != ErrMismatch {
221 t.Fatalf("%s: root byte %d changed, got %v", h, at, err)
222 }
223 }
224 if err := Verify(h, [HashSize]byte{}, height, leaf, msg, proof); err != ErrMismatch {
225 t.Errorf("%s: zero root, got %v", h, err)
226 }
227
228 // The other hash function.
229 other := SHA256
230 if h == SHA256 {
231 other = Keccak256
232 }
233 if err := Verify(other, root, height, leaf, msg, proof); err != ErrMismatch {
234 t.Errorf("%s proof checked with %s, got %v", h, other, err)
235 }
236 }
237}
238
239func TestVerifyRejectsBadArguments(t *testing.T) {
240 const height, leaf = MinHeight, 37
241 msg := []byte("bind this key to my account")
242 root, proof := testProof(SHA256, height, leaf, msg)
243
244 for _, tc := range []struct {
245 name string
246 h Hash
247 height int
248 leaf int
249 proof []byte
250 want error
251 }{
252 {"valid", SHA256, height, leaf, proof, nil},
253 {"hash 0", 0, height, leaf, proof, ErrUnknownHash},
254 {"hash 3", 3, height, leaf, proof, ErrUnknownHash},
255 {"height 0", SHA256, 0, 0, proof, ErrHeight},
256 {"height 5", SHA256, MinHeight - 1, leaf % 32, proof[:ProofSize(MinHeight-1)], ErrHeight},
257 {"height 17", SHA256, MaxHeight + 1, leaf, make([]byte, ProofSize(MaxHeight+1)), ErrHeight},
258 {"height -1", SHA256, -1, leaf, proof, ErrHeight},
259 {"leaf -1", SHA256, height, -1, proof, ErrLeafIndex},
260 {"leaf 64", SHA256, height, 64, proof, ErrLeafIndex},
261 {"leaf 64+37", SHA256, height, 64 + leaf, proof, ErrLeafIndex},
262 {"no proof", SHA256, height, leaf, nil, ErrProofSize},
263 {"proof one byte short", SHA256, height, leaf, proof[:len(proof)-1], ErrProofSize},
264 {"proof one byte long", SHA256, height, leaf, append(append([]byte(nil), proof...), 0), ErrProofSize},
265 {"proof one sibling short", SHA256, height, leaf, proof[:len(proof)-HashSize], ErrProofSize},
266 {"signature alone", SHA256, height, leaf, proof[:SignatureSize], ErrProofSize},
267 // A proof cannot pass for one of a taller or shorter tree.
268 {"height 7, proof of 6", SHA256, height + 1, leaf, proof, ErrProofSize},
269 {"height 7, padded proof", SHA256, height + 1, leaf, append(append([]byte(nil), proof...), make([]byte, HashSize)...), ErrMismatch},
270 } {
271 if err := Verify(tc.h, root, tc.height, tc.leaf, msg, tc.proof); err != tc.want {
272 t.Errorf("%s: got %v, want %v", tc.name, err, tc.want)
273 }
274 }
275}
276
277// TestSHA256TreeIsTheChainTree checks the SHA-256 tree against crypto/merkle,
278// the native implementation of the Merkle tree gno.land itself uses for its
279// blocks: same leaf and node hashing, same root, same proof layout.
280func TestSHA256TreeIsTheChainTree(t *testing.T) {
281 msg := []byte("bind this key to my account")
282 for _, tc := range []struct {
283 height int
284 leaves []int
285 }{
286 {6, []int{0, 41, 63}},
287 {9, []int{1, 170, 341, 511}},
288 } {
289 keys := map[int]testKey{}
290 for _, leaf := range tc.leaves {
291 keys[leaf] = testKey{SHA256, byte(leaf)}
292 }
293 tree := newTestTree(SHA256, tc.height, keys)
294 root := tree.root()
295
296 // The root, computed by crypto/merkle from what the leaves stand for.
297 count := len(tree.items)
298 encoded := []byte{byte(count >> 24), byte(count >> 16), byte(count >> 8), byte(count)}
299 for _, item := range tree.items {
300 size := len(item)
301 encoded = append(encoded, byte(size>>24), byte(size>>16), byte(size>>8), byte(size))
302 encoded = append(encoded, item...)
303 }
304 if got := merkle.HashFromByteSlices(encoded); hex.EncodeToString(got) != hex.EncodeToString(root[:]) {
305 t.Fatalf("height %d: crypto/merkle root %x, tree root %x", tc.height, got, root)
306 }
307
308 // The path part of a proof is a crypto/merkle proof of the public key.
309 for _, leaf := range tc.leaves {
310 path := tree.path(leaf)
311 if !merkle.VerifySimpleProof(root[:], keys[leaf].public(), leaf, count, path) {
312 t.Errorf("height %d, leaf %d: crypto/merkle rejects the path", tc.height, leaf)
313 }
314 proof := append(keys[leaf].sign(msg), path...)
315 if err := Verify(SHA256, root, tc.height, leaf, msg, proof); err != nil {
316 t.Errorf("height %d, leaf %d: %v", tc.height, leaf, err)
317 }
318 }
319 }
320}