From 2ae2a78f38f9cd972eb48fa7e69df7003cd68cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 19 Oct 2016 17:16:06 +0200 Subject: [PATCH] vm: optimize EXP Optimize EXP performance by using simple algorithm that replaces division with a bit mask. --- core/vm/instructions.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 3352877c1f..fcd6274b98 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -192,8 +192,16 @@ func opSmod(instr instruction, pc *uint64, env Environment, contract *Contract, } func opExp(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) { - x, y := stack.pop(), stack.pop() - stack.push(U256(x.Exp(x, y, Pow256))) + base, exponent := stack.pop(), stack.pop() + result := big.NewInt(1) + for exponent.Cmp(common.Big0) != 0 { + if exponent.Bit(0) == 1 { + U256(result.Mul(result, base)) + } + U256(base.Mul(base, base)) + exponent.Rsh(exponent, 1) + } + stack.push(result) } func opSignExtend(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {