github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/vm/crypto_test.go (about)

     1  package vm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bytom/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_HASH160,
   148  		startVM: &virtualMachine{
   149  			runLimit:  50000,
   150  			dataStack: [][]byte{{1}},
   151  		},
   152  		wantVM: &virtualMachine{
   153  			runLimit: 49916,
   154  			dataStack: [][]byte{{
   155  				242, 145, 186, 80, 21, 223, 52, 140, 128, 133, 63, 165, 187, 15, 121, 70, 245, 201, 225, 179,
   156  			}},
   157  		},
   158  	}, {
   159  		op: OP_HASH160,
   160  		startVM: &virtualMachine{
   161  			runLimit:  50000,
   162  			dataStack: [][]byte{make([]byte, 65)},
   163  		},
   164  		wantVM: &virtualMachine{
   165  			runLimit: 49916,
   166  			dataStack: [][]byte{{
   167  				171, 60, 102, 205, 10, 63, 18, 180, 244, 250, 235, 84, 138, 85, 22, 7, 148, 250, 215, 6,
   168  			}},
   169  		},
   170  	}, {
   171  		op: OP_CHECKSIG,
   172  		startVM: &virtualMachine{
   173  			runLimit: 50000,
   174  			dataStack: [][]byte{
   175  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
   176  					"fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   177  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   178  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   179  			},
   180  		},
   181  		wantVM: &virtualMachine{
   182  			deferredCost: -143,
   183  			runLimit:     48976,
   184  			dataStack:    [][]byte{{1}},
   185  		},
   186  	}, {
   187  		op: OP_CHECKSIG,
   188  		startVM: &virtualMachine{
   189  			runLimit: 50000,
   190  			dataStack: [][]byte{
   191  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
   192  					"fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   193  				mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   194  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   195  			},
   196  		},
   197  		wantVM: &virtualMachine{
   198  			deferredCost: -144,
   199  			runLimit:     48976,
   200  			dataStack:    [][]byte{{}},
   201  		},
   202  	}, {
   203  		op: OP_CHECKSIG,
   204  		startVM: &virtualMachine{
   205  			runLimit: 50000,
   206  			dataStack: [][]byte{
   207  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
   208  					"fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   209  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   210  				mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   211  			},
   212  		},
   213  		wantVM: &virtualMachine{
   214  			deferredCost: -144,
   215  			runLimit:     48976,
   216  			dataStack:    [][]byte{{}},
   217  		},
   218  	}, {
   219  		op: OP_CHECKSIG,
   220  		startVM: &virtualMachine{
   221  			runLimit: 50000,
   222  			dataStack: [][]byte{
   223  				mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
   224  					"fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   225  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   226  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   227  			},
   228  		},
   229  		wantVM: &virtualMachine{
   230  			deferredCost: -144,
   231  			runLimit:     48976,
   232  			dataStack:    [][]byte{{}},
   233  		},
   234  	}, {
   235  		op: OP_CHECKSIG,
   236  		startVM: &virtualMachine{
   237  			runLimit: 50000,
   238  			dataStack: [][]byte{
   239  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
   240  					"fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   241  				mustDecodeHex("badbad"),
   242  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   243  			},
   244  		},
   245  		wantErr: ErrBadValue,
   246  	}, {
   247  		op: OP_CHECKSIG,
   248  		startVM: &virtualMachine{
   249  			runLimit: 0,
   250  		},
   251  		wantErr: ErrRunLimitExceeded,
   252  	}, {
   253  		op: OP_CHECKMULTISIG,
   254  		startVM: &virtualMachine{
   255  			runLimit: 50000,
   256  			dataStack: [][]byte{
   257  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   258  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   259  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   260  				{1},
   261  				{1},
   262  			},
   263  		},
   264  		wantVM: &virtualMachine{
   265  			deferredCost: -161,
   266  			runLimit:     48976,
   267  			dataStack:    [][]byte{{1}},
   268  		},
   269  	}, {
   270  		op: OP_CHECKMULTISIG,
   271  		startVM: &virtualMachine{
   272  			runLimit: 50000,
   273  			dataStack: [][]byte{
   274  				mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   275  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   276  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   277  				{1},
   278  				{1},
   279  			},
   280  		},
   281  		wantVM: &virtualMachine{
   282  			deferredCost: -162,
   283  			runLimit:     48976,
   284  			dataStack:    [][]byte{{}},
   285  		},
   286  	}, {
   287  		op: OP_CHECKMULTISIG,
   288  		startVM: &virtualMachine{
   289  			runLimit:  50000,
   290  			dataStack: [][]byte{},
   291  		},
   292  		wantErr: ErrDataStackUnderflow,
   293  	}, {
   294  		op: OP_CHECKMULTISIG,
   295  		startVM: &virtualMachine{
   296  			runLimit: 50000,
   297  			dataStack: [][]byte{
   298  				{1},
   299  				{1},
   300  			},
   301  		},
   302  		wantErr: ErrDataStackUnderflow,
   303  	}, {
   304  		op: OP_CHECKMULTISIG,
   305  		startVM: &virtualMachine{
   306  			runLimit: 50000,
   307  			dataStack: [][]byte{
   308  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   309  				{1},
   310  				{1},
   311  			},
   312  		},
   313  		wantErr: ErrDataStackUnderflow,
   314  	}, {
   315  		op: OP_CHECKMULTISIG,
   316  		startVM: &virtualMachine{
   317  			runLimit: 50000,
   318  			dataStack: [][]byte{
   319  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   320  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   321  				{1},
   322  				{1},
   323  			},
   324  		},
   325  		wantErr: ErrDataStackUnderflow,
   326  	}, {
   327  		op: OP_CHECKMULTISIG,
   328  		startVM: &virtualMachine{
   329  			runLimit: 50000,
   330  			dataStack: [][]byte{
   331  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   332  				mustDecodeHex("badbad"),
   333  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   334  				{1},
   335  				{1},
   336  			},
   337  		},
   338  		wantErr: ErrBadValue,
   339  	}, {
   340  		op: OP_CHECKMULTISIG,
   341  		startVM: &virtualMachine{
   342  			runLimit: 50000,
   343  			dataStack: [][]byte{
   344  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   345  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   346  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   347  				{1},
   348  				{0},
   349  			},
   350  		},
   351  		wantErr: ErrBadValue,
   352  	}, {
   353  		op: OP_CHECKMULTISIG,
   354  		startVM: &virtualMachine{
   355  			runLimit: 50000,
   356  			dataStack: [][]byte{
   357  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   358  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   359  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   360  				{0},
   361  				{1},
   362  			},
   363  		},
   364  		wantErr: ErrBadValue,
   365  	}, {
   366  		op: OP_CHECKMULTISIG,
   367  		startVM: &virtualMachine{
   368  			runLimit: 50000,
   369  			dataStack: [][]byte{
   370  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   371  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   372  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   373  				{2},
   374  				{1},
   375  			},
   376  		},
   377  		wantErr: ErrBadValue,
   378  	}, {
   379  		op: OP_CHECKMULTISIG,
   380  		startVM: &virtualMachine{
   381  			runLimit: 0,
   382  			dataStack: [][]byte{
   383  				mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
   384  				mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
   385  				mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
   386  				{1},
   387  				{1},
   388  			},
   389  		},
   390  		wantErr: ErrRunLimitExceeded,
   391  	}, {
   392  		op: OP_TXSIGHASH,
   393  		startVM: &virtualMachine{
   394  			runLimit: 50000,
   395  			context: &Context{
   396  				TxSigHash: func() []byte {
   397  					return []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,
   402  					}
   403  				},
   404  			},
   405  		},
   406  		wantVM: &virtualMachine{
   407  			runLimit: 49704,
   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,
   413  			}},
   414  		},
   415  	}, {
   416  		op: OP_TXSIGHASH,
   417  		startVM: &virtualMachine{
   418  			runLimit: 0,
   419  			context:  &Context{},
   420  		},
   421  		wantErr: ErrRunLimitExceeded,
   422  	}}
   423  
   424  	hashOps := []Op{OP_SHA256, OP_SHA3}
   425  	for _, op := range hashOps {
   426  		cases = append(cases, testStruct{
   427  			op: op,
   428  			startVM: &virtualMachine{
   429  				runLimit:  0,
   430  				dataStack: [][]byte{{1}},
   431  			},
   432  			wantErr: ErrRunLimitExceeded,
   433  		})
   434  	}
   435  
   436  	for i, c := range cases {
   437  		t.Logf("case %d", i)
   438  
   439  		err := ops[c.op].fn(c.startVM)
   440  		gotVM := c.startVM
   441  
   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)
   444  			continue
   445  		}
   446  		if c.wantErr != nil {
   447  			continue
   448  		}
   449  
   450  		// Hack: the context objects will otherwise compare unequal
   451  		// sometimes (because of the function pointer within?) and we
   452  		// don't care
   453  		c.wantVM.context = gotVM.context
   454  
   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)
   457  		}
   458  	}
   459  }