Skip to content

Commit

Permalink
Merge pull request #345 from 0xPolygonHermez/feature/reduce-push-steps
Browse files Browse the repository at this point in the history
Feature/reduce push steps
  • Loading branch information
krlosMata authored Feb 16, 2024
2 parents 874c8d7 + a0150ba commit 6aed721
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 105 deletions.
64 changes: 32 additions & 32 deletions main/map-opcodes.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -112,38 +112,38 @@ mapping_opcodes:
:JMP(opINVALID) ; 0x5D
:JMP(opINVALID) ; 0x5E
:JMP(opPUSH0) ; 0x5F
:JMP(opPUSH1) ; 0x60
:JMP(opPUSH2) ; 0x61
:JMP(opPUSH3) ; 0x62
:JMP(opPUSH4) ; 0x63
:JMP(opPUSH5) ; 0x64
:JMP(opPUSH6) ; 0x65
:JMP(opPUSH7) ; 0x66
:JMP(opPUSH8) ; 0x67
:JMP(opPUSH9) ; 0x68
:JMP(opPUSH10) ; 0x69
:JMP(opPUSH11) ; 0x6a
:JMP(opPUSH12) ; 0x6b
:JMP(opPUSH13) ; 0x6c
:JMP(opPUSH14) ; 0x6d
:JMP(opPUSH15) ; 0x6e
:JMP(opPUSH16) ; 0x6f
:JMP(opPUSH17) ; 0x70
:JMP(opPUSH18) ; 0x71
:JMP(opPUSH19) ; 0x72
:JMP(opPUSH20) ; 0x73
:JMP(opPUSH21) ; 0x74
:JMP(opPUSH22) ; 0x75
:JMP(opPUSH23) ; 0x76
:JMP(opPUSH24) ; 0x77
:JMP(opPUSH25) ; 0x78
:JMP(opPUSH26) ; 0x79
:JMP(opPUSH27) ; 0x7a
:JMP(opPUSH28) ; 0x7b
:JMP(opPUSH29) ; 0x7c
:JMP(opPUSH30) ; 0x7d
:JMP(opPUSH31) ; 0x7e
:JMP(opPUSH32) ; 0x7f
1 => D :JMP(opPUSH1) ; 0x60
2 => D :JMP(opPUSH2) ; 0x61
3 => D :JMP(opPUSH3) ; 0x62
4 => D :JMP(opPUSH4) ; 0x63
5 => D :JMP(opPUSH5) ; 0x64
6 => D :JMP(opPUSH6) ; 0x65
7 => D :JMP(opPUSH7) ; 0x66
8 => D :JMP(opPUSH8) ; 0x67
9 => D :JMP(opPUSH9) ; 0x68
10 => D :JMP(opPUSH10) ; 0x69
11 => D :JMP(opPUSH11) ; 0x6a
12 => D :JMP(opPUSH12) ; 0x6b
13 => D :JMP(opPUSH13) ; 0x6c
14 => D :JMP(opPUSH14) ; 0x6d
15 => D :JMP(opPUSH15) ; 0x6e
16 => D :JMP(opPUSH16) ; 0x6f
17 => D :JMP(opPUSH17) ; 0x70
18 => D :JMP(opPUSH18) ; 0x71
19 => D :JMP(opPUSH19) ; 0x72
20 => D :JMP(opPUSH20) ; 0x73
21 => D :JMP(opPUSH21) ; 0x74
22 => D :JMP(opPUSH22) ; 0x75
23 => D :JMP(opPUSH23) ; 0x76
24 => D :JMP(opPUSH24) ; 0x77
25 => D :JMP(opPUSH25) ; 0x78
26 => D :JMP(opPUSH26) ; 0x79
27 => D :JMP(opPUSH27) ; 0x7a
28 => D :JMP(opPUSH28) ; 0x7b
29 => D :JMP(opPUSH29) ; 0x7c
30 => D :JMP(opPUSH30) ; 0x7d
31 => D :JMP(opPUSH31) ; 0x7e
32 => D :JMP(opPUSH32) ; 0x7f
:JMP(opDUP1) ; 0x80
:JMP(opDUP2) ; 0x81
:JMP(opDUP3) ; 0x82
Expand Down
33 changes: 0 additions & 33 deletions main/opcodes/stack-operations.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -27,134 +27,101 @@ opPUSH0:
* - stack output: [pushed_value]
*/
opPUSH1:
; number of bytes to push to D
1 => D
; check is a deploy
; if is a deploy, get bytes from memory else, get bytes from stack
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH2:
2 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH3:
3 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH4:
4 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH5:
5 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH6:
6 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH7:
7 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH8:
8 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH9:
9 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH10:
10 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH11:
11 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH12:
12 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH13:
13 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH14:
14 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH15:
15 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH16:
16 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH17:
17 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH18:
18 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH19:
19 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH20:
20 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH21:
21 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH22:
22 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH23:
23 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH24:
24 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH25:
25 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH26:
26 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH27:
27 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH28:
28 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH29:
29 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH30:
30 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH31:
31 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opPUSH32:
32 => D
$ => B :MLOAD(isCreateContract), JMPNZ(opAuxPUSHB, opAuxPUSHA)

