Skip to content

Commit

Permalink
feat(exp): update (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzogentile404 authored Aug 14, 2024
1 parent 5323ddd commit 973f18d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static LeadLogTrimLead fromArgs(EWord rawLead, int cdsCutoff, int ebsCuto
final BigInteger lead = trim.shiftRight(8 * (32 - ebsCutoff));

// lead_log (same as EYP)
final int leadLog = lead.signum() == 0 ? 0 : log2(lead, RoundingMode.FLOOR);
int leadLog = lead.signum() == 0 ? 0 : log2(lead, RoundingMode.FLOOR);

return new LeadLogTrimLead(leadLog, trim);
}
Expand Down Expand Up @@ -138,7 +138,8 @@ public void preCompute() {
pPreprocessingWcpArg2Hi[2] = Bytes.of(0);
pPreprocessingWcpArg2Lo[2] = Bytes.of(LLARGEPO);
pPreprocessingWcpInst[2] = UnsignedByte.of(EVM_INST_LT);
pPreprocessingWcpRes[2] = wcp.callLT(Bytes.of(this.ebsCutoff), Bytes.of(LLARGEPO));
final boolean ebsCutoffLeq16 = wcp.callLT(Bytes.of(this.ebsCutoff), Bytes.of(LLARGEPO));
pPreprocessingWcpRes[2] = ebsCutoffLeq16;

// Fourth row
pPreprocessingWcpFlag[3] = true;
Expand All @@ -147,37 +148,21 @@ public void preCompute() {
pPreprocessingWcpArg2Hi[3] = Bytes.of(0);
pPreprocessingWcpArg2Lo[3] = Bytes.of(0);
pPreprocessingWcpInst[3] = UnsignedByte.of(EVM_INST_ISZERO);
final boolean rawHiPartIsZero = wcp.callISZERO(this.rawLead.hi());
pPreprocessingWcpRes[3] = rawHiPartIsZero;
final boolean rawLeadHiIsZero = wcp.callISZERO(this.rawLead.hi());
pPreprocessingWcpRes[3] = rawLeadHiIsZero;

// Fifth row
final int paddedBase2Log =
8 * nBytesExcludingLeadingByte + nBitsOfLeadingByteExcludingLeadingBit;

pPreprocessingWcpFlag[4] = true;
pPreprocessingWcpArg1Hi[4] = Bytes.of(0);
pPreprocessingWcpArg1Lo[4] = Bytes.of(paddedBase2Log);
pPreprocessingWcpArg2Hi[4] = Bytes.of(0);
pPreprocessingWcpArg2Lo[4] = Bytes.of(0);
pPreprocessingWcpInst[4] = UnsignedByte.of(EVM_INST_ISZERO);
pPreprocessingWcpRes[4] = wcp.callISZERO(Bytes.of(paddedBase2Log));
// Fifth row is filled later since we need pComputationTrimAcc

// Linking constraints and fill rawAcc
// Linking constraints and fill rawAcc and pltJmp
if (minCutoffLeq16) {
pComputationRawAcc = leftPadTo(this.rawLead.hi(), LLARGE);
} else if (!rawHiPartIsZero) {
pComputationRawAcc = leftPadTo(this.rawLead.hi(), LLARGE);
} else {
pComputationRawAcc = leftPadTo(this.rawLead.lo(), LLARGE);
}

// Fill pltJmp
if (minCutoffLeq16) {
pComputationPltJmp = (short) minCutoff;
} else {
if (!rawHiPartIsZero) {
if (!rawLeadHiIsZero) {
pComputationRawAcc = leftPadTo(this.rawLead.hi(), LLARGE);
pComputationPltJmp = (short) 16;
} else {
pComputationRawAcc = leftPadTo(this.rawLead.lo(), LLARGE);
pComputationPltJmp = (short) (minCutoff - 16);
}
}
Expand All @@ -194,5 +179,15 @@ public void preCompute() {
pComputationMsb = UnsignedByte.of(trimByte);
}
}

// Fifth row
pPreprocessingWcpFlag[4] = true;
pPreprocessingWcpArg1Hi[4] = Bytes.of(0);
pPreprocessingWcpArg1Lo[4] = pComputationTrimAcc;
pPreprocessingWcpArg2Hi[4] = Bytes.of(0);
pPreprocessingWcpArg2Lo[4] = Bytes.of(0);
pPreprocessingWcpInst[4] = UnsignedByte.of(EVM_INST_ISZERO);
final boolean trimAccIsZero = wcp.callISZERO(pComputationTrimAcc);
pPreprocessingWcpRes[4] = trimAccIsZero;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.zktracer.opcode.OpCode;
Expand All @@ -28,12 +31,28 @@
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

@Slf4j
public class ExpTest {
// Generates 128, 64, 2, 1 as LD
private static final int[] LD_INDICES = new int[] {1, 2, 7, 8};
private static final int[] C = new int[] {1, 2, 10, 15, 16, 17, 20, 31, 32};
// Generates 128, 64, 2, 1 as LD (leading digit)
// LD_INDICES | LD
// ---------- | ---------------------
// 1 | 128 = 1000 0000 = 0x80
// 2 | 64 = 0100 0000 = 0x40
// 7 | 2 = 0000 0010 = 0x02
// 8 | 1 = 0000 0001 = 0x01
private static final int[] LD_INDICES = new int[] {1, 2, 7, 8}; // LEADING_DIGIT_INDICES

// Generates 1, 2, 10, 15, 16, 17, 20, 31, 32 as ebsCutoff and cdsCutoff
// and k (index of a specific sequence of bytes based on the test case)
private static final int[] C = new int[] {1, 2, 10, 15, 16, 17, 20, 31, 32}; // CUTOFFS

// See https://github.com/Consensys/linea-specification-internal/issues/326 as additional
// documentation

@Test
void testExpLogSingleCase() {
Expand Down Expand Up @@ -73,66 +92,69 @@ void testModexpLogSingleCase() {
bytecodeRunner.run();
}

@Test
void testExpLogFFBlockCase() {
for (int k = 0; k <= 32; k++) {
Bytes exponent = Bytes.fromHexString(ffBlock(k));
BytecodeCompiler program =
BytecodeCompiler.newProgram().push(exponent).push(10).op(OpCode.EXP);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}
@ParameterizedTest
@ValueSource(
ints = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32
})
void testExpLogFFBlockCase(int k) {
Bytes exponent = Bytes.fromHexString(ffBlock(k));
BytecodeCompiler program = BytecodeCompiler.newProgram().push(exponent).push(10).op(OpCode.EXP);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}

@Test
void testExpLogFFAtCase() {
for (int k = 1; k <= 32; k++) {
Bytes exponent = Bytes.fromHexString(ffAt(k));
BytecodeCompiler program =
BytecodeCompiler.newProgram().push(exponent).push(10).op(OpCode.EXP);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}
@ParameterizedTest
@ValueSource(
ints = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32
})
void testExpLogFFAtCase(int k) {
Bytes exponent = Bytes.fromHexString(ffAt(k));
BytecodeCompiler program = BytecodeCompiler.newProgram().push(exponent).push(10).op(OpCode.EXP);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}

@Disabled("EXP tests are disabled due to running for over 30 min.")
@Test
void testModexpLogFFBlockWithLDCase() {
for (int ebsCutoff : C) {
for (int cdsCutoff : C) {
for (int k : C) {
for (int LDIndex : LD_INDICES) {
log.debug("k: " + k);
log.debug("LDIndex: " + LDIndex);
Bytes wordAfterBase = Bytes.fromHexStringLenient(ffBlockWithLd(k, LDIndex));
BytecodeCompiler program =
initProgramInvokingModexp(ebsCutoff, cdsCutoff, wordAfterBase);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}
}
}
}
@Disabled("We may want to run these long tests only during nightly builds")
@ParameterizedTest
@MethodSource("testModexpLogSource")
void testModexpLogFFBlockWithLDCase(int ebsCutoff, int cdsCutoff, int k, int LDIndex) {
log.debug("k: " + k);
log.debug("LDIndex: " + LDIndex);
// 0x00000000000000000000000000000040ffffffffffffffffffffffffffffffff
Bytes wordAfterBase = Bytes.fromHexStringLenient(ffBlockWithLd(k, LDIndex));
BytecodeCompiler program = initProgramInvokingModexp(ebsCutoff, cdsCutoff, wordAfterBase);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}

@Disabled("EXP tests are disabled due to running for over 30 min.")
@Test
void testModexpLogLDAtCase() {
@Disabled("We may want to run these long tests only during nightly builds")
@ParameterizedTest
@MethodSource("testModexpLogSource")
void testModexpLogLDAtCase(int ebsCutoff, int cdsCutoff, int k, int ldIndex) {
log.debug("k: " + k);
log.debug("ldIndex: " + ldIndex);
Bytes wordAfterBase = Bytes.fromHexStringLenient(ldAt(k, ldIndex));
BytecodeCompiler program = initProgramInvokingModexp(ebsCutoff, cdsCutoff, wordAfterBase);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}

private static Stream<Arguments> testModexpLogSource() {
List<Arguments> moxexpLogCases = new ArrayList<>();
for (int ebsCutoff : C) {
for (int cdsCutoff : C) {
for (int k : C) {
for (int ldIndex : LD_INDICES) {
log.debug("k: " + k);
log.debug("ldIndex: " + ldIndex);
Bytes wordAfterBase = Bytes.fromHexStringLenient(ldAt(k, ldIndex));
BytecodeCompiler program =
initProgramInvokingModexp(ebsCutoff, cdsCutoff, wordAfterBase);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
for (int LDIndex : LD_INDICES) {
moxexpLogCases.add(Arguments.of(ebsCutoff, cdsCutoff, k, LDIndex));
}
}
}
}
return moxexpLogCases.stream();
}

@Test
Expand Down Expand Up @@ -161,19 +183,25 @@ void testModexpLogLDAtCaseSpecific() {
bytecodeRunner.run();
}

@Disabled("EXP tests are disabled due to running for over 30 min.")
@Test
void testModexpLogZerosCase() {
@ParameterizedTest
@MethodSource("testModexpLogZerosCaseSource")
void testModexpLogZerosCase(int ebsCutoff, int cdsCutoff) {
Bytes wordAfterBase =
Bytes.fromHexStringLenient(
"0000000000000000000000000000000000000000000000000000000000000000");
BytecodeCompiler program = initProgramInvokingModexp(ebsCutoff, cdsCutoff, wordAfterBase);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
}

private static Stream<Arguments> testModexpLogZerosCaseSource() {
List<Arguments> moxexpLogCases = new ArrayList<>();
for (int ebsCutoff : C) {
for (int cdsCutoff : C) {
Bytes wordAfterBase =
Bytes.fromHexStringLenient(
"0000000000000000000000000000000000000000000000000000000000000000");
BytecodeCompiler program = initProgramInvokingModexp(ebsCutoff, cdsCutoff, wordAfterBase);
BytecodeRunner bytecodeRunner = BytecodeRunner.of(program.compile());
bytecodeRunner.run();
moxexpLogCases.add(Arguments.of(ebsCutoff, cdsCutoff));
}
}
return moxexpLogCases.stream();
}

private BytecodeCompiler initProgramInvokingModexp(
Expand Down Expand Up @@ -205,7 +233,6 @@ private BytecodeCompiler initProgramInvokingModexp(
.op(OpCode.STATICCALL);
}

@Disabled("EXP tests are disabled due to running for over 30 min.")
@Test
void testExpUtils() {
// ExpLog case
Expand Down
2 changes: 1 addition & 1 deletion zkevm-constraints

0 comments on commit 973f18d

Please sign in to comment.