From cf638614d4eaa8e25493831155e12c45a3099863 Mon Sep 17 00:00:00 2001 From: gitglorythegreat Date: Mon, 3 Jun 2024 16:01:08 +0800 Subject: [PATCH 1/3] all: replace division with right shift if possible --- cmd/evm/internal/t8ntool/execution.go | 4 ++-- consensus/ethash/consensus.go | 10 ++-------- core/txpool/blobpool/blobpool_test.go | 4 ++-- core/types/transaction_signing.go | 4 ++-- core/vm/contracts.go | 13 +++++-------- crypto/crypto.go | 2 +- 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index 3c09229e1c5c..6c8d40931db7 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -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 @@ -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) diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index b5e2754c2d94..0bd1a56bce19 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -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. @@ -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) diff --git a/core/txpool/blobpool/blobpool_test.go b/core/txpool/blobpool/blobpool_test.go index 85e13980bee6..9759af884010 100644 --- a/core/txpool/blobpool/blobpool_test.go +++ b/core/txpool/blobpool/blobpool_test.go @@ -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, @@ -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 diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index 6e5f6712f81b..469981276d99 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -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) } diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 8e0f8467752e..dd71a9729f34 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -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) @@ -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), ) } @@ -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 @@ -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)) diff --git a/crypto/crypto.go b/crypto/crypto.go index 7f7171f730a0..9d6af6ea3ccb 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -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") From 2bb65a0afd73ef685f1b37783cc5775c90134240 Mon Sep 17 00:00:00 2001 From: gitglorythegreat Date: Mon, 3 Jun 2024 20:40:24 +0800 Subject: [PATCH 2/3] revert change in test --- core/txpool/blobpool/blobpool_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/txpool/blobpool/blobpool_test.go b/core/txpool/blobpool/blobpool_test.go index 9759af884010..85e13980bee6 100644 --- a/core/txpool/blobpool/blobpool_test.go +++ b/core/txpool/blobpool/blobpool_test.go @@ -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.Rsh(mid, 1) + mid.Div(mid, big.NewInt(2)) if eip1559.CalcBaseFee(bc.config, &types.Header{ Number: blockNumber, @@ -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.Rsh(mid, 1) + mid.Div(mid, big.NewInt(2)) if eip4844.CalcBlobFee(mid.Uint64()).Cmp(bc.blobfee.ToBig()) > 0 { hi = mid From a53d7daba965e9a3de2a9c3d8e9cec38385452f6 Mon Sep 17 00:00:00 2001 From: gitglorythegreat Date: Tue, 4 Jun 2024 15:24:58 +0800 Subject: [PATCH 3/3] revert --- crypto/crypto.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/crypto.go b/crypto/crypto.go index 9d6af6ea3ccb..7f7171f730a0 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -46,7 +46,7 @@ const DigestLength = 32 var ( secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16) - secp256k1halfN = new(big.Int).Rsh(secp256k1N, 1) + secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2)) ) var errInvalidPubkey = errors.New("invalid secp256k1 public key")