From 215c2e6ab544cf5b1e32c8bc93a21858c26bcace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20B=C3=A9gassat?= Date: Wed, 2 Oct 2024 18:34:57 +0200 Subject: [PATCH 1/7] fix: added chainId to the HUB and BLOCK_DATA modules --- .../consensys/linea/zktracer/ZkTracer.java | 2 +- .../zktracer/module/blockdata/Blockdata.java | 20 ++----------------- .../module/blockdata/BlockdataOperation.java | 6 +++--- .../linea/zktracer/module/hub/Hub.java | 8 ++++++-- .../linea/BlockchainReferenceTestTools.java | 5 ++++- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java index 25721b1c81..60e7769d7d 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java @@ -91,7 +91,7 @@ public ZkTracer(BigInteger chainId) { public ZkTracer( final LineaL1L2BridgeSharedConfiguration bridgeConfiguration, BigInteger chainId) { - this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic()); + this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), chainId); this.chainId = chainId; for (Module m : this.hub.getModulesToCount()) { if (!spillings.containsKey(m.moduleKey())) { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java index b8d612e5d7..f1a33b3be4 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java @@ -18,6 +18,7 @@ import static net.consensys.linea.zktracer.module.blockdata.Trace.MAX_CT; import static net.consensys.linea.zktracer.types.TransactionUtils.getChainIdFromTransaction; +import java.math.BigInteger; import java.nio.MappedByteBuffer; import java.util.ArrayDeque; import java.util.Deque; @@ -39,6 +40,7 @@ public class Blockdata implements Module { private final Wcp wcp; private final TxnData txnData; private final RlpTxn rlpTxn; + private final BigInteger chainId; private final Deque operations = new ArrayDeque<>(); private boolean conflationFinished = false; private static final int TIMESTAMP_BYTESIZE = 4; @@ -96,27 +98,9 @@ public void commit(List buffers) { final Trace trace = new Trace(buffers); final long firstBlockNumber = operations.getFirst().absoluteBlockNumber(); - final long chainId = getChainIdFromConflation(); int relblock = 0; for (BlockdataOperation blockData : operations) { blockData.trace(trace, ++relblock, firstBlockNumber, chainId); } } - - private long getChainIdFromConflation() { - // TODO: this doesn't work if all transaction of the batch are WO ChainId - long chainId = -1; - for (RlpTxnOperation tx : rlpTxn.operations().getAll()) { - try { - chainId = getChainIdFromTransaction(tx.tx()); - break; - } catch (Exception e) { - continue; - } - } - if (chainId == -1) { - throw new RuntimeException("No chainId found in the batch"); - } - return chainId; - } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java index 3cbca59969..cd2b4da50c 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java @@ -55,7 +55,7 @@ protected int computeLineCount() { } public void trace( - Trace trace, final int relBlock, final long firstBlockNumber, final long chainId) { + Trace trace, final int relBlock, final long firstBlockNumber, final BigInteger chainId) { for (short ct = 0; ct <= MAX_CT; ct++) { traceBlockConstant(trace, relBlock, firstBlockNumber); traceRowDependant(trace, ct, relBlock, chainId); @@ -64,7 +64,7 @@ public void trace( } private void traceRowDependant( - Trace trace, final short ct, final int relBlock, final long chainId) { + Trace trace, final short ct, final int relBlock, final BigInteger chainId) { trace.ct(ct); Bytes32 data; @@ -90,7 +90,7 @@ private void traceRowDependant( trace.inst(UnsignedByte.of(EVM_INST_GASLIMIT)).wcpFlag(false); } case 5 -> { - data = Bytes32.leftPad(Bytes.ofUnsignedLong(chainId)); + data = Bytes32.fromHexStringLenient(chainId.toString(16)); trace.inst(UnsignedByte.of(EVM_INST_CHAINID)).wcpFlag(false); } case 6 -> { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java index 8ff261aedb..700a4f5eb9 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java @@ -26,6 +26,7 @@ import static net.consensys.linea.zktracer.types.AddressUtils.effectiveToAddress; import static org.hyperledger.besu.evm.frame.MessageFrame.Type.*; +import java.math.BigInteger; import java.nio.MappedByteBuffer; import java.util.HashMap; import java.util.List; @@ -196,6 +197,8 @@ public int lineCount() { return state.lineCounter().lineCount(); } + @Getter private final BigInteger chainId; + /** List of all modules of the ZK-evm */ // stateless modules @Getter private final Wcp wcp = new Wcp(); @@ -394,12 +397,13 @@ public List getModulesToCount() { .toList(); } - public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic) { + public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic, final BigInteger chainId) { + this.chainId = chainId; l2Block = new L2Block(l2l1ContractAddress, LogTopic.of(l2l1Topic)); l2L1Logs = new L2L1Logs(l2Block); keccak = new Keccak(ecRecoverEffectiveCall, l2Block); shakiraData = new ShakiraData(wcp, sha256Blocks, keccak, ripemdBlocks); - blockdata = new Blockdata(wcp, txnData, rlpTxn); + blockdata = new Blockdata(wcp, txnData, rlpTxn, chainId); mmu = new Mmu(euc, wcp); mmio = new Mmio(mmu); diff --git a/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java b/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java index a2609010a0..3c7eb66006 100644 --- a/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java +++ b/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java @@ -20,6 +20,7 @@ import static net.consensys.linea.ReferenceTestWatcher.JSON_INPUT_FILENAME; import static org.assertj.core.api.Assertions.assertThat; +import java.math.BigInteger; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -178,7 +179,9 @@ public static void executeTest(final BlockchainReferenceTestCaseSpec spec) { final MutableBlockchain blockchain = spec.getBlockchain(); final ProtocolContext context = spec.getProtocolContext(); - final ZkTracer zkTracer = new ZkTracer(schedule.getChainId().get()); + final BigInteger chainId = schedule.getChainId().get().abs(); + + final ZkTracer zkTracer = new ZkTracer(chainId); zkTracer.traceStartConflation(spec.getCandidateBlocks().length); for (var candidateBlock : spec.getCandidateBlocks()) { From e3f42cf038b2966bb86ea2a171de59e853bb3806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20B=C3=A9gassat?= Date: Wed, 2 Oct 2024 20:45:11 +0200 Subject: [PATCH 2/7] spotless --- .../consensys/linea/zktracer/module/blockdata/Blockdata.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java index f1a33b3be4..a1df37993e 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/Blockdata.java @@ -16,7 +16,6 @@ package net.consensys.linea.zktracer.module.blockdata; import static net.consensys.linea.zktracer.module.blockdata.Trace.MAX_CT; -import static net.consensys.linea.zktracer.types.TransactionUtils.getChainIdFromTransaction; import java.math.BigInteger; import java.nio.MappedByteBuffer; @@ -28,7 +27,6 @@ import net.consensys.linea.zktracer.ColumnHeader; import net.consensys.linea.zktracer.container.module.Module; import net.consensys.linea.zktracer.module.rlptxn.RlpTxn; -import net.consensys.linea.zktracer.module.rlptxn.RlpTxnOperation; import net.consensys.linea.zktracer.module.txndata.TxnData; import net.consensys.linea.zktracer.module.wcp.Wcp; import org.hyperledger.besu.evm.worldstate.WorldView; From 52c7f4398a9a5b61a42599352ad6e8420a77ffd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20B=C3=A9gassat?= Date: Wed, 2 Oct 2024 21:17:48 +0200 Subject: [PATCH 3/7] fix: make chainId nonnegative --- .../main/java/net/consensys/linea/zktracer/ZkTracer.java | 6 ++++-- .../java/net/consensys/linea/zktracer/module/hub/Hub.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java index 60e7769d7d..fe83ebdb19 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java @@ -28,6 +28,7 @@ import java.util.Optional; import java.util.Set; +import com.google.common.base.Preconditions; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.consensys.linea.plugins.config.LineaL1L2BridgeSharedConfiguration; @@ -91,8 +92,9 @@ public ZkTracer(BigInteger chainId) { public ZkTracer( final LineaL1L2BridgeSharedConfiguration bridgeConfiguration, BigInteger chainId) { - this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), chainId); - this.chainId = chainId; + BigInteger nonnegativeChainId = chainId.abs(); + this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), nonnegativeChainId); + this.chainId = nonnegativeChainId; for (Module m : this.hub.getModulesToCount()) { if (!spillings.containsKey(m.moduleKey())) { throw new IllegalStateException( diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java index 700a4f5eb9..62049d520d 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java @@ -34,6 +34,7 @@ import java.util.Set; import java.util.stream.Stream; +import com.google.common.base.Preconditions; import lombok.Getter; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; @@ -397,8 +398,9 @@ public List getModulesToCount() { .toList(); } - public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic, final BigInteger chainId) { - this.chainId = chainId; + public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic, final BigInteger nonnegativeChainId) { + Preconditions.checkState(nonnegativeChainId.signum() >= 0); + chainId = nonnegativeChainId; l2Block = new L2Block(l2l1ContractAddress, LogTopic.of(l2l1Topic)); l2L1Logs = new L2L1Logs(l2Block); keccak = new Keccak(ecRecoverEffectiveCall, l2Block); From 4b04fd8424d655a2079d6998b203448727b2cc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20B=C3=A9gassat?= Date: Wed, 2 Oct 2024 21:21:30 +0200 Subject: [PATCH 4/7] spotless --- .../src/main/java/net/consensys/linea/zktracer/ZkTracer.java | 4 ++-- .../java/net/consensys/linea/zktracer/module/hub/Hub.java | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java index fe83ebdb19..42d254ca99 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java @@ -28,7 +28,6 @@ import java.util.Optional; import java.util.Set; -import com.google.common.base.Preconditions; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.consensys.linea.plugins.config.LineaL1L2BridgeSharedConfiguration; @@ -93,7 +92,8 @@ public ZkTracer(BigInteger chainId) { public ZkTracer( final LineaL1L2BridgeSharedConfiguration bridgeConfiguration, BigInteger chainId) { BigInteger nonnegativeChainId = chainId.abs(); - this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), nonnegativeChainId); + this.hub = + new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), nonnegativeChainId); this.chainId = nonnegativeChainId; for (Module m : this.hub.getModulesToCount()) { if (!spillings.containsKey(m.moduleKey())) { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java index 62049d520d..212e6a3a8b 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java @@ -398,7 +398,10 @@ public List getModulesToCount() { .toList(); } - public Hub(final Address l2l1ContractAddress, final Bytes l2l1Topic, final BigInteger nonnegativeChainId) { + public Hub( + final Address l2l1ContractAddress, + final Bytes l2l1Topic, + final BigInteger nonnegativeChainId) { Preconditions.checkState(nonnegativeChainId.signum() >= 0); chainId = nonnegativeChainId; l2Block = new L2Block(l2l1ContractAddress, LogTopic.of(l2l1Topic)); From ee7edc6497936a9413841525289578c401fc6448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20B=C3=A9gassat?= Date: Thu, 3 Oct 2024 01:33:26 +0200 Subject: [PATCH 5/7] ras --- .../main/java/net/consensys/linea/zktracer/ZkTracer.java | 8 +++++--- .../net/consensys/linea/BlockchainReferenceTestTools.java | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java index 42d254ca99..3c0d6f801d 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java @@ -82,11 +82,13 @@ public class ZkTracer implements ConflationAwareOperationTracer { @Getter private final List tracingExceptions = new FiniteList<>(50); public ZkTracer() { - this(LineaL1L2BridgeSharedConfiguration.EMPTY, Bytes.fromHexString("c0ffee").toBigInteger()); + this( + LineaL1L2BridgeSharedConfiguration.EMPTY, + Bytes.fromHexString("c0ffee").toBigInteger().abs()); } - public ZkTracer(BigInteger chainId) { - this(LineaL1L2BridgeSharedConfiguration.EMPTY, chainId); + public ZkTracer(BigInteger nonnegativeChainId) { + this(LineaL1L2BridgeSharedConfiguration.EMPTY, nonnegativeChainId); } public ZkTracer( diff --git a/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java b/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java index 3c7eb66006..6355433844 100644 --- a/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java +++ b/reference-tests/src/test/java/net/consensys/linea/BlockchainReferenceTestTools.java @@ -179,9 +179,9 @@ public static void executeTest(final BlockchainReferenceTestCaseSpec spec) { final MutableBlockchain blockchain = spec.getBlockchain(); final ProtocolContext context = spec.getProtocolContext(); - final BigInteger chainId = schedule.getChainId().get().abs(); + final BigInteger nonnegativeChainId = schedule.getChainId().get().abs(); - final ZkTracer zkTracer = new ZkTracer(chainId); + final ZkTracer zkTracer = new ZkTracer(nonnegativeChainId); zkTracer.traceStartConflation(spec.getCandidateBlocks().length); for (var candidateBlock : spec.getCandidateBlocks()) { From 42e5eb2132e850d5c95773372594f87b1094148c Mon Sep 17 00:00:00 2001 From: Francois Bojarski Date: Thu, 3 Oct 2024 10:32:20 +0530 Subject: [PATCH 6/7] use common conversion method Signed-off-by: Francois Bojarski --- .../linea/zktracer/module/blockdata/BlockdataOperation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java index cd2b4da50c..08ff31369a 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/blockdata/BlockdataOperation.java @@ -90,7 +90,7 @@ private void traceRowDependant( trace.inst(UnsignedByte.of(EVM_INST_GASLIMIT)).wcpFlag(false); } case 5 -> { - data = Bytes32.fromHexStringLenient(chainId.toString(16)); + data = Bytes32.leftPad(bigIntegerToBytes(chainId)); trace.inst(UnsignedByte.of(EVM_INST_CHAINID)).wcpFlag(false); } case 6 -> { From 2f49c96c4bbe6ae4975706488b2b7ba15a7ab6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20B=C3=A9gassat?= Date: Thu, 3 Oct 2024 14:43:27 +0200 Subject: [PATCH 7/7] fix: removed chainId field from ZkTracer class --- .../src/main/java/net/consensys/linea/zktracer/ZkTracer.java | 3 --- .../net/consensys/linea/testing/ToyExecutionEnvironmentV2.java | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java index 3c0d6f801d..1c4402218b 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java @@ -60,8 +60,6 @@ public class ZkTracer implements ConflationAwareOperationTracer { private static final Map spillings; - public final BigInteger chainId; - static { try { // Load spillings configured in src/main/resources/spillings.toml. @@ -96,7 +94,6 @@ public ZkTracer( BigInteger nonnegativeChainId = chainId.abs(); this.hub = new Hub(bridgeConfiguration.contract(), bridgeConfiguration.topic(), nonnegativeChainId); - this.chainId = nonnegativeChainId; for (Module m : this.hub.getModulesToCount()) { if (!spillings.containsKey(m.moduleKey())) { throw new IllegalStateException( diff --git a/testing/src/main/java/net/consensys/linea/testing/ToyExecutionEnvironmentV2.java b/testing/src/main/java/net/consensys/linea/testing/ToyExecutionEnvironmentV2.java index 74a5333fc1..e3a56a13cb 100644 --- a/testing/src/main/java/net/consensys/linea/testing/ToyExecutionEnvironmentV2.java +++ b/testing/src/main/java/net/consensys/linea/testing/ToyExecutionEnvironmentV2.java @@ -58,7 +58,7 @@ public class ToyExecutionEnvironmentV2 { @Builder.Default private final Consumer zkTracerValidator = x -> {}; - private final ZkTracer tracer = new ZkTracer(); + private final ZkTracer tracer = new ZkTracer(CHAIN_ID); public void run() { ProtocolSpec protocolSpec = ExecutionEnvironment.getProtocolSpec(CHAIN_ID);