Skip to content

Commit

Permalink
Comment storage-memory opcodes group
Browse files Browse the repository at this point in the history
Comment stack-operations opcodes group


Pr review


Comment log opcodes group


Comment flow control opcodes group


Comment flow crypto opcodes group


PR review
  • Loading branch information
ignasirv committed Nov 11, 2022
1 parent 3c2222a commit 295a1d2
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 198 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
name: Test executor inputs

on:
workflow_dispatch:
pull_request:
branches: [main, develop]

Expand Down
67 changes: 50 additions & 17 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@

/**
* @link [https://www.evm.codes/#20?fork=berlin]
* @zk-counters
* - 192 arith
* - dynamic binary: 193 + length
* - 2 mem align
* - 2 keccak
* - 10 poseidon
* - dynamic steps: 200 + 100 * length
* @process-opcode
* - stack input: [offset, size]
* - stack output: [hash]
*/
opSHA3:

; checks zk-counters
%MAX_CNT_ARITH - CNT_ARITH - 192 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 193 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)
Expand All @@ -9,73 +21,94 @@ opSHA3:
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - 10 :JMPN(outOfCountersPoseidon)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

; check stack underflow
SP - 2 :JMPN(stackUnderflow)

; check out-of-gas
GAS - 30 => GAS :JMPN(outOfGas)
SP - 1 => SP
$ => E :MLOAD(SP--) ; offset
$ => C :MLOAD(SP) ; length
$ => 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
C :MSTORE(lastMemLength)
; gas
GAS - 30 => GAS :JMPN(outOfGas); gas static = 30
:CALL(saveMem)
; Div operation with Arith
; compute memory expansion gas cost
:CALL(saveMem); in: [lastMemOffset, lastMemLength]
; check out-of-gas, dynamic
;${6*((C+31)/32)}
C+31 => A
;(C+31)/32
A :MSTORE(arithA)
32 :MSTORE(arithB)
:CALL(divARITH)
$ => A :MLOAD(arithRes1)
$ => A :MLOAD(arithRes1); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
; Mul operation with Arith
; 6*((C+31)/32)
6 :MSTORE(arithA)
A :MSTORE(arithB)
:CALL(mulARITH)
$ => A :MLOAD(arithRes1)
GAS - A => GAS :JMPN(outOfGas) ; dynamic_gas = dynamic_gas = 6 * minimum_word_size + memory_expansion_cost
GAS - A => GAS :JMPN(outOfGas) ; dynamic_gas = 6 * minimum_word_size + memory_expansion_cost
; new hash id
$ => B :MLOAD(lastHashKIdUsed)
B + 1 => B :MSTORE(lastHashKIdUsed)
; set bytes to hash at D
32 => D
; A new hash with position 0 is started
0 => HASHPOS

opSHA3Loop:

; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 100 :JMPN(outOfCountersStep)

C - 1 :JMPN(opSHA3End)
C - 32 :JMPN(opSHA3Final)
:CALL(MLOAD32)
; load next 32 bytes from memory
:CALL(MLOAD32); in: [E: offset] out: [A: value, E: new offset]
; save new offset at B
E => B
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
; append A to hash pointer E
A :HASHK(E)
; restore new offset at E
B => E
C - 32 => C
:JMP(opSHA3Loop)
opSHA3Final:
:CALL(MLOADX)
; load next C bytes from memory
:CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
; set #bytes to right shift
32 - C => D
:CALL(SHRarith)
:CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
; set remaining bytes length to hash at D
C => D
; append A to hash pointer E
A :HASHK(E)

opSHA3End:
; get current hash pointer
$ => E :MLOAD(lastHashKIdUsed)
; append A to hash pointer E
HASHPOS :HASHKLEN(E)

; Check keccak counters
HASHPOS :MSTORE(arithA)
136 :MSTORE(arithB)
:CALL(divARITH)
:CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
$ => B :MLOAD(arithRes1)
$ => A :MLOAD(cntKeccakPreProcess)
; checks keccak counters
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - A => A
$ :LT, JMPC(outOfCountersKeccak)

