github.com/luckypickle/go-ethereum-vet@v1.14.2/crypto/secp256k1/libsecp256k1/src/group.h (about) 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 **********************************************************************/ 6 7 #ifndef _SECP256K1_GROUP_ 8 #define _SECP256K1_GROUP_ 9 10 #include "num.h" 11 #include "field.h" 12 13 /** A group element of the secp256k1 curve, in affine coordinates. */ 14 typedef struct { 15 vet_secp256k1_fe x; 16 vet_secp256k1_fe y; 17 int infinity; /* whether this represents the point at infinity */ 18 } vet_secp256k1_ge; 19 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} 22 23 /** A group element of the secp256k1 curve, in jacobian coordinates. */ 24 typedef struct { 25 vet_secp256k1_fe x; /* actual X: x/z^2 */ 26 vet_secp256k1_fe y; /* actual Y: y/z^3 */ 27 vet_secp256k1_fe z; 28 int infinity; /* whether this represents the point at infinity */ 29 } vet_secp256k1_gej; 30 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} 33 34 typedef struct { 35 vet_secp256k1_fe_storage x; 36 vet_secp256k1_fe_storage y; 37 } vet_secp256k1_ge_storage; 38 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))} 40 41 #define SECP256K1_GE_STORAGE_CONST_GET(t) SECP256K1_FE_STORAGE_CONST_GET(t.x), SECP256K1_FE_STORAGE_CONST_GET(t.y) 42 43 /** Set a group element equal to the point with given X and Y coordinates */ 44 static void vet_secp256k1_ge_set_xy(vet_secp256k1_ge *r, const vet_secp256k1_fe *x, const vet_secp256k1_fe *y); 45 46 /** Set a group element (affine) equal to the point with the given X coordinate 47 * and a Y coordinate that is a quadratic residue modulo p. The return value 48 * is true iff a coordinate with the given X coordinate exists. 49 */ 50 static int vet_secp256k1_ge_set_xquad(vet_secp256k1_ge *r, const vet_secp256k1_fe *x); 51 52 /** Set a group element (affine) equal to the point with the given X coordinate, and given oddness 53 * for Y. Return value indicates whether the result is valid. */ 54 static int vet_secp256k1_ge_set_xo_var(vet_secp256k1_ge *r, const vet_secp256k1_fe *x, int odd); 55 56 /** Check whether a group element is the point at infinity. */ 57 static int vet_secp256k1_ge_is_infinity(const vet_secp256k1_ge *a); 58 59 /** Check whether a group element is valid (i.e., on the curve). */ 60 static int vet_secp256k1_ge_is_valid_var(const vet_secp256k1_ge *a); 61 62 static void vet_secp256k1_ge_neg(vet_secp256k1_ge *r, const vet_secp256k1_ge *a); 63 64 /** Set a group element equal to another which is given in jacobian coordinates */ 65 static void vet_secp256k1_ge_set_gej(vet_secp256k1_ge *r, vet_secp256k1_gej *a); 66 67 /** Set a batch of group elements equal to the inputs given in jacobian coordinates */ 68 static void vet_secp256k1_ge_set_all_gej_var(vet_secp256k1_ge *r, const vet_secp256k1_gej *a, size_t len, const vet_secp256k1_callback *cb); 69 70 /** Set a batch of group elements equal to the inputs given in jacobian 71 * coordinates (with known z-ratios). zr must contain the known z-ratios such 72 * that mul(a[i].z, zr[i+1]) == a[i+1].z. zr[0] is ignored. */ 73 static void vet_secp256k1_ge_set_table_gej_var(vet_secp256k1_ge *r, const vet_secp256k1_gej *a, const vet_secp256k1_fe *zr, size_t len); 74 75 /** Bring a batch inputs given in jacobian coordinates (with known z-ratios) to 76 * the same global z "denominator". zr must contain the known z-ratios such 77 * that mul(a[i].z, zr[i+1]) == a[i+1].z. zr[0] is ignored. The x and y 78 * coordinates of the result are stored in r, the common z coordinate is 79 * stored in globalz. */ 80 static void vet_secp256k1_ge_globalz_set_table_gej(size_t len, vet_secp256k1_ge *r, vet_secp256k1_fe *globalz, const vet_secp256k1_gej *a, const vet_secp256k1_fe *zr); 81 82 /** Set a group element (jacobian) equal to the point at infinity. */ 83 static void vet_secp256k1_gej_set_infinity(vet_secp256k1_gej *r); 84 85 /** Set a group element (jacobian) equal to another which is given in affine coordinates. */ 86 static void vet_secp256k1_gej_set_ge(vet_secp256k1_gej *r, const vet_secp256k1_ge *a); 87 88 /** Compare the X coordinate of a group element (jacobian). */ 89 static int vet_secp256k1_gej_eq_x_var(const vet_secp256k1_fe *x, const vet_secp256k1_gej *a); 90 91 /** Set r equal to the inverse of a (i.e., mirrored around the X axis) */ 92 static void vet_secp256k1_gej_neg(vet_secp256k1_gej *r, const vet_secp256k1_gej *a); 93 94 /** Check whether a group element is the point at infinity. */ 95 static int vet_secp256k1_gej_is_infinity(const vet_secp256k1_gej *a); 96 97 /** Check whether a group element's y coordinate is a quadratic residue. */ 98 static int vet_secp256k1_gej_has_quad_y_var(const vet_secp256k1_gej *a); 99 100 /** 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). 101 * a may not be zero. Constant time. */ 102 static void vet_secp256k1_gej_double_nonzero(vet_secp256k1_gej *r, const vet_secp256k1_gej *a, vet_secp256k1_fe *rzr); 103 104 /** 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). */ 105 static void vet_secp256k1_gej_double_var(vet_secp256k1_gej *r, const vet_secp256k1_gej *a, vet_secp256k1_fe *rzr); 106 107 /** 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). */ 108 static void vet_secp256k1_gej_add_var(vet_secp256k1_gej *r, const vet_secp256k1_gej *a, const vet_secp256k1_gej *b, vet_secp256k1_fe *rzr); 109 110 /** Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity). */ 111 static void vet_secp256k1_gej_add_ge(vet_secp256k1_gej *r, const vet_secp256k1_gej *a, const vet_secp256k1_ge *b); 112 113 /** Set r equal to the sum of a and b (with b given in affine coordinates). This is more efficient 114 than secp256k1_gej_add_var. It is identical to secp256k1_gej_add_ge but without constant-time 115 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). */ 116 static void vet_secp256k1_gej_add_ge_var(vet_secp256k1_gej *r, const vet_secp256k1_gej *a, const vet_secp256k1_ge *b, vet_secp256k1_fe *rzr); 117 118 /** Set r equal to the sum of a and b (with the inverse of b's Z coordinate passed as bzinv). */ 119 static void vet_secp256k1_gej_add_zinv_var(vet_secp256k1_gej *r, const vet_secp256k1_gej *a, const vet_secp256k1_ge *b, const vet_secp256k1_fe *bzinv); 120 121 #ifdef USE_ENDOMORPHISM 122 /** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */ 123 static void vet_secp256k1_ge_mul_lambda(vet_secp256k1_ge *r, const vet_secp256k1_ge *a); 124 #endif 125 126 /** Clear a secp256k1_gej to prevent leaking sensitive information. */ 127 static void vet_secp256k1_gej_clear(vet_secp256k1_gej *r); 128 129 /** Clear a secp256k1_ge to prevent leaking sensitive information. */ 130 static void vet_secp256k1_ge_clear(vet_secp256k1_ge *r); 131 132 /** Convert a group element to the storage type. */ 133 static void vet_secp256k1_ge_to_storage(vet_secp256k1_ge_storage *r, const vet_secp256k1_ge *a); 134 135 /** Convert a group element back from the storage type. */ 136 static void vet_secp256k1_ge_from_storage(vet_secp256k1_ge *r, const vet_secp256k1_ge_storage *a); 137 138 /** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */ 139 static void vet_secp256k1_ge_storage_cmov(vet_secp256k1_ge_storage *r, const vet_secp256k1_ge_storage *a, int flag); 140 141 /** Rescale a jacobian point by b which must be non-zero. Constant-time. */ 142 static void vet_secp256k1_gej_rescale(vet_secp256k1_gej *r, const vet_secp256k1_fe *b); 143 144 #endif