Skip to content

Commit 7406569

Browse files
FIX: MOD uses UInt256
Signed-off-by: Thomas Zamojski <thomas.zamojski@quadratic-labs.com>
1 parent 7ee807a commit 7406569

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

evm/src/main/java/org/hyperledger/besu/evm/operation/ModOperation.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
import org.hyperledger.besu.evm.EVM;
1818
import org.hyperledger.besu.evm.frame.MessageFrame;
1919
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
20-
21-
import java.math.BigInteger;
22-
import java.util.Arrays;
20+
import org.hyperledger.besu.evm.UInt256;
2321

2422
import org.apache.tuweni.bytes.Bytes;
25-
import org.apache.tuweni.bytes.Bytes32;
2623

2724
/** The Mod operation. */
2825
public class ModOperation extends AbstractFixedCostOperation {
@@ -53,24 +50,11 @@ public Operation.OperationResult executeFixedCostOperation(
5350
public static OperationResult staticOperation(final MessageFrame frame) {
5451
final Bytes value0 = frame.popStackItem();
5552
final Bytes value1 = frame.popStackItem();
56-
if (value1.isZero()) {
57-
frame.pushStackItem(Bytes32.ZERO);
58-
} else {
59-
BigInteger b1 = new BigInteger(1, value0.toArrayUnsafe());
60-
BigInteger b2 = new BigInteger(1, value1.toArrayUnsafe());
61-
final BigInteger result = b1.mod(b2);
62-
63-
Bytes resultBytes = Bytes.wrap(result.toByteArray());
64-
if (resultBytes.size() > 32) {
65-
resultBytes = resultBytes.slice(resultBytes.size() - 32, 32);
66-
}
67-
68-
final byte[] padding = new byte[32 - resultBytes.size()];
69-
Arrays.fill(padding, result.signum() < 0 ? (byte) 0xFF : 0x00);
70-
71-
frame.pushStackItem(Bytes.concatenate(Bytes.wrap(padding), resultBytes));
72-
}
73-
53+
UInt256 b1 = UInt256.fromBytesBE(value0.toArrayUnsafe());
54+
UInt256 b2 = UInt256.fromBytesBE(value1.toArrayUnsafe());
55+
final UInt256 result = b1.mod(b2);
56+
Bytes resultBytes = Bytes.wrap(result.toBytesBE());
57+
frame.pushStackItem(resultBytes);
7458
return modSuccess;
7559
}
7660
}

0 commit comments

Comments
 (0)