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 **********************************************************************/
7 #ifndef _SECP256K1_MODULE_EXTRAKEYS_MAIN_
8 #define _SECP256K1_MODULE_EXTRAKEYS_MAIN_
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 secp256k1_xonly_pubkey_save(pubkey, &pk);
40 int secp256k1_xonly_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) {
43 VERIFY_CHECK(ctx != NULL);
44 ARG_CHECK(output32 != NULL);
45 memset(output32, 0, 32);
46 ARG_CHECK(pubkey != NULL);
48 if (!secp256k1_xonly_pubkey_load(ctx, &pk, pubkey)) {
51 secp256k1_fe_get_b32(output32, &pk.x);
55 /** Keeps a group element as is if it has an even Y and otherwise negates it.
56 * y_parity is set to 0 in the former case and to 1 in the latter case.
57 * Requires that the coordinates of r are normalized. */
58 static int secp256k1_extrakeys_ge_even_y(secp256k1_ge *r) {
60 VERIFY_CHECK(!secp256k1_ge_is_infinity(r));
62 if (secp256k1_fe_is_odd(&r->y)) {
63 secp256k1_fe_negate(&r->y, &r->y, 1);
69 int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context* ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) {
73 VERIFY_CHECK(ctx != NULL);
74 ARG_CHECK(xonly_pubkey != NULL);
75 ARG_CHECK(pubkey != NULL);
77 if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
80 tmp = secp256k1_extrakeys_ge_even_y(&pk);
81 if (pk_parity != NULL) {
84 secp256k1_xonly_pubkey_save(xonly_pubkey, &pk);
88 int secp256k1_xonly_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) {
91 VERIFY_CHECK(ctx != NULL);
92 ARG_CHECK(output_pubkey != NULL);
93 memset(output_pubkey, 0, sizeof(*output_pubkey));
94 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
95 ARG_CHECK(internal_pubkey != NULL);
96 ARG_CHECK(tweak32 != NULL);
98 if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
99 || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
102 secp256k1_pubkey_save(output_pubkey, &pk);
106 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) {
108 unsigned char pk_expected32[32];
110 VERIFY_CHECK(ctx != NULL);
111 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
112 ARG_CHECK(internal_pubkey != NULL);
113 ARG_CHECK(tweaked_pubkey32 != NULL);
114 ARG_CHECK(tweak32 != NULL);
116 if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
117 || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
120 secp256k1_fe_normalize_var(&pk.x);
121 secp256k1_fe_normalize_var(&pk.y);
122 secp256k1_fe_get_b32(pk_expected32, &pk.x);
124 return memcmp(&pk_expected32, tweaked_pubkey32, 32) == 0
125 && secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity;