Skip to content

Commit 073437a

Browse files
committed
fix: make precompile counters work
1 parent ecaea4b commit 073437a

26 files changed

+330
-259
lines changed

acceptance-tests/src/test/resources/moduleLimits.toml

+10-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@ PRECOMPILE_ECADD_EFFECTIVE_CALL = 10000
6565
PRECOMPILE_ECMUL_EFFECTIVE_CALL = 10000
6666
PRECOMPILE_ECPAIRING_EFFECTIVE_CALL = 10000
6767
PRECOMPILE_ECPAIRING_MILLER_LOOP = 10000
68-
PRECOMPILE_BLAKE2F_ROUNDS = 512
68+
PRECOMPILE_BLAKE2F_ROUNDS = 512
69+
70+
PRECOMPILE_BLAKE2F = 512
71+
PRECOMPILE_ECADD = 512
72+
PRECOMPILE_ECMUL = 512
73+
PRECOMPILE_ECPAIRING = 512
74+
PRECOMPILE_ECRECOVER = 512
75+
PRECOMPILE_MODEXP = 512
76+
PRECOMPILE_RIPEMD = 512
77+
PRECOMPILE_SHA2 = 512

acceptance-tests/src/test/resources/noModuleLimits.toml

+10-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@ PRECOMPILE_ECADD_EFFECTIVE_CALL = 10000
6565
PRECOMPILE_ECMUL_EFFECTIVE_CALL = 10000
6666
PRECOMPILE_ECPAIRING_EFFECTIVE_CALL = 10000
6767
PRECOMPILE_ECPAIRING_MILLER_LOOP = 10000
68-
PRECOMPILE_BLAKE2F_ROUNDS = 512
68+
PRECOMPILE_BLAKE2F_ROUNDS = 512
69+
70+
PRECOMPILE_BLAKE2F = 512
71+
PRECOMPILE_ECADD = 512
72+
PRECOMPILE_ECMUL = 512
73+
PRECOMPILE_ECPAIRING = 512
74+
PRECOMPILE_ECRECOVER = 512
75+
PRECOMPILE_MODEXP = 512
76+
PRECOMPILE_RIPEMD = 512
77+
PRECOMPILE_SHA2 = 512

acceptance-tests/src/test/resources/txOverflowModuleLimits.toml

+10-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,13 @@ PRECOMPILE_ECADD_EFFECTIVE_CALL = 10000
6666
PRECOMPILE_ECMUL_EFFECTIVE_CALL = 10000
6767
PRECOMPILE_ECPAIRING_EFFECTIVE_CALL = 10000
6868
PRECOMPILE_ECPAIRING_MILLER_LOOP = 10000
69-
PRECOMPILE_BLAKE2F_ROUNDS = 512
69+
PRECOMPILE_BLAKE2F_ROUNDS = 512
70+
71+
PRECOMPILE_BLAKE2F = 512
72+
PRECOMPILE_ECADD = 512
73+
PRECOMPILE_ECMUL = 512
74+
PRECOMPILE_ECPAIRING = 512
75+
PRECOMPILE_ECRECOVER = 512
76+
PRECOMPILE_MODEXP = 512
77+
PRECOMPILE_RIPEMD = 512
78+
PRECOMPILE_SHA2 = 512

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java

