Skip to content
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(modexp): do not return an error when length cannot be cast to usize #737

Merged
merged 1 commit into from
Mar 31, 2023

Conversation

birchmd
Copy link
Member

@birchmd birchmd commented Mar 30, 2023

Description

EVM precompiles generally do not return errors; they take whatever input users give and do their best to return a sensible output. For example

go-ethereum$ ./build/bin/evm \
    --input 0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000c000000000000000000000000000000000000000000000000000000000000000071000000000000ff600000000000000000000000000000000000000000000000 \
    --receiver 0x0000000000000000000000000000000000000005 \
    run

0x

We see this input calls modexp with the size of both base and modulus both equal to zero, and the size of the exponent equal to u64::MAX. Since the modexp precompile output is bounded by the size of the modulus, we know the output is empty because the size of the modulus is zero, even though an exponent of the requested size could not actually exist in most hardware.

In our implementation we return an error from the modexp precompile if the lengths do not fit in a usize value, which in the case of Wasm is 32 bits. Therefore our implementation returns an error on the above input instead of retuning the empty value that geth returns. This PR fixes the issue by using a saturating cast into usize from the given input. This should be fine because non-trivial inputs (i.e. inputs where the modulus is non-zero) will have lengths that fit into usize no problem, or else the computation will run out of gas.

This problem of returning an error for the usize cast is actually a little worse than just incompatibility with Ethereum. It also creates an inconsistency with the standalone engine because Wasm has 32-bit usize while the standalone tends to run on 64-bit platforms. This means that the above input (with exponent length equal to u64::MAX) returns an error in the Wasm contract, but passed in the standalone engine. After the change in this PR it passes on both compilation targets (as verified by the new integration test case).

Testing

Additional test case in modexp integration tests.

@birchmd birchmd added C-bug Category: Something isn't working. A-precompiles Area: Issues that relate to the precompiles. labels Mar 30, 2023
Copy link
Contributor

@0x3bfc 0x3bfc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@joshuajbouw joshuajbouw added this pull request to the merge queue Mar 31, 2023
Merged via the queue into develop with commit 4d04777 Mar 31, 2023
@joshuajbouw joshuajbouw deleted the fix/birchmd/modexp-error branch March 31, 2023 11:49
@joshuajbouw joshuajbouw mentioned this pull request Apr 5, 2023
joshuajbouw added a commit that referenced this pull request Apr 5, 2023
## [2.9.0] 2023-04-05

### Added

- Enabled XCC for mainnet release by [@birchmd]. ([#694])
- Added `set_owner` contract method which sets the owner of the contract
by [@hskang9]. ([#690])
- New variant of submit function `submit_with_args` which accepts
additional arguments along with the transaction such as the max gas
price a user is ready to pay by [@aleksuss]. ([#696])
- Added the ability to create and fund XCC sub-accounts from external
NEAR accounts by [@birchmd]. ([#735])

### Changes

- Replaced `rjson` with `serde_json` by [@aleksuss]. ([#677])
- Changed owner intended contract methods to now require owner or the
contract itself by [@hskang9]. ([#676])

### Fixes

- Fixed nonce incorrectly being incremented on an out of fund failure by
[@joshuajbouw]. ([#671])
- Fixed a check in promise results before executing cross contract calls
(XCC) callbacks by [@birchmd]. ([#693])
- Fixed a reachable panic in `receive_erc20_tokens` by [@0x3bfc].
([#709])
- Fixed a lack of minimum size checks when instantiating a new `EthGas`
object by [@lempire123]. ([#722])
- Fixed a lack of division by 0 checks in `EthGas::Div()` by
[@lempire123]. ([#718])
- Fixed the validation of the return of `exports:storage_remove` by
[@0x3bfc]. ([#712])
- Fixed missing account validations of NEAR account IDs by [@0x3bfc].
([#703])
- Fixed a reachable panic in the `exitToNear` and `exitToEthereum`
precompiles if the input amount is greater than 1^128 when cast from a
`U256` to `u128` by [@0x3bfc]. ([#681])
- Fixed a reachable panic in `modExp` due to arithmetic overflow by
[@0x3bfc]. ([#688])
- Fixed the ability attaching values to Aurora specific precompiles,
this no longer is possible, by [@0x3bfc]. ([#714])
- Fixed a return error if an ecrecover signature length is not exactly
65 by [@0x3bfc]. ([#717])
- Fixed size checks on input array passed to `exitToNear` and
`exitToEthereum` precompiles by [@0x3bfc]. ([#684])
- Fixed missing gas costs in `exitToNear` and `exitToEthereum`
precompiles by [@lempire123]. ([#687])
- Fixed a reachable panic due to out of memory in the `modExp`
precompile by [@0x3bfc]. ([#689])
- Fixed an assurance that the `sender_id` has a balance greater than the
amount in `ft_transfer_call` by [@0x3bfc]. ([#708])
- Fixed returning `0x` when a length cannot be cast as `usize` instead
of returning an error in the `modExp` precompile by [@birchmd]. ([#737])
- Miscellaneous minor fixes by [@0x3bfc]. ([#738])

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Leandro Casuso Montero <[email protected]>
Co-authored-by: Michael Birch <[email protected]>
Co-authored-by: Alexey Lapitsky <[email protected]>
Co-authored-by: Oleksandr Anyshchenko <[email protected]>
Co-authored-by: Alexey Lapitsky <[email protected]>
Co-authored-by: Hyungsuk Kang <[email protected]>
Co-authored-by: Ahmed Ali <[email protected]>
Co-authored-by: lempire123 <[email protected]>
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
Labels
A-precompiles Area: Issues that relate to the precompiles. C-bug Category: Something isn't working.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants