Skip to content

Commit

Permalink
PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Nov 11, 2022
1 parent 964032e commit 9494e5e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 50 deletions.
10 changes: 5 additions & 5 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* - 10 poseidon
* - dynamic steps: 200 + 100 * length
* @process-opcode
* - stack input: offset, size
* - stack output: hash
* - stack input: [offset, size]
* - stack output: [hash]
*/
opSHA3:
; checks zk-counters
Expand All @@ -27,8 +27,8 @@ opSHA3:
; check out-of-gas
GAS - 30 => GAS :JMPN(outOfGas)
SP - 1 => SP
$ => E :MLOAD(SP--); offset => E
$ => C :MLOAD(SP) ; length => C
$ => E :MLOAD(SP--); [offset => E]
$ => C :MLOAD(SP) ; [size => C]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
; store lastMemLength for memory expansion gas cost
Expand Down Expand Up @@ -108,7 +108,7 @@ opSHA3End:
; compute hash
$ => A :HASHKDIGEST(E)
; store hash
A :MSTORE(SP++)
A :MSTORE(SP++); [hash(A) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
23 changes: 16 additions & 7 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* - 32 binary
* - 120 steps
* @process-opcode
* - stack input: counter
* - stack input: [counter]
* - stack output: none
*/
opJUMP:
Expand All @@ -19,21 +19,30 @@ opJUMP:
; check out-of-gas
GAS - 8 => GAS :JMPN(outOfGas)

$ => PC :MLOAD(SP)
$ => PC :MLOAD(SP); [counter => PC]
; Check PC is a JUMPDEST
:JMP(checkJumpDest)

/**
* @link [https://www.evm.codes/#57?fork=berlin]
* @zk-counters
* - 32 binary
* - 120 steps
* @process-opcode
* - stack input: [counter, value]
* - stack output: none
*/
opJUMPI:

%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)

