6 "github.com/bytom/testutil"
9 func TestCheckSig(t *testing.T) {
16 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
20 // This one has a wrong-length signature
21 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc2 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
25 // This one has a wrong-length message
26 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
30 // This one has a wrong-length pubkey
31 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584 CHECKSIG",
35 // This one has a wrong byte in the signature
36 "0x00ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
40 // This one has a wrong byte in the message
41 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0002030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
45 // This one has a wrong byte in the pubkey
46 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x00ca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
50 "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x040506 1 1 CHECKMULTISIG",
54 "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0x040506 1 1 CHECKMULTISIG",
58 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 1 1 CHECKMULTISIG",
63 for i, c := range cases {
64 prog, err := Assemble(c.prog)
66 t.Fatalf("case %d: %s", i, err)
68 vm := &virtualMachine{
75 t.Errorf("case %d: expected error, got ok result", i)
79 t.Errorf("case %d: expected ok result, got error %s", i, err)
81 } else if !vm.falseResult() {
82 t.Errorf("case %d: expected false VM result, got error %s", i, err)
87 func TestCryptoOps(t *testing.T) {
89 ops[OP_SM3] = opInfo{OP_SM3, "SM3", opSm3}
91 type testStruct struct {
93 startVM *virtualMachine
95 wantVM *virtualMachine
97 cases := []testStruct{{
99 startVM: &virtualMachine{
101 dataStack: [][]byte{{1}},
103 wantVM: &virtualMachine{
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,
112 startVM: &virtualMachine{
114 dataStack: [][]byte{make([]byte, 65)},
116 wantVM: &virtualMachine{
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,
125 startVM: &virtualMachine{
127 dataStack: [][]byte{{1}},
129 wantVM: &virtualMachine{
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,
138 startVM: &virtualMachine{
140 dataStack: [][]byte{make([]byte, 65)},
142 wantVM: &virtualMachine{
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,
151 startVM: &virtualMachine{
153 dataStack: [][]byte{{1}},
155 wantVM: &virtualMachine{
157 dataStack: [][]byte{{
158 242, 145, 186, 80, 21, 223, 52, 140, 128, 133, 63, 165, 187, 15, 121, 70, 245, 201, 225, 179,
163 startVM: &virtualMachine{
165 dataStack: [][]byte{make([]byte, 65)},
167 wantVM: &virtualMachine{
169 dataStack: [][]byte{{
170 171, 60, 102, 205, 10, 63, 18, 180, 244, 250, 235, 84, 138, 85, 22, 7, 148, 250, 215, 6,
175 startVM: &virtualMachine{
177 dataStack: [][]byte{{1}},
179 wantVM: &virtualMachine{
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,
188 startVM: &virtualMachine{
190 dataStack: [][]byte{make([]byte, 65)},
192 wantVM: &virtualMachine{
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,
201 startVM: &virtualMachine{
204 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
205 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
206 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
207 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
210 wantVM: &virtualMachine{
213 dataStack: [][]byte{{1}},
217 startVM: &virtualMachine{
220 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
221 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
222 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
223 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
226 wantVM: &virtualMachine{
229 dataStack: [][]byte{{}},
233 startVM: &virtualMachine{
236 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
237 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
238 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
239 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
242 wantVM: &virtualMachine{
245 dataStack: [][]byte{{}},
249 startVM: &virtualMachine{
252 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
253 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
254 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
255 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
258 wantVM: &virtualMachine{
261 dataStack: [][]byte{{}},
265 startVM: &virtualMachine{
268 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
269 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
270 mustDecodeHex("badbad"),
271 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
274 wantErr: ErrBadValue,
277 startVM: &virtualMachine{
280 wantErr: ErrRunLimitExceeded,
282 op: OP_CHECKMULTISIG,
283 startVM: &virtualMachine{
286 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
287 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
288 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
293 wantVM: &virtualMachine{
296 dataStack: [][]byte{{1}},
299 op: OP_CHECKMULTISIG,
300 startVM: &virtualMachine{
303 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
304 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
305 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
310 wantVM: &virtualMachine{
313 dataStack: [][]byte{{}},
316 op: OP_CHECKMULTISIG,
317 startVM: &virtualMachine{
319 dataStack: [][]byte{},
321 wantErr: ErrDataStackUnderflow,
323 op: OP_CHECKMULTISIG,
324 startVM: &virtualMachine{
331 wantErr: ErrDataStackUnderflow,
333 op: OP_CHECKMULTISIG,
334 startVM: &virtualMachine{
337 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
342 wantErr: ErrDataStackUnderflow,
344 op: OP_CHECKMULTISIG,
345 startVM: &virtualMachine{
348 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
349 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
354 wantErr: ErrDataStackUnderflow,
356 op: OP_CHECKMULTISIG,
357 startVM: &virtualMachine{
360 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
361 mustDecodeHex("badbad"),
362 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
367 wantErr: ErrBadValue,
369 op: OP_CHECKMULTISIG,
370 startVM: &virtualMachine{
373 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
374 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
375 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
380 wantErr: ErrBadValue,
382 op: OP_CHECKMULTISIG,
383 startVM: &virtualMachine{
386 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
387 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
388 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
393 wantErr: ErrBadValue,
395 op: OP_CHECKMULTISIG,
396 startVM: &virtualMachine{
399 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
400 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
401 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
406 wantErr: ErrBadValue,
408 op: OP_CHECKMULTISIG,
409 startVM: &virtualMachine{
412 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
413 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
414 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
419 wantErr: ErrRunLimitExceeded,
422 startVM: &virtualMachine{
425 TxSigHash: func() []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,
435 wantVM: &virtualMachine{
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,
446 startVM: &virtualMachine{
450 wantErr: ErrRunLimitExceeded,
453 hashOps := []Op{OP_SHA256, OP_SHA3, OP_SM3}
454 for _, op := range hashOps {
455 cases = append(cases, testStruct{
457 startVM: &virtualMachine{
459 dataStack: [][]byte{{1}},
461 wantErr: ErrRunLimitExceeded,
465 for i, c := range cases {
468 err := ops[c.op].fn(c.startVM)
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)
475 if c.wantErr != nil {
479 // Hack: the context objects will otherwise compare unequal
480 // sometimes (because of the function pointer within?) and we
482 c.wantVM.context = gotVM.context
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)