Skip to content

Commit

Permalink
add HASHK1 & HASHP1
Browse files Browse the repository at this point in the history
  • Loading branch information
krlosMata committed Dec 13, 2022
1 parent 415e73a commit 6af5c17
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 41 deletions.
7 changes: 2 additions & 5 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,9 @@ readZero:
0 => B
:RETURN
readValueBytecode:
1 => D
$ => E :MLOAD(codecopyHashId)
; read value to write in memory
$ => B :HASHP(E)
$ => B :HASHP1(E)
:RETURN

/**
Expand Down Expand Up @@ -543,14 +542,12 @@ opEXTCODECOPYCheckHash:
:JMP(opEXTCODECOPYCheckHashLoop)

; @info bytes are inserted byte by byte
; TODO: it could be improved by computing how many 32 bytes slots are needed
opEXTCODECOPYCheckHashLoop:
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
; finish reading bytecode
B - 1 :JMPN(opEXTCODECOPYCheckHashLoopEnd)
1 => D
; add bytes to hash contract bytecode
${getBytecode(A, HASHPOS, 1)} :HASHP(E)
${getBytecode(A, HASHPOS, 1)} :HASHP1(E)
B - 1 => B
:JMP(opEXTCODECOPYCheckHashLoop)

Expand Down
9 changes: 3 additions & 6 deletions main/opcodes/flow-control.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ checkJumpDest:
$ :LT,JMPC(invalidJump)
B => PC
B => HASHPOS
; set number of bytes to hashP at D
1 => D
; get hashP address pointer where contract bytecode is stored
$ => E :MLOAD(contractHashId)
$ => A :HASHP(E)
$ => A :HASHP1(E)
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode)
Expand All @@ -71,14 +69,13 @@ checkJumpDestDeployment:
-A :JMPN(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
; add PC to data starts to point the bytes to read for the push
B => PC
HASHPOS + PC => HASHPOS
; get memory pointer for hashing
$ => E :MLOAD(batchHashDataId)
; set number of bytes to hashK at D
1 => D
$ => A :HASHK(E)
$ => A :HASHK1(E)
; check if is a jumpDest (0x5B)
0x5B => B
$ :EQ, JMPC(readCode)
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 @@ -245,13 +245,13 @@ opAuxPUSHBloop:
%MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary)
%MAX_CNT_ARITH - CNT_ARITH - 4 :JMPN(outOfCountersArith)

1 => D
; 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
; add PC to data starts to point the bytes to read for the push
HASHPOS + PC => HASHPOS
PC + 1 => PC
; get memory pointer for hashing
1 => D
$ => E :MLOAD(batchHashDataId)
$ => B :HASHK(E)
; accumulate hash value
Expand Down
33 changes: 12 additions & 21 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ loopBytes:
%MAX_CNT_STEPS - STEP - 10 :JMPN(outOfCountersStep)
A - C - 1 :JMPN(endCalldataIntrinsicGas)
E => B
1 => D
$ => E :MLOAD(batchHashDataId)
$ => D :HASHK(E)
$ => D :HASHK1(E)
B => E
C + 1 => C
D - 1 :JMPN(add4Gas)
Expand Down Expand Up @@ -232,34 +231,30 @@ getContractAddress:
0x80 => B
$ :LT,JMPC(nonce1byte)
$ => C :MLOAD(lengthNonce)
1 => D
; 1 byte length address + 20 bytes address + 1 byte length nonce + C bytes nonce
0xc0 + 22 + C :HASHK(E)
0x94 :HASHK(E)
0xc0 + 22 + C :HASHK1(E)
0x94 :HASHK1(E)
20 => D
$ => B :MLOAD(txSrcAddr)
B :HASHK(E)
1 => D
0x80 + C :HASHK(E)
0x80 + C :HASHK1(E)
C => D
A :HASHK(E)
:JMP(endContractAddress)

nonce1byte:
$ => A :MLOAD(txSrcAddr)
$ => B :MLOAD(txNonce)
1 => D
0xc0 + 22 :HASHK(E)
0x94 :HASHK(E)
0xc0 + 22 :HASHK1(E)
0x94 :HASHK1(E)
20 => D
A :HASHK(E)
1 => D
B - 1 :JMPN(nonceIs0)
B :HASHK(E)
B :HASHK1(E)
:JMP(endContractAddress)

nonceIs0:
0x80 :HASHK(E)
0x80 :HASHK1(E)
:JMP(endContractAddress)

;; compute new contract address as CREATE2 spec: keccak256(0xff ++ address ++ salt ++ keccak256(init_code))[12:] (https://eips.ethereum.org/EIPS/eip-1014)
Expand Down Expand Up @@ -312,8 +307,7 @@ create2end:
0 => HASHPOS
$ => E :MLOAD(lastHashKIdUsed)
E+1 => E :MSTORE(lastHashKIdUsed)
1 => D
0xff :HASHK(E)
0xff :HASHK1(E)
20 => D
$ => A :MLOAD(txSrcAddr)
A :HASHK(E)
Expand Down Expand Up @@ -376,8 +370,7 @@ readDeployBytecode:
$ => HASHPOS :MLOAD(dataStarts)
HASHPOS + PC => HASHPOS
$ => E :MLOAD(batchHashDataId)
1 => D
$ => RR :HASHK(E)
$ => RR :HASHK1(E)
${eventLog(onOpcode(RR))}
PC + 1 => PC
:JMP(@mapping_opcodes + RR)
Expand Down Expand Up @@ -446,7 +439,6 @@ callContract:
$ => A :SLOAD
A :MSTORE(hashContractTxDestAddr)
0 => HASHPOS
1 => D
$ => B :MLOAD(bytecodeLength)

; get a new hashPId
Expand All @@ -459,7 +451,7 @@ callContract:

; set repeat itarations: RCX register + 1
B - 1 => RCX :JMPN(checkHashBytecodeEnd)
${getBytecode(A, HASHPOS, 1)} :HASHP(E), REPEAT(RCX) ; hash contract bytecode
${getBytecode(A, HASHPOS, 1)} :HASHP1(E), REPEAT(RCX) ; hash contract bytecode

checkHashBytecodeEnd:
HASHPOS :HASHPLEN(E)
Expand All @@ -476,8 +468,7 @@ readByteCode:
$ => B :MLOAD(bytecodeLength)
B - PC - 1 :JMPN(defaultOpCode) ; no bytecode treated as 0x00
PC => HASHPOS
1 => D
$ => RR :HASHP(E)
$ => RR :HASHP1(E)
${eventLog(onOpcode(RR))}
PC + 1 => PC
:JMP(@mapping_opcodes + RR)
Expand Down
12 changes: 5 additions & 7 deletions main/utils.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -854,19 +854,18 @@ readPushBlock:
E :MLOAD(contractHashId) ; load hash contract identifier

; read bytes
1 => D
$ => C :HASHP(E)
$ => C :HASHP1(E)
HASHPOS - 2 => HASHPOS

$ => A :HASHP(E)
$ => A :HASHP1(E)
HASHPOS - 2 => HASHPOS
A*256 + C => C

$ => A :HASHP(E)
$ => A :HASHP1(E)
HASHPOS - 2 => HASHPOS
A*65536 + C => C

$ => A :HASHP(E)
$ => A :HASHP1(E)
HASHPOS - 2 => HASHPOS
A*16777216 + C => C

Expand Down Expand Up @@ -906,8 +905,7 @@ endPushLoop:
$ => A :MLOAD(leftBytes)
$ :EQ, JMPC(endPushFinal)

1 => D
$ => D :HASHP(E)
$ => D :HASHP1(E)
HASHPOS - 2 => HASHPOS
B - 1 => A
:JMP(computeFactorLoop)
Expand Down

0 comments on commit 6af5c17

Please sign in to comment.