Skip to content

Commit

Permalink
feat: add counter for sha256 abd ripemd call
Browse files Browse the repository at this point in the history
  • Loading branch information
letypequividelespoubelles committed Dec 18, 2023
1 parent 22b72d7 commit fb70845
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Module> modules;
/* Those modules are not traced, we just compute the number of calls to those precompile to meet the prover limits */
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@RequiredArgsConstructor
public final class Rip160 implements Module {
private final Hub hub;
private final Rip160NbCall rip160NbCall;
private final Stack<Integer> counts = new Stack<>();

@Override
Expand Down Expand Up @@ -87,6 +88,7 @@ public void tracePreOpcode(MessageFrame frame) {

if (gasPaid >= gasNeeded) {
this.counts.push(this.counts.pop() + blockCount);
this.rip160NbCall.countACAllToPrecompile();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<ColumnHeader> columnsHeaders() {
throw new IllegalStateException("should never be called");
}

@Override
public void commit(List<MappedByteBuffer> buffers) {
throw new IllegalStateException("should never be called");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@RequiredArgsConstructor
public final class Sha256 implements Module {
private final Hub hub;
private final Sha256NbCall sha256NbCall;
private final Stack<Integer> counts = new Stack<>();

@Override
Expand Down Expand Up @@ -86,6 +87,7 @@ public void tracePreOpcode(MessageFrame frame) {

if (gasPaid >= gasNeeded) {
this.counts.push(this.counts.pop() + blockCount);
this.sha256NbCall.countACallToPrecompile();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<ColumnHeader> columnsHeaders() {
throw new IllegalStateException("should never be called");
}

@Override
public void commit(List<MappedByteBuffer> buffers) {
throw new IllegalStateException("should never be called");
}
}

0 comments on commit fb70845

Please sign in to comment.