-
Notifications
You must be signed in to change notification settings - Fork 817
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
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
/** | ||
* Compute base^exp mod modulus for *arbitrarily* large BigInts | ||
*/ | ||
function modPow(base: bigint, exp: bigint, modulus: bigint): bigint { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have added
There was a problem hiding this 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?
…-monorepo into implement-eip-7823
…-monorepo into implement-eip-7823
…to implement-eip-7823
…to implement-eip-7823
The issue was because I was using |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
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.