-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()); | ||
|
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. [SHOULD]
// getMultComplexityTIP7883
- long maxLength = max(baseLen, modLen, VMConfig.disableJavaLangMath());
+ long maxLength = StrictMathWrapper.max(baseLen, modLen);
// avoid silent overflow on 2 * words * words
- return 2 * words * words;
+ return StrictMathWrapper.multiplyExact(2L, StrictMathWrapper.multiplyExact(words, words));
// getIterationCountTIP7883
- return max(iterCount, 1, VMConfig.disableJavaLangMath());
+ return StrictMathWrapper.max(iterCount, 1L);
|
||
| 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; | ||
|
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. [P1] Suggestion: use overflow-safe arithmetic for long-term review burden reduction
Using overflow-safe arithmetic makes the code self-evidently safe with zero external reasoning required, reducing the review burden for future readers: return Math.multiplyExact(2, Math.multiplyExact(words, words));This is a long-term maintainability suggestion, not a current correctness issue. |
||
| } | ||
|
|
||
| /** | ||
| * 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(); | ||
|
|
||
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.