6 "github.com/bytom/testutil"
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},
13 func TestCheckSig(t *testing.T) {
20 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
24 // This one has a wrong-length signature
25 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc2 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
29 // This one has a wrong-length message
30 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
34 // This one has a wrong-length pubkey
35 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584 CHECKSIG",
39 // This one has a wrong byte in the signature
40 "0x00ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
44 // This one has a wrong byte in the message
45 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0002030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
49 // This one has a wrong byte in the pubkey
50 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x00ca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
54 "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x040506 1 1 CHECKMULTISIG",
58 "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0x040506 1 1 CHECKMULTISIG",
62 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 1 1 CHECKMULTISIG",
67 for i, c := range cases {
68 prog, err := Assemble(c.prog)
70 t.Fatalf("case %d: %s", i, err)
72 vm := &virtualMachine{
79 t.Errorf("case %d: expected error, got ok result", i)
83 t.Errorf("case %d: expected ok result, got error %s", i, err)
85 } else if !vm.falseResult() {
86 t.Errorf("case %d: expected false VM result, got error %s", i, err)
91 func TestCryptoOps(t *testing.T) {
92 type testStruct struct {
94 startVM *virtualMachine
96 wantVM *virtualMachine
98 cases := []testStruct{{
100 startVM: &virtualMachine{
102 dataStack: [][]byte{{1}},
104 wantVM: &virtualMachine{
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,
113 startVM: &virtualMachine{
115 dataStack: [][]byte{make([]byte, 65)},
117 wantVM: &virtualMachine{
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,
126 startVM: &virtualMachine{
128 dataStack: [][]byte{{1}},
130 wantVM: &virtualMachine{
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,
139 startVM: &virtualMachine{
141 dataStack: [][]byte{make([]byte, 65)},
143 wantVM: &virtualMachine{
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,
152 startVM: &virtualMachine{
155 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
156 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
157 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
158 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
161 wantVM: &virtualMachine{
164 dataStack: [][]byte{{1}},
168 startVM: &virtualMachine{
171 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
172 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
173 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
174 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
177 wantVM: &virtualMachine{
180 dataStack: [][]byte{{}},
184 startVM: &virtualMachine{
187 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
188 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
189 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
190 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
193 wantVM: &virtualMachine{
196 dataStack: [][]byte{{}},
200 startVM: &virtualMachine{
203 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
204 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
205 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
206 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
209 wantVM: &virtualMachine{
212 dataStack: [][]byte{{}},
216 startVM: &virtualMachine{
219 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
220 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
221 mustDecodeHex("badbad"),
222 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
225 wantErr: ErrBadValue,
228 startVM: &virtualMachine{
231 wantErr: ErrRunLimitExceeded,
233 op: OP_CHECKMULTISIG,
234 startVM: &virtualMachine{
237 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
238 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
239 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
244 wantVM: &virtualMachine{
247 dataStack: [][]byte{{1}},
250 op: OP_CHECKMULTISIG,
251 startVM: &virtualMachine{
254 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
255 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
256 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
261 wantVM: &virtualMachine{
264 dataStack: [][]byte{{}},
267 op: OP_CHECKMULTISIG,
268 startVM: &virtualMachine{
270 dataStack: [][]byte{},
272 wantErr: ErrDataStackUnderflow,
274 op: OP_CHECKMULTISIG,
275 startVM: &virtualMachine{
282 wantErr: ErrDataStackUnderflow,
284 op: OP_CHECKMULTISIG,
285 startVM: &virtualMachine{
288 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
293 wantErr: ErrDataStackUnderflow,
295 op: OP_CHECKMULTISIG,
296 startVM: &virtualMachine{
299 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
300 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
305 wantErr: ErrDataStackUnderflow,
307 op: OP_CHECKMULTISIG,
308 startVM: &virtualMachine{
311 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
312 mustDecodeHex("badbad"),
313 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
318 wantErr: ErrBadValue,
320 op: OP_CHECKMULTISIG,
321 startVM: &virtualMachine{
324 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
325 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
326 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
331 wantErr: ErrBadValue,
333 op: OP_CHECKMULTISIG,
334 startVM: &virtualMachine{
337 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
338 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
339 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
344 wantErr: ErrBadValue,
346 op: OP_CHECKMULTISIG,
347 startVM: &virtualMachine{
350 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
351 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
352 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
357 wantErr: ErrBadValue,
359 op: OP_CHECKMULTISIG,
360 startVM: &virtualMachine{
363 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
364 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
365 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
370 wantErr: ErrRunLimitExceeded,
373 startVM: &virtualMachine{
376 TxSigHash: func() []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,
386 wantVM: &virtualMachine{
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,
397 startVM: &virtualMachine{
401 wantErr: ErrRunLimitExceeded,
404 startVM: &virtualMachine{
406 context: emptyBlockVMContext,
408 wantVM: &virtualMachine{
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,
416 context: emptyBlockVMContext,
420 startVM: &virtualMachine{
422 context: emptyBlockVMContext,
424 wantErr: ErrRunLimitExceeded,
427 hashOps := []Op{OP_SHA256, OP_SHA3}
428 for _, op := range hashOps {
429 cases = append(cases, testStruct{
431 startVM: &virtualMachine{
433 dataStack: [][]byte{{1}},
435 wantErr: ErrRunLimitExceeded,
439 for i, c := range cases {
442 err := ops[c.op].fn(c.startVM)
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)
449 if c.wantErr != nil {
453 // Hack: the context objects will otherwise compare unequal
454 // sometimes (because of the function pointer within?) and we
456 c.wantVM.context = gotVM.context
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)