From 8f342d4c2594cccfe4d992c6e262187c03e1b8af Mon Sep 17 00:00:00 2001 From: krlosMata Date: Wed, 14 Dec 2022 12:49:50 +0100 Subject: [PATCH] use labels ecrecover --- main/ecrecover/ecrecover.zkasm | 12 +++++++----- main/precompiled/pre-ecrecover.zkasm | 23 ++++------------------- main/process-tx.zkasm | 2 +- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/main/ecrecover/ecrecover.zkasm b/main/ecrecover/ecrecover.zkasm index 40623390..aeb467c7 100644 --- a/main/ecrecover/ecrecover.zkasm +++ b/main/ecrecover/ecrecover.zkasm @@ -1,7 +1,7 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; -;; ecrecover A = hash, B = r, C = s, D = v -;; E = result +;; ecrecover in: A = hash, B = r, C = s, D = v +;; out: A = result, B = result_code ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -20,7 +20,8 @@ VAR GLOBAL ecrecover_s_upperlimit INCLUDE "constEc.zkasm" -; ERROR CODES +; ERROR CODES (B) +; 0 - no error ; 1 - r is zero (0) ; 2 - r is too big ; 3 - s is zero (0) @@ -178,7 +179,8 @@ ecrecover_v_y2_same_parity: ; for address take only last 20 bytes 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn => B - $ => A :AND,JMP(ecrecover_end) + $ => A :AND + 0 => B :JMP(ecrecover_end) ; ERRORS ecrecover_r_is_zero: @@ -203,7 +205,7 @@ ecrecover_error: 0 => A ecrecover_end: - $ => RR :MLOAD(ecrecover_RR) + $ => RR :MLOAD(ecrecover_RR) :RETURN INCLUDE "addFpEc.zkasm" diff --git a/main/precompiled/pre-ecrecover.zkasm b/main/precompiled/pre-ecrecover.zkasm index 768fe883..007d4f3e 100644 --- a/main/precompiled/pre-ecrecover.zkasm +++ b/main/precompiled/pre-ecrecover.zkasm @@ -9,27 +9,12 @@ funcECRECOVER: ; read data stored in SP + %CALLDATA_OFFSET ; hash [32 bytes], v [32 bytes], r [32 bytes], s [32 bytes] %CALLDATA_OFFSET => SP - $ => E :MLOAD(SP++) ; hash - $ => A :MLOAD(SP++) ; v - ; check v range [27, 28] - 27 => B - $ :LT, JMPC(endECRECOVER) - A => B - 28 => A - $ :LT, JMPC(endECRECOVER) - B => D - E => A + $ => A :MLOAD(SP++) ; hash + $ => D :MLOAD(SP++) ; v $ => B :MLOAD(SP++) ; r - ; check r range [0, secp256k1n] - ;0 - ${comp_gt(B, 115792089237316195423570985008687907852837564279074904382605163141518161494337)} :JMPN(endECRECOVER) $ => C :MLOAD(SP) ; s - ; check s range [0, secp256k1n/2 + 1] - ;0 - ${comp_gt(C, 57896044618658097711785492504343953926418782139537452191302581570759080747169)} :JMPN(endECRECOVER) - ; compute ecrecover - :CALL(ecrecover) - 0 => B - $ => B :EQ - 0 - B :JMPN(endECRECOVER) + :CALL(ecrecover_precompiled) ; in: [A: hash, B: r, C: s, D: v], out: [A: result_ecrecover] + B :JMPNZ(endECRECOVER) ; prepare return data $ => E :MLOAD(retCallOffset) ; $ => C :MLOAD(retCallLength) ; always 32 diff --git a/main/process-tx.zkasm b/main/process-tx.zkasm index 4187c9e7..a1715f86 100644 --- a/main/process-tx.zkasm +++ b/main/process-tx.zkasm @@ -38,7 +38,7 @@ processTx: $ => B :MLOAD(txR) $ => C :MLOAD(txS) $ => D :MLOAD(txV) - :CALL(ecrecover) + :CALL(ecrecover_tx) ; Check result is non-zero checkAndSaveFrom: 0 => B