Skip to content

Commit

Permalink
Optimize constants and changel2 utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Apr 26, 2024
1 parent e8f363e commit 3bde3cb
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# npm run test:counters
- name: run zkasm tests
run: |
npm run test:zkasm
# npm run test:zkasm
- name: Generate tests
run: |
node tools/parallel-testing/gen-parallel-tests.js --skipVCounters
Expand Down
9 changes: 4 additions & 5 deletions main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ CONST %TX_GAS_LIMIT = 30000000
CONSTL %BLOCK_GAS_LIMIT = 2**50
CONST %MAX_MEM_EXPANSION_BYTES = 0x3fffe0
CONST %FORK_ID = 10
CONST %L1INFO_TREE_LEVELS = 32
CONST %CALLDATA_RESERVED_CTX = 1
CONSTL %FOUR_GOLDILOCKS = 0xffffffff00000001ffffffff00000001ffffffff00000001ffffffff00000001n

Expand Down Expand Up @@ -126,13 +125,10 @@ CONST %MAX_CNT_POSEIDON_G = %MAX_CNT_POSEIDON_G_LIMIT - (%MAX_CNT_POSEIDON_G_LIM
CONST %MAX_CNT_SHA256_F = %MAX_CNT_SHA256_F_LIMIT - (%MAX_CNT_SHA256_F_LIMIT / %SAFE_RANGE)
CONST %MAX_CNT_POSEIDON_SLOAD_SSTORE = 518

CONST %MIN_CNT_KECCAK_BATCH = 1 ; minimum necessary keccaks to compute global hash

; ETHEREUM CONSTANTS
CONSTL %MAX_NONCE = 0xffffffffffffffffn
CONSTL %MAX_UINT_256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn
CONST %CODE_SIZE_LIMIT = 0x6000
CONST %BYTECODE_STARTS_EF = 0xEF
CONST %MAX_SIZE_MODEXP = 1024
CONST %MAX_GAS_WORD_MODEXP = 9487
CONSTL %MAX_GAS_IT_MODEXP = 90000000 ; %TX_GAS_LIMIT * 3
Expand All @@ -145,5 +141,8 @@ CONST %TX_TYPE_NUM_BYTES = 1

; CONSTANTS MEM_ALIGN
CONST %MEM_ALIGN_LEN = 2**7
CONST %MEM_ALIGN_OFFSET = 1
CONST %MEM_ALIGN_LEFT_ALIGNMENT = 2**13
CONST %MEM_ALIGN_LITTLE_ENDIAN = 2**14
CONST %MEM_ALIGN_LITTLE_ENDIAN = 2**14

; CHECK not used constants and vars
25 changes: 7 additions & 18 deletions main/l2-tx-hash.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
;; [ 20 bytes ] from
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


VAR CTX l2TxHashPointer ; Pointer to l2TxHash hash address
VAR CTX l2HASHP ; pointer to the l2TxHash to store the bytes
VAR GLOBAL tmpVar_HASHPOS_L2HashTx ; temporary variable register HASHPOS
Expand Down Expand Up @@ -97,9 +96,7 @@ addL2HashTx_isNotDeploy:
;; Write 3 bytes to l2TxHash: data length
addL2HashTx_dataLength:
; store temporary register values
A :MSTORE(tmpVar_A_L2HashTx)
D :MSTORE(tmpVar_D_L2HashTx)
E :MSTORE(tmpVar_E_L2HashTx)
A :SAVE(B,C,D,E,RCX,RR)
HASHPOS :MSTORE(tmpVar_HASHPOS_L2HashTx)

; load pointer l2HashTx and write data length
Expand All @@ -109,18 +106,13 @@ addL2HashTx_dataLength:
${mem.txCalldataLen} :HASHP(E), MLOAD(txCalldataLen)
HASHPOS :MSTORE(l2HASHP)

; load temporary register values
$ => A :MLOAD(tmpVar_A_L2HashTx)
$ => D :MLOAD(tmpVar_D_L2HashTx)
$ => E :MLOAD(tmpVar_E_L2HashTx)
$ => HASHPOS :MLOAD(tmpVar_HASHPOS_L2HashTx), RETURN
$ => HASHPOS :MLOAD(tmpVar_HASHPOS_L2HashTx)
$ => A :RESTORE, RETURN

;; Write 1 byte to l2TxHash: txType
; note: HASHPOS is not recovered and the outcome register is the l2TxHash length
addL2HashTx_txType:
; store temporary register values
A :MSTORE(tmpVar_A_L2HashTx)
E :MSTORE(tmpVar_E_L2HashTx)
A :SAVE(B,C,D,E,RCX,RR)
HASHPOS :MSTORE(tmpVar_HASHPOS_L2HashTx)

; load pointer l2HashTx and write txType
Expand All @@ -132,10 +124,8 @@ addL2HashTx_txType:
addL2HashTx_txType_write_1:
1 :HASHP1(E)
addL2HashTx_txType_finish:
; load temporary register values
$ => A :MLOAD(tmpVar_A_L2HashTx)
$ => E :MLOAD(tmpVar_E_L2HashTx)
$ => HASHPOS :MLOAD(tmpVar_HASHPOS_L2HashTx), RETURN
$ => HASHPOS :MLOAD(tmpVar_HASHPOS_L2HashTx)
$ => A :RESTORE, RETURN

;; Closes l2TxHash and store the result
closeL2TxHash:
Expand All @@ -148,8 +138,7 @@ closeL2TxHash:
$ => E :MLOAD(l2TxHashPointer)
HASHPOS :HASHPLEN(E)
; digest l2TxHash
$ => E :HASHPDIGEST(E)
E :MSTORE(l2TxHash)
$ => E :HASHPDIGEST(E), MSTORE(l2TxHash)

; load temporary register values
$ => E :MLOAD(tmpVar_E_L2HashTx)
Expand Down
9 changes: 3 additions & 6 deletions main/load-change-l2-block-utils.zkasm
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
;; get D bytes from transaction bytes
;@in E: batchHashDataPointer
;@in D: number of bytes to get
;@in C: current data parsed pointer
;@in HASHPOS: batchHashPos
;@out A: D bytes from batch data at offset C
getChangeL2TxBytes:
$ => B :MLOAD(batchL2DataParsed)
$ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidDecodeChangeL2Block)
$ => E :MLOAD(batchHashDataPointer)
$ => HASHPOS :MLOAD(batchHashPos)
$ => A :HASHP(E)
HASHPOS :MSTORE(batchHashPos)
C => HASHPOS
$ => E :MLOAD(txHashPointer), RETURN
$ => A :HASHP(E), RETURN
6 changes: 4 additions & 2 deletions main/load-change-l2-block.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ INCLUDE "load-change-l2-block-utils.zkasm"
decodeChangeL2BlockTx:
; No changeL2BlockTx allowed at forced batches
$ :MLOAD(isForced), JMPNZ(invalidDecodeChangeL2Block)

