Skip to content

Commit

Permalink
use labels ecrecover
Browse files Browse the repository at this point in the history
  • Loading branch information
krlosMata committed Dec 14, 2022
1 parent d6084a5 commit 8f342d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
12 changes: 7 additions & 5 deletions main/ecrecover/ecrecover.zkasm
Original file line number Diff line number Diff line change
@@ -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
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -203,7 +205,7 @@ ecrecover_error:
0 => A

ecrecover_end:
$ => RR :MLOAD(ecrecover_RR)
$ => RR :MLOAD(ecrecover_RR)
:RETURN

INCLUDE "addFpEc.zkasm"
Expand Down
23 changes: 4 additions & 19 deletions main/precompiled/pre-ecrecover.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main/process-tx.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f342d4

Please sign in to comment.