Skip to content

Commit

Permalink
Log optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Feb 14, 2024
1 parent e6bef57 commit 5189e7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 18 additions & 15 deletions main/opcodes/logs.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* - stack output: none
*/
VAR GLOBAL opLogAux

VAR GLOBAL logNextHashPIdAux
opLOG0:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand Down Expand Up @@ -45,15 +45,15 @@ opLOG1:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 3 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
1 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -66,15 +66,15 @@ opLOG2:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 4 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS*2 => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
2 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -87,15 +87,15 @@ opLOG3:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 5 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS*3 => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
3 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -108,15 +108,15 @@ opLOG4:
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
SP - 6 :JMPN(stackUnderflow)
GAS - %LOG_GAS => GAS :JMPN(outOfGas)
GAS - %LOG_GAS - %LOG_TOPIC_GAS*4 => GAS :JMPN(outOfGas)
$ => A :MLOAD(isStaticCall), JMPNZ(invalidStaticTx)
SP - 1 => SP
$ => E :MLOAD(SP--)
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
4 :MSTORE(numTopics)
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
Expand All @@ -136,6 +136,12 @@ initLogLoop:
0 => HASHPOS
32 => D
$ => B :MLOAD(currentLogIndex)
$ => E :MLOAD(nextHashPId)
; Update nextHashPId
E + 1 :MSTORE(nextHashPId)
; store nextHashPId to aux variable
E :MSTORE(logNextHashPIdAux)

logLoop:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -144,7 +150,7 @@ logLoop:
C - 32 :JMPN(opLogFinal)
:CALL(MLOAD32); in: [E: offset] out: [A: value]
E :MSTORE(opLogAux)
$ => E :MLOAD(nextHashPId)
$ => E :MLOAD(logNextHashPIdAux)
A :HASHP(E)
$ => E :MLOAD(opLogAux)
$${storeLog(B, 0, A)} ; storeLog(indexLog, isTopic, bytesToStore)
Expand All @@ -156,14 +162,14 @@ opLogFinal:
$${storeLog(B, 0, A)}; storeLog(indexLog, isTopic, bytesToStore)
32 - C => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
C => D
$ => E :MLOAD(nextHashPId)
$ => E :MLOAD(logNextHashPIdAux)
A :HASHP(E)
:JMP(opSaveTopicsInit) ; instruction added to allow executing $$ function

opSaveTopicsInit:
; save topics
$ => A :MLOAD(numTopics)
$ => E :MLOAD(nextHashPId)
$ => E :MLOAD(logNextHashPIdAux)
32 => D

opSaveTopicsLoop:
Expand All @@ -174,16 +180,13 @@ opSaveTopicsLoop:
; check stack underflow
SP - 1 => SP
; check out-of-gas
GAS - %LOG_TOPIC_GAS => GAS :JMPN(outOfGas)
$ => C :MLOAD(SP) ; [topic => C]
C :HASHP(E)
$${storeLog(B, 1, C)} ; storeLog(indexLog, isTopic, bytesToStore)
A - 1 => A :JMP(opSaveTopicsLoop)

finishSaveTopics:
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
; Update nextHashPId
E + 1 :MSTORE(nextHashPId)
; Compute hash of the log
HASHPOS :HASHPLEN(E)
$ => D :HASHPDIGEST(E), CALL(fillBlockInfoTreeWithLog); in: [D: linearPoseidon(log_data + log_topics)]
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#v4.0.0-fork.7",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop-eldelberry",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/change-block-gas-limit",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#develop-eldelberry",
"chai": "^4.3.6",
"chalk": "^3.0.0",
"eslint": "^8.25.0",
Expand Down

0 comments on commit 5189e7d

Please sign in to comment.