$ => E :MLOAD(batchHashDataPointer)
$ => HASHPOS :MLOAD(batchHashPos)
; Decode deltaTimestamp / 4 bytes
%DELTA_TIMESTAMP_NUM_BYTES => D :CALL(getChangeL2TxBytes)
C + D => C
Expand All @@ -19,7 +20,8 @@ decodeChangeL2BlockTx:
C + D => C
A :MSTORE(indexL1InfoTree)
1 :MSTORE(isChangeL2BlockTx)

; update batchHashPos
HASHPOS :MSTORE(batchHashPos)
; update bytes parsed
$ => A :MLOAD(batchL2DataParsed)
A + C :MSTORE(batchL2DataParsed)
Expand Down
26 changes: 13 additions & 13 deletions main/load-tx-rlp-utils.zkasm
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
;; get D bytes from batchL2Data
;@in D: number of bytes to get
;@in C: current data parsed pointer
;@in E: batchHashDataPointer
;@in HASHPOS: batchHashPos
;@out A: D bytes from batchL2Data at offset C
getBatchL2DataBytes:
$ => B :MLOAD(batchL2DataParsed)
$ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidTxRLP)
$ => E :MLOAD(batchHashDataPointer)
$ => HASHPOS :MLOAD(batchHashPos)
$ => A :HASHP(E)
HASHPOS :MSTORE(batchHashPos)
C => HASHPOS
$ => E :MLOAD(txHashPointer), RETURN
$ => A :HASHP(E), RETURN

