Skip to content

Commit

Permalink
Audit PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Jan 12, 2023
1 parent e8730ba commit bc0ecc8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 31 deletions.
3 changes: 1 addition & 2 deletions counters/tests/opBLOCKHASH.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ start:
1000000 => GAS

operation:
1 => D
2 :HASHK(0)
2 :HASHK1(0)
10 :MSTORE(txCount)
1 :MSTORE(SP++)
:JMP(opBLOCKHASH)
Expand Down
3 changes: 1 addition & 2 deletions counters/tests/opSHA3.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ start:
1000000 => GAS

operation:
1 => D
2 :HASHK(0)
2 :HASHK1(0)
2000 :MSTORE(SP++)
100 :MSTORE(SP++)
:JMP(opSHA3)
Expand Down
9 changes: 6 additions & 3 deletions main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ CONST %SMT_KEY_TOUCHED_SLOTS = 6
CONST %BASE_TX_GAS = 21000
CONST %BASE_TX_DEPLOY_GAS = 53000
CONST %SLOAD_GAS = 100
CONST %SSTORE_SET_GAS = 20000
CONST %SSTORE_RESET_GAS = 2900
CONST %SSTORE_CLEARS_SCHEDULE = 15000
CONST %GAS_QUICK_STEP = 2
CONST %GAS_FASTEST_STEP = 3
CONST %GAS_FAST_STEP = 5
Expand All @@ -54,12 +51,18 @@ CONST %RETURN_GAS_COST = 200; 200 * returned_code_size
CONST %CREATE_GAS = 32000
CONST %CREATE_2_GAS = 32000
CONST %SENDALL_GAS = 5000
CONST %LOG_DATA_GAS = 8
; sstore
CONST %SSTORE_ENTRY_EIP_2200_GAS = 2300 ; Minimum gas required to be present for an SSTORE call, not consumed
CONST %SSTORE_SET_EIP_2200_GAS = 20000 ; Once per SSTORE operation from clean zero to non-zero
CONST %COLD_SLOAD_COST = 2100 ; Cost of using a cols memory slot
CONST %COLD_SLOAD_COST_RED = 2000 ; Cost of using a cols memory slot reduced
CONST %SSTORE_DYNAMIC_GAS = 100
CONST %SSTORE_SET_GAS = 20000
CONST %SSTORE_SET_GAS_RED = 19900 ; sstore gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100)
CONST %SSTORE_RESET_GAS = 2900
CONST %SSTORE_RESET_GAS_RED = 2800 ; sstore update gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100)
CONST %SSTORE_CLEARS_SCHEDULE = 15000
; COUNTERS
CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx
CONST %TOTAL_STEPS = 2**23
Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/comparison.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ opNOT:
; read one item from the stack
$ => A :MLOAD(SP-1)

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**226 - 1 =>
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn => B ; 2**256 - 1 =>
$ => A :XOR,MSTORE(SP-1), JMP(readCode) ; [ NOT a => SP]

