Skip to content

Commit

Permalink
Merge pull request #406 from 0xPolygonHermez/feature/fork-13
Browse files Browse the repository at this point in the history
Feature/fork 13
  • Loading branch information
laisolizq authored Oct 3, 2024
2 parents bc8b1f8 + 68b5d22 commit db8d312
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CONST %BATCH_DIFFICULTY = 0
CONST %TX_GAS_LIMIT = 30000000
CONSTL %BLOCK_GAS_LIMIT = 2**50
CONST %MAX_MEM_EXPANSION_BYTES = 0x3fffe0
CONST %FORK_ID = 12
CONST %FORK_ID = 13
CONST %L1INFO_TREE_LEVELS = 32
CONST %CALLDATA_RESERVED_CTX = 1
CONSTL %FOUR_GOLDILOCKS = 0xffffffff00000001ffffffff00000001ffffffff00000001ffffffff00000001n
Expand Down
79 changes: 40 additions & 39 deletions main/precompiled/pre-modexp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ VAR GLOBAL modexp_Mend
VAR GLOBAL modexp_offset
VAR GLOBAL modexp_returnIndex
VAR GLOBAL modexp_returnFirstIndex
VAR GLOBAL modexp_returnIndexRem
VAR GLOBAL expLenBits
VAR GLOBAL retCopyLen
VAR GLOBAL offsetCopyReturn

funcModexp:

Expand Down Expand Up @@ -206,7 +206,7 @@ lastChecks:
$ :EQ, JMPC(save0out) ; if Bsize = 0 --> save0out
; Bsize > 0 from here

; Check maximum length allowed in the zkEVM reagrding the modExp
; Check maximum length allowed in the zkEVM regarding the modExp
; If parameters (length > %MAX_SIZE_MODEXP) --> zkEVM does a revert returning all the gas consumed
%MAX_SIZE_MODEXP => A
$ => B :MLOAD(modexp_Bsize)
Expand Down Expand Up @@ -273,33 +273,48 @@ finalMODEXP:
B => A

finalMODEXPreturn:
; write data into memory
; save lower between retCallLength and modexd_Msize in retCopyLen
A :MSTORE(retCopyLen)

; write data into memory
0 => B
; memory length === Msize
$ => C :MLOAD(modexp_Msize)
C :MSTORE(arithA)
32 :MSTORE(arithB), CALL(divARITH)
; number of chunks (32 bytes) to copy from modexp_out to memory
$ => E :MLOAD(arithRes1)
; first index of modexp_out is the biggest --> 0x | modexp_out+N | .... | modexp_out+0
E :MSTORE(modexp_returnFirstIndex)
$ => A :MLOAD(arithRes2)
A :MSTORE(modexp_returnIndexRem), JMPZ(memoryLoop)
A :JMPZ(memoryLoop)

; if Msize % 32 > 0, copy last bytes, else --> memoryLoop
A => C
; A = 0x0000000000000000000000000000000000000000XXXXXXXXXXXXXXXXXXXXXXXX
; C = lenght X's
$ => A :MLOAD(modexp_out+E)
32 - C => D :CALL(SHLarith)
; A = 0xXXXXXXXXXXXXXXXXXXXXXXXX0000000000000000000000000000000000000000
A :MSTORE(bytesToStore)
A :MSTORE(modexp_out+E)
B => E
; MSTORE X with C = lenght X's
:CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset]
; E == new offset (0 + length X's)
E => B
$ => A :MLOAD(modexp_Msize)
A - C => C :JMPZ(modexpReturn)
; C new_size = total_size - total_size%32
$ => E :MLOAD(modexp_returnFirstIndex)

memoryLoop:
; loop chunks of 32 bytes to memory

%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)

; E = update index modexp_out
E - 1 => E :MSTORE(modexp_returnIndex)
$ => A :MLOAD(modexp_out+E)
A :MSTORE(bytesToStore)
Expand All @@ -316,47 +331,33 @@ modexpReturn:
0 :MSTORE(retDataOffset)
$ => C :MLOAD(modexp_Msize)
C :MSTORE(retDataLength)
$ => B :MLOAD(retCallOffset)
$ => D :MLOAD(retCallOffset)
D :MSTORE(offsetCopyReturn)
$ => A :MLOAD(originCTX), JMPZ(handleGas)
; set retDataCTX
$ => E :MLOAD(currentCTX)
$ => B :MLOAD(currentCTX)
A => CTX
E :MSTORE(retDataCTX)

B :MSTORE(retDataCTX)
$ => C :MLOAD(retCopyLen)
$ => E :MLOAD(modexp_returnFirstIndex)
$ => A :MLOAD(modexp_returnIndexRem), JMPZ(returnLoop)
A => C
$ => A :MLOAD(modexp_out+E)
32 - C => D :CALL(SHLarith)
A :MSTORE(bytesToStore)
B => E
:CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset]
E => B
$ => A :MLOAD(retCopyLen)
A - C => C :JMPZ(endMODEXP)
$ => E :MLOAD(modexp_returnFirstIndex)

returnLoop:
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)

E - 1 => E :MSTORE(modexp_returnIndex)
C - 32 :JMPN(returnLoopFinal)
$ => A :MLOAD(modexp_out+E)
0 => E
; copy retCopyLen from memory (currentCTX) to retCallOffset (originCTX)
modexpReturnLoop:
C - 32 :JMPN(modexpReturnFinal)
B => CTX :CALL(MLOAD32)
E => D
$ => CTX :MLOAD(originCTX)
A :MSTORE(bytesToStore)
B => E
:CALL(MSTORE32) ; in: [bytesToStore, E: offset] out: [E: new offset]
E => B
$ => E :MLOAD(modexp_returnIndex)
C - 32 => C :JMPZ(endMODEXP, returnLoop)

returnLoopFinal:
$ => A :MLOAD(modexp_out+E)
32 - C => D :CALL(SHLarith)
$ => E :MLOAD(offsetCopyReturn)
:CALL(MSTORE32)
E :MSTORE(offsetCopyReturn)
D => E
C - 32 => C :JMPZ(endMODEXP, modexpReturnLoop)

modexpReturnFinal:
B => CTX :CALL(MLOADX)
$ => CTX :MLOAD(originCTX)
A :MSTORE(bytesToStore)
B => E
:CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset]
$ => E :MLOAD(offsetCopyReturn),CALL(MSTOREX)
:JMP(endMODEXP)

preEndMODEXP:
Expand Down
2 changes: 1 addition & 1 deletion main/precompiled/selector.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ selectorPrecompiled:
A - 3 :JMPN(funcSHA256)
A - 4 :JMPN(revertPrecompiled) ;:JMPN(RIPEMD160)
A - 5 :JMPN(IDENTITY)
A - 6 :JMPN(revertPrecompiled) ;:JMPN(funcModexp)
A - 6 :JMPN(funcModexp)
A - 7 :JMPN(funcEcAdd)
A - 8 :JMPN(funcEcMul)
A - 9 :JMPN(funcEcPairing)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"devDependencies": {
"@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#v8.0.0-fork.12",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#v8.0.0-fork.12",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#v8.0.0-fork.12",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/fork-13",
"chai": "^4.3.6",
"chalk": "^3.0.0",
"eslint": "^8.25.0",
Expand Down

0 comments on commit db8d312

Please sign in to comment.