github.com/humaniq/go-ethereum@v1.6.8-0.20171225131628-061223a13848/core/vm/instructions_test.go (about)

     1  package vm
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/params"
     9  )
    10  
    11  func TestByteOp(t *testing.T) {
    12  	var (
    13  		env   = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
    14  		stack = newstack()
    15  	)
    16  	tests := []struct {
    17  		v        string
    18  		th       uint64
    19  		expected *big.Int
    20  	}{
    21  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)},
    22  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)},
    23  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)},
    24  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)},
    25  		{"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)},
    26  		{"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)},
    27  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)},
    28  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)},
    29  	}
    30  	pc := uint64(0)
    31  	for _, test := range tests {
    32  		val := new(big.Int).SetBytes(common.Hex2Bytes(test.v))
    33  		th := new(big.Int).SetUint64(test.th)
    34  		stack.push(val)
    35  		stack.push(th)
    36  		opByte(&pc, env, nil, nil, stack)
    37  		actual := stack.pop()
    38  		if actual.Cmp(test.expected) != 0 {
    39  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual)
    40  		}
    41  	}
    42  }
    43  
    44  func opBenchmark(bench *testing.B, op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) {
    45  	var (
    46  		env   = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
    47  		stack = newstack()
    48  	)
    49  	// convert args
    50  	byteArgs := make([][]byte, len(args))
    51  	for i, arg := range args {
    52  		byteArgs[i] = common.Hex2Bytes(arg)
    53  	}
    54  	pc := uint64(0)
    55  	bench.ResetTimer()
    56  	for i := 0; i < bench.N; i++ {
    57  		for _, arg := range byteArgs {
    58  			a := new(big.Int).SetBytes(arg)
    59  			stack.push(a)
    60  		}
    61  		op(&pc, env, nil, nil, stack)
    62  		stack.pop()
    63  	}
    64  }
    65  
    66  func BenchmarkOpAdd64(b *testing.B) {
    67  	x := "ffffffff"
    68  	y := "fd37f3e2bba2c4f"
    69  
    70  	opBenchmark(b, opAdd, x, y)
    71  }
    72  
    73  func BenchmarkOpAdd128(b *testing.B) {
    74  	x := "ffffffffffffffff"
    75  	y := "f5470b43c6549b016288e9a65629687"
    76  
    77  	opBenchmark(b, opAdd, x, y)
    78  }
    79  
    80  func BenchmarkOpAdd256(b *testing.B) {
    81  	x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
    82  	y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
    83  
    84  	opBenchmark(b, opAdd, x, y)
    85  }
    86  
    87  func BenchmarkOpSub64(b *testing.B) {
    88  	x := "51022b6317003a9d"
    89  	y := "a20456c62e00753a"
    90  
    91  	opBenchmark(b, opSub, x, y)
    92  }
    93  
    94  func BenchmarkOpSub128(b *testing.B) {
    95  	x := "4dde30faaacdc14d00327aac314e915d"
    96  	y := "9bbc61f5559b829a0064f558629d22ba"
    97  
    98  	opBenchmark(b, opSub, x, y)
    99  }
   100  
   101  func BenchmarkOpSub256(b *testing.B) {
   102  	x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
   103  	y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
   104  
   105  	opBenchmark(b, opSub, x, y)
   106  }
   107  
   108  func BenchmarkOpMul(b *testing.B) {
   109  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   110  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   111  
   112  	opBenchmark(b, opMul, x, y)
   113  }
   114  
   115  func BenchmarkOpDiv256(b *testing.B) {
   116  	x := "ff3f9014f20db29ae04af2c2d265de17"
   117  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   118  	opBenchmark(b, opDiv, x, y)
   119  }
   120  
   121  func BenchmarkOpDiv128(b *testing.B) {
   122  	x := "fdedc7f10142ff97"
   123  	y := "fbdfda0e2ce356173d1993d5f70a2b11"
   124  	opBenchmark(b, opDiv, x, y)
   125  }
   126  
   127  func BenchmarkOpDiv64(b *testing.B) {
   128  	x := "fcb34eb3"
   129  	y := "f97180878e839129"
   130  	opBenchmark(b, opDiv, x, y)
   131  }
   132  
   133  func BenchmarkOpSdiv(b *testing.B) {
   134  	x := "ff3f9014f20db29ae04af2c2d265de17"
   135  	y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
   136  
   137  	opBenchmark(b, opSdiv, x, y)
   138  }
   139  
   140  func BenchmarkOpMod(b *testing.B) {
   141  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   142  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   143  
   144  	opBenchmark(b, opMod, x, y)
   145  }
   146  
   147  func BenchmarkOpSmod(b *testing.B) {
   148  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   149  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   150  
   151  	opBenchmark(b, opSmod, x, y)
   152  }
   153  
   154  func BenchmarkOpExp(b *testing.B) {
   155  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   156  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   157  
   158  	opBenchmark(b, opExp, x, y)
   159  }
   160  
   161  func BenchmarkOpSignExtend(b *testing.B) {
   162  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   163  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   164  
   165  	opBenchmark(b, opSignExtend, x, y)
   166  }
   167  
   168  func BenchmarkOpLt(b *testing.B) {
   169  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   170  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   171  
   172  	opBenchmark(b, opLt, x, y)
   173  }
   174  
   175  func BenchmarkOpGt(b *testing.B) {
   176  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   177  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   178  
   179  	opBenchmark(b, opGt, x, y)
   180  }
   181  
   182  func BenchmarkOpSlt(b *testing.B) {
   183  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   184  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   185  
   186  	opBenchmark(b, opSlt, x, y)
   187  }
   188  
   189  func BenchmarkOpSgt(b *testing.B) {
   190  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   191  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   192  
   193  	opBenchmark(b, opSgt, x, y)
   194  }
   195  
   196  func BenchmarkOpEq(b *testing.B) {
   197  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   198  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   199  
   200  	opBenchmark(b, opEq, x, y)
   201  }
   202  
   203  func BenchmarkOpAnd(b *testing.B) {
   204  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   205  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   206  
   207  	opBenchmark(b, opAnd, x, y)
   208  }
   209  
   210  func BenchmarkOpOr(b *testing.B) {
   211  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   212  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   213  
   214  	opBenchmark(b, opOr, x, y)
   215  }
   216  
   217  func BenchmarkOpXor(b *testing.B) {
   218  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   219  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   220  
   221  	opBenchmark(b, opXor, x, y)
   222  }
   223  
   224  func BenchmarkOpByte(b *testing.B) {
   225  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   226  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   227  
   228  	opBenchmark(b, opByte, x, y)
   229  }
   230  
   231  func BenchmarkOpAddmod(b *testing.B) {
   232  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   233  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   234  	z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   235  
   236  	opBenchmark(b, opAddmod, x, y, z)
   237  }
   238  
   239  func BenchmarkOpMulmod(b *testing.B) {
   240  	x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   241  	y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   242  	z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
   243  
   244  	opBenchmark(b, opMulmod, x, y, z)
   245  }