Skip to content

Commit

Permalink
Binary + steps optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Feb 20, 2024
1 parent 73da8a5 commit d391384
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
npm run test:build:gasLimit:v3
- name: run counters tests
run: |
npm run test:counters
# npm run test:counters
- name: run zkasm tests
run: |
npm run test:zkasm
Expand Down
10 changes: 3 additions & 7 deletions main/opcodes/arithmetic.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,12 @@ opEXP:

; check out-of-gas
$ => C :MLOAD(SP--); [a => C]
$ => B :MLOAD(SP); [exp => B]
$ => B, D :MLOAD(SP), CALL(getLenBytes) ; [exp => B] ; in: [B: number] out: [A: byte length of B]

; check out-of-gas
:CALL(getLenBytes) ; in: [B: number] out: [A: byte length of B]
GAS - %GAS_SLOW_STEP - %EXP_BYTE_GAS * A => GAS :JMPN(outOfGas)

; compute exponentiation
B => D
C => A
zkPC+1 => RR :JMP(expAD) ; in: [A, D] out: [A: A ** D]
A :MSTORE(SP++), JMP(readCode) ; [a ** exp => SP]
Expand Down Expand Up @@ -329,14 +327,12 @@ opSIGNEXTEND: ; following this impl https://github.com/0xPolygonHermez/ethereumj
30 => A
; if signByte is 31 or more, means basically let the number as it is
$ :LT, JMPC(opSIGNEXTENDEnd)
B * 8 + 7 => D, RR; B is less than 31, no need for binary
:CALL(@exp_num + RR)
B * 8 + 7 => RR; B is less than 31, no need for binary
:CALL(@exp_num + RR) ; out:[B: 2**RR]
B => A
$ => D :MLOAD(SP); [x => D]
;Store mask
1 => B
$ => C :SUB ; mask

D => B ; number to convert
$ => B :AND ; check sign bit
0 => A
Expand Down
11 changes: 5 additions & 6 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,10 @@ opCODECOPYloop:

; @info Load 0 if read bytecode position is above bytecode length
opCODECOPYLoadBytes:
0 => A
$ => B :MLOAD(codecopyBytecodeLength)
$ :EQ, JMPC(readZero)
HASHPOS => A
$ :LT, JMPC(readValueBytecode)
; codecopyBytecodeLength is less than 32 bits, length obtained from storage, max bytecode of a contract is 24kbytes
$ => B :MLOAD(codecopyBytecodeLength), JMPZ(readZero)
; if HASPOS < B, read value, else, read zero
HASHPOS - B - 1 :JMPN(readValueBytecode)
readZero:
0 => B :RETURN
readValueBytecode:
Expand Down Expand Up @@ -414,7 +413,7 @@ opEXTCODECOPY2:
A => HASHPOS
$ => D :MLOAD(tmpContractHashId)
D :MSTORE(codecopyHashId) ; set hashId to get bytes from
B :MSTORE(codecopyBytecodeLength) ; set contract bytecode length
B :MSTORE(codecopyBytecodeLength) ; set contract bytecode length, less than 32 bits
C :MSTORE(memOffset)
E :MSTORE(remainingBytes), JMP(opCODECOPYloopInit)

Expand Down
3 changes: 1 addition & 2 deletions main/opcodes/create-terminate-context.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ opCALL2:
$ => C :MLOAD(SP--); [argsSize => C]
C :MSTORE(argsLengthCall)
; store lastMemLength for memory expansion gas cost
C :MSTORE(lastMemLength)
; compute memory expansion gas cost
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]

; store byte offset in the memory in bytes, where to store the return data of the sub context
$ => B :MLOAD(SP--); [retOffset => B]
Expand Down
49 changes: 14 additions & 35 deletions main/opcodes/logs.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ opLOG0:
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
; store number of topics
0 :MSTORE(numTopics)
; C is less than 32 bits after calling saveMem
; calculate data size gas cost => lastMemLength * 8
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
$ => B :MLOAD(arithRes1)
GAS => A
; check out-of-gas
$ :LT,JMPC(outOfGas)
GAS - B => GAS :JMP(initLogLoop)
GAS - %LOG_DATA_GAS * C => GAS :JMPN(outOfGas)
:JMP(initLogLoop)
opLOG1:

%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)
Expand All @@ -52,14 +48,10 @@ opLOG1:
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
; C is less than 32 bits after calling saveMem
1 :MSTORE(numTopics)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
; check out-of-gas
$ :LT,JMPC(outOfGas)
GAS - B => GAS :JMP(initLogLoop)
GAS - %LOG_DATA_GAS * C => GAS :JMPN(outOfGas)
:JMP(initLogLoop)

opLOG2:

Expand All @@ -73,14 +65,10 @@ opLOG2:
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
; C is less than 32 bits after calling saveMem
2 :MSTORE(numTopics)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
; check out-of-gas
$ :LT,JMPC(outOfGas)
GAS - B => GAS :JMP(initLogLoop)
GAS - %LOG_DATA_GAS * C => GAS :JMPN(outOfGas)
:JMP(initLogLoop)

opLOG3:

Expand All @@ -94,14 +82,10 @@ opLOG3:
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
; C is less than 32 bits after calling saveMem
3 :MSTORE(numTopics)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
; check out-of-gas
$ :LT,JMPC(outOfGas)
GAS - B => GAS :JMP(initLogLoop)
GAS - %LOG_DATA_GAS * C => GAS :JMPN(outOfGas)
:JMP(initLogLoop)

opLOG4:

Expand All @@ -115,14 +99,9 @@ opLOG4:
$ => C :MLOAD(SP)
E :MSTORE(lastMemOffset)
C :MSTORE(lastMemLength), CALL(saveMem)
; C is less than 32 bits after calling saveMem
4 :MSTORE(numTopics)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH)
$ => B :MLOAD(arithRes1)
GAS => A
; check out-of-gas
$ :LT,JMPC(outOfGas)
GAS - B => GAS
GAS - %LOG_DATA_GAS * C => GAS :JMPN(outOfGas)

initLogLoop:
; check poseidon counters
Expand Down
3 changes: 2 additions & 1 deletion main/precompiled/identity.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ IDENTITYreturn:

; Get lower between retCallLength and
$ => A :MLOAD(retDataLength)
$ :LT, JMPC(IDENTITYreturn2)
; both retDataLength and retCallLength are less than 32 bits
A - B :JMPN(IDENTITYreturn2)
B => C
$ => B :MLOAD(retCallOffset), JMP(IDENTITYreturnLoop)

Expand Down
1 change: 1 addition & 0 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ computeGasSendCall:
0x3f - D :JMPN(failAssert) ; D is less than 32 bits, we can use JMPN

GAS - C => A
; gasCall can be more than 32 bits, obtained from stack
$ => B :MLOAD(gasCall)
; gas_sent_with_call = min(requested_gas, all_but_one_64th)
$ :LT,JMPC(computeGasSendCallEnd)
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/vcounters-optimizations-1",
"@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 d391384

Please sign in to comment.