7 #ifndef SECP256K1_MODULE_EXTRAKEYS_MAIN_H 8 #define SECP256K1_MODULE_EXTRAKEYS_MAIN_H 10 #include "../../../include/secp256k1.h" 11 #include "../../../include/secp256k1_extrakeys.h" 26 ARG_CHECK(pubkey != NULL);
27 memset(pubkey, 0,
sizeof(*pubkey));
28 ARG_CHECK(input32 != NULL);
30 if (!secp256k1_fe_set_b32(&x, input32)) {
33 if (!secp256k1_ge_set_xo_var(&pk, &x, 0)) {
36 if (!secp256k1_ge_is_in_correct_subgroup(&pk)) {
39 secp256k1_xonly_pubkey_save(pubkey, &pk);
47 ARG_CHECK(output32 != NULL);
48 memset(output32, 0, 32);
49 ARG_CHECK(pubkey != NULL);
51 if (!secp256k1_xonly_pubkey_load(ctx, &pk, pubkey)) {
54 secp256k1_fe_get_b32(output32, &pk.
x);
59 unsigned char out[2][32];
64 pk[0] = pk0; pk[1] = pk1;
65 for (i = 0; i < 2; i++) {
78 memset(out[i], 0,
sizeof(out[i]));
81 return secp256k1_memcmp_var(out[0], out[1],
sizeof(out[1]));
87 static int secp256k1_extrakeys_ge_even_y(
secp256k1_ge *r) {
91 if (secp256k1_fe_is_odd(&r->
y)) {
92 secp256k1_fe_negate(&r->
y, &r->
y, 1);
103 ARG_CHECK(xonly_pubkey != NULL);
104 ARG_CHECK(pubkey != NULL);
106 if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
109 tmp = secp256k1_extrakeys_ge_even_y(&pk);
110 if (pk_parity != NULL) {
113 secp256k1_xonly_pubkey_save(xonly_pubkey, &pk);
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);
126 if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
127 || !secp256k1_ec_pubkey_tweak_add_helper(&pk, tweak32)) {
130 secp256k1_pubkey_save(output_pubkey, &pk);
136 unsigned char pk_expected32[32];
139 ARG_CHECK(internal_pubkey != NULL);
140 ARG_CHECK(tweaked_pubkey32 != NULL);
141 ARG_CHECK(tweak32 != NULL);
143 if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
144 || !secp256k1_ec_pubkey_tweak_add_helper(&pk, tweak32)) {
147 secp256k1_fe_normalize_var(&pk.
x);
148 secp256k1_fe_normalize_var(&pk.
y);
149 secp256k1_fe_get_b32(pk_expected32, &pk.
x);
151 return secp256k1_memcmp_var(&pk_expected32, tweaked_pubkey32, 32) == 0
152 && secp256k1_fe_is_odd(&pk.
y) == tweaked_pk_parity;
156 secp256k1_scalar_get_b32(&
keypair->data[0], sk);
164 ret = secp256k1_scalar_set_b32_seckey(sk, &
keypair->data[0]);
167 secp256k1_declassify(ctx, &ret,
sizeof(ret));
181 secp256k1_declassify(ctx, pubkey,
sizeof(*pubkey));
182 ret = secp256k1_pubkey_load(ctx, pk, pubkey);
184 ret = ret && secp256k1_keypair_seckey_load(ctx, sk,
keypair);
187 *pk = secp256k1_ge_const_g;
189 *sk = secp256k1_scalar_one;
202 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
203 ARG_CHECK(seckey32 != NULL);
205 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &sk, &pk, seckey32);
206 secp256k1_keypair_save(
keypair, &sk, &pk);
209 secp256k1_scalar_clear(&sk);
215 ARG_CHECK(seckey != NULL);
216 memset(seckey, 0, 32);
225 ARG_CHECK(pubkey != NULL);
226 memset(pubkey, 0,
sizeof(*pubkey));
238 ARG_CHECK(pubkey != NULL);
239 memset(pubkey, 0,
sizeof(*pubkey));
242 if (!secp256k1_keypair_load(ctx, NULL, &pk,
keypair)) {
245 tmp = secp256k1_extrakeys_ge_even_y(&pk);
246 if (pk_parity != NULL) {
249 secp256k1_xonly_pubkey_save(pubkey, &pk);
262 ARG_CHECK(tweak32 != NULL);
264 ret = secp256k1_keypair_load(ctx, &sk, &pk,
keypair);
267 y_parity = secp256k1_extrakeys_ge_even_y(&pk);
269 secp256k1_scalar_negate(&sk, &sk);
272 ret &= secp256k1_ec_seckey_tweak_add_helper(&sk, tweak32);
273 ret &= secp256k1_ec_pubkey_tweak_add_helper(&pk, tweak32);
275 secp256k1_declassify(ctx, &ret,
sizeof(ret));
277 secp256k1_keypair_save(
keypair, &sk, &pk);
280 secp256k1_scalar_clear(&sk);
#define VERIFY_CHECK(cond)
std::vector< std::string > keypair
int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey)
int secp256k1_xonly_pubkey_parse(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32)
int secp256k1_keypair_pub(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair)
int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey)
struct secp256k1_context_struct secp256k1_context
int secp256k1_keypair_sec(const secp256k1_context *ctx, unsigned char *seckey, const secp256k1_keypair *keypair)
int secp256k1_keypair_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32)
int secp256k1_xonly_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32)
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)
void * memcpy(void *a, const void *b, size_t c)
int secp256k1_keypair_xonly_pub(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair)
int secp256k1_xonly_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_xonly_pubkey *pk0, const secp256k1_xonly_pubkey *pk1)
int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey32)