-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix underflow in modexp gas calculation #883
Merged
aleksuss
merged 2 commits into
aurora-is-near:develop
from
guidovranken:fix-modexp-calc_iter_count-underflow
Dec 11, 2023
Merged
Fix underflow in modexp gas calculation #883
aleksuss
merged 2 commits into
aurora-is-near:develop
from
guidovranken:fix-modexp-calc_iter_count-underflow
Dec 11, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
birchmd
approved these changes
Dec 11, 2023
aleksuss
approved these changes
Dec 11, 2023
aleksuss
added a commit
that referenced
this pull request
Feb 6, 2024
## Description In `calc_iter_count()`, which is part of the modexp precompile gas calculation, the most-significant bit of the exponent is added to the intermediate result. The MSB is computed using `U256::from(exp.bits()) - U256::from(1))` in: ```rust Ok(U256::from(8) * U256::from(exp_len - 32) + U256::from(exp.bits()) - U256::from(1)) ``` This works only if the exponent is non-zero; subtracting `1` from zero results in an underflow, leading to inaccurate gas requirements for specific inputs. Geth computes the MSB [as follows](https://github.com/ethereum/go-ethereum/blob/fa0df76f3cfd186a1f06f2b80aa5dbb89555b009/core/vm/contracts.go#L340); notice the condition `bitlen > 0`: ```go var msb int if bitlen := expHead.BitLen(); bitlen > 0 { msb = bitlen - 1 } ``` This PR changes the underflowing subtraction into a `saturating_sub` so that an underflow never occurs. ## Performance / NEAR gas cost considerations ## Testing Fuzzing. ## How should this be reviewed A test has been added to `modexp.rs` which asserts that the required gas for the specified input is `65536`; an equivalent Go program which uses Geth to demonstrate this is provided below: ```go package main import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/common" "fmt" ) func main() { input := []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x00} modexp_address := common.BytesToAddress([]byte{0x05}) gas := vm.PrecompiledContractsBerlin[modexp_address].RequiredGas(input) fmt.Println(gas) } ``` ## Additional information Co-authored-by: Oleksandr Anyshchenko <oleksandr.anyshchenko@aurora.dev>
Merged
aleksuss
added a commit
that referenced
this pull request
Feb 6, 2024
## 3.6.0 2024-02-06 ### Fixes - Fixed underflow in the modexp gas calculation by [@guidovranken]. ([#883]) - Prevented subtraction underflow in th xcc module by [@guidovranken]. ([#888]) - Fixed balance and gas overflows in the xcc module by [@guidovranken]. ([#889]) ### Changes - CI was updated by changing self-hosted runner to the GitHub heavy by [@aleksuss]. ([#881]) - Removed a logic of fee calculation in the eth-connector by [@karim-en]. ([#882]) - Version of the rust nightly was updated to 2023-12-15 by [@RomanHodulak]. ([#885]) [#881]: #881 [#882]: #882 [#883]: #883 [#885]: #885 [#888]: #888 [#889]: #889 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Guido Vranken <guidovranken@users.noreply.github.com> Co-authored-by: Karim <karim@aurora.dev> Co-authored-by: Roman Hodulák <roman.hodulak@aurora.dev> Co-authored-by: Michael Birch <birchmd8@gmail.com> Co-authored-by: Michael Birch <michael.birch@aurora.dev> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
In
calc_iter_count()
, which is part of the modexp precompile gas calculation, the most-significant bit of the exponent is added to the intermediate result. The MSB is computed usingU256::from(exp.bits()) - U256::from(1))
in:This works only if the exponent is non-zero; subtracting
1
from zero results in an underflow, leading to inaccurate gas requirements for specific inputs.Geth computes the MSB as follows; notice the condition
bitlen > 0
:This PR changes the underflowing subtraction into a
saturating_sub
so that an underflow never occurs.Performance / NEAR gas cost considerations
Testing
Fuzzing.
How should this be reviewed
A test has been added to
modexp.rs
which asserts that the required gas for the specified input is65536
; an equivalent Go program which uses Geth to demonstrate this is provided below:Additional information