Skip to content

Commit

Permalink
fix wcp line count for operations coming from block data (#1729)
Browse files Browse the repository at this point in the history
* fix wcp line count for operations coming from block data

Signed-off-by: F Bojarski <[email protected]>

* fix: count additional rows for wcp calls from blockhash + popping addtional rows for EUC

Signed-off-by: F Bojarski <[email protected]>

* ras

Signed-off-by: F Bojarski <[email protected]>

* update to last master constraints

Signed-off-by: F Bojarski <[email protected]>

---------

Signed-off-by: F Bojarski <[email protected]>
  • Loading branch information
letypequividelespoubelles authored Jan 28, 2025
1 parent 4cab8f7 commit 0f5bbb5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

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

import static net.consensys.linea.zktracer.module.blockdata.Trace.GAS_LIMIT_MAXIMUM;
import static net.consensys.linea.zktracer.module.blockdata.Trace.nROWS_DEPTH;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.LLARGE;

import java.math.BigInteger;
import java.nio.MappedByteBuffer;
Expand All @@ -30,7 +32,6 @@
import net.consensys.linea.zktracer.opcode.OpCode;
import net.consensys.linea.zktracer.types.EWord;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.evm.worldstate.WorldView;
import org.hyperledger.besu.plugin.data.BlockBody;
import org.hyperledger.besu.plugin.data.BlockHeader;

Expand All @@ -41,12 +42,8 @@ public class Blockdata implements Module {
private final TxnData txnData;

private final Deque<BlockdataOperation> operations = new ArrayDeque<>();
private static final int TIMESTAMP_BYTESIZE = 4;
private BlockHeader prevBlockHeader;
private int traceCounter = 0;
private long firstBlockNumber;
private Bytes chainId;
private boolean shouldBeTraced = true;

final OpCode[] opCodes = {
OpCode.COINBASE,
Expand All @@ -69,42 +66,47 @@ public String moduleKey() {

@Override
public void traceStartConflation(final long blockCount) {
wcp.additionalRows.add(TIMESTAMP_BYTESIZE); // TODO: check
wcp.additionalRows.add(
LLARGE // for COINBASE
+ 6
+ 6 // for TIMESTAMP
+ 1
+ 6 // for NUMBER
+ 1 // for DIFFICULTY
+ Bytes.minimalBytes(GAS_LIMIT_MAXIMUM).size() * 4 // for GASLIMIT
+ LLARGE // for CHAINID
+ LLARGE // for BASEFEE
);

euc.additionalRows.add(8);
}

@Override
public void traceEndBlock(final BlockHeader blockHeader, final BlockBody blockBody) {
final long blockNumber = blockHeader.getNumber();
firstBlockNumber = (traceCounter < opCodes.length) ? blockNumber : firstBlockNumber;
if (shouldBeTraced) {
for (OpCode opCode : opCodes) {
BlockdataOperation operation =
new BlockdataOperation(
txnData.hub(),
blockHeader,
prevBlockHeader,
txnData.currentBlock().getNbOfTxsInBlock(),
wcp,
euc,
chainId,
opCode,
firstBlockNumber);
operations.addLast(operation);
// Increase counter to track where we are in the conflation
traceCounter++;
}
if (operations.isEmpty()) {
firstBlockNumber = blockNumber;
}
final BlockHeader previousBlockHeader =
operations.isEmpty() ? null : operations.getLast().blockHeader();
for (OpCode opCode : opCodes) {
final BlockdataOperation operation =
new BlockdataOperation(
txnData.hub(),
blockHeader,
previousBlockHeader,
txnData.currentBlock().getNbOfTxsInBlock(),
wcp,
euc,
chainId,
opCode,
firstBlockNumber);
operations.addLast(operation);
}
prevBlockHeader = blockHeader;
shouldBeTraced = false;
}

@Override
public void traceEndConflation(final WorldView state) {}

@Override
public void enterTransaction() {
shouldBeTraced = true;
}
public void enterTransaction() {}

@Override
public void popTransaction() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void handleCoinbase() {

private void handleTimestamp() {
data = EWord.of(blockHeader.getTimestamp());
EWord prevData =
final EWord prevData =
prevBlockHeader == null ? EWord.ZERO : EWord.of(prevBlockHeader.getTimestamp());

// row i
Expand Down Expand Up @@ -182,11 +182,11 @@ private void handleGasLimit() {
wcpCallToLEQ(1, data, EWord.of(Bytes.ofUnsignedLong(GAS_LIMIT_MAXIMUM)));

if (!firstBlockInConflation) {
EWord prevGasLimit = EWord.of(prevBlockHeader.getGasLimit());
final EWord prevGasLimit = EWord.of(prevBlockHeader.getGasLimit());
// row i + 2
Bytes maxDeviation = eucCall(2, prevGasLimit, EWord.of(GAS_LIMIT_ADJUSTMENT_FACTOR));
final Bytes maxDeviation = eucCall(2, prevGasLimit, EWord.of(GAS_LIMIT_ADJUSTMENT_FACTOR));
// row i + 3
BigInteger safeGasLimitUpperBound =
final BigInteger safeGasLimitUpperBound =
prevGasLimit.getAsBigInteger().add(maxDeviation.toUnsignedBigInteger());
wcpCallToLT(3, data, EWord.of(safeGasLimitUpperBound));

Expand Down Expand Up @@ -303,9 +303,6 @@ private boolean wcpCallToISZERO(int w, EWord arg1) {
}

private Bytes eucCall(int w, EWord arg1, EWord arg2) {
checkArgument(arg1.bitLength() / 8 <= 16);
checkArgument(arg2.bitLength() / 8 <= 16);

this.arg1[w] = arg1;
this.arg2[w] = arg2;

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

import static com.google.common.base.Preconditions.checkArgument;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.LLARGE;

import java.nio.MappedByteBuffer;
import java.util.HashMap;
Expand Down Expand Up @@ -89,12 +90,14 @@ public void resolvePostExecution(
Hub hub, MessageFrame frame, Operation.OperationResult operationResult) {

final OpCode opCode = OpCode.of(frame.getCurrentOperation().getOpcode());
if (opCode == OpCode.BLOCKHASH) {
final Bytes32 blockhashRes = Bytes32.leftPad(frame.getStackItem(0));
operations.add(new BlockhashOperation(relBlock, absBlock, blockhashArg, blockhashRes, wcp));
if (blockhashRes != Bytes32.ZERO) {
blockHashMap.put(blockhashArg, blockhashRes);
}
checkArgument(opCode == OpCode.BLOCKHASH);
final Bytes32 blockhashRes = Bytes32.leftPad(frame.getStackItem(0));
operations.add(new BlockhashOperation(relBlock, absBlock, blockhashArg, blockhashRes, wcp));
// We have 4 LLARGE and one OLI call to WCP, made at the end of the conflation, so we need to
// add line count to WCP
wcp.additionalRows.add(4 * LLARGE + 1);
if (blockhashRes != Bytes32.ZERO) {
blockHashMap.put(blockhashArg, blockhashRes);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

import static net.consensys.linea.zktracer.module.blockhash.Trace.BLOCKHASH_DEPTH;
import static net.consensys.linea.zktracer.module.blockhash.Trace.nROWS_PRPRC;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.EVM_INST_EQ;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.EVM_INST_LT;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.LLARGE;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.WCP_INST_LEQ;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.*;

import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -60,36 +57,30 @@ public BlockhashOperation(
}

void handlePreprocessing(Bytes32 prevBlockhashArg) {
final Bytes prevBHArgHi = prevBlockhashArg.slice(0, LLARGE);
final Bytes prevBHArgLo = prevBlockhashArg.slice(LLARGE, LLARGE);
final Bytes currBHArgHi = blockhashArg.slice(0, LLARGE);
final Bytes curBHArgLo = blockhashArg.slice(LLARGE, LLARGE);

// NOTE: w goes from 0 to 4 because it refers to the array
// however, rows go from i+1 to i+5 because it refers the MACRO row (index i)
// row i + 1
wcpCallToLEQ(0, prevBHArgHi, prevBHArgLo, currBHArgHi, curBHArgLo);
wcpCallToLEQ(0, prevBlockhashArg, blockhashArg);

// row i + 2
boolean sameBHArg = wcpCallToEQ(1, prevBHArgHi, prevBHArgLo, currBHArgHi, curBHArgLo);
wcpCallToEQ(1, prevBlockhashArg, blockhashArg);

// row i + 3
boolean res3 =
final boolean blockNumberGreaterThan256 =
wcpCallToLEQ(
2, Bytes.of(0), Bytes.ofUnsignedInt(256), Bytes.of(0), Bytes.ofUnsignedLong(absBlock));
long minimalReachable = 0;
if (res3) {
minimalReachable = absBlock - 256;
}
2,
Bytes32.leftPad(Bytes.minimalBytes(BLOCKHASH_MAX_HISTORY)),
Bytes32.leftPad(Bytes.ofUnsignedLong(absBlock)));
final long minimalReachable = blockNumberGreaterThan256 ? absBlock - BLOCKHASH_MAX_HISTORY : 0;

// row i + 4
boolean upperBoundOk =
wcpCallToLT(3, currBHArgHi, curBHArgLo, Bytes.of(0), Bytes.ofUnsignedLong(absBlock));
final boolean upperBoundOk =
wcpCallToLT(3, blockhashArg, Bytes32.leftPad(Bytes.ofUnsignedLong(absBlock)));

// row i + 5
boolean lowerBoundOk =
wcpCallToLEQ(
4, Bytes.of(0), Bytes.ofUnsignedLong(minimalReachable), currBHArgHi, curBHArgLo);
final boolean lowerBoundOk =
wcpCallToLEQ(4, Bytes32.leftPad(Bytes.ofUnsignedLong(minimalReachable)), blockhashArg);
}

@Override
Expand Down Expand Up @@ -132,42 +123,33 @@ public void tracePreprocessing(Trace trace) {
}

// WCP calls
private boolean wcpCallToLT(
int w, Bytes exoArg1Hi, Bytes exoArg1Lo, Bytes exoArg2Hi, Bytes exoArg2Lo) {
private boolean wcpCallToLT(int w, Bytes32 exoArg1, Bytes32 exoArg2) {
this.exoInst[w] = EVM_INST_LT;
this.exoArg1Hi[w] = exoArg1Hi;
this.exoArg1Lo[w] = exoArg1Lo;
this.exoArg2Hi[w] = exoArg2Hi;
this.exoArg2Lo[w] = exoArg2Lo;
this.exoRes[w] =
wcp.callLT(
Bytes.concatenate(exoArg1Hi, exoArg1Lo), Bytes.concatenate(exoArg2Hi, exoArg2Lo));
this.exoArg1Hi[w] = exoArg1.slice(0, LLARGE);
this.exoArg1Lo[w] = exoArg1.slice(LLARGE, LLARGE);
this.exoArg2Hi[w] = exoArg2.slice(0, LLARGE);
this.exoArg2Lo[w] = exoArg2.slice(LLARGE, LLARGE);
this.exoRes[w] = wcp.callLT(exoArg1, exoArg2);
return this.exoRes[w];
}

private boolean wcpCallToLEQ(
int w, Bytes exoArg1Hi, Bytes exoArg1Lo, Bytes exoArg2Hi, Bytes exoArg2Lo) {
private boolean wcpCallToLEQ(int w, Bytes32 exoArg1, Bytes32 exoArg2) {
this.exoInst[w] = WCP_INST_LEQ;
this.exoArg1Hi[w] = exoArg1Hi;
this.exoArg1Lo[w] = exoArg1Lo;
this.exoArg2Hi[w] = exoArg2Hi;
this.exoArg2Lo[w] = exoArg2Lo;
this.exoRes[w] =
wcp.callLEQ(
Bytes.concatenate(exoArg1Hi, exoArg1Lo), Bytes.concatenate(exoArg2Hi, exoArg2Lo));
this.exoArg1Hi[w] = exoArg1.slice(0, LLARGE);
this.exoArg1Lo[w] = exoArg1.slice(LLARGE, LLARGE);
this.exoArg2Hi[w] = exoArg2.slice(0, LLARGE);
this.exoArg2Lo[w] = exoArg2.slice(LLARGE, LLARGE);
this.exoRes[w] = wcp.callLEQ(exoArg1, exoArg2);
return this.exoRes[w];
}

private boolean wcpCallToEQ(
int w, Bytes exoArg1Hi, Bytes exoArg1Lo, Bytes exoArg2Hi, Bytes exoArg2Lo) {
private boolean wcpCallToEQ(int w, Bytes32 exoArg1, Bytes32 exoArg2) {
this.exoInst[w] = EVM_INST_EQ;
this.exoArg1Hi[w] = exoArg1Hi;
this.exoArg1Lo[w] = exoArg1Lo;
this.exoArg2Hi[w] = exoArg2Hi;
this.exoArg2Lo[w] = exoArg2Lo;
this.exoRes[w] =
wcp.callEQ(
Bytes.concatenate(exoArg1Hi, exoArg1Lo), Bytes.concatenate(exoArg2Hi, exoArg2Lo));
this.exoArg1Hi[w] = exoArg1.slice(0, LLARGE);
this.exoArg1Lo[w] = exoArg1.slice(LLARGE, LLARGE);
this.exoArg2Hi[w] = exoArg2.slice(0, LLARGE);
this.exoArg2Lo[w] = exoArg2.slice(LLARGE, LLARGE);
this.exoRes[w] = wcp.callEQ(exoArg1, exoArg2);
return this.exoRes[w];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.experimental.Accessors;
import net.consensys.linea.zktracer.ColumnHeader;
import net.consensys.linea.zktracer.container.module.OperationSetModule;
import net.consensys.linea.zktracer.container.stacked.CountOnlyOperation;
import net.consensys.linea.zktracer.container.stacked.ModuleOperationStackedSet;
import net.consensys.linea.zktracer.module.wcp.Wcp;
import org.apache.tuweni.bytes.Bytes;
Expand All @@ -39,11 +40,26 @@ public class Euc implements OperationSetModule<EucOperation> {
private final ModuleOperationStackedSet<EucOperation> operations =
new ModuleOperationStackedSet<>();

/** count the number of rows that could be added after the sequencer counts the number of line */
public final CountOnlyOperation additionalRows = new CountOnlyOperation();

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

@Override
public void enterTransaction() {
OperationSetModule.super.enterTransaction();
additionalRows.lineCount();
}

@Override
public void popTransaction() {
OperationSetModule.super.popTransaction();
additionalRows.pop();
}

@Override
public List<ColumnHeader> columnsHeaders() {
return Trace.headers(this.lineCount());
Expand All @@ -57,6 +73,13 @@ public void commit(List<MappedByteBuffer> buffers) {
}
}

@Override
public int lineCount() {
return operations.conflationFinished()
? operations.lineCount()
: operations().lineCount() + additionalRows.lineCount();
}

public EucOperation callEUC(final Bytes dividend, final Bytes divisor) {
final BigInteger dividendBI = dividend.toUnsignedBigInteger();
final BigInteger divisorBI = divisor.toUnsignedBigInteger();
Expand Down

0 comments on commit 0f5bbb5

Please sign in to comment.