2018-02-14 06:49:11 -06:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-05-28 16:39:33 -05:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestByteOp(t *testing.T) {
|
|
|
|
var (
|
|
|
|
env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
|
|
|
|
stack = newstack()
|
|
|
|
)
|
|
|
|
tests := []struct {
|
|
|
|
v string
|
|
|
|
th uint64
|
|
|
|
expected *big.Int
|
|
|
|
}{
|
|
|
|
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)},
|
|
|
|
{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)},
|
|
|
|
{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)},
|
|
|
|
{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)},
|
|
|
|
{"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)},
|
|
|
|
{"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)},
|
|
|
|
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)},
|
|
|
|
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)},
|
|
|
|
}
|
|
|
|
pc := uint64(0)
|
|
|
|
for _, test := range tests {
|
|
|
|
val := new(big.Int).SetBytes(common.Hex2Bytes(test.v))
|
|
|
|
th := new(big.Int).SetUint64(test.th)
|
|
|
|
stack.push(val)
|
|
|
|
stack.push(th)
|
|
|
|
opByte(&pc, env, nil, nil, stack)
|
|
|
|
actual := stack.pop()
|
|
|
|
if actual.Cmp(test.expected) != 0 {
|
|
|
|
t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 04:45:45 -05:00
|
|
|
|
|
|
|
func opBenchmark(bench *testing.B, op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) {
|
|
|
|
var (
|
|
|
|
env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
|
|
|
|
stack = newstack()
|
|
|
|
)
|
|
|
|
// convert args
|
|
|
|
byteArgs := make([][]byte, len(args))
|
|
|
|
for i, arg := range args {
|
|
|
|
byteArgs[i] = common.Hex2Bytes(arg)
|
|
|
|
}
|
|
|
|
pc := uint64(0)
|
|
|
|
bench.ResetTimer()
|
|
|
|
for i := 0; i < bench.N; i++ {
|
|
|
|
for _, arg := range byteArgs {
|
|
|
|
a := new(big.Int).SetBytes(arg)
|
|
|
|
stack.push(a)
|
|
|
|
}
|
|
|
|
op(&pc, env, nil, nil, stack)
|
|
|
|
stack.pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpAdd64(b *testing.B) {
|
|
|
|
x := "ffffffff"
|
|
|
|
y := "fd37f3e2bba2c4f"
|
2017-06-28 04:45:45 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
opBenchmark(b, opAdd, x, y)
|
2017-06-28 04:45:45 -05:00
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpAdd128(b *testing.B) {
|
|
|
|
x := "ffffffffffffffff"
|
|
|
|
y := "f5470b43c6549b016288e9a65629687"
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
opBenchmark(b, opAdd, x, y)
|
2017-06-28 04:45:45 -05:00
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpAdd256(b *testing.B) {
|
|
|
|
x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
|
|
|
|
y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
opBenchmark(b, opAdd, x, y)
|
2017-08-10 08:39:43 -05:00
|
|
|
}
|
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpSub64(b *testing.B) {
|
|
|
|
x := "51022b6317003a9d"
|
|
|
|
y := "a20456c62e00753a"
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
opBenchmark(b, opSub, x, y)
|
2017-08-10 08:39:43 -05:00
|
|
|
}
|
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpSub128(b *testing.B) {
|
|
|
|
x := "4dde30faaacdc14d00327aac314e915d"
|
|
|
|
y := "9bbc61f5559b829a0064f558629d22ba"
|
2017-06-28 04:45:45 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
opBenchmark(b, opSub, x, y)
|
2017-06-28 04:45:45 -05:00
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpSub256(b *testing.B) {
|
|
|
|
x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
|
|
|
|
y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
|
2017-06-28 04:45:45 -05:00
|
|
|
|
|
|
|
opBenchmark(b, opSub, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpMul(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opMul, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpDiv256(b *testing.B) {
|
|
|
|
x := "ff3f9014f20db29ae04af2c2d265de17"
|
|
|
|
y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
|
|
|
|
opBenchmark(b, opDiv, x, y)
|
|
|
|
}
|
2017-06-28 04:45:45 -05:00
|
|
|
|
2017-08-14 07:26:47 -05:00
|
|
|
func BenchmarkOpDiv128(b *testing.B) {
|
|
|
|
x := "fdedc7f10142ff97"
|
|
|
|
y := "fbdfda0e2ce356173d1993d5f70a2b11"
|
|
|
|
opBenchmark(b, opDiv, x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOpDiv64(b *testing.B) {
|
|
|
|
x := "fcb34eb3"
|
|
|
|
y := "f97180878e839129"
|
2017-06-28 04:45:45 -05:00
|
|
|
opBenchmark(b, opDiv, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpSdiv(b *testing.B) {
|
2017-08-14 07:26:47 -05:00
|
|
|
x := "ff3f9014f20db29ae04af2c2d265de17"
|
|
|
|
y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
|
2017-06-28 04:45:45 -05:00
|
|
|
|
|
|
|
opBenchmark(b, opSdiv, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpMod(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opMod, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpSmod(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opSmod, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpExp(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opExp, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpSignExtend(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opSignExtend, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpLt(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opLt, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpGt(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opGt, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpSlt(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opSlt, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpSgt(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opSgt, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpEq(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opEq, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpAnd(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opAnd, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpOr(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opOr, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpXor(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opXor, x, y)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpByte(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opByte, x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkOpAddmod(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opAddmod, x, y, z)
|
|
|
|
}
|
2017-08-10 08:39:43 -05:00
|
|
|
|
2017-06-28 04:45:45 -05:00
|
|
|
func BenchmarkOpMulmod(b *testing.B) {
|
|
|
|
x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
|
|
|
|
|
|
|
|
opBenchmark(b, opMulmod, x, y, z)
|
|
|
|
}
|