OSDN Git Service

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