Skip to content

Commit

Permalink
update SHR/SHL arith
Browse files Browse the repository at this point in the history
  • Loading branch information
laisolizq committed May 18, 2022
1 parent a0cfd1d commit 5fb4d14
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 65 deletions.
4 changes: 2 additions & 2 deletions main/loadtx_rlp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ dataREAD:
veryShortData:
1 :MSTORE(txCalldataLen)
31 => D
${A << (D*8)} => A
:CALL(SHLarith)
A :MSTORE(SP++)
:JMP(endData)

Expand Down Expand Up @@ -310,7 +310,7 @@ readDataFinal:
C + D => C

32-D => D
${A << (D*8)} => A
:CALL(SHLarith)
A :MSTORE(SP)

endData:
Expand Down
69 changes: 50 additions & 19 deletions main/opcodes.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,8 @@ opBYTE:
$ => B :MLOAD(SP--)
$ => A :MLOAD(SP)
31 - B => D
${A >> (D*8)} => B
:CALL(SHRarith)
GAS-3 => GAS
B => A
255 => B
$ :AND,MSTORE(SP++)
:JMP(readCode)
Expand All @@ -335,7 +334,14 @@ opSHR:
SP - 1 => SP
$ => D :MLOAD(SP--)
$ => A :MLOAD(SP)
A => E
${exp(2,D)} => B
${B-1} => B
$ => C :AND
${exp(2,D)} => B
${A >> D} => A
0 => D
E :ARITH
A :MSTORE(SP++)
GAS-3 => GAS
:JMP(readCode)
Expand All @@ -344,8 +350,30 @@ opSHL:
SP - 1 => SP
$ => D :MLOAD(SP--)
$ => A :MLOAD(SP)
${A << D} => A
A :MSTORE(SP++)
${A << D} => E
256 - D => D :JMPC(SHL0)
${exp(2,D)} => B
${B-1} => B
$ => C :GT
256 - D => D
${exp(2,D)} => B
0 - C :JMPC(SHLbig)
0 => D,C
E :ARITH
:JMP(SHLfinal)
SHL0:
0 :MSTORE(SP++)
GAS-3 => GAS
:JMP(readCode)
SHLbig:
256 - D => D
${exp(2,D)} => D
${A/D} => D
0 => C
E :ARITH

SHLfinal:
E :MSTORE(SP++)
GAS-3 => GAS
:JMP(readCode)

Expand All @@ -368,7 +396,8 @@ opSAR:
$ => A :MLOAD(SP)
:CALL(abs)
; if more than 32 set to 32
${A >> (D*8)} => C
:CALL(SHRarith)
A => C
B => A
1 => B
$ => B :XOR
Expand Down Expand Up @@ -434,7 +463,7 @@ opSHA3Loop:
opSHA3Final:
:CALL(MLOADX)
32 - C => D
${A >> (D*8)} => A
:CALL(SHRarith)
$ => E :MLOAD(lastHashIdUsed)
C => D
A :HASHK(E)
Expand Down Expand Up @@ -516,10 +545,11 @@ opCALLDATALOAD2:
A => D
1024 + B => SP
$ => A :MLOAD(SP++)
${A << (D*8)} => B
:CALL(SHLarith)
A => B
32 - D => D
$ => A :MLOAD(SP)
${A >> (D*8)} => A
:CALL(SHRarith)
C => SP
B + A :MSTORE(SP++)
GAS - 3 => GAS
Expand Down Expand Up @@ -553,12 +583,13 @@ opCALLDATACOPYinit:
$ => A :MLOAD(arithRes1)
1024 + A => SP
$ => A :MLOAD(SP)
${A << (D*8)} => C
:CALL(SHLarith)
A => C
$ => A :MLOAD(arithRes1)
1024 + A + 1 => SP
32 - D => D
$ => A :MLOAD(SP)
${A >> (D*8)} => A
:CALL(SHRarith)
A + C :MSTORE(bytesToStore)
:CALL(MSTORE32)
$ => SP :MLOAD(SPw)
Expand All @@ -572,7 +603,7 @@ opCALLDATACOPYfinal:
${B%32} => D
1024 + ${B/32} => SP
$ => A :MLOAD(SP)
${A << (D*8)} => A
:CALL(SHLarith)
$ => SP :MLOAD(SPw)
$ => C :MLOAD(SP) ;length
1024 + ${B/32} + 1 => SP
Expand All @@ -581,18 +612,18 @@ opCALLDATACOPYfinal:
A => B
$ => A :MLOAD(SP)
32 - D => D
${A >> (D*8)} => A
:CALL(SHRarith)
32 - C => D
${A << (D*8)} => A
:CALL(SHLarith)
B + A => A
A :MSTORE(bytesToStore)
:CALL(MSTOREX)
:JMP(opCALLDATACOPYend)

