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).
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
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 tou64::MAX
. Since themodexp
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 ausize
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 intousize
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 intousize
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-bitusize
while the standalone tends to run on 64-bit platforms. This means that the above input (with exponent length equal tou64::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.