Skip to content

Commit cf144f3

Browse files
committed
refactor(vm): wrap TIP-7883 energy arithmetic with StrictMathWrapper
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.
1 parent d4bdf30 commit cf144f3

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,10 +755,11 @@ private long getEnergyTIP7883(int baseLen, int modLen,
755755
*/
756756
private long getMultComplexityTIP7883(int baseLen, int modLen) {
757757
long maxLength = StrictMathWrapper.max(baseLen, modLen);
758-
long words = (maxLength + 7) / 8; // ceil(maxLength / 8)
759758
if (maxLength <= 32) {
760759
return 16;
761760
}
761+
// ceil(maxLength / 8)
762+
long words = StrictMathWrapper.floorDiv(StrictMathWrapper.addExact(maxLength, 7L), 8L);
762763
return StrictMathWrapper.multiplyExact(2L, StrictMathWrapper.multiplyExact(words, words));
763764
}
764765

@@ -768,17 +769,20 @@ private long getMultComplexityTIP7883(int baseLen, int modLen) {
768769
*/
769770
private long getIterationCountTIP7883(byte[] expHighBytes, long expLen) {
770771
int leadingZeros = numberOfLeadingZeros(expHighBytes);
771-
int highestBit = 8 * expHighBytes.length - leadingZeros;
772+
long highestBit = StrictMathWrapper.subtractExact(
773+
StrictMathWrapper.multiplyExact(8L, expHighBytes.length), leadingZeros);
772774

773775
if (highestBit > 0) {
774-
highestBit--;
776+
highestBit = StrictMathWrapper.subtractExact(highestBit, 1L);
775777
}
776778

777779
long iterCount;
778780
if (expLen <= 32) {
779781
iterCount = highestBit;
780782
} else {
781-
iterCount = 16 * (expLen - 32) + highestBit;
783+
iterCount = StrictMathWrapper.addExact(
784+
StrictMathWrapper.multiplyExact(16L, StrictMathWrapper.subtractExact(expLen, 32L)),
785+
highestBit);
782786
}
783787

784788
return StrictMathWrapper.max(iterCount, 1L);

0 commit comments

Comments
 (0)