opCALLDATACOPYxor:
32 - C => D
${A >> (D*8)} => A
${A << (D*8)} => A
:CALL(SHRarith)
:CALL(SHLarith)
A :MSTORE(bytesToStore)
:CALL(MSTOREX)

Expand Down Expand Up @@ -650,7 +681,7 @@ opCODECOPYinit:
opCODECOPYfinal:
${getBytecode(A,B,C)} => A
32 - C => D
${A << (D*8)} => A
:CALL(SHLarith)
A :MSTORE(bytesToStore)
:CALL(MSTOREX)

Expand Down Expand Up @@ -705,7 +736,7 @@ opEXTCODECOPYinit:
opEXTCODECOPYfinal:
${getBytecode(A,B,C)} => A
32 - C => D
${A << (D*8)} => A
:CALL(SHLarith)
A :MSTORE(bytesToStore)
:CALL(MSTOREX)

Expand Down Expand Up @@ -929,7 +960,7 @@ opMSTORE8:
$ => A :MLOAD(SP) ;value
1 => C
32 - C => D
${A << (D*8)} => A
:CALL(SHLarith)
A :MSTORE(bytesToStore)
:CALL(MSTOREX)
GAS - 3 => GAS
Expand Down Expand Up @@ -1151,7 +1182,7 @@ opAuxPUSHC:
D - E => D
$ => B :MLOAD(SP)
${getByte(B,C,D)} => B
${A >> (D*8)} => A
:CALL(SHRarith)
$ => SP :MLOAD(SPw)
A + B :MSTORE(SP++)
GAS-3 => GAS
Expand Down
2 changes: 1 addition & 1 deletion main/precompiled/blake2f.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BLAKE2FLoopData:
BLAKE2FEndLoopData:
$ => A :MLOAD(SP)
32 - B => D
${A >> (D*8)} => A
:CALL(SHRarith)
${precompiled_blake2f_add(A, B)}

BLAKE2FDigestReturn:
Expand Down
2 changes: 1 addition & 1 deletion main/precompiled/modexp.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ MODEXP:
;ASSERT C == D
;Result of the computation, with the same number of bytes as M
32 - D => D
${A << (D*8)} => A
:CALL(SHLarith)
A :MSTORE(bytesToStore)
C - 32 :JMPC(MODEXPstoreX)
:CALL(MSTORE32)
Expand Down
2 changes: 1 addition & 1 deletion main/precompiled/ripemd160.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RIPEMD160LoopData: ; set array of bytes in RIPEMD160
RIPEMD160EndLoopData:
$ => A :MLOAD(SP)
32 - B => D
${A >> (D*8)} => A
:CALL(SHRarith)
${precompiled_ripemd160_add(A, B)}

RIPEMD160DigestReturn: ; digest hash and prepare return
Expand Down
2 changes: 1 addition & 1 deletion main/precompiled/sha256.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SHA256LoopData: ; set array of bytes in sha256
SHA256EndLoopData:
$ => A :MLOAD(SP)
32 - B => D
${A >> (D*8)} => A
:CALL(SHRarith)
${precompiled_sha256_add(A, B)}

SHA256DigestReturn: ; digest hash and prepare return
Expand Down
10 changes: 5 additions & 5 deletions main/process_tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ endContractAddress:
HASHPOS :HASHKLEN(E)
$ => A :HASHKDIGEST(E)
12 => D
${A << (D*8)} => A
${A >> (D*8)} => A
:CALL(SHLarith)
:CALL(SHRarith)
A :MSTORE(createContractAddress)
; TODO: Check nonce != 0 OR already_bytecode ==> same behaviour as fail constructor
; TODO: check CREATE or deployment with constructor reverted
Expand Down Expand Up @@ -255,7 +255,7 @@ loopCreate2:
endloopCreate2:
$ => A :MLOAD(SP)
32 - C => D
${A >> (D*8)} => A
:CALL(SHRarith)
C => D
A :HASHK(E)

Expand All @@ -280,8 +280,8 @@ create2end:
HASHPOS :HASHKLEN(E)
$ => A :HASHKDIGEST(E)
12 => D
${A << (D*8)} => A ; // TODO: Could be replaced by a bitwise and: ${bitwise_and(A, 2**160 - 1)
${A >> (D*8)} => A
:CALL(SHLarith) ; // TODO: Could be replaced by a bitwise and: ${bitwise_and(A, 2**160 - 1)
:CALL(SHRarith)
A :MSTORE(createContractAddress)

deploy:
Expand Down
Loading

0 comments on commit 5fb4d14

Please sign in to comment.