Skip to content

Commit

Permalink
Merge pull request #410 from 0xPolygonHermez/feature/add-pre-eip7212
Browse files Browse the repository at this point in the history
Feature/add pre eip7212
  • Loading branch information
laisolizq authored Oct 10, 2024
2 parents d0c888e + a92ad82 commit 454c862
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
2 changes: 2 additions & 0 deletions main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ CONST %SHA2_256_GAS = 60 ; sha256 static gas
CONST %SHA2_256_WORD_GAS = 12 ; sha256 word gas
CONST %KECCAK_GAS = 30 ; Once per KECCAK256 operation.
CONST %KECCAK_WORD_GAS = 6 ; Once per word of the KECCAK256 operation's data.
CONST %P256VERIFY_GAS = 3450 ; p256verify gas price
CONST %LOG_GAS = 375 ; Per LOG* operation.
CONST %LOG_TOPIC_GAS = 375 ; Per LOG topic operation.
CONST %JUMP_DEST_GAS = 1 ; Once per JUMPDEST operation.
Expand All @@ -89,6 +90,7 @@ CONST %CREATE_GAS = 32000
CONST %CREATE_2_GAS = 32000
CONST %SENDALL_GAS = 5000
CONST %LOG_DATA_GAS = 8

; sstore
CONST %SSTORE_ENTRY_EIP_2200_GAS = 2300 ; Minimum gas required to be present for an SSTORE call, not consumed
CONST %SSTORE_SET_EIP_2200_GAS = 20000 ; Once per SSTORE operation from clean zero to non-zero
Expand Down
3 changes: 3 additions & 0 deletions main/precompiled/pre-ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* - stack output: [publicAddress]
*/
funcECRECOVER:
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)

; Move balances if value > 0 just before executing the contract CALL
$ => B :MLOAD(txValue)
0 => A
Expand Down
79 changes: 79 additions & 0 deletions main/precompiled/pre-p256verify.zkasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @link [https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md]
* @zk-counters
* - dynamic steps:
* - dynamic arith:
* - dynamic binary:
* @process-precompiled
* - stack input: [hash, r, s, x, y]
* - stack output: [success]
*/

funcP256VERIFY:
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)

; Move balances if value > 0 just before executing the contract CALL
$ => B :MLOAD(txValue)
0 => A
zkPC+2 => RR
$ :LT, JMPC(moveBalances)

GAS - %P256VERIFY_GAS => GAS :JMPN(outOfGas) ; gas static = 3450
; read data stored in calldata
; read hash [32 bytes]
32 :MSTORE(readXFromCalldataLength)
0 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => A :MLOAD(readXFromCalldataResult)
; read r [32 bytes]
E + 32 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => B :MLOAD(readXFromCalldataResult)
; read s [32 bytes]
E + 32 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => C :MLOAD(readXFromCalldataResult)
; read x [32 bytes]
E + 32 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => D :MLOAD(readXFromCalldataResult)
; read y [32 bytes]
E + 32 => E :MSTORE(readXFromCalldataOffset), CALL(readFromCalldataOffset); in: [readXFromCalldataOffset: offset value, readXFromCalldataLength: length value], out: [readXFromCalldataResult: result value]
$ => E :MLOAD(readXFromCalldataResult), CALL(p256verify) ;in: [A: hash, B: r, C: s, D: x, E: y], out: [A: result, B: result_code]
B :JMPNZ(endP256VERIFYFail)
A :JMPZ(preEndP256VERIFY)

; write result p256verify into memory
0 => E
A :MSTORE(bytesToStore), CALL(MSTORE32); in: [bytesToStore, E: offset] out: [E: new offset]

; prepare return data
0 :MSTORE(retDataOffset)
32 :MSTORE(retDataLength)
$ => A :MLOAD(originCTX), JMPZ(handleGas)
; set retDataCTX
$ => B :MLOAD(currentCTX)
A => CTX
B :MSTORE(retDataCTX)
B => CTX

; write result p256verify into previous context memory
$ => C :MLOAD(retCallLength), JMPZ(preEndP256VERIFY)
$ => E :MLOAD(retCallOffset)

; p256verify result is in bytesToStore
C - 32 :JMPN(continueP256VERIFY)
32 => C

