6 "github.com/vapor/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{
150 dataStack: [][]byte{{1}},
152 wantVM: &virtualMachine{
154 dataStack: [][]byte{{
155 242, 145, 186, 80, 21, 223, 52, 140, 128, 133, 63, 165, 187, 15, 121, 70, 245, 201, 225, 179,
160 startVM: &virtualMachine{
162 dataStack: [][]byte{make([]byte, 65)},
164 wantVM: &virtualMachine{
166 dataStack: [][]byte{{
167 171, 60, 102, 205, 10, 63, 18, 180, 244, 250, 235, 84, 138, 85, 22, 7, 148, 250, 215, 6,
172 startVM: &virtualMachine{
175 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
176 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
177 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
178 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
181 wantVM: &virtualMachine{
184 dataStack: [][]byte{{1}},
188 startVM: &virtualMachine{
191 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
192 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
193 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
194 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
197 wantVM: &virtualMachine{
200 dataStack: [][]byte{{}},
204 startVM: &virtualMachine{
207 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
208 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
209 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
210 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
213 wantVM: &virtualMachine{
216 dataStack: [][]byte{{}},
220 startVM: &virtualMachine{
223 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
224 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
225 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
226 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
229 wantVM: &virtualMachine{
232 dataStack: [][]byte{{}},
236 startVM: &virtualMachine{
239 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
240 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
241 mustDecodeHex("badbad"),
242 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
245 wantErr: ErrBadValue,
248 startVM: &virtualMachine{
251 wantErr: ErrRunLimitExceeded,
253 op: OP_CHECKMULTISIG,
254 startVM: &virtualMachine{
257 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
258 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
259 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
264 wantVM: &virtualMachine{
267 dataStack: [][]byte{{1}},
270 op: OP_CHECKMULTISIG,
271 startVM: &virtualMachine{
274 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
275 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
276 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
281 wantVM: &virtualMachine{
284 dataStack: [][]byte{{}},
287 op: OP_CHECKMULTISIG,
288 startVM: &virtualMachine{
290 dataStack: [][]byte{},
292 wantErr: ErrDataStackUnderflow,
294 op: OP_CHECKMULTISIG,
295 startVM: &virtualMachine{
302 wantErr: ErrDataStackUnderflow,
304 op: OP_CHECKMULTISIG,
305 startVM: &virtualMachine{
308 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
313 wantErr: ErrDataStackUnderflow,
315 op: OP_CHECKMULTISIG,
316 startVM: &virtualMachine{
319 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
320 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
325 wantErr: ErrDataStackUnderflow,
327 op: OP_CHECKMULTISIG,
328 startVM: &virtualMachine{
331 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
332 mustDecodeHex("badbad"),
333 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
338 wantErr: ErrBadValue,
340 op: OP_CHECKMULTISIG,
341 startVM: &virtualMachine{
344 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
345 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
346 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
351 wantErr: ErrBadValue,
353 op: OP_CHECKMULTISIG,
354 startVM: &virtualMachine{
357 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
358 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
359 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
364 wantErr: ErrBadValue,
366 op: OP_CHECKMULTISIG,
367 startVM: &virtualMachine{
370 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
371 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
372 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
377 wantErr: ErrBadValue,
379 op: OP_CHECKMULTISIG,
380 startVM: &virtualMachine{
383 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
384 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
385 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
390 wantErr: ErrRunLimitExceeded,
393 startVM: &virtualMachine{
396 TxSigHash: func() []byte {
398 0x2f, 0x00, 0x3c, 0xdd, 0x64, 0x42, 0x7b, 0x5e,
399 0xed, 0xd6, 0xcc, 0xb5, 0x85, 0x47, 0x02, 0x0b,
400 0x02, 0xde, 0xf2, 0x2d, 0xc5, 0x99, 0x7e, 0x9d,
401 0xa9, 0xac, 0x40, 0x49, 0xc3, 0x4a, 0x58, 0xd8,
406 wantVM: &virtualMachine{
408 dataStack: [][]byte{{
409 47, 0, 60, 221, 100, 66, 123, 94,
410 237, 214, 204, 181, 133, 71, 2, 11,
411 2, 222, 242, 45, 197, 153, 126, 157,
412 169, 172, 64, 73, 195, 74, 88, 216,
417 startVM: &virtualMachine{
421 wantErr: ErrRunLimitExceeded,
424 hashOps := []Op{OP_SHA256, OP_SHA3}
425 for _, op := range hashOps {
426 cases = append(cases, testStruct{
428 startVM: &virtualMachine{
430 dataStack: [][]byte{{1}},
432 wantErr: ErrRunLimitExceeded,
436 for i, c := range cases {
439 err := ops[c.op].fn(c.startVM)
442 if err != c.wantErr {
443 t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
446 if c.wantErr != nil {
450 // Hack: the context objects will otherwise compare unequal
451 // sometimes (because of the function pointer within?) and we
453 c.wantVM.context = gotVM.context
455 if !testutil.DeepEqual(gotVM, c.wantVM) {
456 t.Errorf("case %d, op %s: unexpected vm result\n\tgot: %+v\n\twant: %+v\n", i, ops[c.op].name, gotVM, c.wantVM)