Skip to content

Commit

Permalink
Merge pull request #167 from 0xPolygonHermez/feature/touched-SR
Browse files Browse the repository at this point in the history
implement touched tree
  • Loading branch information
krlosMata authored Nov 11, 2022
2 parents 3c2222a + 95b12e2 commit 850b1b2
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 124 deletions.
6 changes: 5 additions & 1 deletion main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ CONST %LOCAL_EXIT_ROOT_STORAGE_POS = 1
CONST %LAST_TX_STORAGE_POS = 0
CONST %STATE_ROOT_STORAGE_POS = 1

; SMT CONSTANT KEYS
; SMT STATE-TREE CONSTANT KEYS
CONST %SMT_KEY_BALANCE = 0
CONST %SMT_KEY_NONCE = 1
CONST %SMT_KEY_SC_CODE = 2
CONST %SMT_KEY_SC_STORAGE = 3
CONST %SMT_KEY_SC_LENGTH = 4

; SMT TOUCHED-TREE CONSTANT KEYS
CONST %SMT_KEY_TOUCHED_ADDR = 5
CONST %SMT_KEY_TOUCHED_SLOTS = 6

; GAS
CONST %BASE_TX_GAS = 21000
CONST %BASE_TX_DEPLOY_GAS = 32000
Expand Down
40 changes: 20 additions & 20 deletions main/opcodes/arithmetic.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ opADDMOD:

1 => D
$ => A :ADD, JMPC(AddModJumpCarry) ; or arith
0 => D
0 => D

AddModJumpCarry:
A => E ; Store sumResult on E

$ => A :MLOAD(SP) ; load modulus
2 => B
2 => B
$ :LT, JMPC(zeroOneMod)

${_addMod / A} => B ; k: Max should be (2^256 -1) * 2 / 2 --> smaller than 2^256
Expand Down Expand Up @@ -245,17 +245,17 @@ opMULMOD:
SP - 1 => SP

; The following approach will be followed in order to verify the mulmod operation
; A * B + 0 = D*2^256 + E
; A * B + 0 = D*2^256 + E
; K * N + mulModResult = D*2^256 + E

; Since the k can be bigger than 2²⁵⁶ and therefore does not fit in a register we divedit in the
; most significan and less significant part:

; (k.l + k.h * 2²⁵⁶) * N + mulModResult = (D1 + D2) * 2²⁵⁶ + E
; (k.l + k.h * 2²⁵⁶) * N + mulModResult = (D1 + D2) * 2²⁵⁶ + E
; And divide this operation in 2 which fits in 2²⁵⁶ digits
;k.l * N + mulModResult = D1 * 2²⁵⁶ + E
;k.h * 2²⁵⁶ * N = D2 * 2²⁵⁶ --> k.h * N = D2

;k.l * N + mulModResult = D1 * 2²⁵⁶ + E
;k.h * 2²⁵⁶ * N = D2 * 2²⁵⁶ --> k.h * N = D2
$ => A :MLOAD(SP--)
$ => B :MLOAD(SP--)

Expand All @@ -266,31 +266,31 @@ opMULMOD:
; here we perform the: A * B + 0 = D*2^256 + E
; result is stored in: arithRes1(E) and arithOverflow(D)
:CALL(mulARITH)

$ => A :MLOAD(SP) ; Modulus N
; Check if modulus is 0 or 1
2 => B
2 => B
$ :LT, JMPC(zeroOneMod)

; Now we will try to perform the following equation:
; (k.l + k.h * 2²⁵⁶) * N + mulModResult = (D1 + D2) * 2²⁵⁶ + E
; (k.l + k.h * 2²⁵⁶) * N + mulModResult = (D1 + D2) * 2²⁵⁶ + E
A => C ; modulus on C
${(_mulMod / C) >> 256} => B ; k.h
; We can jump with Js, because later it's all verified by the ARITH
${cond(B == 0)} :JMPN(mulModNoKH)
${cond(B == 0)} :JMPN(mulModNoKH)

; Since there's k.h we will split the equation in those 2
;k.l * N + mulModResult = D1 * 2²⁵⁶ + E
;k.h * 2²⁵⁶ * N = D2 * 2²⁵⁶ --> k.h * N = D2
;k.l * N + mulModResult = D1 * 2²⁵⁶ + E
;k.h * 2²⁵⁶ * N = D2 * 2²⁵⁶ --> k.h * N = D2

; k.h * N = D2
; k.h * N = D2
; B * A + 0 = 0 * 2²⁵⁶ + E
; D2 must be less than 2²⁵⁶
C => A ; Modulus
0 => C, D
${B * A} => E :MSTORE(tmpVarD), ARITH ; D2

; k.l * N + mulModResult = D1 * 2²⁵⁶ + E
; k.l * N + mulModResult = D1 * 2²⁵⁶ + E
; B * A + C = D*2^256 + E
; remember that:
; result of mul is stored in: arithRes1(E) and arithOverflow(D)
Expand All @@ -303,7 +303,7 @@ opMULMOD:
; Finally we need to assert the following:
; N>resultModulus
; D1 + D2 = D