continueP256VERIFY:
$ => CTX :MLOAD(originCTX), CALL(MSTOREX) ; in: [bytesToStore, E: offset, C: length] out: [E: new offset]
:JMP(endP256VERIFY)

endP256VERIFYFail:
$ => A :MLOAD(originCTX), JMPZ(handleGas)
A => CTX
0 :MSTORE(retDataCTX)
CTX :MSTORE(currentCTX), JMP(preEnd)

preEndP256VERIFY:
$ => CTX :MLOAD(originCTX)

endP256VERIFY:
CTX :MSTORE(currentCTX), JMP(preEnd)
7 changes: 6 additions & 1 deletion main/precompiled/selector.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ INCLUDE "pre-ecMul.zkasm"
INCLUDE "pre-ecPairing.zkasm"
INCLUDE "pre-modexp.zkasm"
INCLUDE "pre-sha2-256.zkasm"
INCLUDE "pre-p256verify.zkasm"
INCLUDE "../p256verify/p256verify.zkasm"
INCLUDE "../pairings/constants.zkasm"
INCLUDE "../pairings/BN254/ecAdd.zkasm"
INCLUDE "../pairings/BN254/ecMul.zkasm"
Expand Down Expand Up @@ -74,4 +76,7 @@ selectorPrecompiled:
A - 7 :JMPN(funcEcAdd)
A - 8 :JMPN(funcEcMul)
A - 9 :JMPN(funcEcPairing)
A - 10 :JMPN(revertPrecompiled) ;:JMPN(BLAKE2F)
A - 10 :JMPN(revertPrecompiled) ;:JMPN(BLAKE2F)

selectorPrecompiledP256Verify:
:JMP(funcP256VERIFY)
5 changes: 3 additions & 2 deletions main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ endCalldataIntrinsicGas:
;; F - Check transaction type
;;;;;;;;;;;;;;;;;;
txType:

; Compute deployment address if create contract operation
$ => A :MLOAD(isCreateContract), JMPNZ(getContractAddress)
$ => A :MLOAD(txDestAddr)
Expand All @@ -210,8 +209,10 @@ txType:
; Check zero address since zero address is not a precompiled contract
0 => B
$ :EQ, JMPC(callContract)
0x100 => B
$ :EQ, JMPC(selectorPrecompiledP256Verify)
10 => B
$ :LT,JMPC(selectorPrecompiled, callContract)
$ :LT, JMPC(selectorPrecompiled, callContract)

;;;;;;;;;;;;;;;;;;
;; F.2 - Deploy contract
Expand Down
2 changes: 2 additions & 0 deletions main/touched.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ isColdAddress:
; if address is a precompiled considered as warm address
10 => B
$ :LT, JMPC(finishColdPrecompiled)
0x100 => B
$ :EQ, JMPC(finishColdPrecompiled)

checkWarmed:
; save current state root & load touched root
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"test:zkasm": "node tools/run-tests-zkasm.js ./test --helpers ../main/helper.js",
"eslint": "npx eslint tools/**.js && npx eslint counters/counters-executor.js",
"eslint:fix": "npx eslint tools/**.js --fix && npx eslint counters/counters-executor.js --fix",
"test:gen": "node tools/parallel-testing/gen-parallel-tests.js",
"test:gen": "node tools/parallel-testing/gen-parallel-tests.js --skipVCounters",
"test:start": "npx mocha --jobs $NUM_CPUS --timeout 0 --max-old-space-size=8192 --parallel \"tools/parallel-testing/parallel-tests/*.test.js\"",
"report:free-inputs": "node tools/audit-tools/free-inputs-checker.js",
"report:registry-op": "node tools/audit-tools/registry-op-checker.js"
Expand All @@ -44,8 +44,8 @@
},
"devDependencies": {
"@0xpolygonhermez/zkevm-commonjs": "github:0xPolygonHermez/zkevm-commonjs#v8.0.0-fork.12",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#develop-durian",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/fork-13",
"@0xpolygonhermez/zkevm-proverjs": "github:0xPolygonHermez/zkevm-proverjs#fix/secp256r1-dbl",
"@0xpolygonhermez/zkevm-testvectors": "github:0xPolygonHermez/zkevm-testvectors#feature/add-rip7212",
"chai": "^4.3.6",
"chalk": "^3.0.0",
"eslint": "^8.25.0",
Expand Down

0 comments on commit 454c862

Please sign in to comment.