]>
Commit | Line | Data |
---|---|---|
0a433ea2 PW |
1 | // Copyright (c) 2013 Pieter Wuille |
2 | // Distributed under the MIT/X11 software license, see the accompanying | |
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | |
4 | ||
b394396b PW |
5 | #ifndef _SECP256K1_GROUP_ |
6 | #define _SECP256K1_GROUP_ | |
7 | ||
607884fc | 8 | #include "num.h" |
b394396b PW |
9 | #include "field.h" |
10 | ||
7fef6619 | 11 | /** A group element of the secp256k1 curve, in affine coordinates. */ |
254327e4 PW |
12 | typedef struct { |
13 | secp256k1_fe_t x; | |
14 | secp256k1_fe_t y; | |
7fef6619 | 15 | int infinity; // whether this represents the point at infinity |
254327e4 PW |
16 | } secp256k1_ge_t; |
17 | ||
7fef6619 | 18 | /** A group element of the secp256k1 curve, in jacobian coordinates. */ |
254327e4 | 19 | typedef struct { |
7fef6619 PW |
20 | secp256k1_fe_t x; // actual X: x/z^2 |
21 | secp256k1_fe_t y; // actual Y: y/z^3 | |
254327e4 | 22 | secp256k1_fe_t z; |
7fef6619 | 23 | int infinity; // whether this represents the point at infinity |
254327e4 PW |
24 | } secp256k1_gej_t; |
25 | ||
7fef6619 | 26 | /** Global constants related to the group */ |
254327e4 | 27 | typedef struct { |
7fef6619 | 28 | secp256k1_num_t order; // the order of the curve (= order of its generator) |
d0b33489 | 29 | secp256k1_num_t half_order; // half the order of the curve (= order of its generator) |
7fef6619 PW |
30 | secp256k1_ge_t g; // the generator point |
31 | ||
4d79bebd | 32 | #ifdef USE_ENDOMORPHISM |
7fef6619 | 33 | // constants related to secp256k1's efficiently computable endomorphism |
254327e4 PW |
34 | secp256k1_fe_t beta; |
35 | secp256k1_num_t lambda, a1b2, b1, a2; | |
4d79bebd | 36 | #endif |
254327e4 PW |
37 | } secp256k1_ge_consts_t; |
38 | ||
f491cd35 | 39 | static const secp256k1_ge_consts_t *secp256k1_ge_consts = NULL; |
254327e4 | 40 | |
7fef6619 | 41 | /** Initialize the group module. */ |
254327e4 | 42 | void static secp256k1_ge_start(void); |
7fef6619 PW |
43 | |
44 | /** De-initialize the group module. */ | |
254327e4 | 45 | void static secp256k1_ge_stop(void); |
7fef6619 PW |
46 | |
47 | /** Set a group element equal to the point at infinity */ | |
254327e4 | 48 | void static secp256k1_ge_set_infinity(secp256k1_ge_t *r); |
7fef6619 PW |
49 | |
50 | /** Set a group element equal to the point with given X and Y coordinates */ | |
254327e4 | 51 | void static secp256k1_ge_set_xy(secp256k1_ge_t *r, const secp256k1_fe_t *x, const secp256k1_fe_t *y); |
7fef6619 | 52 | |
09ca4f32 PD |
53 | /** Set a group element (affine) equal to the point with the given X coordinate, and given oddness |
54 | * for Y. Return value indicates whether the result is valid. */ | |
55 | int static secp256k1_ge_set_xo(secp256k1_ge_t *r, const secp256k1_fe_t *x, int odd); | |
764332d0 | 56 | |
7fef6619 | 57 | /** Check whether a group element is the point at infinity. */ |
254327e4 | 58 | int static secp256k1_ge_is_infinity(const secp256k1_ge_t *a); |
7fef6619 | 59 | |
764332d0 PW |
60 | /** Check whether a group element is valid (i.e., on the curve). */ |
61 | int static secp256k1_ge_is_valid(const secp256k1_ge_t *a); | |
62 | ||
254327e4 | 63 | void static secp256k1_ge_neg(secp256k1_ge_t *r, const secp256k1_ge_t *a); |
7fef6619 PW |
64 | |
65 | /** Get a hex representation of a point. *rlen will be overwritten with the real length. */ | |
254327e4 | 66 | void static secp256k1_ge_get_hex(char *r, int *rlen, const secp256k1_ge_t *a); |
7fef6619 PW |
67 | |
68 | /** Set a group element equal to another which is given in jacobian coordinates */ | |
f11ff5be | 69 | void static secp256k1_ge_set_gej(secp256k1_ge_t *r, secp256k1_gej_t *a); |
254327e4 | 70 | |
f16be77f | 71 | /** Set a batch of group elements equal to the inputs given in jacobian coordinates */ |
da55986f | 72 | void static secp256k1_ge_set_all_gej_var(size_t len, secp256k1_ge_t r[len], const secp256k1_gej_t a[len]); |
f16be77f | 73 | |
7fef6619 PW |
74 | |
75 | /** Set a group element (jacobian) equal to the point at infinity. */ | |
254327e4 | 76 | void static secp256k1_gej_set_infinity(secp256k1_gej_t *r); |
7fef6619 PW |
77 | |
78 | /** Set a group element (jacobian) equal to the point with given X and Y coordinates. */ | |
254327e4 | 79 | void static secp256k1_gej_set_xy(secp256k1_gej_t *r, const secp256k1_fe_t *x, const secp256k1_fe_t *y); |
7fef6619 | 80 | |
7fef6619 | 81 | /** Set a group element (jacobian) equal to another which is given in affine coordinates. */ |
254327e4 | 82 | void static secp256k1_gej_set_ge(secp256k1_gej_t *r, const secp256k1_ge_t *a); |
7fef6619 PW |
83 | |
84 | /** Get the X coordinate of a group element (jacobian). */ | |
da55986f | 85 | void static secp256k1_gej_get_x_var(secp256k1_fe_t *r, const secp256k1_gej_t *a); |
7fef6619 PW |
86 | |
87 | /** Set r equal to the inverse of a (i.e., mirrored around the X axis) */ | |
254327e4 | 88 | void static secp256k1_gej_neg(secp256k1_gej_t *r, const secp256k1_gej_t *a); |
7fef6619 PW |
89 | |
90 | /** Check whether a group element is the point at infinity. */ | |
254327e4 | 91 | int static secp256k1_gej_is_infinity(const secp256k1_gej_t *a); |
7fef6619 | 92 | |
7fef6619 | 93 | /** Set r equal to the double of a. */ |
da55986f | 94 | void static secp256k1_gej_double_var(secp256k1_gej_t *r, const secp256k1_gej_t *a); |
7fef6619 PW |
95 | |
96 | /** Set r equal to the sum of a and b. */ | |
da55986f | 97 | void static secp256k1_gej_add_var(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_gej_t *b); |
7fef6619 | 98 | |
09ca4f32 | 99 | /** Set r equal to the sum of a and b (with b given in affine coordinates). This is more efficient |
7fef6619 | 100 | than secp256k1_gej_add. */ |
da55986f | 101 | void static secp256k1_gej_add_ge_var(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_ge_t *b); |
7fef6619 PW |
102 | |
103 | /** Get a hex representation of a point. *rlen will be overwritten with the real length. */ | |
254327e4 | 104 | void static secp256k1_gej_get_hex(char *r, int *rlen, const secp256k1_gej_t *a); |
7fef6619 | 105 | |
399c03f2 | 106 | #ifdef USE_ENDOMORPHISM |
7fef6619 | 107 | /** Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast. */ |
254327e4 | 108 | void static secp256k1_gej_mul_lambda(secp256k1_gej_t *r, const secp256k1_gej_t *a); |
7fef6619 PW |
109 | |
110 | /** Find r1 and r2 such that r1+r2*lambda = a, and r1 and r2 are maximum 128 bits long (given that a is | |
111 | not more than 256 bits). */ | |
da55986f | 112 | void static secp256k1_gej_split_exp_var(secp256k1_num_t *r1, secp256k1_num_t *r2, const secp256k1_num_t *a); |
399c03f2 | 113 | #endif |
254327e4 | 114 | |
2f6c8019 GM |
115 | /** Clear a secp256k1_gej_t to prevent leaking sensitive information. */ |
116 | void static secp256k1_gej_clear(secp256k1_gej_t *r); | |
117 | ||
118 | /** Clear a secp256k1_ge_t to prevent leaking sensitive information. */ | |
119 | void static secp256k1_ge_clear(secp256k1_ge_t *r); | |
120 | ||
121 | ||
b394396b | 122 | #endif |