opAuxPUSHA:
Expand Down
8 changes: 4 additions & 4 deletions main/precompiled/pre-ecAdd.zkasm
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* @link [https://www.evm.codes/precompiled#0x06?fork=berlin]
* @zk-counters
* - dynamic steps: 500
* - dynamic steps: 800
* - dynamic arith: 50
* - dynamic binary: 10
* - dynamic binary: 50
* @process-precompiled
* - stack input: [x1, y1, x2, y2]
* - stack output: [x, y]
*/
funcEcAdd:
%MAX_CNT_ARITH - CNT_ARITH - 50 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 50 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 50 :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 800 :JMPN(outOfCountersStep)

; Move balances if value > 0 just before executing the contract CALL
Expand All @@ -35,7 +35,7 @@ funcEcAdd:
A :MSTORE(ecAdd_P2_x)
E + 32 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => A :MLOAD(readXFromCalldataResult) ; y2
A :MSTORE(ecAdd_P2_y),CALL(ecAdd)
A :MSTORE(ecAdd_P2_y), CALL(ecAdd)

; check error
B :JMPNZ(preEndFail)
Expand Down
10 changes: 5 additions & 5 deletions main/precompiled/pre-ecMul.zkasm
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* @link [https://www.evm.codes/precompiled#0x07?fork=berlin]
* @zk-counters
* - dynamic steps: 130000
* - dynamic steps: 175000
* - dynamic arith: 20000
* - dynamic binary: 4000
* - dynamic binary: 20000
* @process-precompiled
* - stack input: [x1, y1, s]
* - stack output: [x, y]
*/
funcEcMul:

%MAX_CNT_BINARY - CNT_BINARY - 4000 :JMPN(outOfCountersBinary)
%MAX_CNT_BINARY - CNT_BINARY - 20000 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 20000 :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 130000 :JMPN(outOfCountersStep)
%MAX_CNT_STEPS - STEP - 175000 :JMPN(outOfCountersStep)

; Move balances if value > 0 just before executing the contract CALL
$ => B :MLOAD(txValue)
Expand All @@ -33,7 +33,7 @@ funcEcMul:
A :MSTORE(ecMul_P_y)
E + 32 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => A :MLOAD(readXFromCalldataResult) ; k
A :MSTORE(ecMul_k),CALL(ecMul)
A :MSTORE(ecMul_k), CALL(ecMul)

; check error
B :JMPNZ(preEndFail)
Expand Down
22 changes: 6 additions & 16 deletions main/precompiled/pre-ecPairing.zkasm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @link [https://www.evm.codes/precompiled#0x08?fork=berlin]
* @zk-counters
* - dynamic steps: 50000, 550000, 700000,...
* - dynamic arith: 4000, 43000, 56000,...
* - dynamic binary: 400, 700, 1100,...
* - dynamic steps: 200000 * nInputs + 175000
* - dynamic arith: 15000 * nInputs + 17500
* - dynamic binary: 4100 * nInputs + 750
* @process-precompiled
* - stack input: [x1, y1, x2, y2, ..., xk, yk]
* - stack output: [success]
Expand All @@ -28,21 +28,11 @@ funcEcPairing:
A :MSTORE(ecPairing_ninputs)

GAS - 34000*A => GAS :JMPN(outOfGas) ; gas = 34000 * inputsLength
A - 1 :JMPZ(counters6)
A - 2 => A

%MAX_CNT_BINARY - CNT_BINARY - 700 - 400*A :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 45000 - 14000*A :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 550000 - 200000*A :JMPN(outOfCountersStep)
:JMP(afterCounts)
%MAX_CNT_BINARY - CNT_BINARY - 750 - 4100*A :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 17500 - 15000*A :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 175000 - 200000*A :JMPN(outOfCountersStep)

counters6:

%MAX_CNT_BINARY - CNT_BINARY - 400 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 4000 :JMPN(outOfCountersArith)
%MAX_CNT_STEPS - STEP - 50000 :JMPN(outOfCountersStep)

afterCounts:
-32 :MSTORE(readXFromCalldataOffset),CALL(ecPairing)
B :JMPNZ(preEndFail)
$ => C :MLOAD(ecPairing_result)
Expand Down
16 changes: 1 addition & 15 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -1066,42 +1066,31 @@ readPush32:
PC => HASHPOS :JMP(_readPush32) ;32

_readPush1:
%MAX_CNT_STEPS - STEP - 2 :JMPN(outOfCountersStep)

$ => E :MLOAD(contractHashId)
$ => E :HASHP1(E),RETURN

_readPush2:
%MAX_CNT_STEPS - STEP - 4 :JMPN(outOfCountersStep)

$ => E :MLOAD(contractHashId)
$ => C :HASHP1(E)
$ => E :HASHP1(E)
C * 256 + E => E :RETURN

_readPush3:
%MAX_CNT_STEPS - STEP - 5 :JMPN(outOfCountersStep)

$ => E :MLOAD(contractHashId)
$ => A :HASHP1(E)
$ => C :HASHP1(E)
$ => E :HASHP1(E)
A * 2**16 + C * 2**8 + E => E :RETURN

_readPush4:
%MAX_CNT_STEPS - STEP - 6 :JMPN(outOfCountersStep)

$ => E :MLOAD(contractHashId)
$ => A :HASHP1(E)
$ => B :HASHP1(E)
$ => C :HASHP1(E)
$ => E :HASHP1(E)
A * 2**24 + B * 2**16 + C * 2**8 + E => E :RETURN


_readPush32:
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)

$ => E :MLOAD(contractHashId)

$ => B :HASHP1(E)
Expand Down Expand Up @@ -1169,9 +1158,7 @@ _readPush32:
32 => D :RETURN

_readPushX:
%MAX_CNT_STEPS - STEP - 20 - D :JMPN(outOfCountersStep)
D => A,E :MSTORE(pushBytes)

D :MSTORE(pushBytes)
$ => B :MLOAD(contractHashId)

readPushBytes:
Expand Down Expand Up @@ -1210,7 +1197,6 @@ readPushBytes:
0 => RCX, B, D, A, C :JMP(readPushBytes_31)
0 => RCX, B, D, A, C :JMP(readPushBytes_32)


readPushBytes_32:
$ => B :HASHP1(E)
readPushBytes_31:
Expand Down

0 comments on commit 6aed721

Please sign in to comment.