forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 38
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
limit modexp call to 8192 bit inputs #1391
Open
hexoscott
wants to merge
4
commits into
zkevm
Choose a base branch
from
modexp-8192-limit
base: zkevm
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−11
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,27 @@ func (c *bigModExp_zkevm) RequiredGas(input []byte) uint64 { | |
} else { | ||
input = input[:0] | ||
} | ||
|
||
// Retrieve the operands and execute the exponentiation | ||
var ( | ||
base = new(big.Int).SetBytes(getData(input, 0, baseLen.Uint64())) | ||
exp = new(big.Int).SetBytes(getData(input, baseLen.Uint64(), expLen.Uint64())) | ||
mod = new(big.Int).SetBytes(getData(input, baseLen.Uint64()+expLen.Uint64(), modLen.Uint64())) | ||
baseBitLen = base.BitLen() | ||
expBitLen = exp.BitLen() | ||
modBitLen = mod.BitLen() | ||
) | ||
|
||
// special revert case for ZK | ||
if baseBitLen == 0 && modBitLen > 8192 { | ||
return 0 | ||
} | ||
|
||
// limit to 8192 bits for base, exp, and mod in ZK - revert if we go over | ||
if baseBitLen > 8192 || expBitLen > 8192 || modBitLen > 8192 { | ||
return 0 | ||
} | ||
|
||
// Retrieve the head 32 bytes of exp for the adjusted exponent length | ||
var expHead *big.Int | ||
if big.NewInt(int64(len(input))).Cmp(baseLen) <= 0 { | ||
|
@@ -373,21 +394,37 @@ func (c *bigModExp_zkevm) Run(input []byte) ([]byte, error) { | |
} else { | ||
input = input[:0] | ||
} | ||
// Handle a special case when both the base and mod length is zero | ||
if baseLen == 0 && modLen == 0 { | ||
return []byte{}, nil | ||
} | ||
|
||
// Retrieve the operands and execute the exponentiation | ||
var ( | ||
base = new(big.Int).SetBytes(getData(input, 0, baseLen)) | ||
exp = new(big.Int).SetBytes(getData(input, baseLen, expLen)) | ||
mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) | ||
v []byte | ||
base = new(big.Int).SetBytes(getData(input, 0, baseLen)) | ||
exp = new(big.Int).SetBytes(getData(input, baseLen, expLen)) | ||
mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) | ||
v []byte | ||
baseBitLen = base.BitLen() | ||
expBitLen = exp.BitLen() | ||
modBitLen = mod.BitLen() | ||
) | ||
switch { | ||
case mod.BitLen() == 0: | ||
|
||
if modBitLen == 0 { | ||
// Modulo 0 is undefined, return zero | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment is not right There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed this comment. It was there from the original erigon implementation. |
||
return common.LeftPadBytes([]byte{}, int(modLen)), nil | ||
} | ||
|
||
if baseBitLen == 0 { | ||
if modBitLen > 8192 { | ||
return nil, ErrExecutionReverted | ||
} else { | ||
return []byte{}, nil | ||
} | ||
} | ||
|
||
// limit to 8192 bits for base, exp, and mod in ZK | ||
if baseBitLen > 8192 || expBitLen > 8192 || modBitLen > 8192 { | ||
return nil, ErrExecutionReverted | ||
} | ||
|
||
switch { | ||
case base.Cmp(libcommon.Big1) == 0: | ||
//If base == 1, then we can just return base % mod (if mod >= 1, which it is) | ||
v = base.Mod(base, mod).Bytes() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 modulo size is 0, the precompile returns 0 and it consumes GAS. This case is not handled.
if base size is 0 & modsize < 8192, it should also return 0 and consume GAS. This case is not handled.
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.