Skip to content

Commit c29f4e4

Browse files
EIP-3855-PUSH0 (#1965)
Signed-off-by: F Bojarski <[email protected]>
1 parent 3d3ff6d commit c29f4e4

File tree

19 files changed

+1649
-327
lines changed

19 files changed

+1649
-327
lines changed

arithmetization/src/main/java/net/consensys/linea/zktracer/Fork.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ public enum Fork {
2020
SHANGHAI,
2121
CANCUN,
2222
PRAGUE;
23+
24+
public static String toString(Fork fork) {
25+
return switch (fork) {
26+
case LONDON -> "london";
27+
case SHANGHAI -> "shanghai";
28+
case CANCUN -> "cancun";
29+
case PRAGUE -> "prague";
30+
};
31+
}
2332
}

arithmetization/src/main/java/net/consensys/linea/zktracer/ZkTracer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package net.consensys.linea.zktracer;
1616

1717
import static net.consensys.linea.zktracer.ChainConfig.FORK_LINEA_CHAIN;
18+
import static net.consensys.linea.zktracer.opcode.OpCodes.loadOpcodes;
1819

1920
import java.io.IOException;
2021
import java.io.RandomAccessFile;
@@ -82,6 +83,7 @@ public ZkTracer(
8283
* @param chain
8384
*/
8485
public ZkTracer(ChainConfig chain) {
86+
loadOpcodes(chain.fork);
8587
this.chain = chain;
8688
this.hub =
8789
switch (chain.fork) {

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/fragment/StackFragment.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.common.base.Preconditions.*;
1919
import static com.google.common.primitives.Ints.min;
20+
import static net.consensys.linea.zktracer.Trace.EVM_INST_PUSH0;
2021
import static net.consensys.linea.zktracer.module.hub.signals.TracedException.*;
2122
import static net.consensys.linea.zktracer.opcode.InstructionFamily.*;
2223
import static net.consensys.linea.zktracer.types.Utils.rightPadTo;
@@ -137,21 +138,21 @@ private StackFragment(
137138
}
138139

139140
this.commonFragmentValues = commonFragmentValues;
140-
this.pushValue = opCode.isPush() ? EWord.of(getPushValue(hub)) : EWord.ZERO;
141+
this.pushValue = opCode.isNonTrivialPush() ? EWord.of(getPushValue(hub)) : EWord.ZERO;
141142
}
142143

143144
private Bytes getPushValue(Hub hub) {
144-
checkState(hub.opCode().isPush());
145+
checkState(hub.opCode().isNonTrivialPush());
145146

146147
final int pc = hub.messageFrame().getPC();
147148
if (pc + 1 >= hub.messageFrame().getCode().getSize()) {
148149
return Bytes.EMPTY;
149150
}
150151

151-
Bytes byteCode = hub.messageFrame().getCode().getBytes();
152-
int nBytesToPush = (opCode.byteValue() & 0xff) - (OpCode.PUSH1.byteValue() & 0xff) + 1;
153-
int nLeftoverBytes = byteCode.size() - (pc + 1);
154-
Bytes partialPushValue = byteCode.slice(pc + 1, min(nLeftoverBytes, nBytesToPush));
152+
final Bytes byteCode = hub.messageFrame().getCode().getBytes();
153+
final int nBytesToPush = (opCode.byteValue() & 0xff) - EVM_INST_PUSH0;
154+
final int nLeftoverBytes = byteCode.size() - (pc + 1);
155+
final Bytes partialPushValue = byteCode.slice(pc + 1, min(nLeftoverBytes, nBytesToPush));
155156
return (nLeftoverBytes >= nBytesToPush)
156157
? partialPushValue
157158
: rightPadTo(partialPushValue, nBytesToPush);
@@ -216,7 +217,8 @@ public Trace.Hub trace(Trace.Hub trace) {
216217
while (it.hasNext()) {
217218
var i = it.nextIndex();
218219
var op = it.next();
219-
final EWord eValue = (i == 3 && opCode().isPush()) ? pushValue : EWord.of(op.value());
220+
final EWord eValue =
221+
(i == 3 && opCode().isNonTrivialPush()) ? pushValue : EWord.of(op.value());
220222

221223
heightTracers.get(i).apply(op.height());
222224
valHiTracers.get(i).apply(eValue.hi());

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/fragment/common/CommonFragmentValues.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package net.consensys.linea.zktracer.module.hub.fragment.common;
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
19+
import static net.consensys.linea.zktracer.Trace.EVM_INST_PUSH0;
1920
import static net.consensys.linea.zktracer.module.hub.HubProcessingPhase.TX_EXEC;
2021
import static net.consensys.linea.zktracer.module.hub.signals.Exceptions.*;
2122
import static net.consensys.linea.zktracer.module.hub.signals.TracedException.*;
@@ -172,10 +173,10 @@ static int computePcNew(final Hub hub, final int pc, boolean stackException, boo
172173
return 0;
173174
}
174175

175-
if (!opCode.isPush() && !opCode.isJump()) return pc + 1;
176+
if (!opCode.isNonTrivialPush() && !opCode.isJump()) return pc + 1;
176177

177-
if (opCode.getData().isPush()) {
178-
return pc + 1 + (opCode.byteValue() - OpCode.PUSH1.byteValue() + 1);
178+
if (opCode.isNonTrivialPush()) {
179+
return pc + 1 + (opCode.byteValue() - EVM_INST_PUSH0);
179180
}
180181

181182
if (opCode.isJump()) {
@@ -191,7 +192,8 @@ static int computePcNew(final Hub hub, final int pc, boolean stackException, boo
191192
}
192193

193194
if (opCode.equals(OpCode.JUMPI)) {
194-
BigInteger condition = hub.currentFrame().frame().getStackItem(1).toUnsignedBigInteger();
195+
final BigInteger condition =
196+
hub.currentFrame().frame().getStackItem(1).toUnsignedBigInteger();
195197
if (!condition.equals(BigInteger.ZERO)) {
196198
return attemptedPcNew;
197199
} else {

arithmetization/src/main/java/net/consensys/linea/zktracer/module/romlex/RomOperation.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@
1515

1616
package net.consensys.linea.zktracer.module.romlex;
1717

18-
import static net.consensys.linea.zktracer.Trace.EVM_INST_INVALID;
19-
import static net.consensys.linea.zktracer.Trace.EVM_INST_JUMPDEST;
20-
import static net.consensys.linea.zktracer.Trace.EVM_INST_PUSH1;
21-
import static net.consensys.linea.zktracer.Trace.EVM_INST_PUSH32;
22-
import static net.consensys.linea.zktracer.Trace.LLARGE;
23-
import static net.consensys.linea.zktracer.Trace.LLARGEMO;
24-
import static net.consensys.linea.zktracer.Trace.WORD_SIZE;
25-
import static net.consensys.linea.zktracer.Trace.WORD_SIZE_MO;
18+
import static net.consensys.linea.zktracer.Trace.*;
2619
import static net.consensys.linea.zktracer.types.Utils.rightPadTo;
2720

2821
import lombok.EqualsAndHashCode;
@@ -31,6 +24,7 @@
3124
import lombok.experimental.Accessors;
3225
import net.consensys.linea.zktracer.Trace;
3326
import net.consensys.linea.zktracer.container.ModuleOperation;
27+
import net.consensys.linea.zktracer.opcode.OpCode;
3428
import net.consensys.linea.zktracer.types.UnsignedByte;
3529
import org.apache.tuweni.bytes.Bytes;
3630

@@ -63,8 +57,8 @@ public void trace(Trace.Rom trace, int cfi, int cfiInfty) {
6357
Bytes pushValueLow = Bytes.minimalBytes(0);
6458

6559
for (int i = 0; i < chunkRowSize; i++) {
66-
boolean codeSizeReached = i >= codeSize;
67-
int sliceNumber = i / LLARGE;
60+
final boolean codeSizeReached = i >= codeSize;
61+
final int sliceNumber = i / LLARGE;
6862

6963
// Fill Generic columns
7064
trace
@@ -99,13 +93,13 @@ public void trace(Trace.Rom trace, int cfi, int cfiInfty) {
9993

10094
// Deal when not in a PUSH instruction
10195
if (pushParameter == 0) {
102-
UnsignedByte opCode = UnsignedByte.of(dataPadded.get(i));
103-
final boolean isPush =
104-
EVM_INST_PUSH1 <= opCode.toInteger() && opCode.toInteger() <= EVM_INST_PUSH32;
96+
final UnsignedByte opCodeUB = UnsignedByte.of(dataPadded.get(i));
97+
final OpCode opcode = OpCode.of(opCodeUB.toInteger());
98+
final boolean isPush = opcode.isNonTrivialPush();
10599

106100
// The OpCode is a PUSH instruction
107101
if (isPush) {
108-
pushParameter = opCode.toInteger() - EVM_INST_PUSH1 + 1;
102+
pushParameter = opCodeUB.toInteger() - EVM_INST_PUSH0;
109103
if (pushParameter > LLARGE) {
110104
pushValueHigh = dataPadded.slice(i + 1, pushParameter - LLARGE);
111105
pushValueLow = dataPadded.slice(i + 1 + pushParameter - LLARGE, LLARGE);
@@ -117,14 +111,14 @@ public void trace(Trace.Rom trace, int cfi, int cfiInfty) {
117111
trace
118112
.isPush(isPush)
119113
.isPushData(false)
120-
.opcode(opCode)
114+
.opcode(opCodeUB)
121115
.pushParameter(UnsignedByte.of(pushParameter))
122116
.counterPush(UnsignedByte.ZERO)
123117
.pushValueAcc(Bytes.EMPTY)
124118
.pushValueHi(pushValueHigh)
125119
.pushValueLo(pushValueLow)
126120
.pushFunnelBit(false)
127-
.isJumpdest(opCode.toInteger() == EVM_INST_JUMPDEST);
121+
.isJumpdest(opcode.getData().isJumpDest());
128122
}
129123
// Deal when in a PUSH instruction
130124
else {

arithmetization/src/main/java/net/consensys/linea/zktracer/module/tables/instructionDecoder/InstructionDecoder.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,20 @@ public List<Trace.ColumnHeader> columnHeaders() {
118118
@Override
119119
public void commit(Trace trace) {
120120
for (int i = 0; i < 256; i++) {
121-
final OpCodeData op = OpCode.of(i).getData();
122-
123-
traceFamily(op, trace.instdecoder);
124-
traceStackSettings(op, trace.instdecoder);
125-
traceBillingSettings(op, trace.instdecoder);
126-
trace
127-
.instdecoder
128-
.opcode(UnsignedByte.of(i))
129-
.isPush(op.isPush())
130-
.isJumpdest(op.isJumpDest())
131-
.validateRow();
121+
traceOpcode(i, trace);
132122
}
133123
}
124+
125+
protected void traceOpcode(final int i, final Trace trace) {
126+
final OpCodeData op = OpCode.of(i).getData();
127+
traceFamily(op, trace.instdecoder);
128+
traceStackSettings(op, trace.instdecoder);
129+
traceBillingSettings(op, trace.instdecoder);
130+
trace
131+
.instdecoder
132+
.opcode(UnsignedByte.of(i))
133+
.isPush(op.isNonTrivialPush())
134+
.isJumpdest(op.isJumpDest())
135+
.validateRow();
136+
}
134137
}

arithmetization/src/main/java/net/consensys/linea/zktracer/opcode/DataLocation.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)