Skip to content

Commit

Permalink
change MEM_ALIGN_WR8 to MEM_ALIGN_WR
Browse files Browse the repository at this point in the history
  • Loading branch information
zkronos73 committed Mar 24, 2024
1 parent a7f9a51 commit c0412f7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/opcode-cost-zk-counters.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ counters = SLOAD
### CODECOPY
L = bytes to copy.
if is createContract -> counters = CALLDATACOPY
else -> counters = LT + L*(LT + MEM_ALIGN_WR8)
else -> counters = LT + L*(LT + MEM_ALIGN_WR)

### EXTCODESIZE
counters = SLOAD

### EXTCODECOPY
L = bytes to copy.
if is createContract -> counters = CALLDATACOPY
else -> counters = LT + L*(LT + MEM_ALIGN_WR8)
else -> counters = LT + L*(LT + MEM_ALIGN_WR)

### RETURNDATACOPY
L = Length to copy.
Expand Down Expand Up @@ -250,7 +250,7 @@ counters = 2*EQ + LT*2 + computeGasSendCall + copySP
| ARITH | 1 | 0 | 0 | 0 | 0 | 0 | false |
| SLOAD | 0 | 0 | 0 | 0 | 0 | 11 | [true](#SLOAD) |
| SSTORE | 0 | 0 | 0 | 0 | 0 | 11 | [true](#SSTORE) |
| MEM_ALIGN_WR8 | 0 | 0 | 1 | 0 | 0 | 0 | false |
| MEM_ALIGN_WR | 0 | 0 | 1 | 0 | 0 | 0 | false |


## Dynamic regs
Expand Down
5 changes: 5 additions & 0 deletions main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ CONST %CHANGE_L2_BLOCK_TX_TYPE = 0x0b
CONST %DELTA_TIMESTAMP_NUM_BYTES = 4
CONST %INDEX_L1INFOTREE_NUM_BYTES = 4
CONST %TX_TYPE_NUM_BYTES = 1

; CONSTANTS MEM_ALIGN
CONST %MEM_ALIGN_LEN = 2**7
CONST %MEM_ALIGN_LEFT_ALIGNMENT = 2**13
CONST %MEM_ALIGN_LITTLE_ENDIAN = 2**14
6 changes: 4 additions & 2 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ opCODECOPYloop:
$ => A :MLOAD(MEM:E)
A :MSTORE(previousValue), CALL(opCODECOPYLoadBytes); in:[codecopyBytecodeLength, codecopyHashId] out: [B: readByte]
$ => A :MLOAD(previousValue)
${memAlignWR8_W0(A,B,C)} => D ; no trust calculate W0
B :MEM_ALIGN_WR8 ; only use LSB of B, rest of bytes could be non zero
C + 1 * %MEM_ALIGN_LEN => C
${memAlignWR_W0(A,B,C)} => D ; no trust calculate W0
B => E ; B amb E must be same value
B :MEM_ALIGN_WR ; only use LSB of B, rest of bytes could be non zero
$ => E :MLOAD(memInteger)
D :MSTORE(MEM:E) ; write W0
; update vars loop
Expand Down
14 changes: 9 additions & 5 deletions main/opcodes/storage-memory.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ opMSTORE8:
; store lastMemLength for memory expansion gas cost. In case of MSTORE8, always 1 byte
1 :MSTORE(lastMemLength), CALL(saveMem); in: [lastMemOffset, lastMemLength]
B => A :CALL(offsetUtil); in: [A: offset] out: [E: offset/32, C: offset%32]
$ => B :MLOAD(SP); [value => B]
$ => RCX :MLOAD(SP); [value => B]
; read from memory position E
$ => A :MLOAD(MEM:E)
${memAlignWR8_W0(A,B,C)} => D ; no trust calculate W0
B :MEM_ALIGN_WR8 ; only use LSB of B, rest of bytes could be non zero.
; write at memory position E
D :MSTORE(MEM:E), JMP(readCode)

; C contains offset, setting len = 1 byte
C + 1 * %MEM_ALIGN_LEN => C

; no trust calculate W0 but if verification fails, proof no generated
; B,E no change because only one byte, but B must be equal to E
${memAlignWR_W0(A,B,C)} => D,B,E :MSTORE(MEM:E)
RCX :MEM_ALIGN_WR, JMP(readCode) ; only use LSB of B, rest of bytes could be non zero.

/**
* @link [https://www.evm.codes/#59?fork=berlin]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"url": "https://github.com/0xPolygonHermez/zkevm-rom.git"
},
"dependencies": {
"@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#develop-feijoa",
"@0xpolygonhermez/zkasmcom": "https://github.com/0xPolygonHermez/zkasmcom.git#feature/remove-mem-align-wr8",
"yargs": "^17.5.1"
},
"devDependencies": {
Expand Down

0 comments on commit c0412f7

Please sign in to comment.