Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelized IDENTITY tests #1567

Open
wants to merge 11 commits into
base: arith-dev
Choose a base branch
from
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other prc tests are in test/java/net/consensys/linea/zktracer/precompiles

Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,39 @@
package net.consensys.linea.zktracer.instructionprocessing.callTests.prc.identity;

import static net.consensys.linea.zktracer.instructionprocessing.utilities.Calls.appendCall;
import static net.consensys.linea.zktracer.instructionprocessing.utilities.Calls.appendGenericCall;
import static net.consensys.linea.zktracer.instructionprocessing.utilities.MonoOpCodeSmcs.keyPair;
import static net.consensys.linea.zktracer.instructionprocessing.utilities.MonoOpCodeSmcs.userAccount;
import static net.consensys.linea.zktracer.instructionprocessing.utilities.enums.GasParam.*;
import static net.consensys.linea.zktracer.instructionprocessing.utilities.enums.GasParam.GAS;
import static net.consensys.linea.zktracer.instructionprocessing.utilities.enums.GasParam.GAS_PLUS_ONE;
import static net.consensys.linea.zktracer.opcode.OpCode.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import net.consensys.linea.testing.BytecodeCompiler;
import net.consensys.linea.testing.ToyAccount;
import net.consensys.linea.testing.ToyExecutionEnvironmentV2;
import net.consensys.linea.testing.ToyTransaction;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.GasParam;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.OfstParam;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.SizeParam;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.ValueParameter;
import net.consensys.linea.zktracer.opcode.OpCode;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;

public class Tests {

Expand Down Expand Up @@ -75,21 +91,41 @@ void nontrivialCallDataIdentityTest(OpCode callOpCode) {
7,
51);
program.op(RETURNDATASIZE); // should return 512
run(program);
}

identityCaller.setCode(program.compile());
@Test
void callCodeTransfersValueToIdentityTest() {
BytecodeCompiler program = BytecodeCompiler.newProgram();
fullCodeCopyOf(program, byteSource);
appendGenericCall(program, CALLCODE, GAS, Address.ID, ValueParameter.ONE, OfstParam.ZERO, SizeParam.ZERO, OfstParam.ZERO, SizeParam.ZERO, 0, 0);
program.op(RETURNDATASIZE);

Transaction transaction =
ToyTransaction.builder()
.sender(userAccount)
.keyPair(keyPair)
.to(identityCaller)
.value(Wei.of(7_000_000_000L))
.build();
ToyExecutionEnvironmentV2.builder()
.accounts(List.of(byteSource, userAccount, identityCaller))
.transaction(transaction)
.build()
.run();
run(program);
}

@Test
void callCodeNoValueValueTransferToIdentityTest() {
BytecodeCompiler program = BytecodeCompiler.newProgram();
fullCodeCopyOf(program, byteSource);
appendGenericCall(program, CALLCODE, GAS, Address.ID, ValueParameter.ZERO, OfstParam.ZERO, SizeParam.ZERO, OfstParam.ZERO, SizeParam.ZERO, 0, 0);
program.op(RETURNDATASIZE);

run(program);
}

// @Tag("nightly")
@ParameterizedTest
@MethodSource("callParameterIDENTITY")
void genericIdentityTests(
OpCode callOpCode, GasParam gasParam, OfstParam cdo, SizeParam cds, OfstParam rao, SizeParam rac) {

BytecodeCompiler program = BytecodeCompiler.newProgram();
fullCodeCopyOf(program, byteSource);
appendGenericCall(program, callOpCode, gasParam, Address.ID, ValueParameter.ONE, cdo, cds, rao, rac, 0, 0);
program.op(RETURNDATASIZE);

run(program);
}

