Skip to content

Commit

Permalink
fix(modexp): add debuged failing ref test (#1500)
Browse files Browse the repository at this point in the history
Signed-off-by: F Bojarski <ceciestunepoubelle@protonmail.ch>
  • Loading branch information
letypequividelespoubelles authored Nov 15, 2024
1 parent 352b8b3 commit ef2e663
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public EWord rawLeadingWord() {
}

public boolean extractModulus() {
return (callData.size() > MBS_MIN_OFFSET + bbsInt() + ebsInt()) && !mbs().isZero();
return (callData.size() > BASE_MIN_OFFSET + bbsInt() + ebsInt()) && !mbs().isZero();
}

public boolean extractBase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.MMIO_INST_RAM_TO_LIMB_ONE_SOURCE;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.MMIO_INST_RAM_TO_LIMB_TRANSPLANT;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.MMIO_INST_RAM_TO_LIMB_TWO_SOURCE;
import static net.consensys.linea.zktracer.module.mmu.Trace.NB_MICRO_ROWS_TOT_MODEXP_DATA;
import static net.consensys.linea.zktracer.types.Conversions.longToBytes;

import java.util.ArrayList;
Expand Down Expand Up @@ -138,10 +139,10 @@ private void row2() {
wcpCallRecords.add(
MmuWcpCallRecord.instLtBuilder().arg1Lo(wcpArg1).arg2Lo(wcpArg2).result(wcpResult).build());

EucOperation eucOp = euc.callEUC(longToBytes(numberRightPaddingBytes), Bytes.of(LLARGE));
final EucOperation eucOp = euc.callEUC(longToBytes(numberRightPaddingBytes), Bytes.of(LLARGE));
initialTotalRightZeroes = eucOp.quotient().toInt();
initialTotalNonTrivial =
Trace.NB_MICRO_ROWS_TOT_MODEXP_DATA - initialTotalLeftZeroes - initialTotalRightZeroes;
NB_MICRO_ROWS_TOT_MODEXP_DATA - initialTotalLeftZeroes - initialTotalRightZeroes;
rightPaddingRemainder = eucOp.remainder().toInt();
eucCallRecords.add(
MmuEucCallRecord.builder()
Expand All @@ -156,9 +157,13 @@ private void row3() {
// row n°3
final Bytes wcpArg1 = longToBytes(initialTotalNonTrivial);
final Bytes wcpArg2 = Bytes.of(1);
final boolean wcpResult = wcp.callEQ(wcpArg1, wcpArg2);
final boolean singleNonTrivialMmioOperation = wcp.callEQ(wcpArg1, wcpArg2);
wcpCallRecords.add(
MmuWcpCallRecord.instEqBuilder().arg1Lo(wcpArg1).arg2Lo(wcpArg2).result(wcpResult).build());
MmuWcpCallRecord.instEqBuilder()
.arg1Lo(wcpArg1)
.arg2Lo(wcpArg2)
.result(singleNonTrivialMmioOperation)
.build());

final long dividend = parameterOffset;
final EucOperation eucOp = euc.callEUC(longToBytes(dividend), Bytes.of(LLARGE));
Expand All @@ -171,7 +176,7 @@ private void row3() {
.quotient(eucOp.quotient().toLong())
.remainder((short) eucOp.remainder().toInt())
.build());
if (wcpResult) {
if (singleNonTrivialMmioOperation) {
firstLimbByteSize = (short) (dataRunsOut ? leftoverDataSize : parameterByteSize);
} else {
firstLimbByteSize = (short) (LLARGE - initialTargetByteOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.GAS_CONST_G_CALL_STIPEND;
import static net.consensys.linea.zktracer.module.constants.GlobalConstants.MAX_CODE_SIZE;
import static net.consensys.linea.zktracer.module.hub.fragment.imc.oob.OobInstruction.*;
import static net.consensys.linea.zktracer.module.hub.precompiles.ModexpMetadata.BASE_MIN_OFFSET;
import static net.consensys.linea.zktracer.module.oob.Trace.CT_MAX_BLAKE2F_CDS;
import static net.consensys.linea.zktracer.module.oob.Trace.CT_MAX_BLAKE2F_PARAMS;
import static net.consensys.linea.zktracer.module.oob.Trace.CT_MAX_CALL;
Expand Down Expand Up @@ -1138,7 +1139,7 @@ private void setModexpExtract(ModexpExtractOobCall prcModexpExtractOobCall) {
callToLT(
3,
BigInteger.ZERO,
BigInteger.valueOf(96)
BigInteger.valueOf(BASE_MIN_OFFSET)
.add(prcModexpExtractOobCall.getBbs().add(prcModexpExtractOobCall.getEbs())),
BigInteger.ZERO,
prcModexpExtractOobCall.getCds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.consensys.linea.testing.BytecodeRunner;
import net.consensys.linea.zktracer.opcode.OpCode;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.datatypes.Address;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -236,4 +237,39 @@ void appendAllZeroCallDataModexpCalls(BytecodeCompiler program, int callDataSize
.op(OpCode.STATICCALL)
.op(OpCode.POP);
}

@Test
// We trigger a ModexpData MMU Call where the sourceOffset = referenceSize for the MmuCall
void referenceSizeEqualsSourceOffset() {
final Bytes bytecode =
BytecodeCompiler.newProgram()
// bbs = 2
.push(Bytes32.leftPad(Bytes.of(2)))
.push(0) // offset
.op(OpCode.MSTORE)
// ebs = 3
.push(Bytes32.leftPad(Bytes.of(3)))
.push(32) // offset
.op(OpCode.MSTORE)
// mbs = 4
.push(Bytes32.leftPad(Bytes.of(4)))
.push(64) // offset
.op(OpCode.MSTORE)
// MSTORE ebm
.push(Bytes32.rightPad(Bytes.fromHexString("0xba7e000ec70000080d")))
.push(96)
.op(OpCode.MSTORE)
// Call Modexp
.push(0) // returnSize
.push(0) // returnOffset
.push(98) // cds = 96 + bbs => trigger a MMU Call where the sourceOffset = referenceSize
.push(0) // cdo
.push(0) // value
.push(Address.MODEXP) // address
.push(0xffff) // gas
.op(OpCode.CALL)
.op(OpCode.POP)
.compile();
BytecodeRunner.of(bytecode).run();
}
}

0 comments on commit ef2e663

Please sign in to comment.