OSDN Git Service

Merge pull request #935 from Bytom/dev
[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 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         type testStruct struct {
89                 op      Op
90                 startVM *virtualMachine
91                 wantErr error
92                 wantVM  *virtualMachine
93         }
94         cases := []testStruct{{
95                 op: OP_SHA256,
96                 startVM: &virtualMachine{
97                         runLimit:  50000,
98                         dataStack: [][]byte{{1}},
99                 },
100                 wantVM: &virtualMachine{
101                         runLimit: 49905,
102                         dataStack: [][]byte{{
103                                 75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227,
104                                 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154,
105                         }},
106                 },
107         }, {
108                 op: OP_SHA256,
109                 startVM: &virtualMachine{
110                         runLimit:  50000,
111                         dataStack: [][]byte{make([]byte, 65)},
112                 },
113                 wantVM: &virtualMachine{
114                         runLimit: 49968,
115                         dataStack: [][]byte{{
116                                 152, 206, 66, 222, 239, 81, 212, 2, 105, 213, 66, 245, 49, 75, 239, 44,
117                                 116, 104, 212, 1, 173, 93, 133, 22, 139, 250, 180, 192, 16, 143, 117, 247,
118                         }},
119                 },
120         }, {
121                 op: OP_SHA3,
122                 startVM: &virtualMachine{
123                         runLimit:  50000,
124                         dataStack: [][]byte{{1}},
125                 },
126                 wantVM: &virtualMachine{
127                         runLimit: 49905,
128                         dataStack: [][]byte{{
129                                 39, 103, 241, 92, 138, 242, 242, 199, 34, 93, 82, 115, 253, 214, 131, 237,
130                                 199, 20, 17, 10, 152, 125, 16, 84, 105, 124, 52, 138, 237, 78, 108, 199,
131                         }},
132                 },
133         }, {
134                 op: OP_SHA3,
135                 startVM: &virtualMachine{
136                         runLimit:  50000,
137                         dataStack: [][]byte{make([]byte, 65)},
138                 },
139                 wantVM: &virtualMachine{
140                         runLimit: 49968,
141                         dataStack: [][]byte{{
142                                 65, 106, 167, 181, 192, 224, 101, 48, 102, 167, 198, 77, 189, 208, 0, 157,
143                                 190, 132, 56, 97, 81, 254, 3, 159, 217, 66, 250, 162, 219, 97, 114, 235,
144                         }},
145                 },
146         }, {
147                 op: OP_CHECKSIG,
148                 startVM: &virtualMachine{
149                         runLimit: 50000,
150                         dataStack: [][]byte{
151                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
152                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
153                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
154                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
155                         },
156                 },
157                 wantVM: &virtualMachine{
158                         deferredCost: -143,
159                         runLimit:     48976,
160                         dataStack:    [][]byte{{1}},
161                 },
162         }, {
163                 op: OP_CHECKSIG,
164                 startVM: &virtualMachine{
165                         runLimit: 50000,
166                         dataStack: [][]byte{
167                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
168                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
169                                 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
170                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
171                         },
172                 },
173                 wantVM: &virtualMachine{
174                         deferredCost: -144,
175                         runLimit:     48976,
176                         dataStack:    [][]byte{{}},
177                 },
178         }, {
179                 op: OP_CHECKSIG,
180                 startVM: &virtualMachine{
181                         runLimit: 50000,
182                         dataStack: [][]byte{
183                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
184                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
185                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
186                                 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
187                         },
188                 },
189                 wantVM: &virtualMachine{
190                         deferredCost: -144,
191                         runLimit:     48976,
192                         dataStack:    [][]byte{{}},
193                 },
194         }, {
195                 op: OP_CHECKSIG,
196                 startVM: &virtualMachine{
197                         runLimit: 50000,
198                         dataStack: [][]byte{
199                                 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
200                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
201                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
202                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
203                         },
204                 },
205                 wantVM: &virtualMachine{
206                         deferredCost: -144,
207                         runLimit:     48976,
208                         dataStack:    [][]byte{{}},
209                 },
210         }, {
211                 op: OP_CHECKSIG,
212                 startVM: &virtualMachine{
213                         runLimit: 50000,
214                         dataStack: [][]byte{
215                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
216                                         "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
217                                 mustDecodeHex("badbad"),
218                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
219                         },
220                 },
221                 wantErr: ErrBadValue,
222         }, {
223                 op: OP_CHECKSIG,
224                 startVM: &virtualMachine{
225                         runLimit: 0,
226                 },
227                 wantErr: ErrRunLimitExceeded,
228         }, {
229                 op: OP_CHECKMULTISIG,
230                 startVM: &virtualMachine{
231                         runLimit: 50000,
232                         dataStack: [][]byte{
233                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
234                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
235                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
236                                 {1},
237                                 {1},
238                         },
239                 },
240                 wantVM: &virtualMachine{
241                         deferredCost: -161,
242                         runLimit:     48976,
243                         dataStack:    [][]byte{{1}},
244                 },
245         }, {
246                 op: OP_CHECKMULTISIG,
247                 startVM: &virtualMachine{
248                         runLimit: 50000,
249                         dataStack: [][]byte{
250                                 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
251                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
252                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
253                                 {1},
254                                 {1},
255                         },
256                 },
257                 wantVM: &virtualMachine{
258                         deferredCost: -162,
259                         runLimit:     48976,
260                         dataStack:    [][]byte{{}},
261                 },
262         }, {
263                 op: OP_CHECKMULTISIG,
264                 startVM: &virtualMachine{
265                         runLimit:  50000,
266                         dataStack: [][]byte{},
267                 },
268                 wantErr: ErrDataStackUnderflow,
269         }, {
270                 op: OP_CHECKMULTISIG,
271                 startVM: &virtualMachine{
272                         runLimit: 50000,
273                         dataStack: [][]byte{
274                                 {1},
275                                 {1},
276                         },
277                 },
278                 wantErr: ErrDataStackUnderflow,
279         }, {
280                 op: OP_CHECKMULTISIG,
281                 startVM: &virtualMachine{
282                         runLimit: 50000,
283                         dataStack: [][]byte{
284                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
285                                 {1},
286                                 {1},
287                         },
288                 },
289                 wantErr: ErrDataStackUnderflow,
290         }, {
291                 op: OP_CHECKMULTISIG,
292                 startVM: &virtualMachine{
293                         runLimit: 50000,
294                         dataStack: [][]byte{
295                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
296                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
297                                 {1},
298                                 {1},
299                         },
300                 },
301                 wantErr: ErrDataStackUnderflow,
302         }, {
303                 op: OP_CHECKMULTISIG,
304                 startVM: &virtualMachine{
305                         runLimit: 50000,
306                         dataStack: [][]byte{
307                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
308                                 mustDecodeHex("badbad"),
309                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
310                                 {1},
311                                 {1},
312                         },
313                 },
314                 wantErr: ErrBadValue,
315         }, {
316                 op: OP_CHECKMULTISIG,
317                 startVM: &virtualMachine{
318                         runLimit: 50000,
319                         dataStack: [][]byte{
320                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
321                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
322                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
323                                 {1},
324                                 {0},
325                         },
326                 },
327                 wantErr: ErrBadValue,
328         }, {
329                 op: OP_CHECKMULTISIG,
330                 startVM: &virtualMachine{
331                         runLimit: 50000,
332                         dataStack: [][]byte{
333                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
334                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
335                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
336                                 {0},
337                                 {1},
338                         },
339                 },
340                 wantErr: ErrBadValue,
341         }, {
342                 op: OP_CHECKMULTISIG,
343                 startVM: &virtualMachine{
344                         runLimit: 50000,
345                         dataStack: [][]byte{
346                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
347                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
348                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
349                                 {2},
350                                 {1},
351                         },
352                 },
353                 wantErr: ErrBadValue,
354         }, {
355                 op: OP_CHECKMULTISIG,
356                 startVM: &virtualMachine{
357                         runLimit: 0,
358                         dataStack: [][]byte{
359                                 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
360                                 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
361                                 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
362                                 {1},
363                                 {1},
364                         },
365                 },
366                 wantErr: ErrRunLimitExceeded,
367         }, {
368                 op: OP_TXSIGHASH,
369                 startVM: &virtualMachine{
370                         runLimit: 50000,
371                         context: &Context{
372                                 TxSigHash: func() []byte {
373                                         return []byte{
374                                                 0x2f, 0x00, 0x3c, 0xdd, 0x64, 0x42, 0x7b, 0x5e,
375                                                 0xed, 0xd6, 0xcc, 0xb5, 0x85, 0x47, 0x02, 0x0b,
376                                                 0x02, 0xde, 0xf2, 0x2d, 0xc5, 0x99, 0x7e, 0x9d,
377                                                 0xa9, 0xac, 0x40, 0x49, 0xc3, 0x4a, 0x58, 0xd8,
378                                         }
379                                 },
380                         },
381                 },
382                 wantVM: &virtualMachine{
383                         runLimit: 49704,
384                         dataStack: [][]byte{{
385                                 47, 0, 60, 221, 100, 66, 123, 94,
386                                 237, 214, 204, 181, 133, 71, 2, 11,
387                                 2, 222, 242, 45, 197, 153, 126, 157,
388                                 169, 172, 64, 73, 195, 74, 88, 216,
389                         }},
390                 },
391         }, {
392                 op: OP_TXSIGHASH,
393                 startVM: &virtualMachine{
394                         runLimit: 0,
395                         context:  &Context{},
396                 },
397                 wantErr: ErrRunLimitExceeded,
398         }}
399
400         hashOps := []Op{OP_SHA256, OP_SHA3}
401         for _, op := range hashOps {
402                 cases = append(cases, testStruct{
403                         op: op,
404                         startVM: &virtualMachine{
405                                 runLimit:  0,
406                                 dataStack: [][]byte{{1}},
407                         },
408                         wantErr: ErrRunLimitExceeded,
409                 })
410         }
411
412         for i, c := range cases {
413                 t.Logf("case %d", i)
414
415                 err := ops[c.op].fn(c.startVM)
416                 gotVM := c.startVM
417
418                 if err != c.wantErr {
419                         t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
420                         continue
421                 }
422                 if c.wantErr != nil {
423                         continue
424                 }
425
426                 // Hack: the context objects will otherwise compare unequal
427                 // sometimes (because of the function pointer within?) and we
428                 // don't care
429                 c.wantVM.context = gotVM.context
430
431                 if !testutil.DeepEqual(gotVM, c.wantVM) {
432                         t.Errorf("case %d, op %s: unexpected vm result\n\tgot:  %+v\n\twant: %+v\n", i, ops[c.op].name, gotVM, c.wantVM)
433                 }
434         }
435 }