mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 15:47:21 +00:00
core/vm: fix modexp gas calculation (#32568)
fixes a bug in the gas calculation found by oss-fuzz
This commit is contained in:
parent
d68528cadb
commit
dd7fe1be4b
2 changed files with 35 additions and 4 deletions
|
|
@ -469,12 +469,19 @@ func modexpIterationCount(expLen uint64, expHead uint256.Int, multiplier uint64)
|
|||
|
||||
// For large exponents (expLen > 32), add (expLen - 32) * multiplier
|
||||
if expLen > 32 {
|
||||
iterationCount = (expLen - 32) * multiplier
|
||||
carry, count := bits.Mul64(expLen-32, multiplier)
|
||||
if carry > 0 {
|
||||
return math.MaxUint64
|
||||
}
|
||||
iterationCount = count
|
||||
}
|
||||
|
||||
// Add the MSB position - 1 if expHead is non-zero
|
||||
if bitLen := expHead.BitLen(); bitLen > 0 {
|
||||
iterationCount += uint64(bitLen - 1)
|
||||
count, carry := bits.Add64(iterationCount, uint64(bitLen-1), 0)
|
||||
if carry > 0 {
|
||||
return math.MaxUint64
|
||||
}
|
||||
iterationCount = count
|
||||
}
|
||||
|
||||
return max(iterationCount, 1)
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
|
|||
func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
|
||||
p := allPrecompiles[common.HexToAddress(addr)]
|
||||
in := common.Hex2Bytes(test.Input)
|
||||
gas := p.RequiredGas(in) - 1
|
||||
gas := test.Gas - 1
|
||||
|
||||
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
|
||||
_, _, err := RunPrecompiledContract(p, in, gas, nil)
|
||||
|
|
@ -257,6 +257,30 @@ func TestPrecompiledModExpOOG(t *testing.T) {
|
|||
for _, test := range modexpTests {
|
||||
testPrecompiledOOG("05", test, t)
|
||||
}
|
||||
modexpTestsEIP2565, err := loadJson("modexp_eip2565")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range modexpTestsEIP2565 {
|
||||
testPrecompiledOOG("f5", test, t)
|
||||
}
|
||||
modexpTestsEIP7883, err := loadJson("modexp_eip7883")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range modexpTestsEIP7883 {
|
||||
testPrecompiledOOG("f6", test, t)
|
||||
}
|
||||
gasCostTest := precompiledTest{
|
||||
Input: "000000000000000000000000000000000000000000000000000000000000082800000000000000000000000000000000000000000000000040000000000000090000000000000000000000000000000000000000000000000000000000000600000000adadadad00000000ff31ff00000006ffffffffffffffffffffffffffffffffffffffff0000000000000004ffffffffffffff0000000000000000000000000000000000000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0000001000200fefffeff01000100000000000000ffff01000100ffffffff01000100ffffffff0000050001000100fefffdff02000300ff000000000000012b000000000000090000000000000000000000000000000000000000000000000000ffffff000000000200fffffeff00000001000000000001000200fefffeff010001000000000000000000423034000000000011006161ffbf640053004f00ff00fffffffffffffff3ff00000000000f00002dffffffffff0000000000000000000061999999999999999999999999899961ffffffff0100010000000000000000000000000600000000adadadad00000000ffff00000006fffffdffffffffffffffffffffffffffffffffff0000000000000004ffffffffffffff000000000000000000000000000000000000000098000000966375726c2f66000030000000000011006161ffbf640053004f002d00000000a200000000000000ff1818183fffffffff3a6e756c6c2c22223a6e7500006c2000000000002d2d0000000000000000000144ccef0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000fdff000000ff00290001000009000000000000000000000000000000000000000000000000a50004ff2800000000000000000000000000000000000000000000000001000000000000090000000000000000000000030000000000000000002b00000000000000000600000000adadadad00000000ffff00000006ffffffffffffffffffffffffffffffffffffffff0000000000000004ffffffffffffff0000000000000000000000000000000000000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d000000000717a1a001a1a1a1a1a1a000000121212121212121212121212121212121212121212d0d0d0d01212121212121212121212121212121212121212121212121212121212121212121212121212121212121212373800002d35373837346137346161610000000000000000d0d0d0d0d0d0d0d0002d3533321a1a000000d0d0d0d0d0d0d0d0d0d0d0d0d0d000000000717a1a001a1a1a1a1a1a000000121212121212121212121212121212121212121212d0d0d0d012121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121a1212121212121212000000000000000000000000d0d0d0d0d0d0d0d0002d3533321a1a0000000000000000000000003300000001000f5b00001100712c6eff9e61000000000061000000fbffff1a1a3a6e353900756c6c7d3b00000000009100002d35ff00600000000000000000002d3533321a1a1a1a3a6e353900756c6c7d3b000000000091373800002d3537383734613734616161d0d0d0d0d000000000717a1a001a1a1a1a1a1a000000121212121212121212121212121212121212121212d0d0d0d012121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121a1212121212121212000000000000000000000000d0d0d0d0d0d0d0d0002d3533321a1a0000000000000000000000003300000001000f5b00001100712c6eff9e61000000000061000000fbffff1a1a3a6e353900756c6c7d3b00000000009100002d35ff00600000000000000000002d3533321a1a1a1a3a6e353900756c6c7d3b000000000091373800002d353738373461373461616100000000000000000000000000000000000000000000000001000000000000090000000000000000000000030000000000000000002b00000000000000000600000000adadadad00000000ffff00000006ffffffffffffffffffffffffffffffffffffffff0000000000000004ffffffffffffff0000000000000000000000000000000000000000d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d000000000717a1a001a1a1a1a1a1a000000121212121212121212121212121212121212121212d0d0d0d01212121212121212121212121212121212121212121212121212121212121212121212121212121212121212373800002d35373837346137346161610000000000000000d0d0d0d0d0d0d0d0002d3533321a1a000000d0d0d0d0d0d0d0d0d0d0d0d0d0d000000000717a1a001a1a1a1a1a1a000000121212121212121212121212121212121212121212d0d0d0d012121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121a1212121212121212000000000000000000000000d0d0d0d0d0d0d0d0002d3533321a1a0000000000000000000000003300000001000f5b00001100712c6eff9e61000000000061000000fbffff1a1a3a6e353900756c6c7d3b00000000009100002d35ff00600000000000000000002d3533321a1a1a1a3a6e353900756c6c7d3b000000000091373800002d3537383734613734616161d0d0d0d0d000000000717a1a001a1a1a1a1a1a0000001212121212121212121212121212121212121212000000000000003300000001000f5b00001100712c6eff9e61000000000061000000fbffff1a1a3a6e353900756c6c7d3b00000000009100002d35ff00600000000000000000002d3533321a1a1a1a3a6e353900756c6c7d3b000000000091373800002d3537383734613734616161",
|
||||
Expected: "000000000000000000000000000000000000000000000000",
|
||||
Name: "oss_fuzz_gas_calc",
|
||||
Gas: 18446744073709551615,
|
||||
NoBenchmark: false,
|
||||
}
|
||||
testPrecompiledOOG("05", gasCostTest, t)
|
||||
testPrecompiledOOG("f5", gasCostTest, t)
|
||||
testPrecompiledOOG("f6", gasCostTest, t)
|
||||
}
|
||||
|
||||
// Tests the sample inputs from the elliptic curve scalar multiplication EIP 213.
|
||||
|
|
|
|||
Loading…
Reference in a new issue