/**
Expand Down
31 changes: 15 additions & 16 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ opJUMP:
GAS - %GAS_MID_STEP => GAS :JMPN(outOfGas)

; Check PC is a JUMPDEST
$ => B :MLOAD(SP), JMP(checkJumpDest); [counter => PC]
$ => A :MLOAD(SP), JMP(checkJumpDest); [counter => PC]
/**
* @link [https://www.evm.codes/#57?fork=berlin]
* @zk-counters
Expand All @@ -41,51 +41,50 @@ opJUMPI:
0 => A
$ :EQ, JMPC(readCode)
; Check PC is a JUMPDEST
$ => B :MLOAD(SP+1), JMP(checkJumpDest); [counter => PC]
$ => A :MLOAD(SP+1), JMP(checkJumpDest); [counter => PC]

checkJumpDest:
; if it is a deploy we have to check the data from the calldata (not the bytecode)
$ => A :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment)
$ => A :MLOAD(bytecodeLength)
$ :LT,JMPC(invalidJump)
$ => B :MLOAD(isCreateContract), JMPNZ(checkJumpDestDeployment)
$ => B :MLOAD(bytecodeLength)
$ :LT,JMPNC(invalidJump)
A => B
B => PC
B => HASHPOS
; get hashP address pointer where contract bytecode is stored
$ => E :MLOAD(contractHashId)
$ => A :HASHP1(E)
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode, invalidJump)
A - 0x5B :JMPZ(readCode, invalidJump)

checkJumpDestDeployment:
; check jumpDest from calldata
$ => A :MLOAD(isCreate), JMPNZ(checkJumpDestDeploymentCreate)
$ => B :MLOAD(isCreate), JMPNZ(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
B => PC
; add PC to data starts to point the bytes to read for the jumpdest
A => PC
HASHPOS + PC => HASHPOS
; get memory pointer for hashing
$ => E :MLOAD(batchHashDataId)
; set number of bytes to hashK at D
; set number of bytes to hashK
$ => A :HASHK1(E)
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode, invalidJump)
A - 0x5B :JMPZ(readCode, invalidJump)

checkJumpDestDeploymentCreate:
; 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
B => PC
A => PC
E + PC => E
; set bytes to read from memory at C
1 => C :CALL(MLOADX); in: [E: offset, C: length] out: [A: value, E: new offset]
$ => CTX :MLOAD(currentCTX)
31 => D :CALL(SHRarith); in: [A: value, D: #bytes to right shift] out: [A: shifted result]
; check if is a jumpDest (0x5B)
0x5B => B
0x5b00000000000000000000000000000000000000000000000000000000000000n => B
$ :EQ, JMPC(readCode, invalidJump)

/**
Expand Down
2 changes: 1 addition & 1 deletion main/opcodes/logs.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ opLOG0:
; store number of topics
0 :MSTORE(numTopics)
; calculate data size gas cost => lastMemLength * 8
8 :MSTORE(arithA)
%LOG_DATA_GAS :MSTORE(arithA)
C :MSTORE(arithB), CALL(mulARITH); in: [arithA, arithB] out: [arithRes1: arithA*arithB]
$ => B :MLOAD(arithRes1)
GAS => A
Expand Down
4 changes: 2 additions & 2 deletions main/opcodes/stack-operations.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ opAuxPUSHBloop:
HASHPOS + PC => HASHPOS
PC + 1 => PC
; get memory pointer for hashing
1 => D
$ => E :MLOAD(batchHashDataId)
$ => B :HASHK(E)
$ => B :HASHK1(E)
; accumulate hash value
A + B => A
; decrease length for loop
C - 1 => C :JMPN(opAuxPUSHBend)
1 => D
:CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
:JMP(opAuxPUSHBloop)

Expand Down
6 changes: 3 additions & 3 deletions main/opcodes/storage-memory.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ opMLOAD:
B :MSTORE(bytesToStore)
; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset), CALL(MSTORE32); in: [bytesToStore, E: offset] out: [A: value]
; store lastMemOffset for memory expansion gas cost
; store lastMemLength for memory expansion gas cost
32 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
:JMP(readCode)

Expand Down Expand Up @@ -252,7 +252,7 @@ opSSTOREdifAB:
$ => A :EQ, JMPNC(opSSTOREdifA2)
; if orig_val == 0
$ => A :MLOAD(gasRefund)
A + 19900 => A :MSTORE(gasRefund), JMP(opSSTOREend)
A + %SSTORE_SET_GAS_RED => A :MSTORE(gasRefund), JMP(opSSTOREend)

opSSTOREdifA1:
; if orig_val != 0
Expand All @@ -277,7 +277,7 @@ opSSTOREdifA2:
; if orig_val != 0
; compute gas refund
$ => A :MLOAD(gasRefund)
A + 2800 => A :MSTORE(gasRefund), JMP(opSSTOREend)
A + %SSTORE_RESET_GAS_RED => A :MSTORE(gasRefund), JMP(opSSTOREend)

opSSTOREdifB:
; if orig_val != 0
Expand Down
1 change: 0 additions & 1 deletion main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ MSTORE32:
B :MSTORE(tmpVarBmstore)
C :MSTORE(tmpVarCmstore)
D :MSTORE(tmpVarDmstore)
E :MSTORE(tmpVarEmstore)
; check offset is lower than max memory
E => A
%MEMORY_LENGTH => B
Expand Down

0 comments on commit bc0ecc8

Please sign in to comment.