Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize constants, changel2 utils and rlp #384

Open
wants to merge 3 commits into
base: develop-feijoa
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 2 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 @@ -144,6 +140,7 @@ CONST %INDEX_L1INFOTREE_NUM_BYTES = 4
CONST %TX_TYPE_NUM_BYTES = 1

; CONSTANTS MEM_ALIGN
CONST %MEM_ALIGN_LEN = 2**7
CONST %MEM_ALIGN_SIZE = 2**7
CONST %MEM_ALIGN_OFFSET = 1
CONST %MEM_ALIGN_LEFT_ALIGNMENT = 2**13
CONST %MEM_ALIGN_LITTLE_ENDIAN = 2**14
2 changes: 1 addition & 1 deletion main/ecrecover/dblScalarMulSecp256k1.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ dblScalarMulSecp256k1_loop_fi:
; high_bit(k1) == 0 high_bit(k2) == ??
dblScalarMulSecp256k1_k10_fi:
; Receive the next MSB bit of k2
$0{(mem.dblScalarMulSecp256k1_k2) >> RR & 0x1} :JMPZ(failAssert, @dblScalarMulSecp256k1_scalar_table_k10_k21 + RR)
$0{(mem.dblScalarMulSecp256k1_k2) >> RR & 0x1} :JMPZ(failAssertEcrecover, @dblScalarMulSecp256k1_scalar_table_k10_k21 + RR)
; ------------------------------


Expand Down
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
27 changes: 13 additions & 14 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 All @@ -40,7 +38,6 @@ addHashTxByteByByte:
$ - HASHPOS - D :F_MLOAD(txRLPLength), JMPN(invalidTxRLP)
$ => B :MLOAD(batchL2DataParsed)
; Save current registers
; CHECK: Is correct to save here in same line as JMPN?
$ - B - C - D :F_MLOAD(batchL2DataLength), JMPN(invalidTxRLP), SAVE(B,C,D,E,RR,RCX)
$ => HASHPOS :MLOAD(batchHashPos)
D => E
Expand Down Expand Up @@ -176,6 +173,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 +184,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 +203,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
17 changes: 10 additions & 7 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 @@ -63,7 +61,7 @@ loadTx_rlp_continue:
A - 0xc1 :JMPN(invalidTxRLP)
A - 0xf8 :JMPN(shortList)
; do not allow lists over 2**24 bytes length
; Transaction could not have more than 120.000 due to smart contract limitation (keccaks counters)
; Transaction could not have more than 4096*31 bytes due to smart contract limitation (keccaks counters)
; meaning that the RLP encoding is wrong
A - 0xfb :JMPN(longList, invalidTxRLP)

Expand Down Expand Up @@ -199,7 +197,7 @@ dataREAD:
A - 0x81 :JMPN(zeroBytesData)
A - 0xb8 :JMPN(shortData)
; do not allow string over 2**24 bytes length
; Transaction could not have more than 120.000 due to smart contract limitation (keccaks counters)
; Transaction could not have more than 4096*31 bytes due to smart contract limitation (keccaks counters)
; meaning that the RLP encoding is wrong
A - 0xbb :JMPN(longData, invalidTxRLP)

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 All @@ -270,7 +267,7 @@ zeroBytesData:

endData:
; Check all bytes read to detect pre EIP-155 tx, if bytes read are the same as txLength, we reached the end, so it's a pre EIP-155 tx
; txRLPLength and C is at most 120.000 bytes, no need to use a binary for comparison
; txRLPLength and C is at most 4096*31 bytes, no need to use a binary for comparison
$ => B :MLOAD(txRLPLength)
C - B :JMPZ(setPreEIP155Flag)

Expand Down Expand Up @@ -305,7 +302,7 @@ setPreEIP155Flag:
;; size verification
; checks RLP length read at the RLP header with bytes read during RLP parsing
sizeVerification:
; txRLPLength and C is at most 120.000 bytes, no need to use a binary for comparison
; txRLPLength and C is at most 4096*31 bytes, no need to use a binary for comparison
$ => B :MLOAD(txRLPLength)
C - B :JMPZ(sizeVerificationSuccess, invalidTxRLP)
sizeVerificationSuccess:
Expand All @@ -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
Loading