; compute hash
$ => A :HASHKDIGEST(E)
A :MSTORE(SP++)
; store hash
A :MSTORE(SP++); [hash(A) => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)
91 changes: 74 additions & 17 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
@@ -1,84 +1,141 @@

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

; checks zk-counters
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
%MAX_CNT_STEPS - STEP - 200 :JMPN(outOfCountersStep)

; check stack underflow
SP - 1 => SP :JMPN(stackUnderflow)
$ => PC :MLOAD(SP)

; check out-of-gas
GAS - 8 => GAS :JMPN(outOfGas)

$ => 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)

; TODO check do not overflow
checkJumpDest:
; If it is a deploy we have to check the data from the calldata (not the bytecode)
; if it is a deploy we have to check the data from the calldata (not the bytecode)
$ => A :MLOAD(isCreateContract)
-A :JMPN(checkJumpDestDeployment)
PC => HASHPOS
HASHPOS => D
; set number of bytes to hashP at D
1 => D
$ => E :MLOAD(contractHashId) ; hash index
; get hashP address pointer where contract bytecode is stored
$ => E :MLOAD(contractHashId)
$ => A :HASHP(E)
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode)
:JMP(invalidJump)
checkJumpDestDeployment:
1 => A
$ => B :MLOAD(isCreate)
$ :EQ,JMPC(checkJumpDestDeploymentCreate)
; check jumpDest from calldata
$ => A :MLOAD(isCreate)
-A :JMPN(checkJumpDestDeploymentCreate)
; get position where data starts in the tx
$ => HASHPOS :MLOAD(dataStarts)
; add PC to data starts to point the bytes to read for the push
HASHPOS + PC => HASHPOS
; get memory pointer for hashing
$ => E :MLOAD(batchHashDataId)
; set number of bytes to hashK at D
1 => D
$ => A :HASHK(E)
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode)
:JMP(invalidJump)

checkJumpDestDeploymentCreate:

; checks zk-counters
%MAX_CNT_ARITH - CNT_ARITH - 224 :JMPN(outOfCountersArith)
%MAX_CNT_BINARY - CNT_BINARY - 225 :JMPN(outOfCountersBinary)
%MAX_CNT_MEM_ALIGN - CNT_MEM_ALIGN - 2 :JMPN(outOfCountersMemalign)

; get bytes from previous context memory
$ => CTX :MLOAD(originCTX)
; get offset call position
$ => E :MLOAD(argsOffsetCall)
; increase current program counter (PC) to offset for getting pushed bytes to read
E + PC => E
; set bytes to read from memory at C
1 => C
:CALL(MLOADX)
:CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
$ => CTX :MLOAD(currentCTX)
31 => D
:CALL(SHRarith)
:CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode)
:JMP(invalidJump)

/**
* @link [https://www.evm.codes/#58?fork=berlin]
* @zk-counters
* - dynamic steps: 120 * pushed bytes
* @process-opcode
* - stack input: none
* - stack output: [PC]
*/
opPC:

; checks zk-counters
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)

; check out-of-gas
GAS - 2 => GAS :JMPN(outOfGas)
PC - 1 :MSTORE(SP++) ; TODO: check PC
; store current PC
PC - 1 :MSTORE(SP++); [PC => SP]
; check stack overflow
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)


/**
* @link [https://www.evm.codes/#5B?fork=berlin]
* @zk-counters
* - dynamic steps: 120 * pushed bytes
* @process-opcode
* - stack input: none
* - stack output: none
*/
opJUMPDEST:

; checks zk-counters
%MAX_CNT_STEPS - STEP - 120 :JMPN(outOfCountersStep)

; check out-of-gas
GAS - 1 => GAS :JMPN(outOfGas)
:JMP(readCode)
Loading

0 comments on commit 295a1d2

Please sign in to comment.