Bitcoin Core  0.20.99
P2P Digital Currency
main_impl.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2020 Jonas Nick *
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_MODULE_EXTRAKEYS_MAIN_
8 #define _SECP256K1_MODULE_EXTRAKEYS_MAIN_
9 
10 #include "include/secp256k1.h"
12 
14  return secp256k1_pubkey_load(ctx, ge, (const secp256k1_pubkey *) pubkey);
15 }
16 
19 }
20 
21 int secp256k1_xonly_pubkey_parse(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) {
22  secp256k1_ge pk;
23  secp256k1_fe x;
24 
25  VERIFY_CHECK(ctx != NULL);
26  ARG_CHECK(pubkey != NULL);
27  memset(pubkey, 0, sizeof(*pubkey));
28  ARG_CHECK(input32 != NULL);
29 
30  if (!secp256k1_fe_set_b32(&x, input32)) {
31  return 0;
32  }
33  if (!secp256k1_ge_set_xo_var(&pk, &x, 0)) {
34  return 0;
35  }
37  return 0;
38  }
39  secp256k1_xonly_pubkey_save(pubkey, &pk);
40  return 1;
41 }
42 
43 int secp256k1_xonly_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) {
44  secp256k1_ge pk;
45 
46  VERIFY_CHECK(ctx != NULL);
47  ARG_CHECK(output32 != NULL);
48  memset(output32, 0, 32);
49  ARG_CHECK(pubkey != NULL);
50 
51  if (!secp256k1_xonly_pubkey_load(ctx, &pk, pubkey)) {
52  return 0;
53  }
54  secp256k1_fe_get_b32(output32, &pk.x);
55  return 1;
56 }
57 
62  int y_parity = 0;
64 
65  if (secp256k1_fe_is_odd(&r->y)) {
66  secp256k1_fe_negate(&r->y, &r->y, 1);
67  y_parity = 1;
68  }
69  return y_parity;
70 }
71 
72 int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context* ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) {
73  secp256k1_ge pk;
74  int tmp;
75 
76  VERIFY_CHECK(ctx != NULL);
77  ARG_CHECK(xonly_pubkey != NULL);
78  ARG_CHECK(pubkey != NULL);
79 
80  if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
81  return 0;
82  }
84  if (pk_parity != NULL) {
85  *pk_parity = tmp;
86  }
87  secp256k1_xonly_pubkey_save(xonly_pubkey, &pk);
88  return 1;
89 }
90 
91 int secp256k1_xonly_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) {
92  secp256k1_ge pk;
93 
94  VERIFY_CHECK(ctx != NULL);
95  ARG_CHECK(output_pubkey != NULL);
96  memset(output_pubkey, 0, sizeof(*output_pubkey));
98  ARG_CHECK(internal_pubkey != NULL);
99  ARG_CHECK(tweak32 != NULL);
100 
101  if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
102  || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
103  return 0;
104  }
105  secp256k1_pubkey_save(output_pubkey, &pk);
106  return 1;
107 }
108 
109 int secp256k1_xonly_pubkey_tweak_add_check(const secp256k1_context* ctx, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) {
110  secp256k1_ge pk;
111  unsigned char pk_expected32[32];
112 
113  VERIFY_CHECK(ctx != NULL);
115  ARG_CHECK(internal_pubkey != NULL);
116  ARG_CHECK(tweaked_pubkey32 != NULL);
117  ARG_CHECK(tweak32 != NULL);
118 
119  if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
120  || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
121  return 0;
122  }
125  secp256k1_fe_get_b32(pk_expected32, &pk.x);
126 
127  return secp256k1_memcmp_var(&pk_expected32, tweaked_pubkey32, 32) == 0
128  && secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity;
129 }
130 
132  secp256k1_scalar_get_b32(&keypair->data[0], sk);
133  secp256k1_pubkey_save((secp256k1_pubkey *)&keypair->data[32], pk);
134 }
135 
136 
138  int ret;
139 
140  ret = secp256k1_scalar_set_b32_seckey(sk, &keypair->data[0]);
141  /* We can declassify ret here because sk is only zero if a keypair function
142  * failed (which zeroes the keypair) and its return value is ignored. */
143  secp256k1_declassify(ctx, &ret, sizeof(ret));
144  ARG_CHECK(ret);
145  return ret;
146 }
147 
148 /* Load a keypair into pk and sk (if non-NULL). This function declassifies pk
149  * and ARG_CHECKs that the keypair is not invalid. It always initializes sk and
150  * pk with dummy values. */
152  int ret;
153  const secp256k1_pubkey *pubkey = (const secp256k1_pubkey *)&keypair->data[32];
154 
155  /* Need to declassify the pubkey because pubkey_load ARG_CHECKs if it's
156  * invalid. */
157  secp256k1_declassify(ctx, pubkey, sizeof(*pubkey));
158  ret = secp256k1_pubkey_load(ctx, pk, pubkey);
159  if (sk != NULL) {
160  ret = ret && secp256k1_keypair_seckey_load(ctx, sk, keypair);
161  }
162  if (!ret) {
163  *pk = secp256k1_ge_const_g;
164  if (sk != NULL) {
165  *sk = secp256k1_scalar_one;
166  }
167  }
168  return ret;
169 }
170 
171 int secp256k1_keypair_create(const secp256k1_context* ctx, secp256k1_keypair *keypair, const unsigned char *seckey32) {
172  secp256k1_scalar sk;
173  secp256k1_ge pk;
174  int ret = 0;
175  VERIFY_CHECK(ctx != NULL);
176  ARG_CHECK(keypair != NULL);
177  memset(keypair, 0, sizeof(*keypair));
179  ARG_CHECK(seckey32 != NULL);
180 
181  ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &sk, &pk, seckey32);
182  secp256k1_keypair_save(keypair, &sk, &pk);
183  memczero(keypair, sizeof(*keypair), !ret);
184 
186  return ret;
187 }
188 
190  VERIFY_CHECK(ctx != NULL);
191  ARG_CHECK(pubkey != NULL);
192  memset(pubkey, 0, sizeof(*pubkey));
193  ARG_CHECK(keypair != NULL);
194 
195  memcpy(pubkey->data, &keypair->data[32], sizeof(*pubkey));
196  return 1;
197 }
198 
199 int secp256k1_keypair_xonly_pub(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) {
200  secp256k1_ge pk;
201  int tmp;
202 
203  VERIFY_CHECK(ctx != NULL);
204  ARG_CHECK(pubkey != NULL);
205  memset(pubkey, 0, sizeof(*pubkey));
206  ARG_CHECK(keypair != NULL);
207 
208  if (!secp256k1_keypair_load(ctx, NULL, &pk, keypair)) {
209  return 0;
210  }
212  if (pk_parity != NULL) {
213  *pk_parity = tmp;
214  }
215  secp256k1_xonly_pubkey_save(pubkey, &pk);
216 
217  return 1;
218 }
219 
220 int secp256k1_keypair_xonly_tweak_add(const secp256k1_context* ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) {
221  secp256k1_ge pk;
222  secp256k1_scalar sk;
223  int y_parity;
224  int ret;
225 
226  VERIFY_CHECK(ctx != NULL);
228  ARG_CHECK(keypair != NULL);
229  ARG_CHECK(tweak32 != NULL);
230 
231  ret = secp256k1_keypair_load(ctx, &sk, &pk, keypair);
232  memset(keypair, 0, sizeof(*keypair));
233 
234  y_parity = secp256k1_extrakeys_ge_even_y(&pk);
235  if (y_parity == 1) {
236  secp256k1_scalar_negate(&sk, &sk);
237  }
238 
239  ret &= secp256k1_ec_seckey_tweak_add_helper(&sk, tweak32);
240  ret &= secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32);
241 
242  secp256k1_declassify(ctx, &ret, sizeof(ret));
243  if (ret) {
244  secp256k1_keypair_save(keypair, &sk, &pk);
245  }
246 
248  return ret;
249 }
250 
251 #endif
static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
#define VERIFY_CHECK(cond)
Definition: util.h:68
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
Normalize a field element, without constant-time guarantee.
int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey)
Serialize an xonly_pubkey object into a 32-byte sequence.
Definition: main_impl.h:43
int secp256k1_xonly_pubkey_parse(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32)
Parse a 32-byte sequence into a xonly_pubkey object.
Definition: main_impl.h:21
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
Set a field element equal to the additive inverse of another.
static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context *ecmult_ctx, secp256k1_ge *p, const unsigned char *tweak)
Definition: secp256k1.c:655
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak)
Definition: secp256k1.c:624
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:263
static int secp256k1_extrakeys_ge_even_y(secp256k1_ge *r)
Keeps a group element as is if it has an even Y and otherwise negates it.
Definition: main_impl.h:61
static int secp256k1_keypair_load(const secp256k1_context *ctx, secp256k1_scalar *sk, secp256k1_ge *pk, const secp256k1_keypair *keypair)
Definition: main_impl.h:151
int secp256k1_keypair_pub(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair)
Get the public key from a keypair.
Definition: main_impl.h:189
int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey)
Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
Definition: main_impl.h:72
static int secp256k1_fe_is_odd(const secp256k1_fe *a)
Check the "oddness" of a field element.
static const secp256k1_ge secp256k1_ge_const_g
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2...
Definition: group_impl.h:53
#define SECP256K1_INLINE
Definition: secp256k1.h:124
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:234
Opaque data structure that holds a parsed and valid "x-only" public key.
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:72
#define ARG_CHECK(cond)
Definition: secp256k1.c:29
static secp256k1_context * ctx
Definition: tests.c:36
static int secp256k1_ec_pubkey_create_helper(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_scalar *seckey_scalar, secp256k1_ge *p, const unsigned char *seckey)
Definition: secp256k1.c:559
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve...
int secp256k1_keypair_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32)
Tweak a keypair by adding tweak32 to the secret key and updating the public key accordingly.
Definition: main_impl.h:220
static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned char *bin)
Set a scalar from a big endian byte array and returns 1 if it is a valid seckey and 0 otherwise...
secp256k1_ecmult_context ecmult_ctx
Definition: secp256k1.c:71
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:14
secp256k1_fe x
Definition: group.h:15
static SECP256K1_INLINE void secp256k1_xonly_pubkey_save(secp256k1_xonly_pubkey *pubkey, secp256k1_ge *ge)
Definition: main_impl.h:17
int secp256k1_xonly_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32)
Tweak an x-only public key by adding the generator multiplied with tweak32 to it. ...
Definition: main_impl.h:91
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
Opaque data structure that holds a keypair consisting of a secret and a public key.
static int secp256k1_keypair_seckey_load(const secp256k1_context *ctx, secp256k1_scalar *sk, const secp256k1_keypair *keypair)
Definition: main_impl.h:137
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Set a group element (affine) equal to the point with the given X coordinate, and given oddness for Y...
int secp256k1_xonly_pubkey_tweak_add_check(const secp256k1_context *ctx, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32)
Checks that a tweaked pubkey is the result of calling secp256k1_xonly_pubkey_tweak_add with internal_...
Definition: main_impl.h:109
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
static void secp256k1_keypair_save(secp256k1_keypair *keypair, const secp256k1_scalar *sk, secp256k1_ge *pk)
Definition: main_impl.h:131
unsigned char data[64]
Definition: secp256k1.h:68
void * memcpy(void *a, const void *b, size_t c)
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:224
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:244
int secp256k1_keypair_xonly_pub(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair)
Get the x-only public key from a keypair.
Definition: main_impl.h:199
static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey)
Definition: main_impl.h:13
secp256k1_fe y
Definition: group.h:16
static const secp256k1_scalar secp256k1_scalar_one
Definition: scalar_impl.h:31
static SECP256K1_INLINE void memczero(void *s, size_t len, int flag)
Definition: util.h:205
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)
int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey32)
Compute the keypair for a secret key.
Definition: main_impl.h:171
unsigned char data[96]
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:67