Skip to content

Commit

Permalink
fix: make precompile counters work
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Dec 20, 2023
1 parent ecaea4b commit 10f03ea
Show file tree
Hide file tree
Showing 26 changed files with 333 additions and 262 deletions.
11 changes: 10 additions & 1 deletion acceptance-tests/src/test/resources/moduleLimits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
11 changes: 10 additions & 1 deletion acceptance-tests/src/test/resources/noModuleLimits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
11 changes: 10 additions & 1 deletion acceptance-tests/src/test/resources/txOverflowModuleLimits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Module> modules;
/* Those modules are not traced, we just compute the number of calls to those precompile to meet the prover limits */
private final List<Module> precompileLimitModules;

public Hub() {
//
// Module
//
this.pch = new PlatformController(this);
this.mmu = new Mmu(this.callStack);
this.mxp = new Mxp(this);
Expand All @@ -213,32 +217,43 @@ 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.hashData,
this.hashInfo,
this.l2Block,
new Keccak(this, ecRec, this.l2Block),
new Keccak(this, this.ecRecover, this.l2Block),
new L2L1Logs(this.l2Block));

this.modules =
Expand All @@ -262,9 +277,7 @@ public Hub() {
this.trm,
// this.txnData,
this.stp,
this.wcp,
this.hashData,
this.hashInfo),
this.wcp),
this.precompileLimitModules.stream())
.toList();
}
Expand All @@ -274,11 +287,15 @@ public Hub() {
*/
public List<Module> getModulesToTrace() {
return List.of(
//
// Reference tables
//
new BinRt(),
new InstructionDecoder(),
new ShfRt(),
//
// Modules
//
this,
this.add,
this.bin,
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class HashData implements Module {

@Override
public String moduleKey() {
return "OLD_PUB_HASH";
return "PUB_HASH";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class HashInfo implements Module {

@Override
public String moduleKey() {
return "OLD_PUB_HASH_INFO";
return "PUB_HASH_INFO";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Integer> 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<Integer> 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<ColumnHeader> columnsHeaders() {
throw new IllegalStateException("should never be called");
public void tick() {
this.callCount.push(this.callCount.pop() + 1);
}

@Override
public void commit(List<MappedByteBuffer> buffers) {
public List<ColumnHeader> columnsHeaders() {
throw new IllegalStateException("should never be called");
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> counts = new Stack<>();

public Module callCounter() {
return this.blake2fCallCounter;
}

@Override
public String moduleKey() {
return "PRECOMPILE_BLAKE2F_ROUNDS";
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> counts = new Stack<>();

public Module callCounter() {
return this.ecAddCallCounter;
}

@Override
public String moduleKey() {
return "PRECOMPILE_ECADD_EFFECTIVE_CALL";
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 10f03ea

Please sign in to comment.