;; Add bytes to generate ethereum signed message
;; - legacy transaction: signedMessage = H_keccak(rlp(nonce, gasprice, gaslimit, to, value, data, chainId, 0, 0))
;; - pre EIP-155: signedMessage = H_keccak(rlp(nonce, gasprice, gaslimit, to, value, data))
; REVIEW: is it necessary?? (first two steps)
; @in D: number of bytes to add
; @in C: current tx data parsed pointer
addHashTx:
$ - HASHPOS - D :F_MLOAD(txRLPLength), JMPN(invalidTxRLP)
addHashTxBegin:
Expand Down Expand Up @@ -176,6 +174,9 @@ readHashBytes:
B => E :JMP(@_readHashBaseTable - E)

;; Check short value is over 127. Error RLP: single byte < 0x80 are not prefixed
; add inputs/outputs at all this file
;@in D: length of value to check, should be 1
;@in A: value to check
checkShortRLP:
D - 1 :JMPNZ(skipCheckShort)
A - %MIN_VALUE_SHORT :JMPN(invalidTxRLP)
Expand All @@ -184,11 +185,13 @@ skipCheckShort:
:RETURN

;; Check long list/value is over 55 bytes long. Error RLP: encoded list too short
;@in A: length of value to check
checkLongRLP:
A - %MIN_BYTES_LONG :JMPN(invalidTxRLP)
:RETURN

;; Check short value is over 127. Error RLP: single byte < 0x80 are not prefixed
;@in A: value to check
checkShortDataRLP:
$ => B :MLOAD(txCalldataLen)
B - 1 :JMPNZ(skipCheckShortData)
Expand All @@ -201,11 +204,8 @@ skipCheckShortData:
VAR GLOBAL tmpVarACheckNonLeadingZeros
VAR GLOBAL tmpVarZkPCcheckNonLeadingZeros
checkNonLeadingZeros:
RR :MSTORE(tmpVarZkPCcheckNonLeadingZeros)
A :MSTORE(tmpVarACheckNonLeadingZeros)
; set value to B and get its
A => B :CALL(getLenBytes) ; in: [B: number] out: [A: byte length of B]
; set value to B and get its length
A => B :SAVE(B,C,D,E,RR,RCX), CALL(getLenBytes) ; in: [B: number] out: [A: byte length of B]
; check (bytes length - encoded length) are not equal
D - A :JMPNZ(invalidTxRLP)
$ => RR :MLOAD(tmpVarZkPCcheckNonLeadingZeros)
$ => A :MLOAD(tmpVarACheckNonLeadingZeros), RETURN
$ => A :RESTORE, RETURN
9 changes: 6 additions & 3 deletions main/load-tx-rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ INCLUDE "l2-tx-hash.zkasm"
;;;;;;;;;;;;;;;;;;

loadTx_rlp:
; check one keccak is available to begin processing the RLP
%MAX_CNT_KECCAK_F - CNT_KECCAK_F - 1 :JMPN(outOfCountersKeccak)

; Pointer to next RLP bytes to read
0 => C
Expand Down Expand Up @@ -251,7 +249,6 @@ readDataFinal:
B - 1 :JMPN(endData)
B => D :CALL(addHashTxByteByByte)
:CALL(addL2HashTx)
; WARNING: check checkShortDataRLP correctness
:CALL(checkShortDataRLP)
32 - D => D :CALL(SHLarith); in: [A: value, D: #bytes to left shift] out: [A: shifted result]
$ => E :MLOAD(globalCalldataMemoryOffset)
Expand Down Expand Up @@ -318,6 +315,8 @@ sizeVerificationSuccess:

;; read ecdsa 'r'
rREADTx:
$ => E :MLOAD(batchHashDataPointer)
$ => HASHPOS :MLOAD(batchHashPos)
32 => D :CALL(getBatchL2DataBytes)
A :MSTORE(txR)
C + D => C
Expand All @@ -339,6 +338,8 @@ effectivePercentageTx:
1 => D :CALL(getBatchL2DataBytes)
A :MSTORE(effectivePercentageRLP)
C + D => C
; Update batch hashPos and restore HASHPOS
HASHPOS :MSTORE(batchHashPos)

;;;;;;;;;
;; D - Finish RLP parsing
Expand All @@ -351,6 +352,8 @@ finishLoadRLP:
$ => A :MLOAD(pendingTxs)
A + 1 :MSTORE(pendingTxs)
;; compute signature
$ => E :MLOAD(txHashPointer)
C => HASHPOS
$ => A :HASHKDIGEST(E)
A :MSTORE(txHash)

Expand Down
Loading

0 comments on commit 3bde3cb

Please sign in to comment.