-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
escalar renamed to the correct scalar
- Loading branch information
Showing
25 changed files
with
362 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; PRE: P ∈ E'(Fp2) | ||
;; POST: The resulting coordinates are in the range [0,BN254_P) because if falls back to addPointBN254 | ||
;; | ||
;; | ||
;; scalarMulBN254: | ||
;; in: k, P = (P.x1 + P.x2·u, P.y1 + P.y2·u) ∈ E'(Fp2), where k ∈ [0,r-1] | ||
;; out: k·P = (Q.x1 + Q.x2·u, Q.y1 + Q.y2·u) ∈ E'(Fp2) | ||
;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
; scalarMulBN254 assumes P belong to E'(Fp2), since it is checked in the pairing. | ||
; However, it must be implemented if scalarMulBN254 wants to be used independently. | ||
|
||
; Since the curve is E'/Fp2: y² = x³ + 3/(9+u), there is no issue in representing the point at infinity as (0, 0). | ||
|
||
VAR GLOBAL scalarMulBN254_k | ||
VAR GLOBAL scalarMulBN254_P_x1 | ||
VAR GLOBAL scalarMulBN254_P_x2 | ||
VAR GLOBAL scalarMulBN254_P_y1 | ||
VAR GLOBAL scalarMulBN254_P_y2 | ||
VAR GLOBAL scalarMulBN254_Q_x1 | ||
VAR GLOBAL scalarMulBN254_Q_x2 | ||
VAR GLOBAL scalarMulBN254_Q_y1 | ||
VAR GLOBAL scalarMulBN254_Q_y2 | ||
|
||
VAR GLOBAL scalarMulBN254_RR | ||
|
||
|
||
scalarMulBN254: | ||
RR :MSTORE(scalarMulBN254_RR) | ||
|
||
; Is P = 𝒪? | ||
0n => B | ||
$ => A :MLOAD(scalarMulBN254_P_x1) | ||
$ :EQ, JMPNC(__scalarMulBN254_P_continue) | ||
$ => A :MLOAD(scalarMulBN254_P_x2) | ||
$ :EQ, JMPNC(__scalarMulBN254_P_continue) | ||
$ => A :MLOAD(scalarMulBN254_P_y1) | ||
$ :EQ, JMPNC(__scalarMulBN254_P_continue) | ||
$ => A :MLOAD(scalarMulBN254_P_y2) | ||
$ :EQ, JMPC(scalarMulBN254_P_is_zero) | ||
__scalarMulBN254_P_continue: | ||
|
||
; Is k = 0? | ||
$ => B :MLOAD(scalarMulBN254_k), CALL(reduceFrBN254) | ||
A :MSTORE(scalarMulBN254_k) | ||
0n => B | ||
$ :EQ, JMPC(scalarMulBN254_k_is_zero) | ||
|
||
257 => RCX | ||
|
||
$ => A :MLOAD(scalarMulBN254_P_x1) | ||
$ => B :MLOAD(scalarMulBN254_P_x2) | ||
$ => C :MLOAD(scalarMulBN254_P_y1) | ||
$ => D :MLOAD(scalarMulBN254_P_y2) | ||
A :MSTORE(scalarMulBN254_Q_x1) | ||
B :MSTORE(scalarMulBN254_Q_x2) | ||
C :MSTORE(scalarMulBN254_Q_y1) | ||
D :MSTORE(scalarMulBN254_Q_y2) | ||
|
||
:JMP(scalarMulBN254_find_MSB_k) | ||
|
||
scalarMulBN254_P_is_zero: | ||
; Q = 𝒪 | ||
0n :MSTORE(scalarMulBN254_Q_x1) | ||
0n :MSTORE(scalarMulBN254_Q_x2) | ||
0n :MSTORE(scalarMulBN254_Q_y1) | ||
0n :MSTORE(scalarMulBN254_Q_y2) | ||
|
||
:JMP(scalarMulBN254_end) | ||
|
||
scalarMulBN254_k_is_zero: | ||
; Q = 𝒪 | ||
0n :MSTORE(scalarMulBN254_Q_x1) | ||
0n :MSTORE(scalarMulBN254_Q_x2) | ||
0n :MSTORE(scalarMulBN254_Q_y1) | ||
0n :MSTORE(scalarMulBN254_Q_y2) | ||
|
||
:JMP(scalarMulBN254_end) | ||
|
||
scalarMulBN254_find_MSB_k: | ||
RCX - 1 => RCX | ||
$ => A,B :MLOAD(scalarMulBN254_k) | ||
; E = 2A | ||
$ => E :ADD,MSTORE(scalarMulBN254_k), JMPNC(scalarMulBN254_find_MSB_k) | ||
|
||
|
||
scalarMulBN254_loop: | ||
RCX - 1 => RCX :JMPZ(scalarMulBN254_end) | ||
|
||
; We always double | ||
$ => A :MLOAD(scalarMulBN254_Q_x1) | ||
$ => B :MLOAD(scalarMulBN254_Q_x2) | ||
$ => C :MLOAD(scalarMulBN254_Q_y1) | ||
$ => D :MLOAD(scalarMulBN254_Q_y2) | ||
A :MSTORE(addPointBN254_P1_x1) | ||
B :MSTORE(addPointBN254_P1_x2) | ||
C :MSTORE(addPointBN254_P1_y1) | ||
D :MSTORE(addPointBN254_P1_y2) | ||
A :MSTORE(addPointBN254_P2_x1) | ||
B :MSTORE(addPointBN254_P2_x2) | ||
C :MSTORE(addPointBN254_P2_y1) | ||
D :MSTORE(addPointBN254_P2_y2), CALL(addPointBN254) | ||
; Q = Q + Q | ||
|
||
$ => A :MLOAD(addPointBN254_P3_x1) | ||
$ => B :MLOAD(addPointBN254_P3_x2) | ||
$ => C :MLOAD(addPointBN254_P3_y1) | ||
$ => D :MLOAD(addPointBN254_P3_y2) | ||
A :MSTORE(scalarMulBN254_Q_x1) | ||
B :MSTORE(scalarMulBN254_Q_x2) | ||
C :MSTORE(scalarMulBN254_Q_y1) | ||
D :MSTORE(scalarMulBN254_Q_y2) | ||
|
||
; We check if the MSB b of k is either 1 or 0. If b==1, we should add P to Q. | ||
; Then, update the value of k. | ||
$ => A,B :MLOAD(scalarMulBN254_k) | ||
; E = 2A | ||
$ => E :ADD,MSTORE(scalarMulBN254_k), JMPNC(scalarMulBN254_loop) | ||
|
||
scalarMulBN254_add: | ||
; We add | ||
$ => A :MLOAD(scalarMulBN254_Q_x1) | ||
$ => B :MLOAD(scalarMulBN254_Q_x2) | ||
$ => C :MLOAD(scalarMulBN254_Q_y1) | ||
$ => D :MLOAD(scalarMulBN254_Q_y2) | ||
A :MSTORE(addPointBN254_P1_x1) | ||
B :MSTORE(addPointBN254_P1_x2) | ||
C :MSTORE(addPointBN254_P1_y1) | ||
D :MSTORE(addPointBN254_P1_y2) | ||
|
||
$ => A :MLOAD(scalarMulBN254_P_x1) | ||
$ => B :MLOAD(scalarMulBN254_P_x2) | ||
$ => C :MLOAD(scalarMulBN254_P_y1) | ||
$ => D :MLOAD(scalarMulBN254_P_y2) | ||
A :MSTORE(addPointBN254_P2_x1) | ||
B :MSTORE(addPointBN254_P2_x2) | ||
C :MSTORE(addPointBN254_P2_y1) | ||
D :MSTORE(addPointBN254_P2_y2), CALL(addPointBN254) | ||
; Q = Q + P | ||
|
||
$ => A :MLOAD(addPointBN254_P3_x1) | ||
$ => B :MLOAD(addPointBN254_P3_x2) | ||
$ => C :MLOAD(addPointBN254_P3_y1) | ||
$ => D :MLOAD(addPointBN254_P3_y2) | ||
A :MSTORE(scalarMulBN254_Q_x1) | ||
B :MSTORE(scalarMulBN254_Q_x2) | ||
C :MSTORE(scalarMulBN254_Q_y1) | ||
D :MSTORE(scalarMulBN254_Q_y2), JMP(scalarMulBN254_loop) | ||
|
||
|
||
scalarMulBN254_end: | ||
$ => RR :MLOAD(scalarMulBN254_RR) | ||
:RETURN |
Oops, something went wrong.