Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overflow for modexp arg #489

Merged
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 @@ -20,7 +20,6 @@

import java.math.BigInteger;

import net.consensys.linea.zktracer.types.EWord;
import net.consensys.linea.zktracer.types.UnsignedByte;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
Expand Down Expand Up @@ -183,10 +182,10 @@ public static Bytes slice(Bytes data, int positionStart, int size) {

if (dataSize >= positionStart) {
if (dataSize >= (positionStart + size)) {
output = EWord.of(data.slice(positionStart, size));
output = data.slice(positionStart, size);
} else {
final int nbPresentBytes = dataSize - positionStart;
output = EWord.of(rightPadTo(data.slice(positionStart, nbPresentBytes), size));
output = rightPadTo(data.slice(positionStart, nbPresentBytes), size);
}
}
return output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class Modexp implements Module {
private final Hub hub;
private final Stack<Integer> counts = new Stack<>();
private static final int PROVER_MAX_INPUT_BIT_SIZE = 4096;
private static final BigInteger PROVER_MAX_INPUT_BIT_SIZE = BigInteger.valueOf(4096);
private static final int EVM_WORD_SIZE = 32;

@Override
Expand Down Expand Up @@ -78,8 +78,9 @@ public void tracePreOpcode(MessageFrame frame) {
}
final Bytes inputData = frame.shadowReadMemory(offset, length);

final int baseLength = slice(inputData, 0, EVM_WORD_SIZE).toInt();
if (baseLength * 8 > PROVER_MAX_INPUT_BIT_SIZE) {
// Get the Base length
final BigInteger baseLength = slice(inputData, 0, EVM_WORD_SIZE).toUnsignedBigInteger();
if (baseLength.multiply(BigInteger.valueOf(8)).compareTo(PROVER_MAX_INPUT_BIT_SIZE) > 0) {
log.info(
"Too big argument, base bit length = {} > {}",
baseLength,
Expand All @@ -88,16 +89,23 @@ public void tracePreOpcode(MessageFrame frame) {
this.counts.push(Integer.MAX_VALUE);
return;
}
final int expLength = slice(inputData, EVM_WORD_SIZE, EVM_WORD_SIZE).toInt();
if (expLength * 8 > PROVER_MAX_INPUT_BIT_SIZE) {

// Get the Exponent length
final BigInteger expLength =
slice(inputData, EVM_WORD_SIZE, EVM_WORD_SIZE).toUnsignedBigInteger();
if (expLength.multiply(BigInteger.valueOf(8)).compareTo(PROVER_MAX_INPUT_BIT_SIZE) > 0) {
log.info(
"Too big argument, exp bit length = {} > {}", expLength, PROVER_MAX_INPUT_BIT_SIZE);
this.counts.pop();
this.counts.push(Integer.MAX_VALUE);
return;
}
final int moduloLength = slice(inputData, 2 * EVM_WORD_SIZE, EVM_WORD_SIZE).toInt();
if (expLength * 8 > PROVER_MAX_INPUT_BIT_SIZE) {

// Get the Modulo length
final BigInteger moduloLength =
slice(inputData, 2 * EVM_WORD_SIZE, EVM_WORD_SIZE).toUnsignedBigInteger();
if (moduloLength.multiply(BigInteger.valueOf(8)).compareTo(PROVER_MAX_INPUT_BIT_SIZE)
> 0) {
log.info(
"Too big argument, modulo bit length = {} > {}",
moduloLength,
Expand All @@ -106,11 +114,23 @@ public void tracePreOpcode(MessageFrame frame) {
this.counts.push(Integer.MAX_VALUE);
return;
}
final Bytes exp = slice(inputData, 3 * EVM_WORD_SIZE + baseLength, expLength);

// Get the Exponent
final Bytes exp =
slice(
inputData,
3 * EVM_WORD_SIZE + baseLength.intValueExact(),
expLength.intValueExact());

final long gasPaid = Words.clampedToLong(frame.getStackItem(0));

if (gasPaid >= gasPrice(baseLength, expLength, moduloLength, exp)) {
// If enough gas, add 1 to the call of the precompile
if (gasPaid
>= gasPrice(
baseLength.intValueExact(),
expLength.intValueExact(),
moduloLength.intValueExact(),
exp)) {
this.counts.push(this.counts.pop() + 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion zkevm-constraints
Loading