]>
Commit | Line | Data |
---|---|---|
a6d68949 | 1 | #ifndef _SECP256K1_ |
8563713a | 2 | # define _SECP256K1_ |
a6d68949 | 3 | |
8563713a | 4 | # ifdef __cplusplus |
254327e4 | 5 | extern "C" { |
8563713a GM |
6 | # endif |
7 | ||
788038d3 LD |
8 | #include <stddef.h> |
9 | ||
dc0ce9fc PW |
10 | /* These rules specify the order of arguments in API calls: |
11 | * | |
12 | * 1. Context pointers go first, followed by output arguments, combined | |
13 | * output/input arguments, and finally input-only arguments. | |
14 | * 2. Array lengths always immediately the follow the argument whose length | |
15 | * they describe, even if this violates rule 1. | |
16 | * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated | |
17 | * later go first. This means: signatures, public nonces, private nonces, | |
18 | * messages, public keys, secret keys, tweaks. | |
19 | * 4. Arguments that are not data pointers go last, from more complex to less | |
20 | * complex: function pointers, algorithm names, messages, void pointers, | |
21 | * counts, flags, booleans. | |
22 | * 5. Opaque data pointers follow the function pointer they are to be passed to. | |
23 | */ | |
24 | ||
f66907f2 PW |
25 | /** Opaque data structure that holds context information (precomputed tables etc.). |
26 | * | |
27 | * The purpose of context structures is to cache large precomputed data tables | |
28 | * that are expensive to construct, and also to maintain the randomization data | |
29 | * for blinding. | |
30 | * | |
31 | * Do not create a new context object for each operation, as construction is | |
32 | * far slower than all other API calls (~100 times slower than an ECDSA | |
33 | * verification). | |
34 | * | |
35 | * A constructed context can safely be used from multiple threads | |
36 | * simultaneously, but API call that take a non-const pointer to a context | |
37 | * need exclusive access to it. In particular this is the case for | |
38 | * secp256k1_context_destroy and secp256k1_context_randomize. | |
39 | * | |
40 | * Regarding randomization, either do it once at creation time (in which case | |
41 | * you do not need any locking for the other calls), or use a read-write lock. | |
42 | */ | |
43 | typedef struct secp256k1_context_struct secp256k1_context_t; | |
44 | ||
45 | /** Opaque data structure that holds a parsed and valid public key. | |
46 | * | |
47 | * The exact representation of data inside is implementation defined and not | |
48 | * guaranteed to be portable between different platforms or versions. It is | |
49 | * however guaranteed to be 64 bytes in size, and can be safely copied/moved. | |
50 | * If you need to convert to a format suitable for storage or transmission, use | |
51 | * secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse. | |
52 | * | |
53 | * Furthermore, it is guaranteed that identical public keys (ignoring | |
54 | * compression) will have identical representation, so they can be memcmp'ed. | |
55 | */ | |
56 | typedef struct { | |
57 | unsigned char data[64]; | |
58 | } secp256k1_pubkey_t; | |
59 | ||
439d34ad PW |
60 | /** Opaque data structured that holds a parsed ECDSA signature. |
61 | * | |
62 | * The exact representation of data inside is implementation defined and not | |
63 | * guaranteed to be portable between different platforms or versions. It is | |
64 | * however guaranteed to be 64 bytes in size, and can be safely copied/moved. | |
65 | * If you need to convert to a format suitable for storage or transmission, use | |
66 | * the secp256k1_ecdsa_signature_serialize_* and | |
67 | * secp256k1_ecdsa_signature_serialize_* functions. | |
68 | * | |
69 | * Furthermore, it is guaranteed to identical signatures will have identical | |
70 | * representation, so they can be memcmp'ed. | |
71 | */ | |
72 | typedef struct { | |
73 | unsigned char data[64]; | |
74 | } secp256k1_ecdsa_signature_t; | |
75 | ||
f66907f2 PW |
76 | /** A pointer to a function to deterministically generate a nonce. |
77 | * | |
78 | * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail. | |
dc0ce9fc | 79 | * Out: nonce32: pointer to a 32-byte array to be filled by the function. |
f66907f2 PW |
80 | * In: msg32: the 32-byte message hash being verified (will not be NULL) |
81 | * key32: pointer to a 32-byte secret key (will not be NULL) | |
82 | * algo16: pointer to a 16-byte array describing the signature | |
83 | * algorithm (will be NULL for ECDSA for compatibility). | |
dc0ce9fc | 84 | * data: Arbitrary data pointer that is passed through. |
f66907f2 PW |
85 | * attempt: how many iterations we have tried to find a nonce. |
86 | * This will almost always be 0, but different attempt values | |
87 | * are required to result in a different nonce. | |
f66907f2 PW |
88 | * |
89 | * Except for test cases, this function should compute some cryptographic hash of | |
90 | * the message, the algorithm, the key and the attempt. | |
91 | */ | |
92 | typedef int (*secp256k1_nonce_function_t)( | |
93 | unsigned char *nonce32, | |
94 | const unsigned char *msg32, | |
95 | const unsigned char *key32, | |
96 | const unsigned char *algo16, | |
dc0ce9fc PW |
97 | const void *data, |
98 | unsigned int attempt | |
f66907f2 PW |
99 | ); |
100 | ||
8563713a GM |
101 | # if !defined(SECP256K1_GNUC_PREREQ) |
102 | # if defined(__GNUC__)&&defined(__GNUC_MINOR__) | |
103 | # define SECP256K1_GNUC_PREREQ(_maj,_min) \ | |
104 | ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min)) | |
105 | # else | |
106 | # define SECP256K1_GNUC_PREREQ(_maj,_min) 0 | |
107 | # endif | |
108 | # endif | |
109 | ||
8563713a GM |
110 | # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) ) |
111 | # if SECP256K1_GNUC_PREREQ(2,7) | |
112 | # define SECP256K1_INLINE __inline__ | |
113 | # elif (defined(_MSC_VER)) | |
114 | # define SECP256K1_INLINE __inline | |
115 | # else | |
116 | # define SECP256K1_INLINE | |
117 | # endif | |
118 | # else | |
119 | # define SECP256K1_INLINE inline | |
120 | # endif | |
121 | ||
122 | /**Warning attributes | |
123 | * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out | |
124 | * some paranoid null checks. */ | |
125 | # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4) | |
126 | # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__)) | |
127 | # else | |
128 | # define SECP256K1_WARN_UNUSED_RESULT | |
129 | # endif | |
130 | # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4) | |
131 | # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x))) | |
132 | # else | |
133 | # define SECP256K1_ARG_NONNULL(_x) | |
134 | # endif | |
135 | ||
a9b6595e PW |
136 | /** Flags to pass to secp256k1_context_create. */ |
137 | # define SECP256K1_CONTEXT_VERIFY (1 << 0) | |
138 | # define SECP256K1_CONTEXT_SIGN (1 << 1) | |
04e34d18 | 139 | |
a9b6595e | 140 | /** Create a secp256k1 context object. |
f66907f2 | 141 | * |
a9b6595e PW |
142 | * Returns: a newly created context object. |
143 | * In: flags: which parts of the context to initialize. | |
b2966ce8 | 144 | */ |
a9b6595e | 145 | secp256k1_context_t* secp256k1_context_create( |
64b730bc | 146 | unsigned int flags |
a9b6595e | 147 | ) SECP256K1_WARN_UNUSED_RESULT; |
b2966ce8 | 148 | |
d899b5b6 | 149 | /** Copies a secp256k1 context object. |
f66907f2 | 150 | * |
d899b5b6 | 151 | * Returns: a newly created context object. |
dc0ce9fc | 152 | * Args: ctx: an existing context to copy (cannot be NULL) |
d899b5b6 AP |
153 | */ |
154 | secp256k1_context_t* secp256k1_context_clone( | |
f66907f2 PW |
155 | const secp256k1_context_t* ctx |
156 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT; | |
d899b5b6 | 157 | |
a9b6595e | 158 | /** Destroy a secp256k1 context object. |
f66907f2 | 159 | * |
a9b6595e | 160 | * The context pointer may not be used afterwards. |
dc0ce9fc | 161 | * Args: ctx: an existing context to destroy (cannot be NULL) |
b2966ce8 | 162 | */ |
a9b6595e | 163 | void secp256k1_context_destroy( |
f66907f2 | 164 | secp256k1_context_t* ctx |
9aac0080 | 165 | ); |
b2966ce8 | 166 | |
995c5487 | 167 | /** Set a callback function to be called when an illegal argument is passed to |
f66907f2 PW |
168 | * an API call. It will only trigger for violations that are mentioned |
169 | * explicitly in the header. | |
170 | * | |
171 | * The philosophy is that these shouldn't be dealt with through a | |
995c5487 PW |
172 | * specific return value, as calling code should not have branches to deal with |
173 | * the case that this code itself is broken. | |
f66907f2 | 174 | * |
995c5487 PW |
175 | * On the other hand, during debug stage, one would want to be informed about |
176 | * such mistakes, and the default (crashing) may be inadvisable. | |
177 | * When this callback is triggered, the API function called is guaranteed not | |
178 | * to cause a crash, though its return value and output arguments are | |
179 | * undefined. | |
f66907f2 | 180 | * |
dc0ce9fc PW |
181 | * Args: ctx: an existing context object (cannot be NULL) |
182 | * In: fun: a pointer to a function to call when an illegal argument is | |
183 | * passed to the API, taking a message and an opaque pointer | |
c9d7c2a4 | 184 | * (NULL restores a default handler that calls abort). |
f66907f2 | 185 | * data: the opaque pointer to pass to fun above. |
995c5487 PW |
186 | */ |
187 | void secp256k1_context_set_illegal_callback( | |
f66907f2 PW |
188 | secp256k1_context_t* ctx, |
189 | void (*fun)(const char* message, void* data), | |
190 | void* data | |
c9d7c2a4 | 191 | ) SECP256K1_ARG_NONNULL(1); |
995c5487 PW |
192 | |
193 | /** Set a callback function to be called when an internal consistency check | |
194 | * fails. The default is crashing. | |
f66907f2 | 195 | * |
995c5487 PW |
196 | * This can only trigger in case of a hardware failure, miscompilation, |
197 | * memory corruption, serious bug in the library, or other error would can | |
198 | * otherwise result in undefined behaviour. It will not trigger due to mere | |
199 | * incorrect usage of the API (see secp256k1_context_set_illegal_callback | |
200 | * for that). After this callback returns, anything may happen, including | |
201 | * crashing. | |
f66907f2 | 202 | * |
dc0ce9fc PW |
203 | * Args: ctx: an existing context object (cannot be NULL) |
204 | * In: fun: a pointer to a function to call when an interal error occurs, | |
c9d7c2a4 LD |
205 | * taking a message and an opaque pointer (NULL restores a default |
206 | * handler that calls abort). | |
f66907f2 | 207 | * data: the opaque pointer to pass to fun above. |
995c5487 PW |
208 | */ |
209 | void secp256k1_context_set_error_callback( | |
f66907f2 PW |
210 | secp256k1_context_t* ctx, |
211 | void (*fun)(const char* message, void* data), | |
212 | void* data | |
c9d7c2a4 | 213 | ) SECP256K1_ARG_NONNULL(1); |
995c5487 | 214 | |
23cfa914 | 215 | /** Parse a variable-length public key into the pubkey object. |
f66907f2 | 216 | * |
23cfa914 PW |
217 | * Returns: 1 if the public key was fully valid. |
218 | * 0 if the public key could not be parsed or is invalid. | |
dc0ce9fc PW |
219 | * Args: ctx: a secp256k1 context object. |
220 | * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a | |
221 | * parsed version of input. If not, its value is undefined. | |
222 | * In: input: pointer to a serialized public key | |
223 | * inputlen: length of the array pointed to by input | |
f66907f2 | 224 | * |
23cfa914 PW |
225 | * This function supports parsing compressed (33 bytes, header byte 0x02 or |
226 | * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header | |
227 | * byte 0x06 or 0x07) format public keys. | |
228 | */ | |
229 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse( | |
f66907f2 PW |
230 | const secp256k1_context_t* ctx, |
231 | secp256k1_pubkey_t* pubkey, | |
232 | const unsigned char *input, | |
788038d3 | 233 | size_t inputlen |
23cfa914 PW |
234 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
235 | ||
236 | /** Serialize a pubkey object into a serialized byte sequence. | |
f66907f2 | 237 | * |
23cfa914 | 238 | * Returns: 1 always. |
dc0ce9fc | 239 | * Args: ctx: a secp256k1 context object. |
23cfa914 PW |
240 | * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if |
241 | * compressed==1) byte array to place the serialized key in. | |
242 | * outputlen: a pointer to an integer which will contain the serialized | |
243 | * size. | |
dc0ce9fc PW |
244 | * In: pubkey: a pointer to a secp256k1_pubkey_t containing an initialized |
245 | * public key. | |
246 | * compressed: whether to serialize in compressed format. | |
23cfa914 PW |
247 | */ |
248 | int secp256k1_ec_pubkey_serialize( | |
f66907f2 PW |
249 | const secp256k1_context_t* ctx, |
250 | unsigned char *output, | |
788038d3 | 251 | size_t *outputlen, |
f66907f2 PW |
252 | const secp256k1_pubkey_t* pubkey, |
253 | int compressed | |
23cfa914 PW |
254 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
255 | ||
74a2acdb | 256 | /** Parse a DER ECDSA signature. |
f66907f2 | 257 | * |
74a2acdb | 258 | * Returns: 1 when the signature could be parsed, 0 otherwise. |
dc0ce9fc PW |
259 | * Args: ctx: a secp256k1 context object |
260 | * Out: sig: a pointer to a signature object | |
261 | * In: input: a pointer to the signature to be parsed | |
262 | * inputlen: the length of the array pointed to be input | |
74a2acdb | 263 | * |
f66907f2 | 264 | * Note that this function also supports some violations of DER and even BER. |
74a2acdb PW |
265 | */ |
266 | int secp256k1_ecdsa_signature_parse_der( | |
f66907f2 PW |
267 | const secp256k1_context_t* ctx, |
268 | secp256k1_ecdsa_signature_t* sig, | |
269 | const unsigned char *input, | |
788038d3 | 270 | size_t inputlen |
74a2acdb PW |
271 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
272 | ||
74a2acdb | 273 | /** Serialize an ECDSA signature in DER format. |
f66907f2 | 274 | * |
74a2acdb | 275 | * Returns: 1 if enough space was available to serialize, 0 otherwise |
dc0ce9fc | 276 | * Args: ctx: a secp256k1 context object |
74a2acdb PW |
277 | * Out: output: a pointer to an array to store the DER serialization |
278 | * In/Out: outputlen: a pointer to a length integer. Initially, this integer | |
279 | * should be set to the length of output. After the call | |
280 | * it will be set to the length of the serialization (even | |
281 | * if 0 was returned). | |
dc0ce9fc | 282 | * In: sig: a pointer to an initialized signature object |
74a2acdb PW |
283 | */ |
284 | int secp256k1_ecdsa_signature_serialize_der( | |
f66907f2 PW |
285 | const secp256k1_context_t* ctx, |
286 | unsigned char *output, | |
788038d3 | 287 | size_t *outputlen, |
f66907f2 | 288 | const secp256k1_ecdsa_signature_t* sig |
74a2acdb PW |
289 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
290 | ||
b2966ce8 | 291 | /** Verify an ECDSA signature. |
f66907f2 | 292 | * |
b2966ce8 | 293 | * Returns: 1: correct signature |
23cfa914 | 294 | * 0: incorrect or unparseable signature |
dc0ce9fc PW |
295 | * Args: ctx: a secp256k1 context object, initialized for verification. |
296 | * In: sig: the signature being verified (cannot be NULL) | |
a9b6595e | 297 | * msg32: the 32-byte message hash being verified (cannot be NULL) |
23cfa914 | 298 | * pubkey: pointer to an initialized public key to verify with (cannot be NULL) |
b2966ce8 | 299 | */ |
8563713a | 300 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify( |
f66907f2 | 301 | const secp256k1_context_t* ctx, |
f66907f2 | 302 | const secp256k1_ecdsa_signature_t *sig, |
dc0ce9fc | 303 | const unsigned char *msg32, |
f66907f2 | 304 | const secp256k1_pubkey_t *pubkey |
74a2acdb | 305 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
b2966ce8 | 306 | |
1573a102 PW |
307 | /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function. |
308 | * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of | |
309 | * extra entropy. | |
310 | */ | |
bbd5ba7c PW |
311 | extern const secp256k1_nonce_function_t secp256k1_nonce_function_rfc6979; |
312 | ||
313 | /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */ | |
314 | extern const secp256k1_nonce_function_t secp256k1_nonce_function_default; | |
315 | ||
42cccdaf | 316 | /** Create an ECDSA signature. |
f66907f2 | 317 | * |
42cccdaf | 318 | * Returns: 1: signature created |
74a2acdb | 319 | * 0: the nonce generation function failed, or the private key was invalid. |
dc0ce9fc PW |
320 | * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL) |
321 | * Out: sig: pointer to an array where the signature will be placed (cannot be NULL) | |
322 | * In: msg32: the 32-byte message hash being signed (cannot be NULL) | |
8030d7c0 | 323 | * seckey: pointer to a 32-byte secret key (cannot be NULL) |
bbd5ba7c | 324 | * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used |
c6e7f4e8 | 325 | * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL) |
74a2acdb | 326 | * |
6cc8425c GM |
327 | * The sig always has an s value in the lower half of the range (From 0x1 |
328 | * to 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, | |
329 | * inclusive), unlike many other implementations. | |
f66907f2 | 330 | * |
6cc8425c GM |
331 | * With ECDSA a third-party can can forge a second distinct signature |
332 | * of the same message given a single initial signature without knowing | |
333 | * the key by setting s to its additive inverse mod-order, 'flipping' the | |
334 | * sign of the random point R which is not included in the signature. | |
335 | * Since the forgery is of the same message this isn't universally | |
336 | * problematic, but in systems where message malleability or uniqueness | |
337 | * of signatures is important this can cause issues. This forgery can be | |
338 | * blocked by all verifiers forcing signers to use a canonical form. The | |
339 | * lower-S form reduces the size of signatures slightly on average when | |
340 | * variable length encodings (such as DER) are used and is cheap to | |
341 | * verify, making it a good choice. Security of always using lower-S is | |
342 | * assured because anyone can trivially modify a signature after the | |
343 | * fact to enforce this property. Adjusting it inside the signing | |
344 | * function avoids the need to re-serialize or have curve specific | |
345 | * constants outside of the library. By always using a canonical form | |
346 | * even in applications where it isn't needed it becomes possible to | |
347 | * impose a requirement later if a need is discovered. | |
348 | * No other forms of ECDSA malleability are known and none seem likely, | |
349 | * but there is no formal proof that ECDSA, even with this additional | |
350 | * restriction, is free of other malleability. Commonly used serialization | |
351 | * schemes will also accept various non-unique encodings, so care should | |
352 | * be taken when this property is required for an application. | |
42cccdaf | 353 | */ |
c6e7f4e8 | 354 | int secp256k1_ecdsa_sign( |
f66907f2 | 355 | const secp256k1_context_t* ctx, |
f66907f2 | 356 | secp256k1_ecdsa_signature_t *sig, |
dc0ce9fc | 357 | const unsigned char *msg32, |
f66907f2 PW |
358 | const unsigned char *seckey, |
359 | secp256k1_nonce_function_t noncefp, | |
360 | const void *ndata | |
a9b6595e | 361 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
50eb498e | 362 | |
42cccdaf | 363 | /** Verify an ECDSA secret key. |
f66907f2 | 364 | * |
42cccdaf PW |
365 | * Returns: 1: secret key is valid |
366 | * 0: secret key is invalid | |
dc0ce9fc PW |
367 | * Args: ctx: pointer to a context object (cannot be NULL) |
368 | * In: seckey: pointer to a 32-byte secret key (cannot be NULL) | |
42cccdaf | 369 | */ |
a9b6595e | 370 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify( |
f66907f2 PW |
371 | const secp256k1_context_t* ctx, |
372 | const unsigned char *seckey | |
a9b6595e | 373 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); |
78239167 | 374 | |
b2966ce8 | 375 | /** Compute the public key for a secret key. |
f66907f2 | 376 | * |
b2966ce8 | 377 | * Returns: 1: secret was valid, public key stores |
5098f625 | 378 | * 0: secret was invalid, try again |
dc0ce9fc PW |
379 | * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL) |
380 | * Out: pubkey: pointer to the created public key (cannot be NULL) | |
381 | * In: seckey: pointer to a 32-byte private key (cannot be NULL) | |
b2966ce8 | 382 | */ |
8563713a | 383 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create( |
f66907f2 PW |
384 | const secp256k1_context_t* ctx, |
385 | secp256k1_pubkey_t *pubkey, | |
386 | const unsigned char *seckey | |
23cfa914 | 387 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
ae6bc76e | 388 | |
f66907f2 PW |
389 | /** Export a private key in BER format. |
390 | * | |
f66907f2 | 391 | * Returns: 1 if the private key was valid. |
dc0ce9fc PW |
392 | * Args: ctx: pointer to a context object, initialized for signing (cannot |
393 | * be NULL) | |
394 | * Out: privkey: pointer to an array for storing the private key in BER. | |
395 | * Should have space for 279 bytes, and cannot be NULL. | |
396 | * privkeylen: Pointer to an int where the length of the private key in | |
397 | * privkey will be stored. | |
398 | * In: seckey: pointer to a 32-byte secret key to export. | |
399 | * compressed: whether the key should be exported in compressed format. | |
f66907f2 PW |
400 | * |
401 | * This function is purely meant for compatibility with applications that | |
402 | * require BER encoded keys. When working with secp256k1-specific code, the | |
403 | * simple 32-byte private keys are sufficient. | |
404 | * | |
405 | * Note that this function does not guarantee correct DER output. It is | |
406 | * guaranteed to be parsable by secp256k1_ec_privkey_import. | |
a9b6595e | 407 | */ |
8563713a | 408 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_export( |
f66907f2 | 409 | const secp256k1_context_t* ctx, |
f66907f2 | 410 | unsigned char *privkey, |
788038d3 | 411 | size_t *privkeylen, |
dc0ce9fc | 412 | const unsigned char *seckey, |
f66907f2 | 413 | int compressed |
a9b6595e | 414 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
78239167 | 415 | |
f66907f2 PW |
416 | /** Import a private key in DER format. |
417 | * Returns: 1 if a private key was extracted. | |
dc0ce9fc PW |
418 | * Args: ctx: pointer to a context object (cannot be NULL). |
419 | * Out: seckey: pointer to a 32-byte array for storing the private key. | |
420 | * (cannot be NULL). | |
421 | * In: privkey: pointer to a private key in DER format (cannot be NULL). | |
422 | * privkeylen: length of the DER private key pointed to be privkey. | |
f66907f2 PW |
423 | * |
424 | * This function will accept more than just strict DER, and even allow some BER | |
425 | * violations. The public key stored inside the DER-encoded private key is not | |
426 | * verified for correctness, nor are the curve parameters. Use this function | |
427 | * only if you know in advance it is supposed to contain a secp256k1 private | |
428 | * key. | |
429 | */ | |
8563713a | 430 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_import( |
f66907f2 PW |
431 | const secp256k1_context_t* ctx, |
432 | unsigned char *seckey, | |
433 | const unsigned char *privkey, | |
788038d3 | 434 | size_t privkeylen |
a9b6595e | 435 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
eb0be8ee | 436 | |
f66907f2 | 437 | /** Tweak a private key by adding tweak to it. |
f66907f2 PW |
438 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
439 | * uniformly random 32-byte arrays, or if the resulting private key | |
440 | * would be invalid (only when the tweak is the complement of the | |
441 | * private key). 1 otherwise. | |
dc0ce9fc PW |
442 | * Args: ctx: pointer to a context object (cannot be NULL). |
443 | * In/Out: seckey: pointer to a 32-byte private key. | |
444 | * In: tweak: pointer to a 32-byte tweak. | |
f66907f2 | 445 | */ |
8563713a | 446 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add( |
f66907f2 PW |
447 | const secp256k1_context_t* ctx, |
448 | unsigned char *seckey, | |
449 | const unsigned char *tweak | |
a9b6595e | 450 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
da3038c7 | 451 | |
ae6bc76e | 452 | /** Tweak a public key by adding tweak times the generator to it. |
f66907f2 PW |
453 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
454 | * uniformly random 32-byte arrays, or if the resulting public key | |
455 | * would be invalid (only when the tweak is the complement of the | |
456 | * corresponding private key). 1 otherwise. | |
dc0ce9fc PW |
457 | * Args: ctx: pointer to a context object initialized for validation |
458 | * (cannot be NULL). | |
459 | * In/Out: pubkey: pointer to a public key object. | |
460 | * In: tweak: pointer to a 32-byte tweak. | |
ae6bc76e | 461 | */ |
8563713a | 462 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add( |
f66907f2 PW |
463 | const secp256k1_context_t* ctx, |
464 | secp256k1_pubkey_t *pubkey, | |
465 | const unsigned char *tweak | |
23cfa914 | 466 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
da3038c7 | 467 | |
f66907f2 | 468 | /** Tweak a private key by multiplying it by a tweak. |
f66907f2 PW |
469 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
470 | * uniformly random 32-byte arrays, or equal to zero. 1 otherwise. | |
dc0ce9fc PW |
471 | * Args: ctx: pointer to a context object (cannot be NULL). |
472 | * In/Out: seckey: pointer to a 32-byte private key. | |
473 | * In: tweak: pointer to a 32-byte tweak. | |
f66907f2 | 474 | */ |
8563713a | 475 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul( |
f66907f2 PW |
476 | const secp256k1_context_t* ctx, |
477 | unsigned char *seckey, | |
478 | const unsigned char *tweak | |
a9b6595e | 479 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
ae6bc76e | 480 | |
f66907f2 | 481 | /** Tweak a public key by multiplying it by a tweak value. |
f66907f2 PW |
482 | * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for |
483 | * uniformly random 32-byte arrays, or equal to zero. 1 otherwise. | |
dc0ce9fc PW |
484 | * Args: ctx: pointer to a context object initialized for validation |
485 | * (cannot be NULL). | |
486 | * In/Out: pubkey: pointer to a public key obkect. | |
487 | * In: tweak: pointer to a 32-byte tweak. | |
ae6bc76e | 488 | */ |
8563713a | 489 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul( |
f66907f2 PW |
490 | const secp256k1_context_t* ctx, |
491 | secp256k1_pubkey_t *pubkey, | |
492 | const unsigned char *tweak | |
23cfa914 | 493 | ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
194eea06 | 494 | |
d2275795 GM |
495 | /** Updates the context randomization. |
496 | * Returns: 1: randomization successfully updated | |
497 | * 0: error | |
dc0ce9fc PW |
498 | * Args: ctx: pointer to a context object (cannot be NULL) |
499 | * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state) | |
d2275795 GM |
500 | */ |
501 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize( | |
f66907f2 PW |
502 | secp256k1_context_t* ctx, |
503 | const unsigned char *seed32 | |
d2275795 GM |
504 | ) SECP256K1_ARG_NONNULL(1); |
505 | ||
a5a66c70 PW |
506 | /** Add a number of public keys together. |
507 | * Returns: 1: the sum of the public keys is valid. | |
508 | * 0: the sum of the public keys is not valid. | |
dc0ce9fc PW |
509 | * Args: ctx: pointer to a context object |
510 | * Out: out: pointer to pubkey for placing the resulting public key | |
a5a66c70 | 511 | * (cannot be NULL) |
dc0ce9fc | 512 | * In: ins: pointer to array of pointers to public keys (cannot be NULL) |
a5a66c70 | 513 | * n: the number of public keys to add together (must be at least 1) |
a5a66c70 PW |
514 | * Use secp256k1_ec_pubkey_compress and secp256k1_ec_pubkey_decompress if the |
515 | * uncompressed format is needed. | |
516 | */ | |
517 | SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine( | |
f66907f2 PW |
518 | const secp256k1_context_t* ctx, |
519 | secp256k1_pubkey_t *out, | |
dc0ce9fc PW |
520 | const secp256k1_pubkey_t * const * ins, |
521 | int n | |
522 | ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); | |
d2275795 | 523 | |
8563713a | 524 | # ifdef __cplusplus |
a6d68949 | 525 | } |
8563713a | 526 | # endif |
a6d68949 PW |
527 | |
528 | #endif |