OSDN Git Service

Add BlockWitness (#54)
[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_HASH160,
151                 startVM: &virtualMachine{
152                         runLimit:  50000,
153                         dataStack: [][]byte{{1}},
154                 },
155                 wantVM: &virtualMachine{
156                         runLimit: 49916,
157                         dataStack: [][]byte{{
158                                 242, 145, 186, 80, 21, 223, 52, 140, 128, 133, 63, 165, 187, 15, 121, 70, 245, 201, 225, 179,
159                         }},
160                 },
161         }, {
162                 op: OP_HASH160,
163                 startVM: &virtualMachine{
164                         runLimit:  50000,
165                         dataStack: [][]byte{make([]byte, 65)},
166                 },
167                 wantVM: &virtualMachine{
168                         runLimit: 49916,
169                         dataStack: [][]byte{{
170                                 171, 60, 102, 205, 10, 63, 18, 180, 244, 250, 235, 84, 138, 85, 22, 7, 148, 250, 215, 6,
171                         }},
172                 },
173         }, {
174                 op: OP_SM3,
175                 startVM: &virtualMachine{
176                         runLimit:  50000,
177                         dataStack: [][]byte{{1}},
178                 },
179                 wantVM: &virtualMachine{
180                         runLimit: 49905,
181                         dataStack: [][]byte{{
182                                 121, 155, 113, 154, 192, 49, 252, 137, 198, 216, 146, 90, 72, 125, 173, 7,
183                                 48, 143, 131, 123, 122, 183, 187, 199, 206, 189, 58, 65, 24, 253, 47, 56,
184                         }},
185                 },
186         }, {
187                 op: OP_SM3,
188                 startVM: &virtualMachine{
189                         runLimit:  50000,
190                         dataStack: [][]byte{make([]byte, 65)},
191                 },
192                 wantVM: &virtualMachine{
193                         runLimit: 49968,
194                         dataStack: [][]byte{{
195                                 177, 247, 110, 45, 29, 65, 214, 241, 187, 59, 9, 192, 155, 130, 25, 218,
196                                 251, 173, 112, 13, 242, 72, 34, 32, 200, 146, 190, 65, 68, 90, 34, 255,
197                         }},
198                 },
199         }, {
200                 op: OP_CHECKSIG,
201                 startVM: &virtualMachine{
202                         runLimit: 50000,
203                         dataStack: [][]byte{
204                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
205                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
206                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
207                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
208                         },
209                 },
210                 wantVM: &virtualMachine{
211                         deferredCost: -143,
212                         runLimit:     48976,
213                         dataStack:    [][]byte{{1}},
214                 },
215         }, {
216                 op: OP_CHECKSIG,
217                 startVM: &virtualMachine{
218                         runLimit: 50000,
219                         dataStack: [][]byte{
220                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
221                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
222                                 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
223                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
224                         },
225                 },
226                 wantVM: &virtualMachine{
227                         deferredCost: -144,
228                         runLimit:     48976,
229                         dataStack:    [][]byte{{}},
230                 },
231         }, {
232                 op: OP_CHECKSIG,
233                 startVM: &virtualMachine{
234                         runLimit: 50000,
235                         dataStack: [][]byte{
236                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
237                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
238                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
239                                 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
240                         },
241                 },
242                 wantVM: &virtualMachine{
243                         deferredCost: -144,
244                         runLimit:     48976,
245                         dataStack:    [][]byte{{}},
246                 },
247         }, {
248                 op: OP_CHECKSIG,
249                 startVM: &virtualMachine{
250                         runLimit: 50000,
251                         dataStack: [][]byte{
252                                 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
253                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
254                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
255                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
256                         },
257                 },
258                 wantVM: &virtualMachine{
259                         deferredCost: -144,
260                         runLimit:     48976,
261                         dataStack:    [][]byte{{}},
262                 },
263         }, {
264                 op: OP_CHECKSIG,
265                 startVM: &virtualMachine{
266                         runLimit: 50000,
267                         dataStack: [][]byte{
268                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
269                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
270                                 mustDecodeHex("badbad"),
271                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
272                         },
273                 },
274                 wantErr: ErrBadValue,
275         }, {
276                 op: OP_CHECKSIG,
277                 startVM: &virtualMachine{
278                         runLimit: 0,
279                 },
280                 wantErr: ErrRunLimitExceeded,
281         }, {
282                 op: OP_CHECKMULTISIG,
283                 startVM: &virtualMachine{
284                         runLimit: 50000,
285                         dataStack: [][]byte{
286                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
287                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
288                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
289                                 {1},
290                                 {1},
291                         },
292                 },
293                 wantVM: &virtualMachine{
294                         deferredCost: -161,
295                         runLimit:     48976,
296                         dataStack:    [][]byte{{1}},
297                 },
298         }, {
299                 op: OP_CHECKMULTISIG,
300                 startVM: &virtualMachine{
301                         runLimit: 50000,
302                         dataStack: [][]byte{
303                                 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
304                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
305                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
306                                 {1},
307                                 {1},
308                         },
309                 },
310                 wantVM: &virtualMachine{
311                         deferredCost: -162,
312                         runLimit:     48976,
313                         dataStack:    [][]byte{{}},
314                 },
315         }, {
316                 op: OP_CHECKMULTISIG,
317                 startVM: &virtualMachine{
318                         runLimit:  50000,
319                         dataStack: [][]byte{},
320                 },
321                 wantErr: ErrDataStackUnderflow,
322         }, {
323                 op: OP_CHECKMULTISIG,
324                 startVM: &virtualMachine{
325                         runLimit: 50000,
326                         dataStack: [][]byte{
327                                 {1},
328                                 {1},
329                         },
330                 },
331                 wantErr: ErrDataStackUnderflow,
332         }, {
333                 op: OP_CHECKMULTISIG,
334                 startVM: &virtualMachine{
335                         runLimit: 50000,
336                         dataStack: [][]byte{
337                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
338                                 {1},
339                                 {1},
340                         },
341                 },
342                 wantErr: ErrDataStackUnderflow,
343         }, {
344                 op: OP_CHECKMULTISIG,
345                 startVM: &virtualMachine{
346                         runLimit: 50000,
347                         dataStack: [][]byte{
348                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
349                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
350                                 {1},
351                                 {1},
352                         },
353                 },
354                 wantErr: ErrDataStackUnderflow,
355         }, {
356                 op: OP_CHECKMULTISIG,
357                 startVM: &virtualMachine{
358                         runLimit: 50000,
359                         dataStack: [][]byte{
360                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
361                                 mustDecodeHex("badbad"),
362                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
363                                 {1},
364                                 {1},
365                         },
366                 },
367                 wantErr: ErrBadValue,
368         }, {
369                 op: OP_CHECKMULTISIG,
370                 startVM: &virtualMachine{
371                         runLimit: 50000,
372                         dataStack: [][]byte{
373                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
374                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
375                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
376                                 {1},
377                                 {0},
378                         },
379                 },
380                 wantErr: ErrBadValue,
381         }, {
382                 op: OP_CHECKMULTISIG,
383                 startVM: &virtualMachine{
384                         runLimit: 50000,
385                         dataStack: [][]byte{
386                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
387                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
388                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
389                                 {0},
390                                 {1},
391                         },
392                 },
393                 wantErr: ErrBadValue,
394         }, {
395                 op: OP_CHECKMULTISIG,
396                 startVM: &virtualMachine{
397                         runLimit: 50000,
398                         dataStack: [][]byte{
399                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
400                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
401                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
402                                 {2},
403                                 {1},
404                         },
405                 },
406                 wantErr: ErrBadValue,
407         }, {
408                 op: OP_CHECKMULTISIG,
409                 startVM: &virtualMachine{
410                         runLimit: 0,
411                         dataStack: [][]byte{
412                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
413                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
414                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
415                                 {1},
416                                 {1},
417                         },
418                 },
419                 wantErr: ErrRunLimitExceeded,
420         }, {
421                 op: OP_TXSIGHASH,
422                 startVM: &virtualMachine{
423                         runLimit: 50000,
424                         context: &Context{
425                                 TxSigHash: func() []byte {
426                                         return []byte{
427                                                 0x2f, 0x00, 0x3c, 0xdd, 0x64, 0x42, 0x7b, 0x5e,
428                                                 0xed, 0xd6, 0xcc, 0xb5, 0x85, 0x47, 0x02, 0x0b,
429                                                 0x02, 0xde, 0xf2, 0x2d, 0xc5, 0x99, 0x7e, 0x9d,
430                                                 0xa9, 0xac, 0x40, 0x49, 0xc3, 0x4a, 0x58, 0xd8,
431                                         }
432                                 },
433                         },
434                 },
435                 wantVM: &virtualMachine{
436                         runLimit: 49704,
437                         dataStack: [][]byte{{
438                                 47, 0, 60, 221, 100, 66, 123, 94,
439                                 237, 214, 204, 181, 133, 71, 2, 11,
440                                 2, 222, 242, 45, 197, 153, 126, 157,
441                                 169, 172, 64, 73, 195, 74, 88, 216,
442                         }},
443                 },
444         }, {
445                 op: OP_TXSIGHASH,
446                 startVM: &virtualMachine{
447                         runLimit: 0,
448                         context:  &Context{},
449                 },
450                 wantErr: ErrRunLimitExceeded,
451         }}
452
453         hashOps := []Op{OP_SHA256, OP_SHA3, OP_SM3}
454         for _, op := range hashOps {
455                 cases = append(cases, testStruct{
456                         op: op,
457                         startVM: &virtualMachine{
458                                 runLimit:  0,
459                                 dataStack: [][]byte{{1}},
460                         },
461                         wantErr: ErrRunLimitExceeded,
462                 })
463         }
464
465         for i, c := range cases {
466                 t.Logf("case %d", i)
467
468                 err := ops[c.op].fn(c.startVM)
469                 gotVM := c.startVM
470
471                 if err != c.wantErr {
472                         t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
473                         continue
474                 }
475                 if c.wantErr != nil {
476                         continue
477                 }
478
479                 // Hack: the context objects will otherwise compare unequal
480                 // sometimes (because of the function pointer within?) and we
481                 // don't care
482                 c.wantVM.context = gotVM.context
483
484                 if !testutil.DeepEqual(gotVM, c.wantVM) {
485                         t.Errorf("case %d, op %s: unexpected vm result\n\tgot:  %+v\n\twant: %+v\n", i, ops[c.op].name, gotVM, c.wantVM)
486                 }
487         }
488 }