Skip to content

Commit 8b4aa99

Browse files
authored
optimization: replace division/multiplication by 2^x with shifts (#15151)
see ethereum/go-ethereum#29911
1 parent 4cf1e67 commit 8b4aa99

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

core/vm/contracts.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,8 @@ type bigModExp struct {
342342

343343
var (
344344
big1 = big.NewInt(1)
345-
big2 = big.NewInt(2)
346345
big3 = big.NewInt(3)
347-
big4 = big.NewInt(4)
348346
big7 = big.NewInt(7)
349-
big8 = big.NewInt(8)
350-
big16 = big.NewInt(16)
351347
big20 = big.NewInt(20)
352348
big32 = big.NewInt(32)
353349
big64 = big.NewInt(64)
@@ -374,13 +370,13 @@ func modExpMultComplexityEip198(x *big.Int) *big.Int {
374370
case x.Cmp(big1024) <= 0:
375371
// (x ** 2 // 4 ) + ( 96 * x - 3072)
376372
x = new(big.Int).Add(
377-
new(big.Int).Div(new(big.Int).Mul(x, x), big4),
373+
new(big.Int).Rsh(new(big.Int).Mul(x, x), 2),
378374
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
379375
)
380376
default:
381377
// (x ** 2 // 16) + (480 * x - 199680)
382378
x = new(big.Int).Add(
383-
new(big.Int).Div(new(big.Int).Mul(x, x), big16),
379+
new(big.Int).Rsh(new(big.Int).Mul(x, x), 4),
384380
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
385381
)
386382
}
@@ -397,7 +393,7 @@ func modExpMultComplexityEip198(x *big.Int) *big.Int {
397393
// where is x is max(length_of_MODULUS, length_of_BASE)
398394
func modExpMultComplexityEip2565(x *big.Int) *big.Int {
399395
x.Add(x, big7)
400-
x.Div(x, big8)
396+
x.Rsh(x, 3) // ÷8
401397
return x.Mul(x, x)
402398
}
403399

@@ -418,7 +414,7 @@ func modExpMultComplexityEip7883(x *big.Int) *big.Int {
418414
if lessOrEq32 {
419415
return x
420416
} else {
421-
return x.Mul(x, big2)
417+
return x.Lsh(x, 1) // ×2
422418
}
423419
}
424420

@@ -454,9 +450,9 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
454450
if expLen.Cmp(big32) > 0 {
455451
adjExpLen.Sub(expLen, big32)
456452
if c.eip7883 {
457-
adjExpLen.Mul(big16, adjExpLen)
453+
adjExpLen.Lsh(adjExpLen, 4) // ×16
458454
} else {
459-
adjExpLen.Mul(big8, adjExpLen)
455+
adjExpLen.Lsh(adjExpLen, 3) // ×8
460456
}
461457
}
462458
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))

erigon-lib/types/transaction_signing.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,30 @@ func MakeSigner(config *chain.Config, blockNumber uint64, blockTime uint64) *Sig
5656
signer.blob = true
5757
signer.setCode = true
5858
signer.chainID.Set(&chainId)
59-
signer.chainIDMul.Mul(&chainId, u256.Num2)
59+
signer.chainIDMul.Lsh(&chainId, 1) // ×2
6060
case config.IsCancun(blockTime):
6161
// All transaction types are still supported
6262
signer.protected = true
6363
signer.accessList = true
6464
signer.dynamicFee = true
6565
signer.blob = true
6666
signer.chainID.Set(&chainId)
67-
signer.chainIDMul.Mul(&chainId, u256.Num2)
67+
signer.chainIDMul.Lsh(&chainId, 1) // ×2
6868
case config.IsLondon(blockNumber):
6969
signer.protected = true
7070
signer.accessList = true
7171
signer.dynamicFee = true
7272
signer.chainID.Set(&chainId)
73-
signer.chainIDMul.Mul(&chainId, u256.Num2)
73+
signer.chainIDMul.Lsh(&chainId, 1) // ×2
7474
case config.IsBerlin(blockNumber):
7575
signer.protected = true
7676
signer.accessList = true
7777
signer.chainID.Set(&chainId)
78-
signer.chainIDMul.Mul(&chainId, u256.Num2)
78+
signer.chainIDMul.Lsh(&chainId, 1) // ×2
7979
case config.IsSpuriousDragon(blockNumber):
8080
signer.protected = true
8181
signer.chainID.Set(&chainId)
82-
signer.chainIDMul.Mul(&chainId, u256.Num2)
82+
signer.chainIDMul.Lsh(&chainId, 1) // ×2
8383
case config.IsHomestead(blockNumber):
8484
default:
8585
// Only allow malleable transactions in Frontier
@@ -110,7 +110,7 @@ func LatestSigner(config *chain.Config) *Signer {
110110
panic("chainID higher than 2^256-1")
111111
}
112112
signer.chainID.Set(chainId)
113-
signer.chainIDMul.Mul(chainId, u256.Num2)
113+
signer.chainIDMul.Lsh(chainId, 1) // ×2
114114
if config.ChainID != nil {
115115
if config.CancunTime != nil {
116116
signer.blob = true
@@ -149,7 +149,7 @@ func LatestSignerForChainID(chainID *big.Int) *Signer {
149149
panic("chainID higher than 2^256-1")
150150
}
151151
signer.chainID.Set(chainId)
152-
signer.chainIDMul.Mul(chainId, u256.Num2)
152+
signer.chainIDMul.Lsh(chainId, 1) // ×2
153153
signer.protected = true
154154
signer.accessList = true
155155
signer.dynamicFee = true
@@ -397,5 +397,5 @@ func DeriveChainId(v *uint256.Int) *uint256.Int {
397397
return new(uint256.Int).SetUint64((v - 35) / 2)
398398
}
399399
r := new(uint256.Int).Sub(v, u256.Num35)
400-
return r.Div(r, u256.Num2)
400+
return r.Rsh(r, 1) // ÷2
401401
}

execution/consensus/ethash/consensus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,10 @@ func AccumulateRewards(config *chain.Config, header *types.Header, uncles []*typ
659659
r.Add(uncleNum, u256.Num8)
660660
r.Sub(r, headerNum)
661661
r.Mul(r, blockReward)
662-
r.Div(r, u256.Num8)
662+
r.Rsh(r, 3) // ÷8
663663
uncleRewards = append(uncleRewards, *r)
664664

665-
r.Div(blockReward, u256.Num32)
665+
r.Rsh(blockReward, 5) // ÷32
666666
reward.Add(reward, r)
667667
}
668668
return *reward, uncleRewards

txnprovider/shutter/internal/crypto/encryption.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ func fp12Exp(base *blst.Fp12, exp *big.Int) *blst.Fp12 {
9696
result := &resultValue
9797

9898
zero := big.NewInt(0)
99-
two := big.NewInt(2)
10099

101100
baseCopyValue := blst.Fp12One()
102101
baseCopy := &baseCopyValue
@@ -108,7 +107,7 @@ func fp12Exp(base *blst.Fp12, exp *big.Int) *blst.Fp12 {
108107
result.MulAssign(baseCopy)
109108
}
110109
baseCopy.MulAssign(baseCopy)
111-
expCopy.Div(expCopy, two)
110+
expCopy.Rsh(expCopy, 1) // ÷2
112111
}
113112

114113
return result

0 commit comments

Comments
 (0)