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) {
88 type testStruct struct {
90 startVM *virtualMachine
92 wantVM *virtualMachine
94 cases := []testStruct{{
96 startVM: &virtualMachine{
98 dataStack: [][]byte{{1}},
100 wantVM: &virtualMachine{
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,
109 startVM: &virtualMachine{
111 dataStack: [][]byte{make([]byte, 65)},
113 wantVM: &virtualMachine{
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,
122 startVM: &virtualMachine{
124 dataStack: [][]byte{{1}},
126 wantVM: &virtualMachine{
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,
135 startVM: &virtualMachine{
137 dataStack: [][]byte{make([]byte, 65)},
139 wantVM: &virtualMachine{
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,
148 startVM: &virtualMachine{
151 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
152 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
153 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
154 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
157 wantVM: &virtualMachine{
160 dataStack: [][]byte{{1}},
164 startVM: &virtualMachine{
167 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
168 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
169 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
170 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
173 wantVM: &virtualMachine{
176 dataStack: [][]byte{{}},
180 startVM: &virtualMachine{
183 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
184 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
185 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
186 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
189 wantVM: &virtualMachine{
192 dataStack: [][]byte{{}},
196 startVM: &virtualMachine{
199 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
200 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
201 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
202 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
205 wantVM: &virtualMachine{
208 dataStack: [][]byte{{}},
212 startVM: &virtualMachine{
215 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
216 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
217 mustDecodeHex("badbad"),
218 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
221 wantErr: ErrBadValue,
224 startVM: &virtualMachine{
227 wantErr: ErrRunLimitExceeded,
229 op: OP_CHECKMULTISIG,
230 startVM: &virtualMachine{
233 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
234 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
235 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
240 wantVM: &virtualMachine{
243 dataStack: [][]byte{{1}},
246 op: OP_CHECKMULTISIG,
247 startVM: &virtualMachine{
250 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
251 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
252 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
257 wantVM: &virtualMachine{
260 dataStack: [][]byte{{}},
263 op: OP_CHECKMULTISIG,
264 startVM: &virtualMachine{
266 dataStack: [][]byte{},
268 wantErr: ErrDataStackUnderflow,
270 op: OP_CHECKMULTISIG,
271 startVM: &virtualMachine{
278 wantErr: ErrDataStackUnderflow,
280 op: OP_CHECKMULTISIG,
281 startVM: &virtualMachine{
284 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
289 wantErr: ErrDataStackUnderflow,
291 op: OP_CHECKMULTISIG,
292 startVM: &virtualMachine{
295 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
296 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
301 wantErr: ErrDataStackUnderflow,
303 op: OP_CHECKMULTISIG,
304 startVM: &virtualMachine{
307 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
308 mustDecodeHex("badbad"),
309 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
314 wantErr: ErrBadValue,
316 op: OP_CHECKMULTISIG,
317 startVM: &virtualMachine{
320 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
321 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
322 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
327 wantErr: ErrBadValue,
329 op: OP_CHECKMULTISIG,
330 startVM: &virtualMachine{
333 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
334 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
335 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
340 wantErr: ErrBadValue,
342 op: OP_CHECKMULTISIG,
343 startVM: &virtualMachine{
346 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
347 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
348 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
353 wantErr: ErrBadValue,
355 op: OP_CHECKMULTISIG,
356 startVM: &virtualMachine{
359 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
360 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
361 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
366 wantErr: ErrRunLimitExceeded,
369 startVM: &virtualMachine{
372 TxSigHash: func() []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,
382 wantVM: &virtualMachine{
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,
393 startVM: &virtualMachine{
397 wantErr: ErrRunLimitExceeded,
400 hashOps := []Op{OP_SHA256, OP_SHA3}
401 for _, op := range hashOps {
402 cases = append(cases, testStruct{
404 startVM: &virtualMachine{
406 dataStack: [][]byte{{1}},
408 wantErr: ErrRunLimitExceeded,
412 for i, c := range cases {
415 err := ops[c.op].fn(c.startVM)
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)
422 if c.wantErr != nil {
426 // Hack: the context objects will otherwise compare unequal
427 // sometimes (because of the function pointer within?) and we
429 c.wantVM.context = gotVM.context
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)