From 073437a7022089869ce3d1698bee38b11455987f Mon Sep 17 00:00:00 2001 From: Franklin Delehelle Date: Wed, 20 Dec 2023 15:15:50 +0100 Subject: [PATCH] fix: make precompile counters work --- .../src/test/resources/moduleLimits.toml | 11 ++- .../src/test/resources/noModuleLimits.toml | 11 ++- .../resources/txOverflowModuleLimits.toml | 11 ++- .../linea/zktracer/module/hub/Hub.java | 83 +++++++++++-------- .../zktracer/module/legacy/hash/HashData.java | 2 +- .../zktracer/module/legacy/hash/HashInfo.java | 2 +- .../{ModExp.java => AbstractCallCounter.java} | 32 ++++--- .../precompiles/Blake2fCallCounter.java | 23 +++++ .../limits/precompiles/Blake2fRounds.java | 10 ++- .../limits/precompiles/EcAddCallCounter.java | 23 +++++ .../precompiles/EcAddEffectiveCall.java | 8 +- .../limits/precompiles/EcMulCallCounter.java | 23 +++++ .../precompiles/EcMulEffectiveCall.java | 6 ++ .../module/limits/precompiles/EcPairing.java | 61 -------------- .../precompiles/EcPairingCallCounter.java | 23 +++++ .../precompiles/EcPairingEffectiveCall.java | 8 +- .../precompiles/EcRecoverCallCounter.java | 23 +++++ .../precompiles/EcRecoverEffectiveCall.java | 8 +- .../limits/precompiles/ModExpCallCounter.java | 23 +++++ .../{ModexpEffectiveCall.java => Modexp.java} | 10 ++- .../limits/precompiles/Rip160Blocks.java | 10 ++- .../limits/precompiles/Rip160CallCounter.java | 23 +++++ .../precompiles/Rip160EffectiveCall.java | 61 -------------- .../limits/precompiles/Sha256Blocks.java | 10 ++- .../limits/precompiles/Sha256CallCounter.java | 23 +++++ .../precompiles/Sha256EffectiveCall.java | 61 -------------- 26 files changed, 330 insertions(+), 259 deletions(-) rename arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/{ModExp.java => AbstractCallCounter.java} (67%) create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fCallCounter.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddCallCounter.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulCallCounter.java delete mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairing.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingCallCounter.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverCallCounter.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExpCallCounter.java rename arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/{ModexpEffectiveCall.java => Modexp.java} (96%) create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160CallCounter.java delete mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160EffectiveCall.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256CallCounter.java delete mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256EffectiveCall.java diff --git a/acceptance-tests/src/test/resources/moduleLimits.toml b/acceptance-tests/src/test/resources/moduleLimits.toml index f28d104dc0..5a5d830997 100644 --- a/acceptance-tests/src/test/resources/moduleLimits.toml +++ b/acceptance-tests/src/test/resources/moduleLimits.toml @@ -65,4 +65,13 @@ PRECOMPILE_ECADD_EFFECTIVE_CALL = 10000 PRECOMPILE_ECMUL_EFFECTIVE_CALL = 10000 PRECOMPILE_ECPAIRING_EFFECTIVE_CALL = 10000 PRECOMPILE_ECPAIRING_MILLER_LOOP = 10000 -PRECOMPILE_BLAKE2F_ROUNDS = 512 \ No newline at end of file +PRECOMPILE_BLAKE2F_ROUNDS = 512 + +PRECOMPILE_BLAKE2F = 512 +PRECOMPILE_ECADD = 512 +PRECOMPILE_ECMUL = 512 +PRECOMPILE_ECPAIRING = 512 +PRECOMPILE_ECRECOVER = 512 +PRECOMPILE_MODEXP = 512 +PRECOMPILE_RIPEMD = 512 +PRECOMPILE_SHA2 = 512 \ No newline at end of file diff --git a/acceptance-tests/src/test/resources/noModuleLimits.toml b/acceptance-tests/src/test/resources/noModuleLimits.toml index 8898ec27a2..99656d01c3 100644 --- a/acceptance-tests/src/test/resources/noModuleLimits.toml +++ b/acceptance-tests/src/test/resources/noModuleLimits.toml @@ -65,4 +65,13 @@ PRECOMPILE_ECADD_EFFECTIVE_CALL = 10000 PRECOMPILE_ECMUL_EFFECTIVE_CALL = 10000 PRECOMPILE_ECPAIRING_EFFECTIVE_CALL = 10000 PRECOMPILE_ECPAIRING_MILLER_LOOP = 10000 -PRECOMPILE_BLAKE2F_ROUNDS = 512 \ No newline at end of file +PRECOMPILE_BLAKE2F_ROUNDS = 512 + +PRECOMPILE_BLAKE2F = 512 +PRECOMPILE_ECADD = 512 +PRECOMPILE_ECMUL = 512 +PRECOMPILE_ECPAIRING = 512 +PRECOMPILE_ECRECOVER = 512 +PRECOMPILE_MODEXP = 512 +PRECOMPILE_RIPEMD = 512 +PRECOMPILE_SHA2 = 512 \ No newline at end of file diff --git a/acceptance-tests/src/test/resources/txOverflowModuleLimits.toml b/acceptance-tests/src/test/resources/txOverflowModuleLimits.toml index 5ba9e2823c..0ca392793e 100644 --- a/acceptance-tests/src/test/resources/txOverflowModuleLimits.toml +++ b/acceptance-tests/src/test/resources/txOverflowModuleLimits.toml @@ -66,4 +66,13 @@ PRECOMPILE_ECADD_EFFECTIVE_CALL = 10000 PRECOMPILE_ECMUL_EFFECTIVE_CALL = 10000 PRECOMPILE_ECPAIRING_EFFECTIVE_CALL = 10000 PRECOMPILE_ECPAIRING_MILLER_LOOP = 10000 -PRECOMPILE_BLAKE2F_ROUNDS = 512 \ No newline at end of file +PRECOMPILE_BLAKE2F_ROUNDS = 512 + +PRECOMPILE_BLAKE2F = 512 +PRECOMPILE_ECADD = 512 +PRECOMPILE_ECMUL = 512 +PRECOMPILE_ECPAIRING = 512 +PRECOMPILE_ECRECOVER = 512 +PRECOMPILE_MODEXP = 512 +PRECOMPILE_RIPEMD = 512 +PRECOMPILE_SHA2 = 512 \ No newline at end of file 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 5b85e62fc0..49474b6583 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 @@ -48,16 +48,12 @@ import net.consensys.linea.zktracer.module.limits.precompiles.Blake2fRounds; import net.consensys.linea.zktracer.module.limits.precompiles.EcAddEffectiveCall; import net.consensys.linea.zktracer.module.limits.precompiles.EcMulEffectiveCall; -import net.consensys.linea.zktracer.module.limits.precompiles.EcPairing; import net.consensys.linea.zktracer.module.limits.precompiles.EcPairingEffectiveCall; import net.consensys.linea.zktracer.module.limits.precompiles.EcPairingMillerLoop; import net.consensys.linea.zktracer.module.limits.precompiles.EcRecoverEffectiveCall; -import net.consensys.linea.zktracer.module.limits.precompiles.ModExp; -import net.consensys.linea.zktracer.module.limits.precompiles.ModexpEffectiveCall; +import net.consensys.linea.zktracer.module.limits.precompiles.Modexp; import net.consensys.linea.zktracer.module.limits.precompiles.Rip160Blocks; -import net.consensys.linea.zktracer.module.limits.precompiles.Rip160EffectiveCall; import net.consensys.linea.zktracer.module.limits.precompiles.Sha256Blocks; -import net.consensys.linea.zktracer.module.limits.precompiles.Sha256EffectiveCall; import net.consensys.linea.zktracer.module.logData.LogData; import net.consensys.linea.zktracer.module.logInfo.LogInfo; import net.consensys.linea.zktracer.module.mmu.Mmu; @@ -189,19 +185,27 @@ public void addTraceSection(TraceSection section) { private final Trm trm = new Trm(); private final Stp stp = new Stp(this, wcp, mod); private final L2Block l2Block = new L2Block(); - private final Sha256EffectiveCall sha256NbEffectiveCall = new Sha256EffectiveCall(); - private final Rip160EffectiveCall rip160NbEffectiveCall = new Rip160EffectiveCall(); - private final EcPairing ecPairingNbCall = new EcPairing(); - private final ModExp modExp = new ModExp(); - private final ModexpEffectiveCall modexpEffectiveCall = new ModexpEffectiveCall(this, modExp); private final HashInfo hashInfo; private final HashData hashData; + // Precompiles stuff + Blake2fRounds blake2f; + EcAddEffectiveCall ecAdd; + EcMulEffectiveCall ecMul; + EcPairingEffectiveCall ecPairing; + EcRecoverEffectiveCall ecRecover; + Modexp modexp; + Rip160Blocks rip160; + Sha256Blocks sha256; + private final List modules; /* Those modules are not traced, we just compute the number of calls to those precompile to meet the prover limits */ private final List precompileLimitModules; public Hub() { + // + // Module + // this.pch = new PlatformController(this); this.mmu = new Mmu(this.callStack); this.mxp = new Mxp(this); @@ -213,32 +217,41 @@ public Hub() { this.hashData = new HashData(this); this.hashInfo = new HashInfo(this); - final EcRecoverEffectiveCall ecRec = new EcRecoverEffectiveCall(this, ecRecover); - final EcPairingEffectiveCall ecpairingNbEffectiveCall = - new EcPairingEffectiveCall(this, ecPairingNbCall); + // + // Precompiles + // + this.blake2f = new Blake2fRounds(this); + this.ecAdd = new EcAddEffectiveCall(this); + this.ecMul = new EcMulEffectiveCall(this); + this.ecPairing = new EcPairingEffectiveCall(this); + this.ecRecover = new EcRecoverEffectiveCall(this); + this.modexp = new Modexp(this); + this.rip160 = new Rip160Blocks(this); + this.sha256 = new Sha256Blocks(this); + this.precompileLimitModules = List.of( - sha256NbCall, - sha256NbEffectiveCall, - new Sha256Blocks(this, sha256NbCall, sha256NbEffectiveCall), - ecRec, - rip160NbCall, - rip160NbEffectiveCall, - new Rip160Blocks(this, rip160NbCall, rip160NbEffectiveCall), - modExp, - modexpEffectiveCall, - ecAdd, - new EcAddEffectiveCall(this, ecAdd), - ecMul, - new EcMulEffectiveCall(this, ecMul), - ecPairingNbCall, - ecpairingNbEffectiveCall, - new EcPairingMillerLoop(ecpairingNbEffectiveCall), - blake2f, - new Blake2fRounds(this, blake2f), + this.blake2f, + this.blake2f.callCounter(), + this.ecAdd, + this.ecAdd.callCounter(), + this.ecMul, + this.ecMul.callCounter(), + this.ecPairing, + this.ecPairing.callCounter(), + new EcPairingMillerLoop(this.ecPairing), + this.ecRecover, + this.ecRecover.callCounter(), + this.modexp, + this.modexp.callCounter(), + this.rip160, + this.rip160.callCounter(), + this.sha256, + this.sha256.callCounter(), + // Block level limits this.l2Block, - new Keccak(this, ecRec, this.l2Block), + new Keccak(this, this.ecRecover, this.l2Block), new L2L1Logs(this.l2Block)); this.modules = @@ -274,11 +287,15 @@ public Hub() { */ public List getModulesToTrace() { return List.of( + // // Reference tables + // new BinRt(), new InstructionDecoder(), new ShfRt(), + // // Modules + // this, this.add, this.bin, @@ -538,7 +555,7 @@ void triggerModules(MessageFrame frame) { // this.stp.tracePreOpcode(frame); } if (this.pch.signals().exp()) { - this.modexpEffectiveCall.tracePreOpcode(frame); + this.modexp.tracePreOpcode(frame); } if (this.pch.signals().trm()) { this.trm.tracePreOpcode(frame); diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashData.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashData.java index b4520f19e9..83a70ecd53 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashData.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashData.java @@ -36,7 +36,7 @@ public class HashData implements Module { @Override public String moduleKey() { - return "OLD_PUB_HASH"; + return "PUB_HASH"; } @Override diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashInfo.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashInfo.java index 5cbd61cc52..60c4a48b81 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashInfo.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashInfo.java @@ -33,7 +33,7 @@ public class HashInfo implements Module { @Override public String moduleKey() { - return "OLD_PUB_HASH_INFO"; + return "PUB_HASH_INFO"; } @Override diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExp.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/AbstractCallCounter.java similarity index 67% rename from arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExp.java rename to arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/AbstractCallCounter.java index 2248ad5808..17bae8215e 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExp.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/AbstractCallCounter.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys Inc. + * 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 @@ -15,47 +15,43 @@ package net.consensys.linea.zktracer.module.limits.precompiles; -import java.nio.MappedByteBuffer; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.List; -import java.util.Stack; import net.consensys.linea.zktracer.ColumnHeader; import net.consensys.linea.zktracer.module.Module; +import org.apache.commons.lang3.NotImplementedException; + +public class AbstractCallCounter implements Module { + private final Deque callCount = new ArrayDeque<>(); -public final class ModExp implements Module { @Override public String moduleKey() { - return "PRECOMPILE_MODEXP"; + throw new NotImplementedException("must be implemented by derived class"); } - private final Stack counts = new Stack<>(); - @Override public void enterTransaction() { - this.counts.push(0); + this.callCount.push(0); } @Override public void popTransaction() { - this.counts.pop(); - } - - public void countACAllToPrecompile() { - this.counts.push(this.counts.pop() + 1); + this.callCount.pop(); } @Override public int lineCount() { - return this.counts.stream().mapToInt(x -> x).sum(); + return this.callCount.stream().mapToInt(x -> x).sum(); } - @Override - public List columnsHeaders() { - throw new IllegalStateException("should never be called"); + public void tick() { + this.callCount.push(this.callCount.pop() + 1); } @Override - public void commit(List buffers) { + public List columnsHeaders() { throw new IllegalStateException("should never be called"); } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fCallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fCallCounter.java new file mode 100644 index 0000000000..c33ea10475 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fCallCounter.java @@ -0,0 +1,23 @@ +/* + * 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.module.limits.precompiles; + +public class Blake2fCallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_BLAKE2F"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fRounds.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fRounds.java index 53a39cbf35..f631772c17 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fRounds.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fRounds.java @@ -30,10 +30,14 @@ @RequiredArgsConstructor public final class Blake2fRounds implements Module { - final Hub hub; - final Blake2f blake2f; + private final Hub hub; + private final Blake2fCallCounter blake2fCallCounter = new Blake2fCallCounter(); private final Stack counts = new Stack<>(); + public Module callCounter() { + return this.blake2fCallCounter; + } + @Override public String moduleKey() { return "PRECOMPILE_BLAKE2F_ROUNDS"; @@ -57,7 +61,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.BLAKE2B_F_COMPRESSION)) { - this.blake2f.countACAllToPrecompile(); + this.blake2fCallCounter.tick(); long length = 0; long offset = 0; switch (opCode) { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddCallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddCallCounter.java new file mode 100644 index 0000000000..06037849f8 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddCallCounter.java @@ -0,0 +1,23 @@ +/* + * 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.module.limits.precompiles; + +public class EcAddCallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_ECADD"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddEffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddEffectiveCall.java index ae5b344438..497d035cb5 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddEffectiveCall.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddEffectiveCall.java @@ -31,9 +31,13 @@ @RequiredArgsConstructor public final class EcAddEffectiveCall implements Module { private final Hub hub; - private final EcAdd ecAdd; + private final EcAddCallCounter ecAddCallCounter = new EcAddCallCounter(); private final Stack counts = new Stack<>(); + public Module callCounter() { + return this.ecAddCallCounter; + } + @Override public String moduleKey() { return "PRECOMPILE_ECADD_EFFECTIVE_CALL"; @@ -59,7 +63,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.ALTBN128_ADD)) { - this.ecAdd.countACAllToPrecompile(); + this.ecAddCallCounter.tick(); final long gasPaid = Words.clampedToLong(frame.getStackItem(0)); if (gasPaid >= PRECOMPILE_GAS_FEE) { this.counts.push(this.counts.pop() + 1); diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulCallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulCallCounter.java new file mode 100644 index 0000000000..3e5bf0fd77 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulCallCounter.java @@ -0,0 +1,23 @@ +/* + * 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.module.limits.precompiles; + +public class EcMulCallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_ECMUL"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulEffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulEffectiveCall.java index b53b154223..29f19b3f57 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulEffectiveCall.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcMulEffectiveCall.java @@ -32,8 +32,13 @@ @RequiredArgsConstructor public final class EcMulEffectiveCall implements Module { private final Hub hub; + private final EcMulCallCounter ecMulCallCounter = new EcMulCallCounter(); private final Stack counts = new Stack<>(); + public Module callCounter() { + return this.ecMulCallCounter; + } + @Override public String moduleKey() { return "PRECOMPILE_ECMUL_EFFECTIVE_CALL"; @@ -59,6 +64,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.ALTBN128_MUL)) { + this.ecMulCallCounter.tick(); final long gasPaid = Words.clampedToLong(frame.getStackItem(0)); if (gasPaid >= PRECOMPILE_GAS_FEE) { this.counts.push(this.counts.pop() + 1); diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairing.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairing.java deleted file mode 100644 index b4c19d9e0d..0000000000 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairing.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright ConsenSys 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.module.limits.precompiles; - -import java.nio.MappedByteBuffer; -import java.util.List; -import java.util.Stack; - -import net.consensys.linea.zktracer.ColumnHeader; -import net.consensys.linea.zktracer.module.Module; - -public final class EcPairing implements Module { - @Override - public String moduleKey() { - return "PRECOMPILE_ECPAIRING"; - } - - private final Stack counts = new Stack<>(); - - @Override - public void enterTransaction() { - this.counts.push(0); - } - - @Override - public void popTransaction() { - this.counts.pop(); - } - - public void countACAllToPrecompile() { - this.counts.push(this.counts.pop() + 1); - } - - @Override - public int lineCount() { - return this.counts.stream().mapToInt(x -> x).sum(); - } - - @Override - public List columnsHeaders() { - throw new IllegalStateException("should never be called"); - } - - @Override - public void commit(List buffers) { - throw new IllegalStateException("should never be called"); - } -} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingCallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingCallCounter.java new file mode 100644 index 0000000000..4b41000913 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingCallCounter.java @@ -0,0 +1,23 @@ +/* + * Copyright ConsenSys 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.module.limits.precompiles; + +public final class EcPairingCallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_ECPAIRING"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingEffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingEffectiveCall.java index 36a6ba5280..78c1a98218 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingEffectiveCall.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcPairingEffectiveCall.java @@ -34,12 +34,16 @@ @RequiredArgsConstructor public final class EcPairingEffectiveCall implements Module { private final Hub hub; - private final EcPairing ecPairing; + private final EcPairingCallCounter ecPairingCallCounter = new EcPairingCallCounter(); @Getter private final Stack counts = new Stack<>(); private static final int PRECOMPILE_BASE_GAS_FEE = 45000; // cf EIP-1108 private static final int PRECOMPILE_MILLER_LOOP_GAS_FEE = 34000; // cf EIP-1108 private static final int ECPAIRING_NB_BYTES_PER_MILLER_LOOP = 192; + public Module callCounter() { + return this.ecPairingCallCounter; + } + @Override public String moduleKey() { return "PRECOMPILE_ECPAIRING_EFFECTIVE_CALL"; @@ -63,7 +67,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.ALTBN128_PAIRING)) { - this.ecPairing.countACAllToPrecompile(); + this.ecPairingCallCounter.tick(); long length = 0; switch (opCode) { case CALL, CALLCODE -> length = Words.clampedToLong(frame.getStackItem(4)); diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverCallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverCallCounter.java new file mode 100644 index 0000000000..81e741c1ec --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverCallCounter.java @@ -0,0 +1,23 @@ +/* + * 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.module.limits.precompiles; + +public class EcRecoverCallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_ECRECOVER"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverEffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverEffectiveCall.java index 069ebbbe1a..032f083a3b 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverEffectiveCall.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcRecoverEffectiveCall.java @@ -35,9 +35,13 @@ @RequiredArgsConstructor public final class EcRecoverEffectiveCall implements Module { private final Hub hub; - private final EcRecover ecRecover; + private final EcRecoverCallCounter ecRecoverCallCounter = new EcRecoverCallCounter(); private final Stack counts = new Stack<>(); + public Module callCounter() { + return this.ecRecoverCallCounter; + } + @Override public String moduleKey() { return "PRECOMPILE_ECRECOVER_EFFECTIVE_CALL"; @@ -67,7 +71,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.ECREC)) { - this.ecRecover.countACAllToPrecompile(); + this.ecRecoverCallCounter.tick(); long length = 0; long offset = 0; switch (opCode) { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExpCallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExpCallCounter.java new file mode 100644 index 0000000000..fdb1f1d825 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModExpCallCounter.java @@ -0,0 +1,23 @@ +/* + * Copyright ConsenSys 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.module.limits.precompiles; + +public final class ModExpCallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_MODEXP"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModexpEffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Modexp.java similarity index 96% rename from arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModexpEffectiveCall.java rename to arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Modexp.java index 74fc6279f4..d72f91f7ec 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/ModexpEffectiveCall.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Modexp.java @@ -35,13 +35,17 @@ @Slf4j @RequiredArgsConstructor -public class ModexpEffectiveCall implements Module { +public class Modexp implements Module { private final Hub hub; - private final ModExp modexp; + public final ModExpCallCounter modExpCallCounter = new ModExpCallCounter(); private final Stack counts = new Stack<>(); private static final BigInteger PROVER_MAX_INPUT_BIT_SIZE = BigInteger.valueOf(4096); private static final int EVM_WORD_SIZE = 32; + public Module callCounter() { + return this.modExpCallCounter; + } + @Override public String moduleKey() { return "PRECOMPILE_MODEXP_EFFECTIVE_CALL"; @@ -65,7 +69,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.MODEXP)) { - this.modexp.countACAllToPrecompile(); + this.modExpCallCounter.tick(); long length = 0; long offset = 0; switch (opCode) { diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160Blocks.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160Blocks.java index e426767dc5..e4092cecad 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160Blocks.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160Blocks.java @@ -31,8 +31,7 @@ @RequiredArgsConstructor public final class Rip160Blocks implements Module { private final Hub hub; - private final Rip160 rip160NbCall; - private final Rip160EffectiveCall rip160NbEffectiveCall; + private final Rip160CallCounter rip160CallCounter = new Rip160CallCounter(); private final Stack counts = new Stack<>(); @Override @@ -47,6 +46,10 @@ public String moduleKey() { private static final int RIPEMD160_LENGTH_APPEND = 64; private static final int RIPEMD160_ND_PADDED_ONE = 1; + public Module callCounter() { + return this.rip160CallCounter; + } + @Override public void enterTransaction() { counts.push(0); @@ -65,7 +68,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.RIPEMD160)) { - this.rip160NbCall.countACAllToPrecompile(); + this.rip160CallCounter.tick(); long dataByteLength = 0; switch (opCode) { case CALL, CALLCODE -> dataByteLength = Words.clampedToLong(frame.getStackItem(4)); @@ -90,7 +93,6 @@ public void tracePreOpcode(MessageFrame frame) { if (gasPaid >= gasNeeded) { this.counts.push(this.counts.pop() + blockCount); - this.rip160NbEffectiveCall.countACAllToPrecompile(); } } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160CallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160CallCounter.java new file mode 100644 index 0000000000..b16ee2f604 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160CallCounter.java @@ -0,0 +1,23 @@ +/* + * Copyright ConsenSys 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.module.limits.precompiles; + +public final class Rip160CallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_RIPEMD"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160EffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160EffectiveCall.java deleted file mode 100644 index 4b4587b22c..0000000000 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160EffectiveCall.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright ConsenSys 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.module.limits.precompiles; - -import java.nio.MappedByteBuffer; -import java.util.List; -import java.util.Stack; - -import net.consensys.linea.zktracer.ColumnHeader; -import net.consensys.linea.zktracer.module.Module; - -public final class Rip160EffectiveCall implements Module { - @Override - public String moduleKey() { - return "PRECOMPILE_RIPEMD_EFFECTIVE_CALL"; - } - - private final Stack counts = new Stack<>(); - - @Override - public void enterTransaction() { - this.counts.push(0); - } - - @Override - public void popTransaction() { - this.counts.pop(); - } - - public void countACAllToPrecompile() { - this.counts.push(this.counts.pop() + 1); - } - - @Override - public int lineCount() { - return this.counts.stream().mapToInt(x -> x).sum(); - } - - @Override - public List columnsHeaders() { - throw new IllegalStateException("should never be called"); - } - - @Override - public void commit(List buffers) { - throw new IllegalStateException("should never be called"); - } -} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256Blocks.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256Blocks.java index c82dcce0f5..3437f61964 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256Blocks.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256Blocks.java @@ -31,8 +31,7 @@ @RequiredArgsConstructor public final class Sha256Blocks implements Module { private final Hub hub; - private final Sha256 sha256NbCall; - private final Sha256EffectiveCall sha256NbEffectiveCall; + private final Sha256CallCounter sha256CallCounter = new Sha256CallCounter(); private final Stack counts = new Stack<>(); @Override @@ -47,6 +46,10 @@ public String moduleKey() { private static final int SHA256_PADDING_LENGTH = 64; private static final int SHA256_NB_PADDED_ONE = 1; + public Module callCounter() { + return this.sha256CallCounter; + } + @Override public void enterTransaction() { counts.push(0); @@ -65,7 +68,7 @@ public void tracePreOpcode(MessageFrame frame) { case CALL, STATICCALL, DELEGATECALL, CALLCODE -> { final Address target = Words.toAddress(frame.getStackItem(1)); if (target.equals(Address.SHA256)) { - this.sha256NbCall.countACallToPrecompile(); + this.sha256CallCounter.tick(); long dataByteLength = 0; switch (opCode) { case CALL, CALLCODE -> dataByteLength = Words.clampedToLong(frame.getStackItem(4)); @@ -89,7 +92,6 @@ public void tracePreOpcode(MessageFrame frame) { if (gasPaid >= gasNeeded) { this.counts.push(this.counts.pop() + blockCount); - this.sha256NbEffectiveCall.countACallToPrecompile(); } } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256CallCounter.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256CallCounter.java new file mode 100644 index 0000000000..20a2e53943 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256CallCounter.java @@ -0,0 +1,23 @@ +/* + * Copyright ConsenSys 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.module.limits.precompiles; + +public final class Sha256CallCounter extends AbstractCallCounter { + @Override + public String moduleKey() { + return "PRECOMPILE_SHA2"; + } +} diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256EffectiveCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256EffectiveCall.java deleted file mode 100644 index 027ddd33c2..0000000000 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256EffectiveCall.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright ConsenSys 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.module.limits.precompiles; - -import java.nio.MappedByteBuffer; -import java.util.List; -import java.util.Stack; - -import net.consensys.linea.zktracer.ColumnHeader; -import net.consensys.linea.zktracer.module.Module; - -public final class Sha256EffectiveCall implements Module { - @Override - public String moduleKey() { - return "PRECOMPILE_SHA2_EFFECTIVE_CALL"; - } - - private final Stack counts = new Stack<>(); - - @Override - public void enterTransaction() { - this.counts.push(0); - } - - @Override - public void popTransaction() { - this.counts.pop(); - } - - public void countACallToPrecompile() { - this.counts.push(this.counts.pop() + 1); - } - - @Override - public int lineCount() { - return this.counts.stream().mapToInt(x -> x).sum(); - } - - @Override - public List columnsHeaders() { - throw new IllegalStateException("should never be called"); - } - - @Override - public void commit(List buffers) { - throw new IllegalStateException("should never be called"); - } -}