Electroneum
secp256k1_extrakeys.h File Reference
#include "secp256k1.h"
Include dependency graph for secp256k1_extrakeys.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  secp256k1_xonly_pubkey
 
struct  secp256k1_keypair
 

Functions

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse (const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 
SECP256K1_API int secp256k1_xonly_pubkey_serialize (const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 
SECP256K1_API int secp256k1_xonly_pubkey_cmp (const secp256k1_context *ctx, const secp256k1_xonly_pubkey *pk1, const secp256k1_xonly_pubkey *pk2) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey (const secp256k1_context *ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add (const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create (const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec (const secp256k1_context *ctx, unsigned char *seckey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub (const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub (const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
 
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add (const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
 

Function Documentation

◆ secp256k1_keypair_create()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create ( const secp256k1_context ctx,
secp256k1_keypair keypair,
const unsigned char *  seckey 
)

Compute the keypair for a secret key.

Returns: 1: secret was valid, keypair is ready to use 0: secret was invalid, try again with a different secret Args: ctx: pointer to a context object (not secp256k1_context_static). Out: keypair: pointer to the created keypair. In: seckey: pointer to a 32-byte secret key.

Definition at line 195 of file main_impl.h.

195  {
196  secp256k1_scalar sk;
197  secp256k1_ge pk;
198  int ret = 0;
199  VERIFY_CHECK(ctx != NULL);
200  ARG_CHECK(keypair != NULL);
201  memset(keypair, 0, sizeof(*keypair));
202  ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
203  ARG_CHECK(seckey32 != NULL);
204 
205  ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &sk, &pk, seckey32);
206  secp256k1_keypair_save(keypair, &sk, &pk);
207  secp256k1_memczero(keypair, sizeof(*keypair), !ret);
208 
209  secp256k1_scalar_clear(&sk);
210  return ret;
211 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
std::vector< std::string > keypair

◆ secp256k1_keypair_pub()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub ( const secp256k1_context ctx,
secp256k1_pubkey pubkey,
const secp256k1_keypair keypair 
)

Get the public key from a keypair.

Returns: 1 always. Args: ctx: pointer to a context object. Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to the keypair public key. If not, it's set to an invalid value. In: keypair: pointer to a keypair.

Definition at line 223 of file main_impl.h.

223  {
224  VERIFY_CHECK(ctx != NULL);
225  ARG_CHECK(pubkey != NULL);
226  memset(pubkey, 0, sizeof(*pubkey));
227  ARG_CHECK(keypair != NULL);
228 
229  memcpy(pubkey->data, &keypair->data[32], sizeof(*pubkey));
230  return 1;
231 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
std::vector< std::string > keypair
unsigned char data[64]
Definition: secp256k1.h:75
void * memcpy(void *a, const void *b, size_t c)
Here is the call graph for this function:

◆ secp256k1_keypair_sec()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec ( const secp256k1_context ctx,
unsigned char *  seckey,
const secp256k1_keypair keypair 
)

Get the secret key from a keypair.

Returns: 1 always. Args: ctx: pointer to a context object. Out: seckey: pointer to a 32-byte buffer for the secret key. In: keypair: pointer to a keypair.

Definition at line 213 of file main_impl.h.

213  {
214  VERIFY_CHECK(ctx != NULL);
215  ARG_CHECK(seckey != NULL);
216  memset(seckey, 0, 32);
217  ARG_CHECK(keypair != NULL);
218 
219  memcpy(seckey, &keypair->data[0], 32);
220  return 1;
221 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
std::vector< std::string > keypair
void * memcpy(void *a, const void *b, size_t c)
Here is the call graph for this function:

◆ secp256k1_keypair_xonly_pub()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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.

This is the same as calling secp256k1_keypair_pub and then secp256k1_xonly_pubkey_from_pubkey.

Returns: 1 always. Args: ctx: pointer to a context object. Out: pubkey: pointer to an xonly_pubkey object. If 1 is returned, it is set to the keypair public key after converting it to an xonly_pubkey. If not, it's set to an invalid value. pk_parity: Ignored if NULL. Otherwise, pointer to an integer that will be set to the pk_parity argument of secp256k1_xonly_pubkey_from_pubkey. In: keypair: pointer to a keypair.

Definition at line 233 of file main_impl.h.

233  {
234  secp256k1_ge pk;
235  int tmp;
236 
237  VERIFY_CHECK(ctx != NULL);
238  ARG_CHECK(pubkey != NULL);
239  memset(pubkey, 0, sizeof(*pubkey));
240  ARG_CHECK(keypair != NULL);
241 
242  if (!secp256k1_keypair_load(ctx, NULL, &pk, keypair)) {
243  return 0;
244  }
245  tmp = secp256k1_extrakeys_ge_even_y(&pk);
246  if (pk_parity != NULL) {
247  *pk_parity = tmp;
248  }
249  secp256k1_xonly_pubkey_save(pubkey, &pk);
250 
251  return 1;
252 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
std::vector< std::string > keypair

◆ secp256k1_keypair_xonly_tweak_add()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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.

Calling this function and then secp256k1_keypair_pub results in the same public key as calling secp256k1_keypair_xonly_pub and then secp256k1_xonly_pubkey_tweak_add.

Returns: 0 if the arguments are invalid or the resulting keypair would be invalid (only when the tweak is the negation of the keypair's secret key). 1 otherwise.

Args: ctx: pointer to a context object. In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to an invalid value if this function returns 0. In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to secp256k1_ec_seckey_verify, this function returns 0. For uniformly random 32-byte arrays the chance of being invalid is negligible (around 1 in 2^128).

Definition at line 254 of file main_impl.h.

254  {
255  secp256k1_ge pk;
256  secp256k1_scalar sk;
257  int y_parity;
258  int ret;
259 
260  VERIFY_CHECK(ctx != NULL);
261  ARG_CHECK(keypair != NULL);
262  ARG_CHECK(tweak32 != NULL);
263 
264  ret = secp256k1_keypair_load(ctx, &sk, &pk, keypair);
265  memset(keypair, 0, sizeof(*keypair));
266 
267  y_parity = secp256k1_extrakeys_ge_even_y(&pk);
268  if (y_parity == 1) {
269  secp256k1_scalar_negate(&sk, &sk);
270  }
271 
272  ret &= secp256k1_ec_seckey_tweak_add_helper(&sk, tweak32);
273  ret &= secp256k1_ec_pubkey_tweak_add_helper(&pk, tweak32);
274 
275  secp256k1_declassify(ctx, &ret, sizeof(ret));
276  if (ret) {
277  secp256k1_keypair_save(keypair, &sk, &pk);
278  }
279 
280  secp256k1_scalar_clear(&sk);
281  return ret;
282 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
std::vector< std::string > keypair

◆ secp256k1_xonly_pubkey_cmp()

SECP256K1_API int secp256k1_xonly_pubkey_cmp ( const secp256k1_context ctx,
const secp256k1_xonly_pubkey pk1,
const secp256k1_xonly_pubkey pk2 
)

Compare two x-only public keys using lexicographic order

Returns: <0 if the first public key is less than the second >0 if the first public key is greater than the second 0 if the two public keys are equal Args: ctx: a secp256k1 context object. In: pubkey1: first public key to compare pubkey2: second public key to compare

Definition at line 58 of file main_impl.h.

58  {
59  unsigned char out[2][32];
60  const secp256k1_xonly_pubkey* pk[2];
61  int i;
62 
63  VERIFY_CHECK(ctx != NULL);
64  pk[0] = pk0; pk[1] = pk1;
65  for (i = 0; i < 2; i++) {
66  /* If the public key is NULL or invalid, xonly_pubkey_serialize will
67  * call the illegal_callback and return 0. In that case we will
68  * serialize the key as all zeros which is less than any valid public
69  * key. This results in consistent comparisons even if NULL or invalid
70  * pubkeys are involved and prevents edge cases such as sorting
71  * algorithms that use this function and do not terminate as a
72  * result. */
73  if (!secp256k1_xonly_pubkey_serialize(ctx, out[i], pk[i])) {
74  /* Note that xonly_pubkey_serialize should already set the output to
75  * zero in that case, but it's not guaranteed by the API, we can't
76  * test it and writing a VERIFY_CHECK is more complex than
77  * explicitly memsetting (again). */
78  memset(out[i], 0, sizeof(out[i]));
79  }
80  }
81  return secp256k1_memcmp_var(out[0], out[1], sizeof(out[1]));
82 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey)
Definition: main_impl.h:43
Here is the call graph for this function:

◆ secp256k1_xonly_pubkey_from_pubkey()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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.

Returns: 1 always.

Args: ctx: pointer to a context object. Out: xonly_pubkey: pointer to an x-only public key object for placing the converted public key. pk_parity: Ignored if NULL. Otherwise, pointer to an integer that will be set to 1 if the point encoded by xonly_pubkey is the negation of the pubkey and set to 0 otherwise. In: pubkey: pointer to a public key that is converted.

Definition at line 98 of file main_impl.h.

98  {
99  secp256k1_ge pk;
100  int tmp;
101 
102  VERIFY_CHECK(ctx != NULL);
103  ARG_CHECK(xonly_pubkey != NULL);
104  ARG_CHECK(pubkey != NULL);
105 
106  if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
107  return 0;
108  }
109  tmp = secp256k1_extrakeys_ge_even_y(&pk);
110  if (pk_parity != NULL) {
111  *pk_parity = tmp;
112  }
113  secp256k1_xonly_pubkey_save(xonly_pubkey, &pk);
114  return 1;
115 }
#define VERIFY_CHECK(cond)
Definition: util.h:96

◆ secp256k1_xonly_pubkey_parse()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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.

Returns: 1 if the public key was fully valid. 0 if the public key could not be parsed or is invalid.

Args: ctx: a secp256k1 context object. Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a parsed version of input. If not, it's set to an invalid value. In: input32: pointer to a serialized xonly_pubkey.

Definition at line 21 of file main_impl.h.

21  {
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  }
36  if (!secp256k1_ge_is_in_correct_subgroup(&pk)) {
37  return 0;
38  }
39  secp256k1_xonly_pubkey_save(pubkey, &pk);
40  return 1;
41 }
#define VERIFY_CHECK(cond)
Definition: util.h:96

◆ secp256k1_xonly_pubkey_serialize()

SECP256K1_API 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.

Returns: 1 always.

Args: ctx: a secp256k1 context object. Out: output32: a pointer to a 32-byte array to place the serialized key in. In: pubkey: a pointer to a secp256k1_xonly_pubkey containing an initialized public key.

Definition at line 43 of file main_impl.h.

43  {
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 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
secp256k1_fe x
Definition: group.h:17
Here is the caller graph for this function:

◆ secp256k1_xonly_pubkey_tweak_add()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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.

Note that the resulting point can not in general be represented by an x-only pubkey because it may have an odd Y coordinate. Instead, the output_pubkey is a normal secp256k1_pubkey.

Returns: 0 if the arguments are invalid or the resulting public key would be invalid (only when the tweak is the negation of the corresponding secret key). 1 otherwise.

Args: ctx: pointer to a context object. Out: output_pubkey: pointer to a public key to store the result. Will be set to an invalid value if this function returns 0. In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to. tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to secp256k1_ec_seckey_verify, this function returns 0. For uniformly random 32-byte arrays the chance of being invalid is negligible (around 1 in 2^128).

Definition at line 117 of file main_impl.h.

117  {
118  secp256k1_ge pk;
119 
120  VERIFY_CHECK(ctx != NULL);
121  ARG_CHECK(output_pubkey != NULL);
122  memset(output_pubkey, 0, sizeof(*output_pubkey));
123  ARG_CHECK(internal_pubkey != NULL);
124  ARG_CHECK(tweak32 != NULL);
125 
126  if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
127  || !secp256k1_ec_pubkey_tweak_add_helper(&pk, tweak32)) {
128  return 0;
129  }
130  secp256k1_pubkey_save(output_pubkey, &pk);
131  return 1;
132 }
#define VERIFY_CHECK(cond)
Definition: util.h:96

◆ secp256k1_xonly_pubkey_tweak_add_check()

SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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_pubkey and tweak32.

The tweaked pubkey is represented by its 32-byte x-only serialization and its pk_parity, which can both be obtained by converting the result of tweak_add to a secp256k1_xonly_pubkey.

Note that this alone does not verify that the tweaked pubkey is a commitment. If the tweak is not chosen in a specific way, the tweaked pubkey can easily be the result of a different internal_pubkey and tweak.

Returns: 0 if the arguments are invalid or the tweaked pubkey is not the result of tweaking the internal_pubkey with tweak32. 1 otherwise. Args: ctx: pointer to a context object. In: tweaked_pubkey32: pointer to a serialized xonly_pubkey. tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization is passed in as tweaked_pubkey32). This must match the pk_parity value that is returned when calling secp256k1_xonly_pubkey with the tweaked pubkey, or this function will fail. internal_pubkey: pointer to an x-only public key object to apply the tweak to. tweak32: pointer to a 32-byte tweak.

Definition at line 134 of file main_impl.h.

134  {
135  secp256k1_ge pk;
136  unsigned char pk_expected32[32];
137 
138  VERIFY_CHECK(ctx != NULL);
139  ARG_CHECK(internal_pubkey != NULL);
140  ARG_CHECK(tweaked_pubkey32 != NULL);
141  ARG_CHECK(tweak32 != NULL);
142 
143  if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
144  || !secp256k1_ec_pubkey_tweak_add_helper(&pk, tweak32)) {
145  return 0;
146  }
147  secp256k1_fe_normalize_var(&pk.x);
148  secp256k1_fe_normalize_var(&pk.y);
149  secp256k1_fe_get_b32(pk_expected32, &pk.x);
150 
151  return secp256k1_memcmp_var(&pk_expected32, tweaked_pubkey32, 32) == 0
152  && secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity;
153 }
#define VERIFY_CHECK(cond)
Definition: util.h:96
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18