SP - 2 => SP :JMPN(stackUnderflow)
$ => B :MLOAD(SP)
$ => B :MLOAD(SP); [value => B]
GAS - 10 => GAS :JMPN(outOfGas)
0 => A
$ :EQ, JMPC(readCode)
SP + 1 => SP
$ => PC :MLOAD(SP--) ;
$ => PC :MLOAD(SP--); [counter => PC]
PC => B
; Check PC is a JUMPDEST
:JMP(checkJumpDest)
Expand Down Expand Up @@ -99,7 +108,7 @@ checkJumpDestDeploymentCreate:
* - dynamic steps: 120 * pushed bytes
* @process-opcode
* - stack input: none
* - stack output: PC
* - stack output: [PC]
*/
opPC:
; checks zk-counters
Expand All @@ -108,7 +117,7 @@ opPC:
; check out-of-gas
GAS - 2 => GAS :JMPN(outOfGas)
; store current PC
PC - 1 :MSTORE(SP++)
PC - 1 :MSTORE(SP++); [PC => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
Expand Down
10 changes: 5 additions & 5 deletions main/opcodes/logs.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* - dynamic binary
* - 200 steps
* @process-opcode
* - stack input: offset, size, topic
* - stack output: nonde
* - stack input: [offset, size, topic]
* - stack output: none
*/
opLOG0:
; checks zk-counters
Expand All @@ -23,8 +23,8 @@ opLOG0:
0 - A :JMPN(invalidStaticTx)

SP - 1 => SP
$ => E :MLOAD(SP--) ; offset => E
$ => C :MLOAD(SP) ; length => C
$ => E :MLOAD(SP--) ; [offset => E]
$ => C :MLOAD(SP) ; [size => C]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
; store lastMemLength for memory expansion gas cost
Expand Down Expand Up @@ -184,7 +184,7 @@ opSaveTopicsLoop:
SP - 1 => SP :JMPN(stackUnderflow)
; check out-of-gas
GAS - 375 => GAS :JMPN(outOfGas)
$ => C :MLOAD(SP) ; topicX
$ => C :MLOAD(SP) ; [topic => C]
${storeLog(B, 1, C)} ; storeLog(indexLog, isTopic, bytesToStore)
A - 1 => A
:JMP(opSaveTopicsLoop)
28 changes: 14 additions & 14 deletions main/opcodes/stack-operations.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* - dynamic steps: 120 * pushed bytes
* @process-opcode
* - stack input: none
* - stack output: pushed value
* - stack output: [pushed_value]
*/
opPUSH1:
; number of bytes to push to D
Expand Down Expand Up @@ -214,7 +214,7 @@ opAuxPUSHA:
; read bytes from calldata
:CALL(readPush); in: [D: bytes to read] out: [E: value read]
; store stack output
E :MSTORE(SP++)
E :MSTORE(SP++); [pushed_value(E) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
; rectify PC
Expand Down Expand Up @@ -258,7 +258,7 @@ opAuxPUSHBend:
; check out-of-gas
GAS - 3 => GAS :JMPN(outOfGas)
; store stack output (value)
A :MSTORE(SP++)
A :MSTORE(SP++); [pushed_value(E) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
Expand Down Expand Up @@ -294,8 +294,8 @@ opAuxPUSHBcreate:
* @zk-counters
* - 120 steps
* @process-opcode
* - stack input: value
* - stack output: duplicated value, original value
* - stack input: [value]
* - stack output: [duplicated_value, original_value]
*/
opDUP1:
; checks zk-counters
Expand All @@ -307,11 +307,11 @@ opDUP1:
; check out-of-gas
GAS - 3 => GAS :JMPN(outOfGas)

$ => A :MLOAD(SP++); value => A
$ => A :MLOAD(SP++); [value => A]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
; store duplicated value
A :MSTORE(SP++)
A :MSTORE(SP++); [duplicated_value(A) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
Expand Down Expand Up @@ -500,8 +500,8 @@ opDUP16:
* @zk-counters
* - 120 steps
* @process-opcode
* - stack input: value to swap a, value to swap b
* - stack output: swapped value a, swapped value b
* - stack input: [value_to_swap_a, value_to_swap_b]
* - stack output: [swapped_value_a, swapped_value_b]
*/
opSWAP1:
; checks zk-counters
Expand All @@ -514,14 +514,14 @@ opSWAP1:
GAS - 3 => GAS :JMPN(outOfGas)

SP - 1 => SP
$ => A :MLOAD(SP--); value to swap a => A
$ => B :MLOAD(SP); value to swap b => B
$ => A :MLOAD(SP--); [value_to_swap_a => A]
$ => B :MLOAD(SP); [value_to_swap_b => B]
; store swapped value a
A :MSTORE(SP++)
A :MSTORE(SP++); [swapped_value_a => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
; store swapped value b
B :MSTORE(SP++)
B :MSTORE(SP++); [swapped_value_b => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
Expand Down Expand Up @@ -771,7 +771,7 @@ opSWAP16:
* @zk-counters
* - 120 steps
* @process-opcode
* - stack input: stack item
* - stack input: [stack_item]
* - stack output: none
*/
opPOP:
Expand Down
38 changes: 19 additions & 19 deletions main/opcodes/storage-memory.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: offset
* - stack output: value
* - stack input: [offset]
* - stack output: [value]
*/
opMLOAD:
; checks zk-counters
Expand All @@ -24,7 +24,7 @@ opMLOAD:
; check out-of-gas
GAS - 3 => GAS :JMPN(outOfGas)

$ => E :MLOAD(SP); offset => E
$ => E :MLOAD(SP); [offset => E]
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
Expand All @@ -33,7 +33,7 @@ opMLOAD:
; compute memory expansion gas cost
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
; store stack output
A :MSTORE(SP++)
A :MSTORE(SP++); [value(A) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
Expand All @@ -47,7 +47,7 @@ opMLOAD:
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: offset [E], value [B]
* - stack input: [offset, value]
* - stack output: none
*/
opMSTORE:
Expand All @@ -65,8 +65,8 @@ opMLOAD:
; check out-of-gas
GAS - 3 => GAS :JMPN(outOfGas)

$ => E :MLOAD(SP--); offset => E
$ => B :MLOAD(SP); value => B
$ => E :MLOAD(SP--); [offset => E]
$ => B :MLOAD(SP); [value => B]

; store bytesToStore for MSTORE32 execution
B :MSTORE(bytesToStore)
Expand All @@ -88,7 +88,7 @@ opMLOAD:
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: offset, value
* - stack input: [offset, value]
* - stack output: none
*/
opMSTORE8:
Expand All @@ -105,7 +105,7 @@ opMSTORE8:
; check out-of-gas
GAS - 3 => GAS :JMPN(outOfGas)

$ => B :MLOAD(SP--); offset => B
$ => B :MLOAD(SP--); [offset => B]
; store lastMemOffset for memory expansion gas cost
B :MSTORE(lastMemOffset)
; store lastMemLength for memory expansion gas cost. In case of MSTORE8, always 1 byte
Expand All @@ -115,7 +115,7 @@ opMSTORE8:
$ :LT,JMPC(errorMLOADMSTORE)
B => A
:CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
$ => B :MLOAD(SP); value => B
$ => B :MLOAD(SP); [value => B]
; read from memory position E
$ => A :MLOAD(MEM:E)
${memAlignWR8_W0(A,B,C)} => D ; no trust calculate W0
Expand All @@ -133,7 +133,7 @@ opMSTORE8:
* - 200 steps
* @process-opcode
* - stack input: none
* - stack output: size
* - stack output: [size]
*/
opMSIZE:
; checks zk-counters
Expand Down Expand Up @@ -161,7 +161,7 @@ opMSIZE:
C * 32 => E

MSIZEend:
E :MSTORE(SP++); E => size
E :MSTORE(SP++); [size(E) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
Expand All @@ -171,8 +171,8 @@ MSIZEend:
* - 255 poseidon
* - 120 steps
* @process-opcode
* - stack input: key
* - stack output: value
* - stack input: [key]
* - stack output: [value]
*/
opSLOAD:
; checks zk-counters
Expand All @@ -182,13 +182,13 @@ opSLOAD:
; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)

$ => C :MLOAD(SP); key => C
$ => C :MLOAD(SP); [key => C]
; get current storage address
$ => A :MLOAD(storageAddr)
; set key for smt storage query
%SMT_KEY_SC_STORAGE => B
$ => E :SLOAD
E :MSTORE(SP++); E => value
E :MSTORE(SP++); [value(E) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
; set key(C) as warmed storage for address(A)
Expand All @@ -205,7 +205,7 @@ opSLOAD:
* - 765 poseidon
* - 120 steps
* @process-opcode
* - stack input: key, value
* - stack input: [key, value]
* - stack output: none
*/
opSSTORE:
Expand All @@ -224,9 +224,9 @@ opSSTORE:
0 - A :JMPN(invalidStaticTx)

SP - 1 => SP
$ => C :MLOAD(SP--) ; key => C
$ => C :MLOAD(SP--) ; [key => C]
C :MSTORE(tmpVarC)
$ => D :MLOAD(SP) ; value => D
$ => D :MLOAD(SP) ; [value => D]
; check if is a create call
$ => A :MLOAD(isCreateContract)
0 - A :JMPN(deploymentSSTORE)
Expand Down

0 comments on commit 9494e5e

Please sign in to comment.