core/vm: implement eip 7883 (modexp repricing)

This commit is contained in:
Jared Wasinger 2025-04-10 22:15:26 +08:00 committed by MariusVanDerWijden
parent 6b3c85964e
commit e8d13ca9fb
2 changed files with 14 additions and 4 deletions

View file

@ -317,6 +317,7 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
// bigModExp implements a native big integer exponential modular operation. // bigModExp implements a native big integer exponential modular operation.
type bigModExp struct { type bigModExp struct {
eip2565 bool eip2565 bool
eip7883 bool
} }
var ( var (
@ -402,6 +403,7 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
} else { } else {
gas.Set(modLen) gas.Set(modLen)
} }
maxLen := new(big.Int).Set(gas)
if c.eip2565 { if c.eip2565 {
// EIP-2565 has three changes // EIP-2565 has three changes
// 1. Different multComplexity (inlined here) // 1. Different multComplexity (inlined here)
@ -415,6 +417,14 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
gas.Rsh(gas, 3) gas.Rsh(gas, 3)
gas.Mul(gas, gas) gas.Mul(gas, gas)
var minPrice uint64 = 200
if c.eip7883 {
minPrice = 500
if maxLen.Cmp(big32) > 0 {
gas.Mul(gas, big.NewInt(2))
}
}
if adjExpLen.Cmp(big1) > 0 { if adjExpLen.Cmp(big1) > 0 {
gas.Mul(gas, adjExpLen) gas.Mul(gas, adjExpLen)
} }
@ -423,9 +433,9 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
if gas.BitLen() > 64 { if gas.BitLen() > 64 {
return math.MaxUint64 return math.MaxUint64
} }
// 3. Minimum price of 200 gas
if gas.Uint64() < 200 { if gas.Uint64() < minPrice {
return 200 return minPrice
} }
return gas.Uint64() return gas.Uint64()
} }

@ -1 +1 @@
Subproject commit 81862e4848585a438d64f911a19b3825f0f4cd95 Subproject commit faf33b471465d3c6cdc3d04fbd690895f78d33f2