From fb70845eb913078ef0b437642bb7354affb92419 Mon Sep 17 00:00:00 2001 From: Francois Bojarski Date: Mon, 18 Dec 2023 22:38:10 +0100 Subject: [PATCH] feat: add counter for sha256 abd ripemd call --- .../linea/zktracer/module/hub/Hub.java | 10 ++- .../module/limits/precompiles/Rip160.java | 2 + .../limits/precompiles/Rip160NbCall.java | 61 +++++++++++++++++++ .../module/limits/precompiles/Sha256.java | 2 + .../limits/precompiles/Sha256NbCall.java | 61 +++++++++++++++++++ 5 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160NbCall.java create mode 100644 arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256NbCall.java 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 129418af6d..eb2b856d4e 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 @@ -51,7 +51,9 @@ import net.consensys.linea.zktracer.module.limits.precompiles.EcRecover; import net.consensys.linea.zktracer.module.limits.precompiles.Modexp; import net.consensys.linea.zktracer.module.limits.precompiles.Rip160; +import net.consensys.linea.zktracer.module.limits.precompiles.Rip160NbCall; import net.consensys.linea.zktracer.module.limits.precompiles.Sha256; +import net.consensys.linea.zktracer.module.limits.precompiles.Sha256NbCall; import net.consensys.linea.zktracer.module.logData.LogData; import net.consensys.linea.zktracer.module.logInfo.LogInfo; import net.consensys.linea.zktracer.module.mmu.Mmu; @@ -184,6 +186,8 @@ public void addTraceSection(TraceSection section) { private final Modexp modexp; private final Stp stp = new Stp(this, wcp, mod); private final L2Block l2Block = new L2Block(); + private final Sha256NbCall sha256NbCall = new Sha256NbCall(); + private final Rip160NbCall rip160NbCall = new Rip160NbCall(); private final List modules; /* Those modules are not traced, we just compute the number of calls to those precompile to meet the prover limits */ @@ -204,9 +208,11 @@ public Hub() { final EcPairingCall ecpairingCall = new EcPairingCall(this); this.precompileLimitModules = List.of( - new Sha256(this), + sha256NbCall, + new Sha256(this, sha256NbCall), ecRec, - new Rip160(this), + rip160NbCall, + new Rip160(this, rip160NbCall), this.modexp, new EcAdd(this), new EcMul(this), diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160.java index 5342198454..409483c044 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160.java @@ -31,6 +31,7 @@ @RequiredArgsConstructor public final class Rip160 implements Module { private final Hub hub; + private final Rip160NbCall rip160NbCall; private final Stack counts = new Stack<>(); @Override @@ -87,6 +88,7 @@ public void tracePreOpcode(MessageFrame frame) { if (gasPaid >= gasNeeded) { this.counts.push(this.counts.pop() + blockCount); + this.rip160NbCall.countACAllToPrecompile(); } } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160NbCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160NbCall.java new file mode 100644 index 0000000000..29bdf389c8 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Rip160NbCall.java @@ -0,0 +1,61 @@ +/* + * 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 Rip160NbCall implements Module { + @Override + public String moduleKey() { + return "PRECOMPILE_RIP160_NB_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/Sha256.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256.java index f945dad4dc..52184af28e 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256.java @@ -31,6 +31,7 @@ @RequiredArgsConstructor public final class Sha256 implements Module { private final Hub hub; + private final Sha256NbCall sha256NbCall; private final Stack counts = new Stack<>(); @Override @@ -86,6 +87,7 @@ public void tracePreOpcode(MessageFrame frame) { if (gasPaid >= gasNeeded) { this.counts.push(this.counts.pop() + blockCount); + this.sha256NbCall.countACallToPrecompile(); } } } diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256NbCall.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256NbCall.java new file mode 100644 index 0000000000..b3a28c0f59 --- /dev/null +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Sha256NbCall.java @@ -0,0 +1,61 @@ +/* + * 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 Sha256NbCall implements Module { + @Override + public String moduleKey() { + return "PRECOMPILE_SHA2_NB_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"); + } +}