Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package net.consensys.linea.zktracer;

import static net.consensys.linea.zktracer.Fork.*;
import static net.consensys.linea.zktracer.Trace.*;
import static net.consensys.linea.zktracer.Trace.Ecdata.TOTAL_SIZE_ECPAIRING_DATA_MIN;
import static net.consensys.linea.zktracer.TraceCancun.Oob.CT_MAX_CALL;
import static net.consensys.linea.zktracer.TraceCancun.Oob.CT_MAX_CREATE;
import static net.consensys.linea.zktracer.module.ModuleName.*;
import static net.consensys.linea.zktracer.module.ModuleName.GAS;
import static net.consensys.linea.zktracer.module.add.AddOperation.NB_ROWS_ADD;
import static net.consensys.linea.zktracer.module.blake2fmodexpdata.BlakeModexpDataOperation.NB_ROWS_BLAKEMODEPX_BLAKE;
import static net.consensys.linea.zktracer.module.blake2fmodexpdata.BlakeModexpDataOperation.NB_ROWS_BLAKEMODEXP_MODEXP;
import static net.consensys.linea.zktracer.module.blockdata.module.CancunBlockData.NB_ROWS_BLOCK_DATA;
import static net.consensys.linea.zktracer.module.blockhash.BlockhashOperation.NB_ROWS_BLOCKHASH;
Expand All @@ -42,6 +44,7 @@
import static net.consensys.linea.zktracer.module.hub.section.StackOnlySection.NB_ROWS_HUB_SIMPLE_STACK_OP;
import static net.consensys.linea.zktracer.module.hub.section.StackRamSection.NB_ROWS_HUB_STACKRAM;
import static net.consensys.linea.zktracer.module.hub.section.call.CallSection.NB_ROWS_HUB_CALL;
import static net.consensys.linea.zktracer.module.hub.section.call.precompileSubsection.BlakeSubsection.NB_ROWS_HUB_PRC_BLAKE;
import static net.consensys.linea.zktracer.module.hub.section.call.precompileSubsection.EllipticCurvePrecompileSubsection.NB_ROWS_HUB_PRC_ELLIPTIC_CURVE;
import static net.consensys.linea.zktracer.module.hub.section.call.precompileSubsection.IdentitySubsection.NB_ROWS_HUB_PRC_IDENTITY;
import static net.consensys.linea.zktracer.module.hub.section.call.precompileSubsection.ModexpSubsection.NB_ROWS_HUB_PRC_MODEXP;
Expand All @@ -65,7 +68,6 @@
import static net.consensys.linea.zktracer.module.hub.section.txInitializationSection.TxInitializationSection.NB_ROWS_HUB_INIT;
import static net.consensys.linea.zktracer.module.logdata.LogData.lineCountForLogData;
import static net.consensys.linea.zktracer.module.loginfo.LogInfo.lineCountForLogInfo;
import static net.consensys.linea.zktracer.module.mod.ModOperation.NB_ROWS_MOD;
import static net.consensys.linea.zktracer.module.mxp.moduleCall.CancunMSizeMxpCall.NB_ROWS_MXP_MSIZE;
import static net.consensys.linea.zktracer.module.mxp.moduleCall.CancunStateUpdateMxpCall.NB_ROWS_MXP_UPDT_B;
import static net.consensys.linea.zktracer.module.mxp.moduleCall.CancunStateUpdateWordPricingMxpCall.NB_ROWS_MXP_UPDT_W;
Expand All @@ -88,7 +90,6 @@
import java.util.*;
import java.util.stream.Stream;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.plugins.config.LineaL1L2BridgeSharedConfiguration;
import net.consensys.linea.zktracer.container.module.CountingOnlyModule;
Expand Down Expand Up @@ -209,16 +210,9 @@ public class ZkCounter implements LineCountingTracer {
// related to Shakira:
private final Keccak keccak;
private final Sha256Blocks sha256Blocks = new Sha256Blocks();
private final IncrementAndDetectModule ripemdBlocks =
new IncrementAndDetectModule(PRECOMPILE_RIPEMD_BLOCKS);
private final CountingOnlyModule ripemdBlocks = new CountingOnlyModule(PRECOMPILE_RIPEMD_BLOCKS);

// Related to Bls
// remove me when Linea supports Cancun & Prague precompiles
@Getter
private final IncrementAndDetectModule pointEval = new IncrementAndDetectModule(POINT_EVAL) {};

@Getter private final IncrementAndDetectModule bls = new IncrementAndDetectModule(BLS) {};

final IncrementingModule pointEvaluationEffectiveCall =
new IncrementingModule(PRECOMPILE_BLS_POINT_EVALUATION_EFFECTIVE_CALLS);
final IncrementingModule pointEvaluationFailureCall =
Expand Down Expand Up @@ -313,8 +307,6 @@ public List<Module> checkedModules() {
modexpEffectiveCall,
modexpLargeCall,
blakeEffectiveCall,
bls,
pointEval,
pointEvaluationEffectiveCall,
pointEvaluationFailureCall,
blsG1AddEffectiveCall,
Expand Down Expand Up @@ -694,7 +686,6 @@ public void tracePrecompileCall(MessageFrame frame, long gasRequirement, Bytes o
final boolean prcSuccess = frame.getState() == COMPLETED_SUCCESS;
final Bytes returnData = output == null ? Bytes.EMPTY : output;

// MMU
switch (precompile) {
case PRC_ECRECOVER, PRC_ECADD, PRC_ECMUL -> {
// trigger EcData to count the underlying EC operations
Expand All @@ -708,28 +699,25 @@ public void tracePrecompileCall(MessageFrame frame, long gasRequirement, Bytes o
case PRC_SHA2_256 -> {
hub.updateTally(NB_ROWS_HUB_PRC_SHARIP);
oob.updateTally(oobLineCountForPrc(precompile));
mod.updateTally(NB_ROWS_MOD); // coming from OOB call
mod.updateTally(modLinesComingFromOobCall(precompile));
if (prcSuccess && callDataSize != 0) {
shakiradata.updateTally(fromDataSizeToLimbNbRows(callDataSize) + NB_ROWS_SHAKIRA_RESULT);
sha256Blocks.updateTally(callData.size());
}
}
case PRC_RIPEMD_160 -> {
ripemdBlocks.detectEvent();
// Reenable me when RIPEMD is supported by the prover
// hub.updateTally(NB_ROWS_HUB_PRC_SHARIP);
// oob.updateTally(oobLineCountforPrc(precompile));
// mod.updateTally(NB_ROWS_MOD); // coming from OOB call
// if (prcSuccess && callDataSize != 0) {
// shakiradata.updateTally(fromDataSizeToLimbNbRows(callDataSize) +
// NB_ROWS_SHAKIRA_RESULT);
// ripemdBlocks.updateTally(callDataSize);
// }
hub.updateTally(NB_ROWS_HUB_PRC_SHARIP);
oob.updateTally(oobLineCountForPrc(precompile));
mod.updateTally(modLinesComingFromOobCall(precompile));
if (prcSuccess && callDataSize != 0) {
shakiradata.updateTally(fromDataSizeToLimbNbRows(callDataSize) + NB_ROWS_SHAKIRA_RESULT);
ripemdBlocks.updateTally(callDataSize);
}
}
case PRC_IDENTITY -> {
hub.updateTally(NB_ROWS_HUB_PRC_IDENTITY);
oob.updateTally(oobLineCountForPrc(precompile));
mod.updateTally(NB_ROWS_MOD); // coming from OOB call
mod.updateTally(modLinesComingFromOobCall(precompile));
}
case PRC_MODEXP -> {
hub.updateTally(NB_ROWS_HUB_PRC_MODEXP);
Expand All @@ -747,7 +735,7 @@ public void tracePrecompileCall(MessageFrame frame, long gasRequirement, Bytes o
exp.call(modexpLogCallToExp);
}
oob.updateTally(oobLineCountForPrc(precompile));
mod.updateTally(2 * NB_ROWS_MOD); // 2 coming from OOB pricing call
mod.updateTally(modLinesComingFromOobCall(precompile));
}
case PRC_ECPAIRING -> {
// trigger EcData to count the underlying EC operations
Expand All @@ -757,9 +745,15 @@ public void tracePrecompileCall(MessageFrame frame, long gasRequirement, Bytes o
}
hub.updateTally(NB_ROWS_HUB_PRC_ELLIPTIC_CURVE);
oob.updateTally(oobLineCountForPrc(precompile));
mod.updateTally(NB_ROWS_MOD); // coming from OOB call
mod.updateTally(modLinesComingFromOobCall(precompile));
}
case PRC_BLAKE2F -> {
blakeEffectiveCall.updateTally(true);
hub.updateTally(NB_ROWS_HUB_PRC_BLAKE);
oob.updateTally(oobLineCountForPrc(PRC_BLAKE2F));
blakemodexp.updateTally(NB_ROWS_BLAKEMODEPX_BLAKE);
// TODO: still unchecked module for now: blakeRounds.updateTally();
}
case PRC_BLAKE2F -> blakeEffectiveCall.detectEvent();
case PRC_BLS_G1_ADD,
PRC_BLS_G1_MSM,
PRC_BLS_G2_ADD,
Expand All @@ -768,18 +762,12 @@ public void tracePrecompileCall(MessageFrame frame, long gasRequirement, Bytes o
PRC_BLS_MAP_FP_TO_G1,
PRC_BLS_MAP_FP2_TO_G2,
PRC_POINT_EVALUATION -> {
if (precompile == PRC_POINT_EVALUATION) {
pointEval.detectEvent();
} else {
bls.detectEvent();
}
if (callDataSize != 0) {
// TODO: temporary commented out
// blsdata.callBls(0, precompile, frame.getInputData(), returnData, prcSuccess);
if (validCallDataSize(precompile, callDataSize)) {
blsdata.callBls(0, precompile, frame.getInputData(), returnData, prcSuccess);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A BLS precompile call triggers a BLS_DATA call as soon as the call data is of the right size and gas is sufficient. Why use a partial condition ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because in ZkCounter, we're plugged at tracePrecompileCall which is trigger only if enough gas if I'm not mistaken whereas the hub is trigger by the call preOpcode

}
hub.updateTally(NB_ROWS_HUB_PRC_ELLIPTIC_CURVE);
oob.updateTally(oobLineCountForPrc(precompile));
// TODO: check if we have some MOD coming from OOB call
mod.updateTally(modLinesComingFromOobCall(precompile));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, maybe add here that a comment saying that for PAIRING_CHECK we have a calll to DIV, while for MSM a call to DIV and one to MSM.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while for MSM a call to DIV and one to MSM

What do you mean @lorenzogentile404 ? Do you mean DIV and MOD ?

default -> throw new IllegalStateException("Unsupported precompile: " + precompile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@

package net.consensys.linea.zktracer.container.module;

import static com.google.common.base.Preconditions.checkState;

import lombok.Setter;
import net.consensys.linea.zktracer.module.ModuleName;

public class IncrementAndDetectModule extends IncrementingModule {

public static final String ERROR_MESSAGE_TRIED_TO_COMMIT_UNPROVABLE_TX =
"Shouldn't commit transaction as an unprovable event has been detected.";

@Setter boolean eventDetected = false;

public IncrementAndDetectModule(ModuleName moduleKey) {
Expand All @@ -33,7 +28,6 @@ public IncrementAndDetectModule(ModuleName moduleKey) {

@Override
public void commitTransactionBundle() {
checkState(!eventDetected, ERROR_MESSAGE_TRIED_TO_COMMIT_UNPROVABLE_TX);
super.commitTransactionBundle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public enum ModuleName {
PRECOMPILE_RIPEMD_BLOCKS,

// bls:
POINT_EVAL,
BLS,
PRECOMPILE_POINT_EVALUATION_FAILURE_EFFECTIVE_CALLS,
PRECOMPILE_BLS_POINT_EVALUATION_EFFECTIVE_CALLS,
PRECOMPILE_BLS_G1_ADD_EFFECTIVE_CALLS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@

@Accessors(fluent = true)
public class BlakeModexpDataOperation extends ModuleOperation {
public static final int MODEXP_COMPONENT_BYTE_SIZE = LLARGE * (INDEX_MAX_MODEXP + 1);
public static final int NB_ROWS_BLAKEMODEXP_MODEXP =
public static final short MODEXP_COMPONENT_BYTE_SIZE = LLARGE * (INDEX_MAX_MODEXP + 1);
public static final short NB_ROWS_BLAKEMODEXP_MODEXP =
(INDEX_MAX_MODEXP_BASE + 1)
+ (INDEX_MAX_MODEXP_EXPONENT + 1)
+ (INDEX_MAX_MODEXP_MODULUS + 1)
+ (INDEX_MAX_MODEXP_RESULT + 1);
private static final int BLAKE2f_COMPONENTS_LINE_COUNT =
public static final short NB_ROWS_BLAKEMODEPX_BLAKE =
(INDEX_MAX_BLAKE_DATA + 1) + (INDEX_MAX_BLAKE_PARAMS + 1) + (INDEX_MAX_BLAKE_RESULT + 1);
public static final short BLAKE2f_R_SIZE = 4;
public static final short BLAKE2f_HASH_INPUT_OFFSET = BLAKE2f_R_SIZE;
Expand All @@ -78,11 +78,10 @@ public BlakeModexpDataOperation(final BlakeComponents blakeComponents, final int

@Override
protected int computeLineCount() {
return modexpMetaData.isPresent() ? NB_ROWS_BLAKEMODEXP_MODEXP : BLAKE2f_COMPONENTS_LINE_COUNT;
return modexpMetaData.isPresent() ? NB_ROWS_BLAKEMODEXP_MODEXP : NB_ROWS_BLAKEMODEPX_BLAKE;
}

void trace(Trace.Blake2fmodexpdata trace, final int stamp) {
final UnsignedByte stampByte = UnsignedByte.of(stamp);

if (modexpMetaData.isPresent()) {
traceBase(trace, stamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,6 @@ public int spillage(Trace trace) {
private final BlakeRounds blakeRounds = new BlakeRounds();

// Related to Bls
// TODO: remove me when Linea supports Cancun & Prague precompiles
private final IncrementAndDetectModule pointEval = new IncrementAndDetectModule(POINT_EVAL) {};
private final IncrementAndDetectModule bls = new IncrementAndDetectModule(BLS) {};

final IncrementingModule pointEvaluationEffectiveCall =
new IncrementingModule(PRECOMPILE_BLS_POINT_EVALUATION_EFFECTIVE_CALLS);
final IncrementingModule pointEvaluationFailureCall =
Expand Down Expand Up @@ -336,9 +332,7 @@ public List<Module> getTracelessModules() {
blsG1MembershipCalls,
blsG2MembershipCalls,
l1BlockSize,
l2L1Logs,
pointEval,
bls);
l2L1Logs);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.consensys.linea.zktracer.module.hub.fragment.scenario;

import static java.util.Map.entry;
import static net.consensys.linea.zktracer.Trace.*;
import static net.consensys.linea.zktracer.TraceCancun.Oob.CT_MAX_IDENTITY;
import static net.consensys.linea.zktracer.TraceCancun.Oob.CT_MAX_RIPEMD;
import static net.consensys.linea.zktracer.TracePrague.Blsdata.CT_MAX_POINT_EVALUATION;
Expand All @@ -34,6 +35,7 @@
import static net.consensys.linea.zktracer.module.hub.fragment.imc.oob.precompiles.modexp.ModexpXbsOobCall.NB_ROWS_OOB_MODEXP_XBS;
import static net.consensys.linea.zktracer.module.hub.fragment.scenario.PrecompileScenarioFragment.PrecompileFlag.*;
import static net.consensys.linea.zktracer.module.hub.fragment.scenario.PrecompileScenarioFragment.PrecompileScenario.*;
import static net.consensys.linea.zktracer.module.mod.ModOperation.NB_ROWS_MOD;

import java.util.Map;

Expand Down Expand Up @@ -228,6 +230,34 @@ public static short oobLineCountForPrc(PrecompileFlag prc) {
default -> throw new IllegalArgumentException("Precompile not supported:" + prc);
};
}

public static boolean validCallDataSize(
PrecompileScenarioFragment.PrecompileFlag prc, int cds) {
return switch (prc) {
case PRC_POINT_EVALUATION -> cds == PRECOMPILE_CALL_DATA_SIZE___POINT_EVALUATION;
case PRC_BLS_G1_ADD -> cds == PRECOMPILE_CALL_DATA_SIZE___G1_ADD;
case PRC_BLS_G1_MSM -> cds != 0 && cds % PRECOMPILE_CALL_DATA_UNIT_SIZE___BLS_G1_MSM == 0;
case PRC_BLS_G2_ADD -> cds == PRECOMPILE_CALL_DATA_SIZE___G2_ADD;
case PRC_BLS_G2_MSM -> cds != 0 && cds % PRECOMPILE_CALL_DATA_UNIT_SIZE___BLS_G2_MSM == 0;
case PRC_BLS_PAIRING_CHECK -> cds != 0
&& cds % PRECOMPILE_CALL_DATA_UNIT_SIZE___BLS_PAIRING_CHECK == 0;
case PRC_BLS_MAP_FP_TO_G1 -> cds == PRECOMPILE_CALL_DATA_SIZE___FP_TO_G1;
case PRC_BLS_MAP_FP2_TO_G2 -> cds == PRECOMPILE_CALL_DATA_SIZE___FP2_TO_G2;
default -> throw new IllegalArgumentException("not implemented for prc: " + prc);
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the cases could be ordered by increasing addresses.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


public static short modLinesComingFromOobCall(PrecompileScenarioFragment.PrecompileFlag prc) {
return switch (prc) {
case PRC_SHA2_256,
PRC_RIPEMD_160,
PRC_IDENTITY,
PRC_BLS_PAIRING_CHECK,
PRC_ECPAIRING -> NB_ROWS_MOD;
case PRC_MODEXP, PRC_BLS_G1_MSM, PRC_BLS_G2_MSM -> (short) (NB_ROWS_MOD * 2);
default -> 0;
};
}
}

final PrecompileSubsection precompileSubSection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,6 @@ private void smcProcessing(Hub hub, MessageFrame frame) {
}

private void prcProcessing(Hub hub) {
// TODO: remove me when Linea supports Cancun & Prague precompiles
if (isKzgPrecompileCall(calleeAddress, hub.fork)) {
hub.pointEval().detectEvent();
}
if (isBlsPrecompileCall(calleeAddress, hub.fork)) {
hub.bls().detectEvent();
}

precompileSubsection = ADDRESS_TO_PRECOMPILE.get(calleeFirst.address()).apply(hub, this);
hub.defers().scheduleForContextEntry(this);
hub.defers().scheduleForContextReEntry(this, hub.currentFrame());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
import org.apache.tuweni.bytes.Bytes;

public class BlakeSubsection extends PrecompileSubsection {
// 3 = SCEN + MISC + CON (squash caller return data): if size != 213
// 4 = SCEN + MISC + MISC + CON (squash caller return data): if insufficient gas / f not a bit
// 6 = SCEN + MISC + (3 * MISC) + CON (provide caller with return data): if success
// we don't optimize as BLAKE calls are rare
public static final short NB_ROWS_HUB_PRC_BLAKE = 6;
final Blake2fCallDataSizeOobCall blakeCdsOobCall;
ImcFragment secondImcFragment;
Blake2fParamsOobCall blake2fParamsOobCall;
Expand Down Expand Up @@ -123,10 +128,6 @@ public void resolveAtContextReEntry(Hub hub, CallFrame callFrame) {

@Override
protected short maxNumberOfLines() {
// 3 = SCEN + MISC + CON (squash caller return data): if size != 213
// 4 = SCEN + MISC + MISC + CON (squash caller return data): if insufficient gas / f not a bit
// 6 = SCEN + MISC + (3 * MISC) + CON (provide caller with return data): if success
// we don't optimize as BLAKE calls are rare
return 6;
return NB_ROWS_HUB_PRC_BLAKE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ void twoSuccessfullL2l1Logs(TestInfo testInfo) {
assertEquals(0, lineCountMap.get(ModuleName.PRECOMPILE_MODEXP_EFFECTIVE_CALLS.toString()));
assertEquals(0, lineCountMap.get(PRECOMPILE_RIPEMD_BLOCKS.toString()));
assertEquals(0, lineCountMap.get(PRECOMPILE_BLAKE_EFFECTIVE_CALLS.toString()));
assertEquals(0, lineCountMap.get(POINT_EVAL.toString()));
assertEquals(0, lineCountMap.get(BLS.toString()));

// L1 block size > 0
assertTrue(lineCountMap.get(BLOCK_L1_SIZE.toString()) > 0);
Expand Down Expand Up @@ -236,8 +234,6 @@ void unsuccessfullL2l1Logs(TestInfo testInfo) {
assertEquals(0, lineCountMap.get(ModuleName.PRECOMPILE_MODEXP_EFFECTIVE_CALLS.toString()));
assertEquals(0, lineCountMap.get(PRECOMPILE_RIPEMD_BLOCKS.toString()));
assertEquals(0, lineCountMap.get(PRECOMPILE_BLAKE_EFFECTIVE_CALLS.toString()));
assertEquals(0, lineCountMap.get(POINT_EVAL.toString()));
assertEquals(0, lineCountMap.get(BLS.toString()));

// L1 block size > 0
assertTrue(lineCountMap.get(BLOCK_L1_SIZE.toString()) > 0);
Expand Down
Loading
Loading