+50-33
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,12 @@
4848
import net.consensys.linea.zktracer.module.limits.precompiles.Blake2fRounds;
4949
import net.consensys.linea.zktracer.module.limits.precompiles.EcAddEffectiveCall;
5050
import net.consensys.linea.zktracer.module.limits.precompiles.EcMulEffectiveCall;
51-
import net.consensys.linea.zktracer.module.limits.precompiles.EcPairing;
5251
import net.consensys.linea.zktracer.module.limits.precompiles.EcPairingEffectiveCall;
5352
import net.consensys.linea.zktracer.module.limits.precompiles.EcPairingMillerLoop;
5453
import net.consensys.linea.zktracer.module.limits.precompiles.EcRecoverEffectiveCall;
55-
import net.consensys.linea.zktracer.module.limits.precompiles.ModExp;
56-
import net.consensys.linea.zktracer.module.limits.precompiles.ModexpEffectiveCall;
54+
import net.consensys.linea.zktracer.module.limits.precompiles.Modexp;
5755
import net.consensys.linea.zktracer.module.limits.precompiles.Rip160Blocks;
58-
import net.consensys.linea.zktracer.module.limits.precompiles.Rip160EffectiveCall;
5956
import net.consensys.linea.zktracer.module.limits.precompiles.Sha256Blocks;
60-
import net.consensys.linea.zktracer.module.limits.precompiles.Sha256EffectiveCall;
6157
import net.consensys.linea.zktracer.module.logData.LogData;
6258
import net.consensys.linea.zktracer.module.logInfo.LogInfo;
6359
import net.consensys.linea.zktracer.module.mmu.Mmu;
@@ -189,19 +185,27 @@ public void addTraceSection(TraceSection section) {
189185
private final Trm trm = new Trm();
190186
private final Stp stp = new Stp(this, wcp, mod);
191187
private final L2Block l2Block = new L2Block();
192-
private final Sha256EffectiveCall sha256NbEffectiveCall = new Sha256EffectiveCall();
193-
private final Rip160EffectiveCall rip160NbEffectiveCall = new Rip160EffectiveCall();
194-
private final EcPairing ecPairingNbCall = new EcPairing();
195-
private final ModExp modExp = new ModExp();
196-
private final ModexpEffectiveCall modexpEffectiveCall = new ModexpEffectiveCall(this, modExp);
197188
private final HashInfo hashInfo;
198189
private final HashData hashData;
199190

191+
// Precompiles stuff
192+
Blake2fRounds blake2f;
193+
EcAddEffectiveCall ecAdd;
194+
EcMulEffectiveCall ecMul;
195+
EcPairingEffectiveCall ecPairing;
196+
EcRecoverEffectiveCall ecRecover;
197+
Modexp modexp;
198+
Rip160Blocks rip160;
199+
Sha256Blocks sha256;
200+
200201
private final List<Module> modules;
201202
/* Those modules are not traced, we just compute the number of calls to those precompile to meet the prover limits */
202203
private final List<Module> precompileLimitModules;
203204

204205
public Hub() {
206+
//
207+
// Module
208+
//
205209
this.pch = new PlatformController(this);
206210
this.mmu = new Mmu(this.callStack);
207211
this.mxp = new Mxp(this);
@@ -213,32 +217,41 @@ public Hub() {
213217
this.hashData = new HashData(this);
214218
this.hashInfo = new HashInfo(this);
215219

216-
final EcRecoverEffectiveCall ecRec = new EcRecoverEffectiveCall(this, ecRecover);
217-
final EcPairingEffectiveCall ecpairingNbEffectiveCall =
218-
new EcPairingEffectiveCall(this, ecPairingNbCall);
220+
//
221+
// Precompiles
222+
//
223+
this.blake2f = new Blake2fRounds(this);
224+
this.ecAdd = new EcAddEffectiveCall(this);
225+
this.ecMul = new EcMulEffectiveCall(this);
226+
this.ecPairing = new EcPairingEffectiveCall(this);
227+
this.ecRecover = new EcRecoverEffectiveCall(this);
228+
this.modexp = new Modexp(this);
229+
this.rip160 = new Rip160Blocks(this);
230+
this.sha256 = new Sha256Blocks(this);
231+
219232
this.precompileLimitModules =
220233
List.of(
221-
sha256NbCall,
222-
sha256NbEffectiveCall,
223-
new Sha256Blocks(this, sha256NbCall, sha256NbEffectiveCall),
224-
ecRec,
225-
rip160NbCall,
226-
rip160NbEffectiveCall,
227-
new Rip160Blocks(this, rip160NbCall, rip160NbEffectiveCall),
228-
modExp,
229-
modexpEffectiveCall,
230-
ecAdd,
231-
new EcAddEffectiveCall(this, ecAdd),
232-
ecMul,
233-
new EcMulEffectiveCall(this, ecMul),
234-
ecPairingNbCall,
235-
ecpairingNbEffectiveCall,
236-
new EcPairingMillerLoop(ecpairingNbEffectiveCall),
237-
blake2f,
238-
new Blake2fRounds(this, blake2f),
234+
this.blake2f,
235+
this.blake2f.callCounter(),
236+
this.ecAdd,
237+
this.ecAdd.callCounter(),
238+
this.ecMul,
239+
this.ecMul.callCounter(),
240+
this.ecPairing,
241+
this.ecPairing.callCounter(),
242+
new EcPairingMillerLoop(this.ecPairing),
243+
this.ecRecover,
244+
this.ecRecover.callCounter(),
245+
this.modexp,
246+
this.modexp.callCounter(),
247+
this.rip160,
248+
this.rip160.callCounter(),
249+
this.sha256,
250+
this.sha256.callCounter(),
251+
239252
// Block level limits
240253
this.l2Block,
241-
new Keccak(this, ecRec, this.l2Block),
254+
new Keccak(this, this.ecRecover, this.l2Block),
242255
new L2L1Logs(this.l2Block));
243256

244257
this.modules =
@@ -274,11 +287,15 @@ public Hub() {
274287
*/
275288
public List<Module> getModulesToTrace() {
276289
return List.of(
290+
//
277291
// Reference tables
292+
//
278293
new BinRt(),
279294
new InstructionDecoder(),
280295
new ShfRt(),
296+
//
281297
// Modules
298+
//
282299
this,
283300
this.add,
284301
this.bin,
@@ -538,7 +555,7 @@ void triggerModules(MessageFrame frame) {
538555
// this.stp.tracePreOpcode(frame);
539556
}
540557
if (this.pch.signals().exp()) {
541-
this.modexpEffectiveCall.tracePreOpcode(frame);
558+
this.modexp.tracePreOpcode(frame);
542559
}
543560
if (this.pch.signals().trm()) {
544561
this.trm.tracePreOpcode(frame);

arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HashData implements Module {
3636

3737
@Override
3838
public String moduleKey() {
39-
return "OLD_PUB_HASH";
39+
return "PUB_HASH";
4040
}
4141

4242
@Override

arithmetization/src/main/java/net/consensys/linea/zktracer/module/legacy/hash/HashInfo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class HashInfo implements Module {
3333

3434
@Override
3535
public String moduleKey() {
36-
return "OLD_PUB_HASH_INFO";
36+
return "PUB_HASH_INFO";
3737
}
3838

3939
@Override
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ConsenSys Inc.
2+
* Copyright ConsenSys AG.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -15,47 +15,43 @@
1515

1616
package net.consensys.linea.zktracer.module.limits.precompiles;
1717

18-
import java.nio.MappedByteBuffer;
18+
import java.util.ArrayDeque;
19+
import java.util.Deque;
1920
import java.util.List;
20-
import java.util.Stack;
2121

2222
import net.consensys.linea.zktracer.ColumnHeader;
2323
import net.consensys.linea.zktracer.module.Module;
24+
import org.apache.commons.lang3.NotImplementedException;
25+
26+
public class AbstractCallCounter implements Module {
27+
private final Deque<Integer> callCount = new ArrayDeque<>();
2428

25-
public final class ModExp implements Module {
2629
@Override
2730
public String moduleKey() {
28-
return "PRECOMPILE_MODEXP";
31+
throw new NotImplementedException("must be implemented by derived class");
2932
}
3033

31-
private final Stack<Integer> counts = new Stack<>();
32-
3334
@Override
3435
public void enterTransaction() {
35-
this.counts.push(0);
36+
this.callCount.push(0);
3637
}
3738

3839
@Override
3940
public void popTransaction() {
40-
this.counts.pop();
41-
}
42-
43-
public void countACAllToPrecompile() {
44-
this.counts.push(this.counts.pop() + 1);
41+
this.callCount.pop();
4542
}
4643

4744
@Override
4845
public int lineCount() {
49-
return this.counts.stream().mapToInt(x -> x).sum();
46+
return this.callCount.stream().mapToInt(x -> x).sum();
5047
}
5148

52-
@Override
53-
public List<ColumnHeader> columnsHeaders() {
54-
throw new IllegalStateException("should never be called");
49+
public void tick() {
50+
this.callCount.push(this.callCount.pop() + 1);
5551
}
5652

5753
@Override
58-
public void commit(List<MappedByteBuffer> buffers) {
54+
public List<ColumnHeader> columnsHeaders() {
5955
throw new IllegalStateException("should never be called");
6056
}
6157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright ConsenSys AG.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.limits.precompiles;
17+
18+
public class Blake2fCallCounter extends AbstractCallCounter {
19+
@Override
20+
public String moduleKey() {
21+
return "PRECOMPILE_BLAKE2F";
22+
}
23+
}

arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/Blake2fRounds.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030

3131
@RequiredArgsConstructor
3232
public final class Blake2fRounds implements Module {
33-
final Hub hub;
34-
final Blake2f blake2f;
33+
private final Hub hub;
34+
private final Blake2fCallCounter blake2fCallCounter = new Blake2fCallCounter();
3535
private final Stack<Integer> counts = new Stack<>();
3636

37+
public Module callCounter() {
38+
return this.blake2fCallCounter;
39+
}
40+
3741
@Override
3842
public String moduleKey() {
3943
return "PRECOMPILE_BLAKE2F_ROUNDS";
@@ -57,7 +61,7 @@ public void tracePreOpcode(MessageFrame frame) {
5761
case CALL, STATICCALL, DELEGATECALL, CALLCODE -> {
5862
final Address target = Words.toAddress(frame.getStackItem(1));
5963
if (target.equals(Address.BLAKE2B_F_COMPRESSION)) {
60-
this.blake2f.countACAllToPrecompile();
64+
this.blake2fCallCounter.tick();
6165
long length = 0;
6266
long offset = 0;
6367
switch (opCode) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright ConsenSys AG.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.limits.precompiles;
17+
18+
public class EcAddCallCounter extends AbstractCallCounter {
19+
@Override
20+
public String moduleKey() {
21+
return "PRECOMPILE_ECADD";
22+
}
23+
}

arithmetization/src/main/java/net/consensys/linea/zktracer/module/limits/precompiles/EcAddEffectiveCall.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131
@RequiredArgsConstructor
3232
public final class EcAddEffectiveCall implements Module {
3333
private final Hub hub;
34-
private final EcAdd ecAdd;
34+
private final EcAddCallCounter ecAddCallCounter = new EcAddCallCounter();
3535
private final Stack<Integer> counts = new Stack<>();
3636

37+
public Module callCounter() {
38+
return this.ecAddCallCounter;
39+
}
40+
3741
@Override
3842
public String moduleKey() {
3943
return "PRECOMPILE_ECADD_EFFECTIVE_CALL";
@@ -59,7 +63,7 @@ public void tracePreOpcode(MessageFrame frame) {
5963
case CALL, STATICCALL, DELEGATECALL, CALLCODE -> {
6064
final Address target = Words.toAddress(frame.getStackItem(1));
6165
if (target.equals(Address.ALTBN128_ADD)) {
62-
this.ecAdd.countACAllToPrecompile();
66+
this.ecAddCallCounter.tick();
6367
final long gasPaid = Words.clampedToLong(frame.getStackItem(0));
6468
if (gasPaid >= PRECOMPILE_GAS_FEE) {
6569
this.counts.push(this.counts.pop() + 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright ConsenSys AG.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.limits.precompiles;
17+
18+
public class EcMulCallCounter extends AbstractCallCounter {
19+
@Override
20+
public String moduleKey() {
21+
return "PRECOMPILE_ECMUL";
22+
}
23+
}

0 commit comments

Comments
 (0)