1 #ifndef SECP256K1_EXTRAKEYS_H
2 #define SECP256K1_EXTRAKEYS_H
10 /** Opaque data structure that holds a parsed and valid "x-only" public key.
11 * An x-only pubkey encodes a point whose Y coordinate is even. It is
12 * serialized using only its X coordinate (32 bytes). See BIP-340 for more
13 * information about x-only pubkeys.
15 * The exact representation of data inside is implementation defined and not
16 * guaranteed to be portable between different platforms or versions. It is
17 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
18 * If you need to convert to a format suitable for storage, transmission, or
19 * comparison, use secp256k1_xonly_pubkey_serialize and
20 * secp256k1_xonly_pubkey_parse.
23 unsigned char data[64];
24 } secp256k1_xonly_pubkey;
26 /** Opaque data structure that holds a keypair consisting of a secret and a
29 * The exact representation of data inside is implementation defined and not
30 * guaranteed to be portable between different platforms or versions. It is
31 * however guaranteed to be 96 bytes in size, and can be safely copied/moved.
34 unsigned char data[96];
37 /** Parse a 32-byte sequence into a xonly_pubkey object.
39 * Returns: 1 if the public key was fully valid.
40 * 0 if the public key could not be parsed or is invalid.
42 * Args: ctx: a secp256k1 context object (cannot be NULL).
43 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
44 * parsed version of input. If not, it's set to an invalid value.
46 * In: input32: pointer to a serialized xonly_pubkey (cannot be NULL)
48 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse(
49 const secp256k1_context* ctx,
50 secp256k1_xonly_pubkey* pubkey,
51 const unsigned char *input32
52 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
54 /** Serialize an xonly_pubkey object into a 32-byte sequence.
58 * Args: ctx: a secp256k1 context object (cannot be NULL).
59 * Out: output32: a pointer to a 32-byte array to place the serialized key in
61 * In: pubkey: a pointer to a secp256k1_xonly_pubkey containing an
62 * initialized public key (cannot be NULL).
64 SECP256K1_API int secp256k1_xonly_pubkey_serialize(
65 const secp256k1_context* ctx,
66 unsigned char *output32,
67 const secp256k1_xonly_pubkey* pubkey
68 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
70 /** Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
72 * Returns: 1 if the public key was successfully converted
75 * Args: ctx: pointer to a context object (cannot be NULL)
76 * Out: xonly_pubkey: pointer to an x-only public key object for placing the
77 * converted public key (cannot be NULL)
78 * pk_parity: pointer to an integer that will be set to 1 if the point
79 * encoded by xonly_pubkey is the negation of the pubkey and
80 * set to 0 otherwise. (can be NULL)
81 * In: pubkey: pointer to a public key that is converted (cannot be NULL)
83 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey(
84 const secp256k1_context* ctx,
85 secp256k1_xonly_pubkey *xonly_pubkey,
87 const secp256k1_pubkey *pubkey
88 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
90 /** Tweak an x-only public key by adding the generator multiplied with tweak32
93 * Note that the resulting point can not in general be represented by an x-only
94 * pubkey because it may have an odd Y coordinate. Instead, the output_pubkey
95 * is a normal secp256k1_pubkey.
97 * Returns: 0 if the arguments are invalid or the resulting public key would be
98 * invalid (only when the tweak is the negation of the corresponding
99 * secret key). 1 otherwise.
101 * Args: ctx: pointer to a context object initialized for verification
103 * Out: output_pubkey: pointer to a public key to store the result. Will be set
104 * to an invalid value if this function returns 0 (cannot
106 * In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
108 * tweak32: pointer to a 32-byte tweak. If the tweak is invalid
109 * according to secp256k1_ec_seckey_verify, this function
110 * returns 0. For uniformly random 32-byte arrays the
111 * chance of being invalid is negligible (around 1 in
112 * 2^128) (cannot be NULL).
114 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add(
115 const secp256k1_context* ctx,
116 secp256k1_pubkey *output_pubkey,
117 const secp256k1_xonly_pubkey *internal_pubkey,
118 const unsigned char *tweak32
119 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
121 /** Checks that a tweaked pubkey is the result of calling
122 * secp256k1_xonly_pubkey_tweak_add with internal_pubkey and tweak32.
124 * The tweaked pubkey is represented by its 32-byte x-only serialization and
125 * its pk_parity, which can both be obtained by converting the result of
126 * tweak_add to a secp256k1_xonly_pubkey.
128 * Note that this alone does _not_ verify that the tweaked pubkey is a
129 * commitment. If the tweak is not chosen in a specific way, the tweaked pubkey
130 * can easily be the result of a different internal_pubkey and tweak.
132 * Returns: 0 if the arguments are invalid or the tweaked pubkey is not the
133 * result of tweaking the internal_pubkey with tweak32. 1 otherwise.
134 * Args: ctx: pointer to a context object initialized for verification
136 * In: tweaked_pubkey32: pointer to a serialized xonly_pubkey (cannot be NULL)
137 * tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
138 * is passed in as tweaked_pubkey32). This must match the
139 * pk_parity value that is returned when calling
140 * secp256k1_xonly_pubkey with the tweaked pubkey, or
141 * this function will fail.
142 * internal_pubkey: pointer to an x-only public key object to apply the
143 * tweak to (cannot be NULL)
144 * tweak32: pointer to a 32-byte tweak (cannot be NULL)
146 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_check(
147 const secp256k1_context* ctx,
148 const unsigned char *tweaked_pubkey32,
149 int tweaked_pk_parity,
150 const secp256k1_xonly_pubkey *internal_pubkey,
151 const unsigned char *tweak32
152 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
154 /** Compute the keypair for a secret key.
156 * Returns: 1: secret was valid, keypair is ready to use
157 * 0: secret was invalid, try again with a different secret
158 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
159 * Out: keypair: pointer to the created keypair (cannot be NULL)
160 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
162 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(
163 const secp256k1_context* ctx,
164 secp256k1_keypair *keypair,
165 const unsigned char *seckey
166 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
168 /** Get the public key from a keypair.
170 * Returns: 0 if the arguments are invalid. 1 otherwise.
171 * Args: ctx: pointer to a context object (cannot be NULL)
172 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to
173 * the keypair public key. If not, it's set to an invalid value.
175 * In: keypair: pointer to a keypair (cannot be NULL)
177 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub(
178 const secp256k1_context* ctx,
179 secp256k1_pubkey *pubkey,
180 const secp256k1_keypair *keypair
181 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
183 /** Get the x-only public key from a keypair.
185 * This is the same as calling secp256k1_keypair_pub and then
186 * secp256k1_xonly_pubkey_from_pubkey.
188 * Returns: 0 if the arguments are invalid. 1 otherwise.
189 * Args: ctx: pointer to a context object (cannot be NULL)
190 * Out: pubkey: pointer to an xonly_pubkey object. If 1 is returned, it is set
191 * to the keypair public key after converting it to an
192 * xonly_pubkey. If not, it's set to an invalid value (cannot be
194 * pk_parity: pointer to an integer that will be set to the pk_parity
195 * argument of secp256k1_xonly_pubkey_from_pubkey (can be NULL).
196 * In: keypair: pointer to a keypair (cannot be NULL)
198 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub(
199 const secp256k1_context* ctx,
200 secp256k1_xonly_pubkey *pubkey,
202 const secp256k1_keypair *keypair
203 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
205 /** Tweak a keypair by adding tweak32 to the secret key and updating the public
208 * Calling this function and then secp256k1_keypair_pub results in the same
209 * public key as calling secp256k1_keypair_xonly_pub and then
210 * secp256k1_xonly_pubkey_tweak_add.
212 * Returns: 0 if the arguments are invalid or the resulting keypair would be
213 * invalid (only when the tweak is the negation of the keypair's
214 * secret key). 1 otherwise.
216 * Args: ctx: pointer to a context object initialized for verification
218 * In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to
219 * an invalid value if this function returns 0 (cannot be
221 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according
222 * to secp256k1_ec_seckey_verify, this function returns 0. For
223 * uniformly random 32-byte arrays the chance of being invalid
224 * is negligible (around 1 in 2^128) (cannot be NULL).
226 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(
227 const secp256k1_context* ctx,
228 secp256k1_keypair *keypair,
229 const unsigned char *tweak32
230 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
236 #endif /* SECP256K1_EXTRAKEYS_H */