feat(vm): implement TIP-7883 ModExp gas cost increase#6654
Conversation
be82ead to
f13e430
Compare
| 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; |
There was a problem hiding this comment.
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.
| */ | ||
| private long getMultComplexityTIP7883(int baseLen, int modLen) { | ||
| long maxLength = max(baseLen, modLen, VMConfig.disableJavaLangMath()); | ||
| long words = (maxLength + 7) / 8; // ceil(maxLength / 8) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
[P1] Suggestion: use overflow-safe arithmetic for long-term review burden reduction
2 * words * words uses plain long multiplication. Proving it won't overflow requires tracing the full input chain: parseLen() → intValueSafe() returns int → max words = 268,435,456 → 2 * words² = 1.44×10¹⁷ < Long.MAX_VALUE. This reasoning is correct today, but not self-evident — if parseLen() return type or UPPER_BOUND ever changes, the safety assumption silently breaks.
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.
There was a problem hiding this comment.
Thanks for flagging this. Applied in e512e21 — used StrictMathWrapper.multiplyExact instead of Math.multiplyExact to stay consistent with the cross-platform-determinism convention this repo follows (the Maths javadoc explicitly redirects new code to StrictMathWrapper). Same fail-fast guarantee, and reviewers no longer need to trace parseLen() → int → UPPER_BOUND to convince themselves the multiplication is safe.
| * 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()); |
There was a problem hiding this comment.
[SHOULD]getMultComplexityTIP7883 and getIterationCountTIP7883 are new methods with no legacy constraints. They should use StrictMathWrapper directly instead of the deprecated Maths.max(a, b, boolean) pattern.
Suggested changes:
// 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);Reasons:
Mathsis@Deprecated— new code should not add more callers.2 * words * wordsrelies on manual reasoning about value ranges for overflow safety. UsingmultiplyExactmakes this explicit and fail-fast.
There was a problem hiding this comment.
Thanks, all three suggestions applied in e512e21 — getMultComplexityTIP7883 / getIterationCountTIP7883 now call StrictMathWrapper.max directly, and the 2 * words * words product is wrapped in StrictMathWrapper.multiplyExact so the overflow argument no longer depends on parseLen() returning int. Agreed that new code shouldn't keep adding callers to the deprecated Maths helper.
e512e21 to
287c0b7
Compare
Raise the floor to 500, switch the >32-byte branch to 2*words², and bump the long-exponent multiplier to 16. Drop the OSAKA gate's config-file plumbing — the on-chain proposal is the only switch.
Verify the new floor, the doubled-formula branch at the 33-byte boundary, and that pre-OSAKA pricing is preserved when the gate is off.
287c0b7 to
d4bdf30
Compare
Make overflow safety explicit instead of relying on int range, and keep all arithmetic in the new energy helpers consistent with the existing StrictMathWrapper-based ops.
2bd536e to
7d9201f
Compare
…-test-fixes # Conflicts: # framework/src/test/java/org/tron/common/runtime/vm/AllowTvmOsakaTest.java
Summary
Implements the current TIP-7883 draft (TRON adoption of EIP-7883) by switching the ModExp precompile to the EIP-7883 pricing formula under
allowTvmOsaka, and removes the now-redundant config-file plumbing forallowTvmOsakaso activation is proposal-only.Baseline note: TRON legacy pricing is not EIP-2565
EIP-7883 is specified as a change from EIP-2565, but java-tron's pre-Osaka ModExp pricing still follows the existing EIP-198-style path: the piecewise multiplication complexity with
GQUAD_DIVISOR = 20. This PR intentionally implements the TIP/EIP-7883 formula at activation time; it is not a monotonic multiplier over java-tron's current legacy pricing for every possible input.Examples versus java-tron legacy pricing:
nagydani_1_square:204 -> 500nagydani_2_square:665 -> 512(lower than legacy because TIP-7883 follows the newer EIP-2565-family complexity shape)nagydani_5_pow0x10001:285900 -> 524288The net energy change is therefore input-dependent. The intended compatibility target is the TIP-7883/EIP-7883 schedule, not preserving a strict increase over every legacy java-tron input.
Pricing formula changes under
allowTvmOsaka500.16formaxLen <= 32, and2 * ceil(maxLen / 8)^2formaxLen > 32.1; forexpLen > 32, the multiplier changes from the EIP-2565-family8to16.allowTvmOsaka == 0.allowTvmOsakaconfig-knob removalThe Osaka gate was previously settable from a config file as well as the proposal. This PR makes it proposal-only, mirroring the
ALLOW_TVM_SELFDESTRUCT_RESTRICTIONshape:CommonParameter.allowTvmOsakafield deletedCommitteeConfig.allowTvmOsakafield deletedArgs.applyCommitteeConfigassignment deletedframework/src/main/resources/config.confsample line deletedcommon/src/main/resources/reference.confdefault deletedDynamicPropertiesStore.getAllowTvmOsakano longer falls back toCommonParameter; reads the DB and.orElse(0L)Operators do not lose the governance activation path: Osaka activates strictly through the on-chain proposal.
Gate granularity
allowTvmOsakaintentionally gates Osaka-aligned TVM changes as a coherent upgrade flag rather than one flag per TIP. That keeps TRON's activation model aligned with upstream fork semantics and avoids a partial-Osaka state for cross-chain tooling and gas estimators. If a testnet-only issue is found before activation, the proposal can simply remain off. If a post-activation issue requires separating one behavior, the cleaner follow-up is a subsequent fork-level override flag rather than splitting this activation gate now.Tests
testEIP7883ModExpPricingcovers the new floor, the doubled-formula branch at the 33-byte boundary, and standard nagydani vectors.testEIP7883DisabledPreservesOldPricingpins the legacy EIP-198-style pricing whenallowTvmOsaka == 0.testEIP7883CanBeLowerThanLegacyPricingdocuments the non-monotonic comparison against java-tron's current legacy formula.Spec