; N>resultModulus ; LT; ASSERT
A => B ; modulus
C => A ; mulModResult
Expand All @@ -321,7 +321,7 @@ opMULMOD:

mulModNoKH:
; if theres no K.h the equation is simplified as:
; K * N + mulModResult = D*2^256 + E
; K * N + mulModResult = D*2^256 + E
; B * A + C = D*2^256 + E

C => A ; Modulus on A
Expand Down Expand Up @@ -368,7 +368,7 @@ opSIGNEXTEND: ; following this impl https://github.com/ethereumjs/ethereumjs-mon
$ => D :MLOAD(SP) ; number to convert
30 => A
$ :LT, JMPC(opSIGNEXTENDEnd); if signByte is 31 or more, means basically let the number as it is
; TODO we could divide this opcode in a table with constants, only 31 cases
; TODO we could divide this opcode in a table with constants, only 31 cases
B * 8 + 7 => B ; B is less than 31, no need for binary
${exp(2, B)} => A ; signBit TODO table for shifting + multipliyng

Expand Down
51 changes: 6 additions & 45 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ opCALLDATASIZE:
GAS-2 => GAS :JMPN(outOfGas)
:JMP(readCode)

opCALLDATASIZEdep:
opCALLDATASIZEdep:
0 :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
GAS-2 => GAS :JMPN(outOfGas)
Expand Down Expand Up @@ -120,7 +120,7 @@ opCALLDATACOPYinit:
$ => SP :MLOAD(SPw)
$ => C :MLOAD(SP) ;length
C - 32 => C
C :MSTORE(SP)
C :MSTORE(SP)
B + 32 => B
:JMP(opCALLDATACOPYinit)

Expand Down Expand Up @@ -304,7 +304,7 @@ opEXTCODESIZE:
SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
:CALL(maskAddress) ; Mask address to 20 bytes
${isWarmedAddress(A)} => D
:CALL(isColdAddress)
%SMT_KEY_SC_LENGTH => B
0 => C
$ => A :SLOAD
Expand All @@ -331,7 +331,7 @@ opEXTCODECOPY:
SP - 1 => SP
$ => A :MLOAD(SP--) ;addr
:CALL(maskAddress) ; Mask address to 20 bytes
${isWarmedAddress(A)} => D
:CALL(isColdAddress)
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:CALL(opEXTCODECOPYCheckHash)
$ => C :MLOAD(SP--) ;destOffset, memory offset
Expand Down Expand Up @@ -556,50 +556,11 @@ opEXTCODEHASH:
SP - 1 => SP :JMPN(stackUnderflow)
$ => A :MLOAD(SP)
:CALL(maskAddress) ; Mask address to 20 bytes
${isWarmedAddress(A)} => D
:CALL(isColdAddress)
GAS-100-D*2500 => GAS :JMPN(outOfGas)
%SMT_KEY_SC_CODE => B
0 => C
$ => A :SLOAD
A :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
:JMP(readCode)

; @info opEXTCODEHASHL2 returns the keccak of the bytecode
; opEXTCODEHASH returns the hash bytecode stored in the smt (poseidon linear hash)
;opEXTCODEHASHL2:
; SP - 1 => SP :JMPN(stackUnderflow)
; $ => A :MLOAD(SP)
; ${isWarmedAddress(A)} => D
; GAS-100-D*2500 => GAS :JMPN(outOfGas)
; %SMT_KEY_SC_CODE => B
; 0 => C
; $ => A :SLOAD
; $${p = 0}
; ${getBytecodeLength(A)} => C
; $ => E :MLOAD(lastHashKIdUsed)
; E+1 => E :MSTORE(lastHashKIdUsed)
; ; A new hash with position 0 is started
; 0 => HASHPOS
;
;opEXTCODEHASHinit:
; 32 => D
; C - 1 :JMPN(opEXTCODEHASHend)
; C - 32 :JMPN(opEXTCODEHASHfinal)
; ${getBytecode(A,p,D)} => B
; B :HASHK(E)
; C - D => C
; $${p = p + D}
; :JMP(opEXTCODEHASHinit)
;
;opEXTCODEHASHfinal:
; C => D
; ${getBytecode(A,p,C)} => B
; B :HASHK(E)
;
;opEXTCODEHASHend:
; HASHPOS :HASHKLEN(E)
; $ => A :HASHKDIGEST(E)
; A :MSTORE(SP++)
; 1024 - SP :JMPN(stackOverflow)
; :JMP(readCode)
:JMP(readCode)
2 changes: 1 addition & 1 deletion main/opcodes/context-information.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ opBALANCE:
$ => D :SLOAD
D :MSTORE(SP++)
1024 - SP :JMPN(stackOverflow)
${isWarmedAddress(A)} => D
:CALL(isColdAddress)
GAS-100-D*2500 => GAS :JMPN(outOfGas)
:JMP(readCode)

Expand Down
Loading

0 comments on commit 850b1b2

Please sign in to comment.