Skip to content

Commit

Permalink
Merge main into alu-text
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Trintinalia committed Apr 25, 2023
2 parents 778282d + 3d3c3c7 commit 6147fd6
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 61 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,14 @@ allprojects {

implementation "org.hyperledger.besu:plugin-api:$besuVersion"

compileOnly 'com.fasterxml.jackson.core:jackson-databind'

errorprone("com.google.errorprone:error_prone_core")

compileOnly 'io.vertx:vertx-core:4.3.8'

implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'

compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'

implementation 'org.apache.tuweni:tuweni-bytes:2.3.1'
implementation 'org.apache.tuweni:tuweni-units:2.3.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,28 @@ public Bytes getRange(final int i, final int start, final int length) {
public void set(int i, int j, byte b) {
bytesArray[i].set(j, b);
}

// set the whole chunk of bytes at the given index.
// assumes index is one of 0,1,2,3
// assumes length of bytes is 8
public void setChunk(int index, Bytes bytes) {
set(index, bytes);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("hi=").append(getHigh()).append(", ");
sb.append("lo=").append(getLow()).append(("\n"));
sb.append(" 0 1 2 3\n ");
sb.append(get(0))
.append(" ")
.append(get(1))
.append(" ")
.append(get(2))
.append(" ")
.append(get(3))
.append(" ");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright ConsenSys AG.
*
* 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;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.operation.Operation;

import java.util.List;

import net.consensys.linea.zktracer.corset.CorsetValidator;
import net.consensys.linea.zktracer.module.ModuleTracer;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
public abstract class AbstractBaseModuleTracerTest {
private ZkTracer zkTracer;
private ZkTraceBuilder zkTraceBuilder;
MessageFrame mockFrame;
Operation mockOperation;
static ModuleTracer moduleTracer;

protected void runTest(final OpCode opCode, final List<Bytes32> arguments) {
when(mockOperation.getOpcode()).thenReturn((int) opCode.value);
for (int i = 0; i < arguments.size(); i++) {
when(mockFrame.getStackItem(i)).thenReturn(arguments.get(i));
}
zkTracer.tracePreExecution(mockFrame);
assertThat(CorsetValidator.isValid(zkTraceBuilder.build().toJson())).isTrue();
}

protected String generateTrace(OpCode opCode, List<Bytes32> arguments) {
when(mockOperation.getOpcode()).thenReturn((int) opCode.value);
for (int i = 0; i < arguments.size(); i++) {
when(mockFrame.getStackItem(i)).thenReturn(arguments.get(i));
}
zkTracer.tracePreExecution(mockFrame);
return zkTraceBuilder.build().toJson();
}

@BeforeEach
void setUp() {
zkTraceBuilder = new ZkTraceBuilder();
moduleTracer = getModuleTracer();
zkTracer = new ZkTracer(zkTraceBuilder, List.of(moduleTracer));
mockFrame = mock(MessageFrame.class);
mockOperation = mock(Operation.class);
when(mockFrame.getCurrentOperation()).thenReturn(mockOperation);
}

protected abstract ModuleTracer getModuleTracer();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright ConsenSys AG.
*
* 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;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Preconditions.checkState;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@TestInstance(Lifecycle.PER_CLASS)
public abstract class AbstractModuleTracerBySpecTest extends AbstractBaseModuleTracerTest {
private static final ObjectMapper MAPPER = new ObjectMapper();

@ParameterizedTest(name = "{index} {0}")
@MethodSource("specs")
public void traceWithSpecFile(final String ignored, URL specUrl) throws IOException {
traceOperation(specUrl);
}

private void traceOperation(final URL specFile) throws IOException {
InputStream inputStream = specFile.openStream();
final ObjectNode specNode = (ObjectNode) MAPPER.readTree(inputStream);
final JsonNode request = specNode.get("input");
final JsonNode expectedTrace = specNode.get("output");
final JsonNode actualTrace = generateTrace(getModuleTracer().jsonKey(), request);

assertThat(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(actualTrace))
.isEqualTo(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(expectedTrace));
}

private JsonNode generateTrace(String moduleName, JsonNode jsonNodeParams)
throws JsonProcessingException {
OpCode opcode = OpCode.valueOf(jsonNodeParams.get("opcode").asText());
List<Bytes32> arguments = new ArrayList<>();
JsonNode arg = jsonNodeParams.get("params");
arg.forEach(bytes -> arguments.add(Bytes32.fromHexString(bytes.asText())));
String trace = generateTrace(opcode, arguments);
return MAPPER.readTree(trace).get(moduleName);
}

public static Object[][] findSpecFiles(final String[] subDirectoryPaths) {
final List<Object[]> specFiles = new ArrayList<>();
for (final String path : subDirectoryPaths) {
final URL url = AbstractModuleTracerBySpecTest.class.getResource(path);
checkState(url != null, "Cannot find test directory " + path);
final Path dir;
try {
dir = Paths.get(url.toURI());
} catch (final URISyntaxException e) {
throw new RuntimeException("Problem converting URL to URI " + url, e);
}
try (final Stream<Path> s = Files.walk(dir, 1)) {
s.map(Path::toFile)
.filter(f -> f.getPath().endsWith(".json"))
.map(AbstractModuleTracerBySpecTest::fileToParams)
.forEach(specFiles::add);
} catch (final IOException e) {
throw new RuntimeException("Problem reading directory " + dir, e);
}
}
final Object[][] result = new Object[specFiles.size()][2];
for (int i = 0; i < specFiles.size(); i++) {
result[i] = specFiles.get(i);
}
return result;
}

private static Object[] fileToParams(final File file) {
try {
final String fileName = file.toPath().getFileName().toString();
final URL fileURL = file.toURI().toURL();
return new Object[] {fileName, fileURL};
} catch (final MalformedURLException e) {
throw new RuntimeException("Problem reading spec file " + file.getAbsolutePath(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,35 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.operation.Operation;
package net.consensys.linea.zktracer;

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

import net.consensys.linea.CorsetValidator;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.ZkTraceBuilder;
import net.consensys.linea.zktracer.ZkTracer;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;

@TestInstance(Lifecycle.PER_CLASS)
public abstract class AbstractModuleTracerTest {
public abstract class AbstractModuleTracerCorsetTest extends AbstractBaseModuleTracerTest {
static final Random rand = new Random();
private static final int TEST_REPETITIONS = 8;

private ZkTracer zkTracer;
private ZkTraceBuilder zkTraceBuilder;
@Mock MessageFrame mockFrame;
@Mock Operation mockOperation;
static ModuleTracer moduleTracer;

@ParameterizedTest()
@MethodSource("provideRandomArguments")
void randomArgumentsTest(final OpCode opCode, final List<Bytes32> args) {
runTest(opCode, args);
void randomArgumentsTest(OpCode opCode, List<Bytes32> params) {
runTest(opCode, params);
}

@ParameterizedTest()
@MethodSource("provideNonRandomArguments")
void nonRandomArgumentsTest(final OpCode opCode, final List<Bytes32> arguments) {
runTest(opCode, arguments);
}

protected void runTest(final OpCode opCode, final List<Bytes32> arguments) {
when(mockOperation.getOpcode()).thenReturn((int) opCode.value);
for (int i = 0; i < arguments.size(); i++) {
when(mockFrame.getStackItem(i)).thenReturn(arguments.get(i));
}
zkTracer.tracePreExecution(mockFrame);
assertThat(CorsetValidator.isValid(zkTraceBuilder.build().toJson())).isTrue();
}

@BeforeEach
void setUp() {
zkTraceBuilder = new ZkTraceBuilder();
moduleTracer = getModuleTracer();
zkTracer = new ZkTracer(zkTraceBuilder, List.of(moduleTracer));
when(mockFrame.getCurrentOperation()).thenReturn(mockOperation);
void nonRandomArgumentsTest(OpCode opCode, List<Bytes32> params) {
runTest(opCode, params);
}

protected abstract Stream<Arguments> provideNonRandomArguments();
Expand All @@ -90,9 +55,7 @@ public Stream<Arguments> provideRandomArguments() {
return arguments.stream();
}

protected abstract ModuleTracer getModuleTracer();

protected OpCode getRandomSupportedOpcode() {
public OpCode getRandomSupportedOpcode() {
var supportedOpCodes = getModuleTracer().supportedOpCodes();
int index = rand.nextInt(supportedOpCodes.size());
return supportedOpCodes.get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.consensys.linea.zktracer.bytes.Bytes16;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.jupiter.api.Test;

public class BaseThetaTest {
Expand Down Expand Up @@ -114,4 +115,30 @@ public void setTest() {
assertThat(baseTheta.get(2, 7)).isEqualTo(Bytes.fromHexString("0x0b").get(0));
assertThat(baseTheta.get(3, 0)).isEqualTo(Bytes.fromHexString("0x0d").get(0));
}

@Test
public void setBytesTest() {
BaseTheta aBaseTheta = BaseTheta.fromBytes32(UInt256.valueOf(43532));

Bytes a0 = Bytes.fromHexString("0x000000000000aa0c");
assertThat(aBaseTheta.get(0)).isEqualTo(a0);
assertThat(aBaseTheta.get(1).isZero()).isTrue();
assertThat(aBaseTheta.get(2).isZero()).isTrue();
assertThat(aBaseTheta.get(3).isZero()).isTrue();

Bytes b3 = Bytes.fromHexString("0x533a124790000000");
Bytes b2 = Bytes.fromHexString("0xfaa47d49bf1d1e67");
Bytes b1 = Bytes.fromHexString("0x952951f4425bf6f3");
Bytes b0 = Bytes.fromHexString("0x0000000000d55835");
Bytes32 bytes32 = Bytes32.wrap(Bytes.concatenate(b3, b2, b1, b0));
BaseTheta bBaseTheta = BaseTheta.fromBytes32(bytes32);

assertThat(bBaseTheta.get(3)).isEqualTo(b3);
assertThat(bBaseTheta.get(2)).isEqualTo(b2);
assertThat(bBaseTheta.get(1)).isEqualTo(b1);
assertThat(bBaseTheta.get(0)).isEqualTo(b0);

bBaseTheta.setChunk(0, b3);
assertThat(bBaseTheta.get(0)).isEqualTo(b3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea;
package net.consensys.linea.zktracer.corset;

import static java.nio.file.StandardOpenOption.WRITE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.alu.add;
package net.consensys.linea.zktracer.corset.module.alu.add;

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

import net.consensys.linea.zktracer.AbstractModuleTracerCorsetTest;
import net.consensys.linea.zktracer.OpCode;
import net.consensys.linea.zktracer.module.AbstractModuleTracerTest;
import net.consensys.linea.zktracer.module.ModuleTracer;
import net.consensys.linea.zktracer.module.alu.add.AddTracer;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
Expand All @@ -32,7 +33,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AddTracerTest extends AbstractModuleTracerTest {
class AddTracerTest extends AbstractModuleTracerCorsetTest {
private static final Random rand = new Random();

private static final int TEST_ADD_REPETITIONS = 16;
Expand Down
Loading

0 comments on commit 6147fd6

Please sign in to comment.