|
17 | 17 | import org.hyperledger.besu.evm.EVM; |
18 | 18 | import org.hyperledger.besu.evm.frame.MessageFrame; |
19 | 19 | 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; |
23 | 21 |
|
24 | 22 | import org.apache.tuweni.bytes.Bytes; |
25 | | -import org.apache.tuweni.bytes.Bytes32; |
26 | 23 |
|
27 | 24 | /** The Mod operation. */ |
28 | 25 | public class ModOperation extends AbstractFixedCostOperation { |
@@ -53,24 +50,11 @@ public Operation.OperationResult executeFixedCostOperation( |
53 | 50 | public static OperationResult staticOperation(final MessageFrame frame) { |
54 | 51 | final Bytes value0 = frame.popStackItem(); |
55 | 52 | 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); |
74 | 58 | return modSuccess; |
75 | 59 | } |
76 | 60 | } |
0 commit comments