From 82087096c596f1b4188ecacc8e83886eaa1fdd54 Mon Sep 17 00:00:00 2001 From: Fynn Date: Tue, 6 Nov 2018 16:26:45 +0100 Subject: [PATCH] common/math, core/vm: Move optimizations to math Move exponentiation optimizations in opExp to math.Exp --- common/math/big.go | 12 ++++++++++++ core/vm/instructions.go | 17 ++--------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/common/math/big.go b/common/math/big.go index 9d2e7946d1..08405c4a79 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -198,6 +198,18 @@ func S256(x *big.Int) *big.Int { // Courtesy @karalabe and @chfast func Exp(base, exponent *big.Int) *big.Int { result := big.NewInt(1) + // some shortcuts + cmpToOne := exponent.Cmp(result) + if cmpToOne < 0 { // Exponent is zero + // x ^ 0 == 1 + return result + } else if base.Sign() == 0 { + // 0 ^ y, if y != 0, == 0 + return result.SetUint64(0) + } else if cmpToOne == 0 { // Exponent is one + // x ^ 1 == x + return result.Set(base) + } for _, word := range exponent.Bits() { for i := 0; i < wordBits; i++ { diff --git a/core/vm/instructions.go b/core/vm/instructions.go index b7c3ca5323..9ff53ac937 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -124,21 +124,8 @@ func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { base, exponent := stack.pop(), stack.pop() - // some shortcuts - cmpToOne := exponent.Cmp(big1) - if cmpToOne < 0 { // Exponent is zero - // x ^ 0 == 1 - stack.push(base.SetUint64(1)) - } else if base.Sign() == 0 { - // 0 ^ y, if y != 0, == 0 - stack.push(base.SetUint64(0)) - } else if cmpToOne == 0 { // Exponent is one - // x ^ 1 == x - stack.push(base) - } else { - stack.push(math.Exp(base, exponent)) - interpreter.intPool.put(base) - } + stack.push(math.Exp(base, exponent)) + interpreter.intPool.put(base) interpreter.intPool.put(exponent) return nil, nil }