Skip to content

Commit

Permalink
Merge pull request #174 from 0xPolygonHermez/feature/keccacks-update
Browse files Browse the repository at this point in the history
Update keccacks computation
  • Loading branch information
krlosMata authored Nov 17, 2022
2 parents 298e4a7 + 4b8d279 commit d58875a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ CONST %MAX_CNT_MEM_ALIGN = %MAX_CNT_STEPS / 32
CONST %MAX_CNT_KECCAK_F = (%MAX_CNT_STEPS / 158418) * 9
CONST %MAX_CNT_PADDING_PG = (%MAX_CNT_STEPS / 56)
CONST %MAX_CNT_POSEIDON_G = (%MAX_CNT_STEPS / 30)
CONST %MIN_CNT_KECCAK_BATCH = 2 ; minimum necessary keccaks to compute global hash
CONST %MIN_CNT_KECCAK_BATCH = 1 ; minimum necessary keccaks to compute global hash
; ETHEREUM CONSTANTS
CONSTL %MAX_NONCE = 0xffffffffffffffffn
4 changes: 1 addition & 3 deletions main/main.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,14 @@ skipSetGlobalExitRoot:

; Compute necessary keccak counters to finish batch
$ => A :MLOAD(batchL2DataLength)
; batchHashData: batchL2DataLength + 32 (globalExitRoot) + 20 (sequencerAddress)
A + 52 => A
; Divide the total data length by 136 to obtain the keccak counter increment.
; 136 is the value used by the prover to increment keccak counters
A :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
; Compute minimum necessary keccaks to finish the batch
B + %MIN_CNT_KECCAK_BATCH :MSTORE(cntKeccakPreProcess)
B + 1 + %MIN_CNT_KECCAK_BATCH :MSTORE(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F => A
$ :LT, JMPC(outOfCountersKeccak)

Expand Down
5 changes: 3 additions & 2 deletions main/opcodes/block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ opBLOCKHASH:

%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 255 :JMPN(outOfCountersPoseidon)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 :JMPN(outOfCountersKeccak)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCountersKeccak)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

SP - 1 => SP :JMPN(stackUnderflow)
Expand All @@ -23,7 +23,8 @@ opBLOCKHASH:
A :HASHK(E)
%STATE_ROOT_STORAGE_POS :HASHK(E)
HASHPOS :HASHKLEN(E)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - %MIN_CNT_KECCAK_BATCH - 2:JMPN(outOfCountersKeccak)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1:JMPN(outOfCountersKeccak)
$ => C :HASHKDIGEST(E)
%ADDRESS_SYSTEM => A
%SMT_KEY_SC_STORAGE => B
Expand Down
4 changes: 2 additions & 2 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ opSHA3:
%MAX_CNT_BINARY - CNT_BINARY - 193 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 2 :JMPN(outOfCountersKeccak)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 :JMPN(outOfCountersKeccak)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 10 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

Expand Down Expand Up @@ -103,7 +103,7 @@ opSHA3End:
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
; checks keccak counters
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A
$ :LT, JMPC(outOfCountersKeccak)
; compute hash
$ => A :HASHKDIGEST(E)
Expand Down
6 changes: 3 additions & 3 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ processTx:
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A
$ :LT, JMPC(outOfCountersKeccak)

; Get hash address previously stored in RLP parsing
Expand Down Expand Up @@ -314,7 +314,7 @@ create2end:
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A
$ :LT, JMPC(outOfCountersKeccak)
$ => C :HASHKDIGEST(E)
; new hash with position 0 is started
Expand All @@ -341,7 +341,7 @@ endContractAddress:
:CALL(divARITH)
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1 => A
$ :LT, JMPC(outOfCountersKeccak)

$ => A :HASHKDIGEST(E)
Expand Down
3 changes: 2 additions & 1 deletion main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,8 @@ updateSystemData:
A :HASHK(E)
%STATE_ROOT_STORAGE_POS :HASHK(E)
HASHPOS :HASHKLEN(E)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - %MIN_CNT_KECCAK_BATCH - 2:JMPN(outOfCountersKeccak)
$ => A :MLOAD(cntKeccakPreProcess)
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A - 1:JMPN(outOfCountersKeccak)
$ => C :HASHKDIGEST(E)
%ADDRESS_SYSTEM => A
SR => D
Expand Down

0 comments on commit d58875a

Please sign in to comment.