1 /***********************************************************************
2 * Copyright (c) 2020 Jonas Nick *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
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"
13 static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context* ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey) {
14 return secp256k1_pubkey_load(ctx, ge, (const secp256k1_pubkey *) pubkey);
17 static SECP256K1_INLINE void secp256k1_xonly_pubkey_save(secp256k1_xonly_pubkey *pubkey, secp256k1_ge *ge) {
18 secp256k1_pubkey_save((secp256k1_pubkey *) pubkey, ge);
21 int secp256k1_xonly_pubkey_parse(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) {
25 VERIFY_CHECK(ctx != NULL);
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);
43 int secp256k1_xonly_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) {
46 VERIFY_CHECK(ctx != NULL);
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);
58 /** Keeps a group element as is if it has an even Y and otherwise negates it.
59 * y_parity is set to 0 in the former case and to 1 in the latter case.
60 * Requires that the coordinates of r are normalized. */
61 static int secp256k1_extrakeys_ge_even_y(secp256k1_ge *r) {
63 VERIFY_CHECK(!secp256k1_ge_is_infinity(r));
65 if (secp256k1_fe_is_odd(&r->y)) {
66 secp256k1_fe_negate(&r->y, &r->y, 1);
72 int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context* ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) {
76 VERIFY_CHECK(ctx != NULL);
77 ARG_CHECK(xonly_pubkey != NULL);
78 ARG_CHECK(pubkey != NULL);
80 if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
83 tmp = secp256k1_extrakeys_ge_even_y(&pk);
84 if (pk_parity != NULL) {
87 secp256k1_xonly_pubkey_save(xonly_pubkey, &pk);
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) {
94 VERIFY_CHECK(ctx != NULL);
95 ARG_CHECK(output_pubkey != NULL);
96 memset(output_pubkey, 0, sizeof(*output_pubkey));
97 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
98 ARG_CHECK(internal_pubkey != NULL);
99 ARG_CHECK(tweak32 != NULL);
101 if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
102 || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
105 secp256k1_pubkey_save(output_pubkey, &pk);
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) {
111 unsigned char pk_expected32[32];
113 VERIFY_CHECK(ctx != NULL);
114 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
115 ARG_CHECK(internal_pubkey != NULL);
116 ARG_CHECK(tweaked_pubkey32 != NULL);
117 ARG_CHECK(tweak32 != NULL);
119 if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
120 || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
123 secp256k1_fe_normalize_var(&pk.x);
124 secp256k1_fe_normalize_var(&pk.y);
125 secp256k1_fe_get_b32(pk_expected32, &pk.x);
127 return secp256k1_memcmp_var(&pk_expected32, tweaked_pubkey32, 32) == 0
128 && secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity;
131 static void secp256k1_keypair_save(secp256k1_keypair *keypair, const secp256k1_scalar *sk, secp256k1_ge *pk) {
132 secp256k1_scalar_get_b32(&keypair->data[0], sk);
133 secp256k1_pubkey_save((secp256k1_pubkey *)&keypair->data[32], pk);
137 static int secp256k1_keypair_seckey_load(const secp256k1_context* ctx, secp256k1_scalar *sk, const secp256k1_keypair *keypair) {
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));
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. */
151 static int secp256k1_keypair_load(const secp256k1_context* ctx, secp256k1_scalar *sk, secp256k1_ge *pk, const secp256k1_keypair *keypair) {
153 const secp256k1_pubkey *pubkey = (const secp256k1_pubkey *)&keypair->data[32];
155 /* Need to declassify the pubkey because pubkey_load ARG_CHECKs if it's
157 secp256k1_declassify(ctx, pubkey, sizeof(*pubkey));
158 ret = secp256k1_pubkey_load(ctx, pk, pubkey);
160 ret = ret && secp256k1_keypair_seckey_load(ctx, sk, keypair);
163 *pk = secp256k1_ge_const_g;
165 *sk = secp256k1_scalar_one;
171 int secp256k1_keypair_create(const secp256k1_context* ctx, secp256k1_keypair *keypair, const unsigned char *seckey32) {
175 VERIFY_CHECK(ctx != NULL);
176 ARG_CHECK(keypair != NULL);
177 memset(keypair, 0, sizeof(*keypair));
178 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
179 ARG_CHECK(seckey32 != NULL);
181 ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &sk, &pk, seckey32);
182 secp256k1_keypair_save(keypair, &sk, &pk);
183 secp256k1_memczero(keypair, sizeof(*keypair), !ret);
185 secp256k1_scalar_clear(&sk);
189 int secp256k1_keypair_sec(const secp256k1_context* ctx, unsigned char *seckey, const secp256k1_keypair *keypair) {
190 VERIFY_CHECK(ctx != NULL);
191 ARG_CHECK(seckey != NULL);
192 memset(seckey, 0, 32);
193 ARG_CHECK(keypair != NULL);
195 memcpy(seckey, &keypair->data[0], 32);
199 int secp256k1_keypair_pub(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) {
200 VERIFY_CHECK(ctx != NULL);
201 ARG_CHECK(pubkey != NULL);
202 memset(pubkey, 0, sizeof(*pubkey));
203 ARG_CHECK(keypair != NULL);
205 memcpy(pubkey->data, &keypair->data[32], sizeof(*pubkey));
209 int secp256k1_keypair_xonly_pub(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) {
213 VERIFY_CHECK(ctx != NULL);
214 ARG_CHECK(pubkey != NULL);
215 memset(pubkey, 0, sizeof(*pubkey));
216 ARG_CHECK(keypair != NULL);
218 if (!secp256k1_keypair_load(ctx, NULL, &pk, keypair)) {
221 tmp = secp256k1_extrakeys_ge_even_y(&pk);
222 if (pk_parity != NULL) {
225 secp256k1_xonly_pubkey_save(pubkey, &pk);
230 int secp256k1_keypair_xonly_tweak_add(const secp256k1_context* ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) {
236 VERIFY_CHECK(ctx != NULL);
237 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
238 ARG_CHECK(keypair != NULL);
239 ARG_CHECK(tweak32 != NULL);
241 ret = secp256k1_keypair_load(ctx, &sk, &pk, keypair);
242 memset(keypair, 0, sizeof(*keypair));
244 y_parity = secp256k1_extrakeys_ge_even_y(&pk);
246 secp256k1_scalar_negate(&sk, &sk);
249 ret &= secp256k1_ec_seckey_tweak_add_helper(&sk, tweak32);
250 ret &= secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32);
252 secp256k1_declassify(ctx, &ret, sizeof(ret));
254 secp256k1_keypair_save(keypair, &sk, &pk);
257 secp256k1_scalar_clear(&sk);