/**
Expand All @@ -108,4 +144,54 @@ public static void fullCodeCopyOf(BytecodeCompiler program, ToyAccount account)
.push(address)
.op(EXTCODECOPY); // copies the entire code to RAM
}

static Stream<Arguments> callParameterIDENTITY() {


List<OpCode> callOpCodes = List.of(CALL, CALLCODE, DELEGATECALL, STATICCALL);
List<GasParam> gasParams = List.of(GAS, GAS_PLUS_ONE);
List<Arguments> arguments = new ArrayList<>();

for (OpCode callOpCode : callOpCodes) {
for (GasParam gasParam : gasParams) {
for (OfstParam cdo : OfstParam.values()) {
for (SizeParam cds : SizeParam.values()) {
if ((cds == SizeParam.ZERO) && !(cdo == OfstParam.ZERO || cdo == OfstParam.SIXTEEN)) {
continue;
}
for (OfstParam rao : OfstParam.values()) {
for (SizeParam rac : SizeParam.values()) {

if ((rac == SizeParam.ZERO) && !(rao == OfstParam.ZERO || rao == OfstParam.SIXTEEN)) {
continue;
}

arguments.add(Arguments.of(callOpCode, gasParam, cdo, cds, rao, rac));
}
}
}
}
}
}
return arguments.stream();
}

void run(BytecodeCompiler program) {

identityCaller.setCode(program.compile());

Transaction transaction =
ToyTransaction.builder()
.sender(userAccount)
.keyPair(keyPair)
.to(identityCaller)
.value(Wei.of(7_000_000_000L))
.build();
ToyExecutionEnvironmentV2.builder()
.accounts(List.of(byteSource, userAccount, identityCaller))
.transaction(transaction)
.build()
.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import net.consensys.linea.testing.BytecodeCompiler;
import net.consensys.linea.testing.ToyAccount;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.GasParam;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.OfstParam;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.SizeParam;
import net.consensys.linea.zktracer.instructionprocessing.utilities.enums.ValueParameter;
import net.consensys.linea.zktracer.opcode.OpCode;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.datatypes.Address;
Expand All @@ -36,6 +40,45 @@ public class Calls {
public static Bytes32 randCdo =
Bytes32.fromHexString("1a3b88fc78471a5d0ce2df8a5799299b7eefd8e6bfd6d6afb0e437e0a6311878");

public enum Revert {
WILL_REVERT,
WONT_REVERT
}

public static void appendGenericCall(
BytecodeCompiler program,
OpCode callOpcode,
GasParam gasParam,
Address to,
ValueParameter valueParameter,
OfstParam cdo,
SizeParam cds,
OfstParam rao,
SizeParam rac,
int userDefinedGas,
int userDefinedValue) {
program.push(rac.value()).push(rao.value()).push(cds.value()).push(cdo.value());
if (callOpcode.callHasValueArgument()) {
switch (valueParameter) {
case ZERO -> program.push(0);
case ONE -> program.push(1);
case BALANCE -> program.op(BALANCE);
case BALANCE_PLUS_ONE -> program.op(BALANCE).push(1).op(ADD);
case MAX_UINT_256 -> program.push("ff".repeat(32));
case USER_DEFINED -> program.push(userDefinedValue);
}
}
program.push(to);
switch (gasParam) {
case ZERO -> program.push(0);
case GAS -> program.op(GAS);
case GAS_PLUS_ONE -> program.op(GAS).push(1).op(ADD);
case MAX_UINT_256 -> program.push("ff".repeat(32));
case USER_DEFINED -> program.push(userDefinedGas);
}
program.op(callOpcode);
}

public static void appendFullGasCall(
BytecodeCompiler program,
OpCode callOpcode,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Consensys Software Inc.
*
* 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 net.consensys.linea.zktracer.instructionprocessing.utilities.enums;

public enum GasParam {
ZERO,
GAS,
GAS_PLUS_ONE,
MAX_UINT_256,
USER_DEFINED;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Consensys Software Inc.
*
* 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 net.consensys.linea.zktracer.instructionprocessing.utilities.enums;

public enum OfstParam {
// aligned
ZERO("00"),
SIXTEEN("10"),
THIRTY_TWO("20"),
// unaligned
THIRTEEN("0d"), // -3
THIRTY_SIX("24"), // + 4
FIFTY_FIVE("37"), // + 7
HUGE("124ab874bbaa123456098f861235567bbbbacde765213"),
MAX("ff".repeat(32));

private final String value;
OfstParam(String s) {
this.value = s;
}
public String value() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Consensys Software Inc.
*
* 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 net.consensys.linea.zktracer.instructionprocessing.utilities.enums;

public enum SizeParam {
// aligned
ZERO("00"),
SIXTEEN("10"),
EVM_WORD("20"),
// unaligned
THREE("03"), // + 3
SIXTY("3c"), // - 4
FIFTY_FIVE("37"), // + 7
HUGE("43565efefffeeabc54213576345234"),
MAX("ff".repeat(32))
;

private final String value;
SizeParam(String s) {
this.value = s;
}
public String value() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Consensys Software Inc.
*
* 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 net.consensys.linea.zktracer.instructionprocessing.utilities.enums;

public enum ValueParameter {
ZERO,
ONE,
BALANCE,
BALANCE_PLUS_ONE,
MAX_UINT_256,
USER_DEFINED;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# junit.jupiter.execution.parallel.enabled = true
# junit.jupiter.execution.parallel.mode.default = concurrent
# junit.jupiter.execution.parallel.config.strategy = dynamic
# junit.jupiter.execution.parallel.config.strategy = dynamic
Loading