Skip to content

Commit

Permalink
all: replace division with right shift if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gitglorythegreat authored Jun 3, 2024
1 parent d4b81f0 commit cf63861
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
var (
blockReward = big.NewInt(miningReward)
minerReward = new(big.Int).Set(blockReward)
perOmmer = new(big.Int).Div(blockReward, big.NewInt(32))
perOmmer = new(big.Int).Rsh(blockReward, 5)
)
for _, ommer := range pre.Env.Ommers {
// Add 1/32th for each ommer included
Expand All @@ -332,7 +332,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
reward := big.NewInt(8)
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
reward.Mul(reward, blockReward)
reward.Div(reward, big.NewInt(8))
reward.Rsh(reward, 3)
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
}
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)
Expand Down
10 changes: 2 additions & 8 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,6 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
return hash
}

// Some weird constants to avoid constant memory allocs for them.
var (
u256_8 = uint256.NewInt(8)
u256_32 = uint256.NewInt(32)
)

// accumulateRewards credits the coinbase of the given block with the mining
// reward. The total reward consists of the static block reward and rewards for
// included uncles. The coinbase of each uncle block is also rewarded.
Expand All @@ -589,10 +583,10 @@ func accumulateRewards(config *params.ChainConfig, stateDB *state.StateDB, heade
r.AddUint64(uNum, 8)
r.Sub(r, hNum)
r.Mul(r, blockReward)
r.Div(r, u256_8)
r.Rsh(r, 3)
stateDB.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle)

r.Div(blockReward, u256_32)
r.Rsh(blockReward, 5)
reward.Add(reward, r)
}
stateDB.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock)
Expand Down
4 changes: 2 additions & 2 deletions core/txpool/blobpool/blobpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (bc *testBlockChain) CurrentBlock() *types.Header {

for new(big.Int).Add(lo, big.NewInt(1)).Cmp(hi) != 0 {
mid := new(big.Int).Add(lo, hi)
mid.Div(mid, big.NewInt(2))
mid.Rsh(mid, 1)

if eip1559.CalcBaseFee(bc.config, &types.Header{
Number: blockNumber,
Expand All @@ -118,7 +118,7 @@ func (bc *testBlockChain) CurrentBlock() *types.Header {

for new(big.Int).Add(lo, big.NewInt(1)).Cmp(hi) != 0 {
mid := new(big.Int).Add(lo, hi)
mid.Div(mid, big.NewInt(2))
mid.Rsh(mid, 1)

if eip4844.CalcBlobFee(mid.Uint64()).Cmp(bc.blobfee.ToBig()) > 0 {
hi = mid
Expand Down
4 changes: 2 additions & 2 deletions core/types/transaction_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,6 @@ func deriveChainId(v *big.Int) *big.Int {
}
return new(big.Int).SetUint64((v - 35) / 2)
}
v = new(big.Int).Sub(v, big.NewInt(35))
return v.Div(v, big.NewInt(2))
v.Sub(v, big.NewInt(35))
return v.Rsh(v, 1)
}
13 changes: 5 additions & 8 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ type bigModExp struct {
var (
big1 = big.NewInt(1)
big3 = big.NewInt(3)
big4 = big.NewInt(4)
big7 = big.NewInt(7)
big8 = big.NewInt(8)
big16 = big.NewInt(16)
big20 = big.NewInt(20)
big32 = big.NewInt(32)
big64 = big.NewInt(64)
Expand All @@ -325,13 +322,13 @@ func modexpMultComplexity(x *big.Int) *big.Int {
case x.Cmp(big1024) <= 0:
// (x ** 2 // 4 ) + ( 96 * x - 3072)
x = new(big.Int).Add(
new(big.Int).Div(new(big.Int).Mul(x, x), big4),
new(big.Int).Rsh(new(big.Int).Mul(x, x), 2),
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
)
default:
// (x ** 2 // 16) + (480 * x - 199680)
x = new(big.Int).Add(
new(big.Int).Div(new(big.Int).Mul(x, x), big16),
new(big.Int).Rsh(new(big.Int).Mul(x, x), 4),
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
)
}
Expand Down Expand Up @@ -369,7 +366,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
adjExpLen := new(big.Int)
if expLen.Cmp(big32) > 0 {
adjExpLen.Sub(expLen, big32)
adjExpLen.Mul(big8, adjExpLen)
adjExpLen.Lsh(adjExpLen, 3)
}
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
// Calculate the gas cost of the operation
Expand All @@ -383,8 +380,8 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
// ceiling(x/8)^2
//
//where is x is max(length_of_MODULUS, length_of_BASE)
gas = gas.Add(gas, big7)
gas = gas.Div(gas, big8)
gas.Add(gas, big7)
gas.Rsh(gas, 3)
gas.Mul(gas, gas)

gas.Mul(gas, math.BigMax(adjExpLen, big1))
Expand Down
2 changes: 1 addition & 1 deletion crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const DigestLength = 32

var (
secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2))
secp256k1halfN = new(big.Int).Rsh(secp256k1N, 1)
)

var errInvalidPubkey = errors.New("invalid secp256k1 public key")
Expand Down

0 comments on commit cf63861

Please sign in to comment.