-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(vm): implement TIP-7883 ModExp gas cost increase #6654
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
yanghang8612
wants to merge
1
commit into
tronprotocol:develop
Choose a base branch
from
yanghang8612:implement-tip-7883
base: develop
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.
+155
−0
Open
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -639,6 +639,10 @@ public long getEnergyForData(byte[] data) { | |
| byte[] expHighBytes = parseBytes(data, addSafely(ARGS_OFFSET, baseLen), min(expLen, 32, | ||
| VMConfig.disableJavaLangMath())); | ||
|
|
||
| if (VMConfig.allowTvmOsaka()) { | ||
| return getEnergyTIP7883(baseLen, modLen, expHighBytes, expLen); | ||
| } | ||
|
|
||
| long multComplexity = getMultComplexity(max(baseLen, modLen, VMConfig.disableJavaLangMath())); | ||
| long adjExpLen = getAdjustedExponentLength(expHighBytes, expLen); | ||
|
|
||
|
|
@@ -722,6 +726,63 @@ private long getAdjustedExponentLength(byte[] expHighBytes, long expLen) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * TIP-7883: ModExp gas cost increase. | ||
| * New pricing formula with higher minimum cost and no divisor. | ||
| */ | ||
| private long getEnergyTIP7883(int baseLen, int modLen, | ||
| byte[] expHighBytes, int expLen) { | ||
| long multComplexity = getMultComplexityTIP7883(baseLen, modLen); | ||
| long iterCount = getIterationCountTIP7883(expHighBytes, expLen); | ||
|
|
||
| // use big numbers to stay safe in case of overflow | ||
| BigInteger energy = BigInteger.valueOf(multComplexity) | ||
| .multiply(BigInteger.valueOf(iterCount)); | ||
|
|
||
| BigInteger minEnergy = BigInteger.valueOf(500); | ||
| if (isLessThan(energy, minEnergy)) { | ||
| return 500L; | ||
| } | ||
|
|
||
| return isLessThan(energy, BigInteger.valueOf(Long.MAX_VALUE)) ? energy.longValueExact() | ||
| : Long.MAX_VALUE; | ||
| } | ||
|
|
||
| /** | ||
| * TIP-7883: New multiplication complexity formula. | ||
| * Minimal complexity of 16; doubled complexity for base/modulus > 32 bytes. | ||
| */ | ||
| private long getMultComplexityTIP7883(int baseLen, int modLen) { | ||
| long maxLength = max(baseLen, modLen, VMConfig.disableJavaLangMath()); | ||
| long words = (maxLength + 7) / 8; // ceil(maxLength / 8) | ||
|
Collaborator
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. The inline comment // ceil(maxLength / 8) is a nice touch — it makes the intent of (maxLength + 7) / 8 immediately clear without needing to mentally decode the arithmetic. The structure also maps 1:1 to the spec's pseudocode, which makes auditing straightforward. |
||
| if (maxLength <= 32) { | ||
| return 16; | ||
| } | ||
| return 2 * words * words; | ||
| } | ||
|
|
||
| /** | ||
| * TIP-7883: New iteration count formula. | ||
| * Multiplier for exponents > 32 bytes increased from 8 to 16. | ||
| */ | ||
| private long getIterationCountTIP7883(byte[] expHighBytes, long expLen) { | ||
| int leadingZeros = numberOfLeadingZeros(expHighBytes); | ||
| int highestBit = 8 * expHighBytes.length - leadingZeros; | ||
|
|
||
| if (highestBit > 0) { | ||
| highestBit--; | ||
| } | ||
|
|
||
| long iterCount; | ||
| if (expLen <= 32) { | ||
| iterCount = highestBit; | ||
| } else { | ||
| iterCount = 16 * (expLen - 32) + highestBit; | ||
| } | ||
|
|
||
| return max(iterCount, 1, VMConfig.disableJavaLangMath()); | ||
| } | ||
|
|
||
| private int parseLen(byte[] data, int idx) { | ||
| byte[] bytes = parseBytes(data, 32 * idx, 32); | ||
| return new DataWord(bytes).intValueSafe(); | ||
|
|
||
This file contains hidden or 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
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.
Good defensive coding — using BigInteger for the intermediate multiplication avoids any potential overflow with large baseLen/modLen values (up to 1024 bytes), and the Long.MAX_VALUE cap ensures the result is always safe to return as a long. Well handled.