diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/AndOperationV2.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/AndOperationV2.java new file mode 100644 index 00000000000..55852ecb98a --- /dev/null +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/AndOperationV2.java @@ -0,0 +1,64 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.evm.operation.v2; + +import org.hyperledger.besu.evm.EVM; +import org.hyperledger.besu.evm.frame.MessageFrame; +import org.hyperledger.besu.evm.gascalculator.GasCalculator; +import org.hyperledger.besu.evm.operation.Operation; + +/** EVM v2 AND operation using long[] stack representation. */ +public class AndOperationV2 extends AbstractFixedCostOperationV2 { + + private static final Operation.OperationResult AND_SUCCESS = + new Operation.OperationResult(3, null); + + /** + * Instantiates a new And operation. + * + * @param gasCalculator the gas calculator + */ + public AndOperationV2(final GasCalculator gasCalculator) { + super(0x16, "AND", 2, 1, gasCalculator, gasCalculator.getVeryLowTierGasCost()); + } + + @Override + public Operation.OperationResult executeFixedCostOperation( + final MessageFrame frame, final EVM evm) { + return staticOperation(frame, frame.stackDataV2()); + } + + /** + * Performs AND on the v2 long[] stack. + * + * @param frame the message frame + * @param s the stack data as a long[] array + * @return the operation result + */ + public static Operation.OperationResult staticOperation( + final MessageFrame frame, final long[] s) { + final int top = frame.stackTopV2(); + final int a = (top - 1) * 4; + final int b = (top - 2) * 4; + + s[b] = s[a] & s[b]; + s[b + 1] = s[a + 1] & s[b + 1]; + s[b + 2] = s[a + 2] & s[b + 2]; + s[b + 3] = s[a + 3] & s[b + 3]; + + frame.setTopV2(top - 1); + return AND_SUCCESS; + } +} diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/NotOperationV2.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/NotOperationV2.java new file mode 100644 index 00000000000..168d5d2838a --- /dev/null +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/NotOperationV2.java @@ -0,0 +1,62 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.evm.operation.v2; + +import org.hyperledger.besu.evm.EVM; +import org.hyperledger.besu.evm.frame.MessageFrame; +import org.hyperledger.besu.evm.gascalculator.GasCalculator; +import org.hyperledger.besu.evm.operation.Operation; + +/** EVM v2 NOT operation using long[] stack representation. */ +public class NotOperationV2 extends AbstractFixedCostOperationV2 { + + private static final Operation.OperationResult NOT_SUCCESS = + new Operation.OperationResult(3, null); + + /** + * Instantiates a new Not operation. + * + * @param gasCalculator the gas calculator + */ + public NotOperationV2(final GasCalculator gasCalculator) { + super(0x19, "NOT", 1, 1, gasCalculator, gasCalculator.getVeryLowTierGasCost()); + } + + @Override + public Operation.OperationResult executeFixedCostOperation( + final MessageFrame frame, final EVM evm) { + return staticOperation(frame, frame.stackDataV2()); + } + + /** + * Performs NOT on the v2 long[] stack. + * + * @param frame the message frame + * @param s the stack data as a long[] array + * @return the operation result + */ + public static Operation.OperationResult staticOperation( + final MessageFrame frame, final long[] s) { + final int top = frame.stackTopV2(); + final int a = (top - 1) * 4; + + s[a] = ~s[a]; + s[a + 1] = ~s[a + 1]; + s[a + 2] = ~s[a + 2]; + s[a + 3] = ~s[a + 3]; + + return NOT_SUCCESS; + } +} diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/OrOperationV2.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/OrOperationV2.java new file mode 100644 index 00000000000..a259195f291 --- /dev/null +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/OrOperationV2.java @@ -0,0 +1,64 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.evm.operation.v2; + +import org.hyperledger.besu.evm.EVM; +import org.hyperledger.besu.evm.frame.MessageFrame; +import org.hyperledger.besu.evm.gascalculator.GasCalculator; +import org.hyperledger.besu.evm.operation.Operation; + +/** EVM v2 OR operation using long[] stack representation. */ +public class OrOperationV2 extends AbstractFixedCostOperationV2 { + + private static final Operation.OperationResult OR_SUCCESS = + new Operation.OperationResult(3, null); + + /** + * Instantiates a new Or operation. + * + * @param gasCalculator the gas calculator + */ + public OrOperationV2(final GasCalculator gasCalculator) { + super(0x17, "OR", 2, 1, gasCalculator, gasCalculator.getVeryLowTierGasCost()); + } + + @Override + public Operation.OperationResult executeFixedCostOperation( + final MessageFrame frame, final EVM evm) { + return staticOperation(frame, frame.stackDataV2()); + } + + /** + * Performs OR on the v2 long[] stack. + * + * @param frame the message frame + * @param s the stack data as a long[] array + * @return the operation result + */ + public static Operation.OperationResult staticOperation( + final MessageFrame frame, final long[] s) { + final int top = frame.stackTopV2(); + final int a = (top - 1) * 4; + final int b = (top - 2) * 4; + + s[b] = s[a] | s[b]; + s[b + 1] = s[a + 1] | s[b + 1]; + s[b + 2] = s[a + 2] | s[b + 2]; + s[b + 3] = s[a + 3] | s[b + 3]; + + frame.setTopV2(top - 1); + return OR_SUCCESS; + } +} diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/XorOperationV2.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/XorOperationV2.java new file mode 100644 index 00000000000..bd6d64d654b --- /dev/null +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/v2/XorOperationV2.java @@ -0,0 +1,64 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.evm.operation.v2; + +import org.hyperledger.besu.evm.EVM; +import org.hyperledger.besu.evm.frame.MessageFrame; +import org.hyperledger.besu.evm.gascalculator.GasCalculator; +import org.hyperledger.besu.evm.operation.Operation; + +/** EVM v2 XOR operation using long[] stack representation. */ +public class XorOperationV2 extends AbstractFixedCostOperationV2 { + + private static final Operation.OperationResult XOR_SUCCESS = + new Operation.OperationResult(3, null); + + /** + * Instantiates a new Xor operation. + * + * @param gasCalculator the gas calculator + */ + public XorOperationV2(final GasCalculator gasCalculator) { + super(0x18, "XOR", 2, 1, gasCalculator, gasCalculator.getVeryLowTierGasCost()); + } + + @Override + public Operation.OperationResult executeFixedCostOperation( + final MessageFrame frame, final EVM evm) { + return staticOperation(frame, frame.stackDataV2()); + } + + /** + * Performs XOR on the v2 long[] stack. + * + * @param frame the message frame + * @param s the stack data as a long[] array + * @return the operation result + */ + public static Operation.OperationResult staticOperation( + final MessageFrame frame, final long[] s) { + final int top = frame.stackTopV2(); + final int a = (top - 1) * 4; + final int b = (top - 2) * 4; + + s[b] = s[a] ^ s[b]; + s[b + 1] = s[a + 1] ^ s[b + 1]; + s[b + 2] = s[a + 2] ^ s[b + 2]; + s[b + 3] = s[a + 3] ^ s[b + 3]; + + frame.setTopV2(top - 1); + return XOR_SUCCESS; + } +}