OSDN Git Service

add query
[bytom/bytom.git] / protocol / vm / crypto_test.go
1 package vm
2
3 import (
4         "testing"
5
6         "github.com/bytom/testutil"
7 )
8
9 var emptyBlockVMContext = &Context{
10         BlockHash: &[]uint8{0xf0, 0x85, 0x4f, 0x88, 0xb4, 0x89, 0x0, 0x99, 0x2f, 0xec, 0x40, 0x43, 0xf9, 0x65, 0xfa, 0x2, 0x9d, 0xeb, 0x8a, 0xd6, 0x93, 0xcf, 0x37, 0x11, 0xfe, 0x83, 0x9, 0xb3, 0x90, 0x6a, 0x5a, 0x86},
11 }
12
13 func TestCheckSig(t *testing.T) {
14         cases := []struct {
15                 prog    string
16                 ok, err bool
17         }{
18                 {
19                         // This one's OK
20                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
21                         true, false,
22                 },
23                 {
24                         // This one has a wrong-length signature
25                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc2 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
26                         false, false,
27                 },
28                 {
29                         // This one has a wrong-length message
30                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
31                         false, true,
32                 },
33                 {
34                         // This one has a wrong-length pubkey
35                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584 CHECKSIG",
36                         false, false,
37                 },
38                 {
39                         // This one has a wrong byte in the signature
40                         "0x00ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
41                         false, false,
42                 },
43                 {
44                         // This one has a wrong byte in the message
45                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0002030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
46                         false, false,
47                 },
48                 {
49                         // This one has a wrong byte in the pubkey
50                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x00ca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
51                         false, false,
52                 },
53                 {
54                         "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x040506 1 1 CHECKMULTISIG",
55                         false, false,
56                 },
57                 {
58                         "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0x040506 1 1 CHECKMULTISIG",
59                         false, true,
60                 },
61                 {
62                         "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 1 1 CHECKMULTISIG",
63                         true, false,
64                 },
65         }
66
67         for i, c := range cases {
68                 prog, err := Assemble(c.prog)
69                 if err != nil {
70                         t.Fatalf("case %d: %s", i, err)
71                 }
72                 vm := &virtualMachine{
73                         program:  prog,
74                         runLimit: 50000,
75                 }
76                 err = vm.run()
77                 if c.err {
78                         if err == nil {
79                                 t.Errorf("case %d: expected error, got ok result", i)
80                         }
81                 } else if c.ok {
82                         if err != nil {
83                                 t.Errorf("case %d: expected ok result, got error %s", i, err)
84                         }
85                 } else if !vm.falseResult() {
86                         t.Errorf("case %d: expected false VM result, got error %s", i, err)
87                 }
88         }
89 }
90
91 func TestCryptoOps(t *testing.T) {
92         type testStruct struct {
93                 op      Op
94                 startVM *virtualMachine
95                 wantErr error
96                 wantVM  *virtualMachine
97         }
98         cases := []testStruct{{
99                 op: OP_SHA256,
100                 startVM: &virtualMachine{
101                         runLimit:  50000,
102                         dataStack: [][]byte{{1}},
103                 },
104                 wantVM: &virtualMachine{
105                         runLimit: 49905,
106                         dataStack: [][]byte{{
107                                 75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227,
108                                 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154,
109                         }},
110                 },
111         }, {
112                 op: OP_SHA256,
113                 startVM: &virtualMachine{
114                         runLimit:  50000,
115                         dataStack: [][]byte{make([]byte, 65)},
116                 },
117                 wantVM: &virtualMachine{
118                         runLimit: 49968,
119                         dataStack: [][]byte{{
120                                 152, 206, 66, 222, 239, 81, 212, 2, 105, 213, 66, 245, 49, 75, 239, 44,
121                                 116, 104, 212, 1, 173, 93, 133, 22, 139, 250, 180, 192, 16, 143, 117, 247,
122                         }},
123                 },
124         }, {
125                 op: OP_SHA3,
126                 startVM: &virtualMachine{
127                         runLimit:  50000,
128                         dataStack: [][]byte{{1}},
129                 },
130                 wantVM: &virtualMachine{
131                         runLimit: 49905,
132                         dataStack: [][]byte{{
133                                 39, 103, 241, 92, 138, 242, 242, 199, 34, 93, 82, 115, 253, 214, 131, 237,
134                                 199, 20, 17, 10, 152, 125, 16, 84, 105, 124, 52, 138, 237, 78, 108, 199,
135                         }},
136                 },
137         }, {
138                 op: OP_SHA3,
139                 startVM: &virtualMachine{
140                         runLimit:  50000,
141                         dataStack: [][]byte{make([]byte, 65)},
142                 },
143                 wantVM: &virtualMachine{
144                         runLimit: 49968,
145                         dataStack: [][]byte{{
146                                 65, 106, 167, 181, 192, 224, 101, 48, 102, 167, 198, 77, 189, 208, 0, 157,
147                                 190, 132, 56, 97, 81, 254, 3, 159, 217, 66, 250, 162, 219, 97, 114, 235,
148                         }},
149                 },
150         }, {
151                 op: OP_CHECKSIG,
152                 startVM: &virtualMachine{
153                         runLimit: 50000,
154                         dataStack: [][]byte{
155                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
156                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
157                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
158                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
159                         },
160                 },
161                 wantVM: &virtualMachine{
162                         deferredCost: -143,
163                         runLimit:     48976,
164                         dataStack:    [][]byte{{1}},
165                 },
166         }, {
167                 op: OP_CHECKSIG,
168                 startVM: &virtualMachine{
169                         runLimit: 50000,
170                         dataStack: [][]byte{
171                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
172                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
173                                 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
174                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
175                         },
176                 },
177                 wantVM: &virtualMachine{
178                         deferredCost: -144,
179                         runLimit:     48976,
180                         dataStack:    [][]byte{{}},
181                 },
182         }, {
183                 op: OP_CHECKSIG,
184                 startVM: &virtualMachine{
185                         runLimit: 50000,
186                         dataStack: [][]byte{
187                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
188                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
189                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
190                                 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
191                         },
192                 },
193                 wantVM: &virtualMachine{
194                         deferredCost: -144,
195                         runLimit:     48976,
196                         dataStack:    [][]byte{{}},
197                 },
198         }, {
199                 op: OP_CHECKSIG,
200                 startVM: &virtualMachine{
201                         runLimit: 50000,
202                         dataStack: [][]byte{
203                                 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
204                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
205                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
206                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
207                         },
208                 },
209                 wantVM: &virtualMachine{
210                         deferredCost: -144,
211                         runLimit:     48976,
212                         dataStack:    [][]byte{{}},
213                 },
214         }, {
215                 op: OP_CHECKSIG,
216                 startVM: &virtualMachine{
217                         runLimit: 50000,
218                         dataStack: [][]byte{
219                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
220                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
221                                 mustDecodeHex("badbad"),
222                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
223                         },
224                 },
225                 wantErr: ErrBadValue,
226         }, {
227                 op: OP_CHECKSIG,
228                 startVM: &virtualMachine{
229                         runLimit: 0,
230                 },
231                 wantErr: ErrRunLimitExceeded,
232         }, {
233                 op: OP_CHECKMULTISIG,
234                 startVM: &virtualMachine{
235                         runLimit: 50000,
236                         dataStack: [][]byte{
237                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
238                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
239                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
240                                 {1},
241                                 {1},
242                         },
243                 },
244                 wantVM: &virtualMachine{
245                         deferredCost: -161,
246                         runLimit:     48976,
247                         dataStack:    [][]byte{{1}},
248                 },
249         }, {
250                 op: OP_CHECKMULTISIG,
251                 startVM: &virtualMachine{
252                         runLimit: 50000,
253                         dataStack: [][]byte{
254                                 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
255                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
256                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
257                                 {1},
258                                 {1},
259                         },
260                 },
261                 wantVM: &virtualMachine{
262                         deferredCost: -162,
263                         runLimit:     48976,
264                         dataStack:    [][]byte{{}},
265                 },
266         }, {
267                 op: OP_CHECKMULTISIG,
268                 startVM: &virtualMachine{
269                         runLimit:  50000,
270                         dataStack: [][]byte{},
271                 },
272                 wantErr: ErrDataStackUnderflow,
273         }, {
274                 op: OP_CHECKMULTISIG,
275                 startVM: &virtualMachine{
276                         runLimit: 50000,
277                         dataStack: [][]byte{
278                                 {1},
279                                 {1},
280                         },
281                 },
282                 wantErr: ErrDataStackUnderflow,
283         }, {
284                 op: OP_CHECKMULTISIG,
285                 startVM: &virtualMachine{
286                         runLimit: 50000,
287                         dataStack: [][]byte{
288                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
289                                 {1},
290                                 {1},
291                         },
292                 },
293                 wantErr: ErrDataStackUnderflow,
294         }, {
295                 op: OP_CHECKMULTISIG,
296                 startVM: &virtualMachine{
297                         runLimit: 50000,
298                         dataStack: [][]byte{
299                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
300                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
301                                 {1},
302                                 {1},
303                         },
304                 },
305                 wantErr: ErrDataStackUnderflow,
306         }, {
307                 op: OP_CHECKMULTISIG,
308                 startVM: &virtualMachine{
309                         runLimit: 50000,
310                         dataStack: [][]byte{
311                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
312                                 mustDecodeHex("badbad"),
313                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
314                                 {1},
315                                 {1},
316                         },
317                 },
318                 wantErr: ErrBadValue,
319         }, {
320                 op: OP_CHECKMULTISIG,
321                 startVM: &virtualMachine{
322                         runLimit: 50000,
323                         dataStack: [][]byte{
324                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
325                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
326                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
327                                 {1},
328                                 {0},
329                         },
330                 },
331                 wantErr: ErrBadValue,
332         }, {
333                 op: OP_CHECKMULTISIG,
334                 startVM: &virtualMachine{
335                         runLimit: 50000,
336                         dataStack: [][]byte{
337                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
338                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
339                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
340                                 {0},
341                                 {1},
342                         },
343                 },
344                 wantErr: ErrBadValue,
345         }, {
346                 op: OP_CHECKMULTISIG,
347                 startVM: &virtualMachine{
348                         runLimit: 50000,
349                         dataStack: [][]byte{
350                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
351                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
352                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
353                                 {2},
354                                 {1},
355                         },
356                 },
357                 wantErr: ErrBadValue,
358         }, {
359                 op: OP_CHECKMULTISIG,
360                 startVM: &virtualMachine{
361                         runLimit: 0,
362                         dataStack: [][]byte{
363                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
364                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
365                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
366                                 {1},
367                                 {1},
368                         },
369                 },
370                 wantErr: ErrRunLimitExceeded,
371         }, {
372                 op: OP_TXSIGHASH,
373                 startVM: &virtualMachine{
374                         runLimit: 50000,
375                         context: &Context{
376                                 TxSigHash: func() []byte {
377                                         return []byte{
378                                                 0x2f, 0x00, 0x3c, 0xdd, 0x64, 0x42, 0x7b, 0x5e,
379                                                 0xed, 0xd6, 0xcc, 0xb5, 0x85, 0x47, 0x02, 0x0b,
380                                                 0x02, 0xde, 0xf2, 0x2d, 0xc5, 0x99, 0x7e, 0x9d,
381                                                 0xa9, 0xac, 0x40, 0x49, 0xc3, 0x4a, 0x58, 0xd8,
382                                         }
383                                 },
384                         },
385                 },
386                 wantVM: &virtualMachine{
387                         runLimit: 49704,
388                         dataStack: [][]byte{{
389                                 47, 0, 60, 221, 100, 66, 123, 94,
390                                 237, 214, 204, 181, 133, 71, 2, 11,
391                                 2, 222, 242, 45, 197, 153, 126, 157,
392                                 169, 172, 64, 73, 195, 74, 88, 216,
393                         }},
394                 },
395         }, {
396                 op: OP_TXSIGHASH,
397                 startVM: &virtualMachine{
398                         runLimit: 0,
399                         context:  &Context{},
400                 },
401                 wantErr: ErrRunLimitExceeded,
402         }, {
403                 op: OP_BLOCKHASH,
404                 startVM: &virtualMachine{
405                         runLimit: 50000,
406                         context:  emptyBlockVMContext,
407                 },
408                 wantVM: &virtualMachine{
409                         runLimit: 49959,
410                         dataStack: [][]byte{{
411                                 240, 133, 79, 136, 180, 137, 0, 153,
412                                 47, 236, 64, 67, 249, 101, 250, 2,
413                                 157, 235, 138, 214, 147, 207, 55, 17,
414                                 254, 131, 9, 179, 144, 106, 90, 134,
415                         }},
416                         context: emptyBlockVMContext,
417                 },
418         }, {
419                 op: OP_BLOCKHASH,
420                 startVM: &virtualMachine{
421                         runLimit: 0,
422                         context:  emptyBlockVMContext,
423                 },
424                 wantErr: ErrRunLimitExceeded,
425         }}
426
427         hashOps := []Op{OP_SHA256, OP_SHA3}
428         for _, op := range hashOps {
429                 cases = append(cases, testStruct{
430                         op: op,
431                         startVM: &virtualMachine{
432                                 runLimit:  0,
433                                 dataStack: [][]byte{{1}},
434                         },
435                         wantErr: ErrRunLimitExceeded,
436                 })
437         }
438
439         for i, c := range cases {
440                 t.Logf("case %d", i)
441
442                 err := ops[c.op].fn(c.startVM)
443                 gotVM := c.startVM
444
445                 if err != c.wantErr {
446                         t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
447                         continue
448                 }
449                 if c.wantErr != nil {
450                         continue
451                 }
452
453                 // Hack: the context objects will otherwise compare unequal
454                 // sometimes (because of the function pointer within?) and we
455                 // don't care
456                 c.wantVM.context = gotVM.context
457
458                 if !testutil.DeepEqual(gotVM, c.wantVM) {
459                         t.Errorf("case %d, op %s: unexpected vm result\n\tgot:  %+v\n\twant: %+v\n", i, ops[c.op].name, gotVM, c.wantVM)
460                 }
461         }
462 }