]> Git Repo - secp256k1.git/blame - include/secp256k1_extrakeys.h
add `secp256k1_xonly_pubkey_cmp` method
[secp256k1.git] / include / secp256k1_extrakeys.h
CommitLineData
47e6618e
JN
1#ifndef SECP256K1_EXTRAKEYS_H
2#define SECP256K1_EXTRAKEYS_H
3
4#include "secp256k1.h"
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
4cd2ee47
JN
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.
14 *
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.
6eceec6d
AP
18 * If you need to convert to a format suitable for storage, transmission, use
19 * use secp256k1_xonly_pubkey_serialize and secp256k1_xonly_pubkey_parse. To
20 * compare keys, use secp256k1_xonly_pubkey_cmp.
4cd2ee47
JN
21 */
22typedef struct {
23 unsigned char data[64];
24} secp256k1_xonly_pubkey;
25
58254463
JN
26/** Opaque data structure that holds a keypair consisting of a secret and a
27 * public key.
28 *
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.
32 */
33typedef struct {
34 unsigned char data[96];
35} secp256k1_keypair;
36
4cd2ee47
JN
37/** Parse a 32-byte sequence into a xonly_pubkey object.
38 *
39 * Returns: 1 if the public key was fully valid.
40 * 0 if the public key could not be parsed or is invalid.
41 *
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.
45 * (cannot be NULL).
46 * In: input32: pointer to a serialized xonly_pubkey (cannot be NULL)
47 */
48SECP256K1_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);
53
54/** Serialize an xonly_pubkey object into a 32-byte sequence.
55 *
56 * Returns: 1 always.
57 *
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
60 * (cannot be NULL).
61 * In: pubkey: a pointer to a secp256k1_xonly_pubkey containing an
62 * initialized public key (cannot be NULL).
63 */
64SECP256K1_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);
69
6eceec6d
AP
70/** Compare two x-only public keys using lexicographic order
71 *
72 * Returns: <0 if the first public key is less than the second
73 * >0 if the first public key is greater than the second
74 * 0 if the two public keys are equal
75 * Args: ctx: a secp256k1 context object.
76 * In: pubkey1: first public key to compare
77 * pubkey2: second public key to compare
78 */
79SECP256K1_API int secp256k1_xonly_pubkey_cmp(
80 const secp256k1_context* ctx,
81 const secp256k1_xonly_pubkey* pk1,
82 const secp256k1_xonly_pubkey* pk2
83) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
84
4cd2ee47
JN
85/** Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
86 *
87 * Returns: 1 if the public key was successfully converted
88 * 0 otherwise
89 *
90 * Args: ctx: pointer to a context object (cannot be NULL)
91 * Out: xonly_pubkey: pointer to an x-only public key object for placing the
92 * converted public key (cannot be NULL)
93 * pk_parity: pointer to an integer that will be set to 1 if the point
94 * encoded by xonly_pubkey is the negation of the pubkey and
95 * set to 0 otherwise. (can be NULL)
96 * In: pubkey: pointer to a public key that is converted (cannot be NULL)
97 */
98SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey(
99 const secp256k1_context* ctx,
100 secp256k1_xonly_pubkey *xonly_pubkey,
101 int *pk_parity,
102 const secp256k1_pubkey *pubkey
103) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
104
910d9c28
JN
105/** Tweak an x-only public key by adding the generator multiplied with tweak32
106 * to it.
107 *
108 * Note that the resulting point can not in general be represented by an x-only
109 * pubkey because it may have an odd Y coordinate. Instead, the output_pubkey
110 * is a normal secp256k1_pubkey.
111 *
112 * Returns: 0 if the arguments are invalid or the resulting public key would be
113 * invalid (only when the tweak is the negation of the corresponding
114 * secret key). 1 otherwise.
115 *
116 * Args: ctx: pointer to a context object initialized for verification
117 * (cannot be NULL)
118 * Out: output_pubkey: pointer to a public key to store the result. Will be set
119 * to an invalid value if this function returns 0 (cannot
120 * be NULL)
121 * In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
122 * (cannot be NULL).
123 * tweak32: pointer to a 32-byte tweak. If the tweak is invalid
124 * according to secp256k1_ec_seckey_verify, this function
125 * returns 0. For uniformly random 32-byte arrays the
126 * chance of being invalid is negligible (around 1 in
127 * 2^128) (cannot be NULL).
128 */
129SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add(
130 const secp256k1_context* ctx,
131 secp256k1_pubkey *output_pubkey,
132 const secp256k1_xonly_pubkey *internal_pubkey,
133 const unsigned char *tweak32
134) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
135
136/** Checks that a tweaked pubkey is the result of calling
137 * secp256k1_xonly_pubkey_tweak_add with internal_pubkey and tweak32.
138 *
139 * The tweaked pubkey is represented by its 32-byte x-only serialization and
140 * its pk_parity, which can both be obtained by converting the result of
141 * tweak_add to a secp256k1_xonly_pubkey.
142 *
143 * Note that this alone does _not_ verify that the tweaked pubkey is a
144 * commitment. If the tweak is not chosen in a specific way, the tweaked pubkey
145 * can easily be the result of a different internal_pubkey and tweak.
146 *
147 * Returns: 0 if the arguments are invalid or the tweaked pubkey is not the
148 * result of tweaking the internal_pubkey with tweak32. 1 otherwise.
149 * Args: ctx: pointer to a context object initialized for verification
150 * (cannot be NULL)
151 * In: tweaked_pubkey32: pointer to a serialized xonly_pubkey (cannot be NULL)
152 * tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
153 * is passed in as tweaked_pubkey32). This must match the
154 * pk_parity value that is returned when calling
155 * secp256k1_xonly_pubkey with the tweaked pubkey, or
156 * this function will fail.
157 * internal_pubkey: pointer to an x-only public key object to apply the
158 * tweak to (cannot be NULL)
159 * tweak32: pointer to a 32-byte tweak (cannot be NULL)
160 */
161SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_check(
162 const secp256k1_context* ctx,
163 const unsigned char *tweaked_pubkey32,
164 int tweaked_pk_parity,
165 const secp256k1_xonly_pubkey *internal_pubkey,
166 const unsigned char *tweak32
167) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
168
58254463
JN
169/** Compute the keypair for a secret key.
170 *
171 * Returns: 1: secret was valid, keypair is ready to use
172 * 0: secret was invalid, try again with a different secret
173 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
174 * Out: keypair: pointer to the created keypair (cannot be NULL)
175 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
176 */
177SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(
178 const secp256k1_context* ctx,
179 secp256k1_keypair *keypair,
180 const unsigned char *seckey
181) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
182
fc96aa73
ET
183/** Get the secret key from a keypair.
184 *
185 * Returns: 0 if the arguments are invalid. 1 otherwise.
186 * Args: ctx: pointer to a context object (cannot be NULL)
187 * Out: seckey: pointer to a 32-byte buffer for the secret key (cannot be NULL)
188 * In: keypair: pointer to a keypair (cannot be NULL)
189 */
190SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec(
191 const secp256k1_context* ctx,
192 unsigned char *seckey,
193 const secp256k1_keypair *keypair
194) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
195
58254463
JN
196/** Get the public key from a keypair.
197 *
198 * Returns: 0 if the arguments are invalid. 1 otherwise.
199 * Args: ctx: pointer to a context object (cannot be NULL)
200 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to
201 * the keypair public key. If not, it's set to an invalid value.
202 * (cannot be NULL)
203 * In: keypair: pointer to a keypair (cannot be NULL)
204 */
205SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub(
206 const secp256k1_context* ctx,
207 secp256k1_pubkey *pubkey,
208 const secp256k1_keypair *keypair
209) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
210
211/** Get the x-only public key from a keypair.
212 *
213 * This is the same as calling secp256k1_keypair_pub and then
214 * secp256k1_xonly_pubkey_from_pubkey.
215 *
216 * Returns: 0 if the arguments are invalid. 1 otherwise.
217 * Args: ctx: pointer to a context object (cannot be NULL)
218 * Out: pubkey: pointer to an xonly_pubkey object. If 1 is returned, it is set
219 * to the keypair public key after converting it to an
220 * xonly_pubkey. If not, it's set to an invalid value (cannot be
221 * NULL).
222 * pk_parity: pointer to an integer that will be set to the pk_parity
223 * argument of secp256k1_xonly_pubkey_from_pubkey (can be NULL).
224 * In: keypair: pointer to a keypair (cannot be NULL)
225 */
226SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub(
227 const secp256k1_context* ctx,
228 secp256k1_xonly_pubkey *pubkey,
229 int *pk_parity,
230 const secp256k1_keypair *keypair
231) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
232
6fcb5b84
JN
233/** Tweak a keypair by adding tweak32 to the secret key and updating the public
234 * key accordingly.
235 *
236 * Calling this function and then secp256k1_keypair_pub results in the same
237 * public key as calling secp256k1_keypair_xonly_pub and then
238 * secp256k1_xonly_pubkey_tweak_add.
239 *
240 * Returns: 0 if the arguments are invalid or the resulting keypair would be
241 * invalid (only when the tweak is the negation of the keypair's
242 * secret key). 1 otherwise.
243 *
244 * Args: ctx: pointer to a context object initialized for verification
245 * (cannot be NULL)
246 * In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to
247 * an invalid value if this function returns 0 (cannot be
248 * NULL).
249 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according
250 * to secp256k1_ec_seckey_verify, this function returns 0. For
251 * uniformly random 32-byte arrays the chance of being invalid
252 * is negligible (around 1 in 2^128) (cannot be NULL).
253 */
254SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(
255 const secp256k1_context* ctx,
256 secp256k1_keypair *keypair,
257 const unsigned char *tweak32
258) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
259
47e6618e
JN
260#ifdef __cplusplus
261}
262#endif
263
264#endif /* SECP256K1_EXTRAKEYS_H */
This page took 0.049735 seconds and 4 git commands to generate.