diff --git a/test/state/precompiles.cpp b/test/state/precompiles.cpp index a41bab9632..f20177ecc3 100644 --- a/test/state/precompiles.cpp +++ b/test/state/precompiles.cpp @@ -168,14 +168,21 @@ PrecompileAnalysis expmod_analyze(bytes_view input, evmc_revision rev) noexcept const auto exp_len256 = be::unsafe::load(&input_header[32]); const auto mod_len256 = be::unsafe::load(&input_header[64]); - if (base_len256 == 0 && mod_len256 == 0) - return {min_gas, 0}; - + // Check the declared input lengths against the practical (2**32) + // or specified (EIP-7823: 2**10) limits. const auto len_limit = rev < EVMC_OSAKA ? std::numeric_limits::max() : MODEXP_LEN_LIMIT_EIP7823; - if (base_len256 > len_limit || exp_len256 > len_limit || mod_len256 > len_limit) + if (base_len256 > len_limit || mod_len256 > len_limit) return {GasCostMax, 0}; + if (exp_len256 > len_limit) + { + // Before EIP-7823, the big exponent may be canceled with zero multiplication complexity. + if (rev < EVMC_OSAKA && base_len256 == 0 && mod_len256 == 0) + return {min_gas, 0}; + return {GasCostMax, 0}; + } + const auto base_len = static_cast(base_len256); const auto exp_len = static_cast(exp_len256); const auto mod_len = static_cast(mod_len256); diff --git a/test/unittests/precompiles_expmod_test.cpp b/test/unittests/precompiles_expmod_test.cpp index e9944e4a1e..72f5799bdc 100644 --- a/test/unittests/precompiles_expmod_test.cpp +++ b/test/unittests/precompiles_expmod_test.cpp @@ -182,6 +182,9 @@ TEST(expmod, incomplete_inputs) {"0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000003 ba ee 000000 fe", "000000"}, {"0000000000000000000000000000000000000000000000000000000000000010 0000000000000000000000000000000000000000000000000000000000000010 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000001", "00"}, {"000000000000000000000000000000000000000000000000000000000000ffff 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000001 80", "00"}, + {"0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000000", ""}, + {"0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000002 0000000000000000000000000000000000000000000000000000000000000000 80", ""}, + {"0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000100000000 0000000000000000000000000000000000000000000000000000000000000000 80", ""}, // clang-format on };