common/math, core/vm: Move optimizations to math

Move exponentiation optimizations in opExp to math.Exp
This commit is contained in:
Fynn 2018-11-06 16:26:45 +01:00
parent 53eb4e0b0f
commit 82087096c5
No known key found for this signature in database
GPG key ID: 50A23814D8B3FC8E
2 changed files with 14 additions and 15 deletions

View file

@ -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++ {

View file

@ -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)
}
interpreter.intPool.put(exponent)
return nil, nil
}