Skip to content

Commit 06f1d07

Browse files
all: replace division with right shift if possible (#29911)
1 parent 4939c25 commit 06f1d07

File tree

4 files changed

+11
-20
lines changed

4 files changed

+11
-20
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
325325
var (
326326
blockReward = big.NewInt(miningReward)
327327
minerReward = new(big.Int).Set(blockReward)
328-
perOmmer = new(big.Int).Div(blockReward, big.NewInt(32))
328+
perOmmer = new(big.Int).Rsh(blockReward, 5)
329329
)
330330
for _, ommer := range pre.Env.Ommers {
331331
// Add 1/32th for each ommer included
@@ -334,7 +334,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
334334
reward := big.NewInt(8)
335335
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
336336
reward.Mul(reward, blockReward)
337-
reward.Div(reward, big.NewInt(8))
337+
reward.Rsh(reward, 3)
338338
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
339339
}
340340
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)

consensus/ethash/consensus.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,6 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
562562
return hash
563563
}
564564

565-
// Some weird constants to avoid constant memory allocs for them.
566-
var (
567-
u256_8 = uint256.NewInt(8)
568-
u256_32 = uint256.NewInt(32)
569-
)
570-
571565
// accumulateRewards credits the coinbase of the given block with the mining
572566
// reward. The total reward consists of the static block reward and rewards for
573567
// included uncles. The coinbase of each uncle block is also rewarded.
@@ -589,10 +583,10 @@ func accumulateRewards(config *params.ChainConfig, stateDB *state.StateDB, heade
589583
r.AddUint64(uNum, 8)
590584
r.Sub(r, hNum)
591585
r.Mul(r, blockReward)
592-
r.Div(r, u256_8)
586+
r.Rsh(r, 3)
593587
stateDB.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle)
594588

595-
r.Div(blockReward, u256_32)
589+
r.Rsh(blockReward, 5)
596590
reward.Add(reward, r)
597591
}
598592
stateDB.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock)

core/types/transaction_signing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,6 @@ func deriveChainId(v *big.Int) *big.Int {
572572
}
573573
return new(big.Int).SetUint64((v - 35) / 2)
574574
}
575-
v = new(big.Int).Sub(v, big.NewInt(35))
576-
return v.Div(v, big.NewInt(2))
575+
v.Sub(v, big.NewInt(35))
576+
return v.Rsh(v, 1)
577577
}

core/vm/contracts.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ type bigModExp struct {
296296
var (
297297
big1 = big.NewInt(1)
298298
big3 = big.NewInt(3)
299-
big4 = big.NewInt(4)
300299
big7 = big.NewInt(7)
301-
big8 = big.NewInt(8)
302-
big16 = big.NewInt(16)
303300
big20 = big.NewInt(20)
304301
big32 = big.NewInt(32)
305302
big64 = big.NewInt(64)
@@ -325,13 +322,13 @@ func modexpMultComplexity(x *big.Int) *big.Int {
325322
case x.Cmp(big1024) <= 0:
326323
// (x ** 2 // 4 ) + ( 96 * x - 3072)
327324
x = new(big.Int).Add(
328-
new(big.Int).Div(new(big.Int).Mul(x, x), big4),
325+
new(big.Int).Rsh(new(big.Int).Mul(x, x), 2),
329326
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
330327
)
331328
default:
332329
// (x ** 2 // 16) + (480 * x - 199680)
333330
x = new(big.Int).Add(
334-
new(big.Int).Div(new(big.Int).Mul(x, x), big16),
331+
new(big.Int).Rsh(new(big.Int).Mul(x, x), 4),
335332
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
336333
)
337334
}
@@ -369,7 +366,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
369366
adjExpLen := new(big.Int)
370367
if expLen.Cmp(big32) > 0 {
371368
adjExpLen.Sub(expLen, big32)
372-
adjExpLen.Mul(big8, adjExpLen)
369+
adjExpLen.Lsh(adjExpLen, 3)
373370
}
374371
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
375372
// Calculate the gas cost of the operation
@@ -383,8 +380,8 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
383380
// ceiling(x/8)^2
384381
//
385382
//where is x is max(length_of_MODULUS, length_of_BASE)
386-
gas = gas.Add(gas, big7)
387-
gas = gas.Div(gas, big8)
383+
gas.Add(gas, big7)
384+
gas.Rsh(gas, 3)
388385
gas.Mul(gas, gas)
389386

390387
gas.Mul(gas, math.BigMax(adjExpLen, big1))

0 commit comments

Comments
 (0)