1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef _SECP256K1_GROUP_
8 #define _SECP256K1_GROUP_
13 /** A group element of the secp256k1 curve, in affine coordinates. */
17 int infinity; /* whether this represents the point at infinity */
20 #define SECP256K1_GE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_CONST((i),(j),(k),(l),(m),(n),(o),(p)), 0}
21 #define SECP256K1_GE_CONST_INFINITY {SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), 1}
23 /** A group element of the secp256k1 curve, in jacobian coordinates. */
25 secp256k1_fe x; /* actual X: x/z^2 */
26 secp256k1_fe y; /* actual Y: y/z^3 */
28 int infinity; /* whether this represents the point at infinity */
31 #define SECP256K1_GEJ_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_CONST((i),(j),(k),(l),(m),(n),(o),(p)), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1), 0}
32 #define SECP256K1_GEJ_CONST_INFINITY {SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 0), 1}
35 secp256k1_fe_storage x;
36 secp256k1_fe_storage y;
37 } secp256k1_ge_storage;
39 #define SECP256K1_GE_STORAGE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {SECP256K1_FE_STORAGE_CONST((a),(b),(c),(d),(e),(f),(g),(h)), SECP256K1_FE_STORAGE_CONST((i),(j),(k),(l),(m),(n),(o),(p))}
41 #define SECP256K1_GE_STORAGE_CONST_GET(t) SECP256K1_FE_STORAGE_CONST_GET(t.x), SECP256K1_FE_STORAGE_CONST_GET(t.y)
43 /** Set a group element equal to the point at infinity */
44 static void secp256k1_ge_set_infinity(secp256k1_ge *r);
46 /** Set a group element equal to the point with given X and Y coordinates */
47 static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y);
49 /** Set a group element (affine) equal to the point with the given X coordinate, and given oddness
50 * for Y. Return value indicates whether the result is valid. */
51 static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd);
53 /** Check whether a group element is the point at infinity. */
54 static int secp256k1_ge_is_infinity(const secp256k1_ge *a);
56 /** Check whether a group element is valid (i.e., on the curve). */
57 static int secp256k1_ge_is_valid_var(const secp256k1_ge *a);
59 static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a);
61 /** Set a group element equal to another which is given in jacobian coordinates */
62 static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a);
64 /** Set a batch of group elements equal to the inputs given in jacobian coordinates */
65 static void secp256k1_ge_set_all_gej_var(size_t len, secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_callback *cb);
67 /** Set a batch of group elements equal to the inputs given in jacobian
68 * coordinates (with known z-ratios). zr must contain the known z-ratios such
69 * that mul(a[i].z, zr[i+1]) == a[i+1].z. zr[0] is ignored. */
70 static void secp256k1_ge_set_table_gej_var(size_t len, secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zr);
72 /** Bring a batch inputs given in jacobian coordinates (with known z-ratios) to
73 * the same global z "denominator". zr must contain the known z-ratios such
74 * that mul(a[i].z, zr[i+1]) == a[i+1].z. zr[0] is ignored. The x and y
75 * coordinates of the result are stored in r, the common z coordinate is
76 * stored in globalz. */
77 static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr);
79 /** Set a group element (jacobian) equal to the point at infinity. */
80 static void secp256k1_gej_set_infinity(secp256k1_gej *r);
82 /** Set a group element (jacobian) equal to the point with given X and Y coordinates. */
83 static void secp256k1_gej_set_xy(secp256k1_gej *r, const secp256k1_fe *x, const secp256k1_fe *y);
85 /** Set a group element (jacobian) equal to another which is given in affine coordinates. */
86 static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a);
88 /** Compare the X coordinate of a group element (jacobian). */
89 static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a);
91 /** Set r equal to the inverse of a (i.e., mirrored around the X axis) */
92 static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a);
94 /** Check whether a group element is the point at infinity. */
95 static int secp256k1_gej_is_infinity(const secp256k1_gej *a);
97 /** Set r equal to the double of a. If rzr is not-NULL, r->z = a->z * *rzr (where infinity means an implicit z = 0).
98 * a may not be zero. Constant time. */
99 static void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr);
101 /** Set r equal to the double of a. If rzr is not-NULL, r->z = a->z * *rzr (where infinity means an implicit z = 0). */
102 static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr);
104 /** Set r equal to the sum of a and b. If rzr is non-NULL, r->z = a->z * *rzr (a cannot be infinity in that case). */
105 static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr);
107 /** Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity). */
108 static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b);
110 /** Set r equal to the sum of a and b (with b given in affine coordinates). This is more efficient
111 than secp256k1_gej_add_var. It is identical to secp256k1_gej_add_ge but without constant-time
112 guarantee, and b is allowed to be infinity. If rzr is non-NULL, r->z = a->z * *rzr (a cannot be infinity in that case). */
113 static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr);
115 /** Set r equal to the sum of a and b (with the inverse of b's Z coordinate passed as bzinv). */
116 static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv);
118 #ifdef USE_ENDOMORPHISM
119 /** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */
120 static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a);
123 /** Clear a secp256k1_gej to prevent leaking sensitive information. */
124 static void secp256k1_gej_clear(secp256k1_gej *r);
126 /** Clear a secp256k1_ge to prevent leaking sensitive information. */
127 static void secp256k1_ge_clear(secp256k1_ge *r);
129 /** Convert a group element to the storage type. */
130 static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge*);
132 /** Convert a group element back from the storage type. */
133 static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage*);
135 /** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
136 static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag);
138 /** Rescale a jacobian point by b which must be non-zero. Constant-time. */
139 static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b);