Skip to content

Commit

Permalink
Reg fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Feb 9, 2024
1 parent aba71da commit dfeb414
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
15 changes: 9 additions & 6 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ opEXTCODECOPY:
$ => D :MLOAD(SP+1); [offset => D]
$ => E :MLOAD(SP); [size => E]

; store lastMemOffset for memory expansion gas cost
C :MSTORE(lastMemOffset)
; store lastMemOffset for memory expansion gas cost, we store also at B to recover later
C => B :MSTORE(lastMemOffset)

; store lastMemLength for memory expansion gas cost
; store lastMemLength for memory expansion gas cost, we store also at RCX to recover later
; compute memory expansion gas cost
E :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
E => RCX :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]

; check out-of-gas
;${3*((E+31)/32)}
Expand All @@ -401,6 +401,8 @@ opEXTCODECOPY:
; 3*((E+31)/32)
; check out-of-gas
GAS - 3 * E => GAS :JMPN(outOfGas)
RCX => E
B => C
; if offset is above data len, length => offset
D => A
$ => B :MLOAD(tmpContractLength)
Expand Down Expand Up @@ -504,8 +506,8 @@ opRETURNDATACOPY:
$ => C :MLOAD(SP); [size => C]
; store lastMemOffset for memory expansion gas cost
D :MSTORE(lastMemOffset)
; store lastMemLength for memory expansion gas cost
C :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
; store lastMemLength for memory expansion gas cost, we store also at RCX to recover later
C => RCX :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
; if retDataCTX is 0, end opcode execution
$ => B :MLOAD(retDataCTX), JMPZ(opRETURNDATACOPYEmpty)
; Load ret data length from last ctx
Expand All @@ -527,6 +529,7 @@ opRETURNDATACOPY:
; C is secured to be less than 32 bits after calling saveMem
;(C+31)/32
C + 31 => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
RCX => C
; 3*((C+31)/32)
; check out-of-gas
GAS - 3 * E => GAS :JMPN(outOfGas)
Expand Down
7 changes: 5 additions & 2 deletions main/opcodes/crypto.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ opSHA3:
; check out-of-gas
GAS - %KECCAK_GAS => GAS :JMPN(outOfGas)
SP - 1 => SP
$ => E :MLOAD(SP--); [offset => E]
$ => C :MLOAD(SP) ; [size => C]
$ => E, D :MLOAD(SP--); [offset => E]
$ => C, B :MLOAD(SP) ; [size => C]

; store lastMemOffset for memory expansion gas cost
E :MSTORE(lastMemOffset)
Expand All @@ -34,6 +34,9 @@ opSHA3:
; E is less than 32 bits because C is less than 32 bits and E = C/32
; 6*((C+31)/32)
GAS - 6 * E => GAS :JMPN(outOfGas) ; dynamic_gas = 6 * minimum_word_size + memory_expansion_cost
; Recover offset and size at E and C
D => E
B => C
; check keccak counters
C + 1 :MSTORE(arithA)
136 :MSTORE(arithB), CALL(divARITH); in: [arithA, arithB] out: [arithRes1: arithA/arithB, arithRes2: arithA%arithB]
Expand Down
3 changes: 2 additions & 1 deletion main/precompiled/identity.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ IDENTITY:
$ :LT, JMPC(moveBalances)

GAS - %IDENTITY_GAS => GAS :JMPN(outOfGas)
$ => C :MLOAD(txCalldataLen)
$ => C, D :MLOAD(txCalldataLen)
;(C+31)/32 => A
C + 31 => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
D => C
GAS - %IDENTITY_WORD_GAS*E => GAS :JMPN(outOfGas)
0 => E, D :MSTORE(retDataOffset)
C :MSTORE(retDataLength)
Expand Down
4 changes: 2 additions & 2 deletions main/precompiled/pre-sha2-256.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ funcSHA256:
; GAS - staticGas
GAS - %SHA2_256_GAS => GAS :JMPN(outOfGas)

$ => C :MLOAD(txCalldataLen)
$ => C, D :MLOAD(txCalldataLen)

;words => A === (C+31)/32 => A
C + 31 => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]

D => C
; GAS - dynamicGas
GAS - %SHA2_256_WORD_GAS*E => GAS :JMPN(outOfGas)

Expand Down
2 changes: 1 addition & 1 deletion main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,10 @@ SHRarithBit:
E :MSTORE(tmpVarESHX)

SHRarithinit:
A => E :MSTORE(arithA)
0 => B
; if A == 0 --> no shift
$ :EQ,JMPC(SHRarithfinal)
A => E :MSTORE(arithA)
; B bits
D => B
255 => A
Expand Down

0 comments on commit dfeb414

Please sign in to comment.