Skip to content

EIP-7823: Set upper bounds for MODEXP #4070

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Conversation

am1r021
Copy link
Contributor

@am1r021 am1r021 commented May 6, 2025

This change implements EIP-7823, which is being considered for inclusion in Fusaka. With this EIP activated, the MODEXP precompile will reject any requests with any inputs of length greater than 1024 bytes, and will consume all gas in the process.

Copy link

codecov bot commented May 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.34%. Comparing base (358a097) to head (aecc4b5).

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
block 84.33% <ø> (ø)
blockchain 89.32% <ø> (ø)
client 67.61% <ø> (ø)
common 97.50% <100.00%> (+<0.01%) ⬆️
devp2p 86.78% <ø> (ø)
evm 73.11% <100.00%> (+0.03%) ⬆️
mpt 89.74% <ø> (-0.11%) ⬇️
statemanager 69.06% <ø> (ø)
static 99.11% <ø> (ø)
tx 89.83% <ø> (ø)
util 88.70% <100.00%> (+<0.01%) ⬆️
vm 55.31% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

/**
* Compute base^exp mod modulus for *arbitrarily* large BigInts
*/
function modPow(base: bigint, exp: bigint, modulus: bigint): bigint {
Copy link
Member

Choose a reason for hiding this comment

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

Can't this be done directly with bigints?

Copy link
Contributor Author

@am1r021 am1r021 May 26, 2025

Choose a reason for hiding this comment

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

When exponentiation is done natively with bigints using the ** operator, the value that's being raised to a power is literally formed in memory (base×base×⋯×base), which for very large numbers is astronomically huge and will quickly blow up with Maximum BigInt size exceeded. This helper function uses the binary exponentiation algorithm, similar to the actual precompile implementation, in order to get around that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've also updated to remove the new helper method and just export the expMod helper function used in the precomp itself.

it(`MODEXP should reject and consume all gas for inputs over 8192 bits in length - case ${maxInputLen} bytes`, async () => {
const result = await MODEXP({
data: input,
gasLimit: BigInt(0xffffffff),
Copy link
Member

Choose a reason for hiding this comment

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

I think there should be a sanity check to confirm that this gasLimit is enough to pay for the precompile, but that the precompile now rejects it due to the incode length limit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have added

Copy link
Member

@jochem-brouwer jochem-brouwer left a comment

Choose a reason for hiding this comment

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

One of EVM tests is failing, but when comparing the output it seems like the bytes have shifted by one byte (first entry is the zero byte, the remainder is all the same) so it seems to be some kind of offset problem where data is written to?

@am1r021
Copy link
Contributor Author

am1r021 commented May 29, 2025

One of EVM tests is failing, but when comparing the output it seems like the bytes have shifted by one byte (first entry is the zero byte, the remainder is all the same) so it seems to be some kind of offset problem where data is written to?

The issue was because I was using bigIntToBytes to convert the expected value to bytes to compare to the actual, which right pads till the value is of even length. Have fixed! 🙂

@am1r021 am1r021 requested a review from jochem-brouwer May 29, 2025 22:02
Copy link
Member

@jochem-brouwer jochem-brouwer left a comment

Choose a reason for hiding this comment

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

One small suggestion! 😄 👍

@@ -51,3 +71,81 @@ describe('Precompiles: MODEXP', () => {
assert.strictEqual(result.executionGasUsed, gas)
})
})

function enoughGas(opts: PrecompileInput): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

Now the "utility methods" are exported to calculate the gas fee here, but I think it would be more clean to instead write a gas calculator method inside the precompile and export that to calculate the required gas.

}

// sanity check to make sure there is enough gas in order to isolate error to cases causing OOG due to size limit of inputs
while (!enoughGas(opts)) opts.gasLimit *= BigInt(256)
Copy link
Member

Choose a reason for hiding this comment

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

If we then have the "calculate gas" method, we can thus "just set" this as the gasLimit. Even if the gas limit is enough, then it will still run out-of-gas because of the inputs being too large.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants