10 /* These rules specify the order of arguments in API calls:
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.
25 /** Opaque data structure that holds context information (precomputed tables etc.).
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
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
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.
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.
43 typedef struct secp256k1_context_struct secp256k1_context;
45 /** Opaque data structure that holds a parsed and valid public key.
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.
53 * Furthermore, it is guaranteed that identical public keys (ignoring
54 * compression) will have identical representation, so they can be memcmp'ed.
57 unsigned char data[64];
60 /** Opaque data structured that holds a parsed ECDSA signature.
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.
69 * Furthermore, it is guaranteed to identical signatures will have identical
70 * representation, so they can be memcmp'ed.
73 unsigned char data[64];
74 } secp256k1_ecdsa_signature;
76 /** A pointer to a function to deterministically generate a nonce.
78 * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
79 * Out: nonce32: pointer to a 32-byte array to be filled by the function.
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).
84 * data: Arbitrary data pointer that is passed through.
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.
89 * Except for test cases, this function should compute some cryptographic hash of
90 * the message, the algorithm, the key and the attempt.
92 typedef int (*secp256k1_nonce_function)(
93 unsigned char *nonce32,
94 const unsigned char *msg32,
95 const unsigned char *key32,
96 const unsigned char *algo16,
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))
106 # define SECP256K1_GNUC_PREREQ(_maj,_min) 0
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
116 # define SECP256K1_INLINE
119 # define SECP256K1_INLINE inline
122 #ifndef SECP256K1_API
124 # ifdef SECP256K1_BUILD
125 # define SECP256K1_API __declspec(dllexport)
127 # define SECP256K1_API
129 # elif defined(__GNUC__) && defined(SECP256K1_BUILD)
130 # define SECP256K1_API __attribute__ ((visibility ("default")))
132 # define SECP256K1_API
136 /**Warning attributes
137 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
138 * some paranoid null checks. */
139 # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
140 # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
142 # define SECP256K1_WARN_UNUSED_RESULT
144 # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
145 # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
147 # define SECP256K1_ARG_NONNULL(_x)
150 /** Flags to pass to secp256k1_context_create. */
151 # define SECP256K1_CONTEXT_VERIFY (1 << 0)
152 # define SECP256K1_CONTEXT_SIGN (1 << 1)
154 /** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */
155 # define SECP256K1_EC_COMPRESSED (1 << 0)
157 /** Create a secp256k1 context object.
159 * Returns: a newly created context object.
160 * In: flags: which parts of the context to initialize.
162 SECP256K1_API secp256k1_context* secp256k1_context_create(
164 ) SECP256K1_WARN_UNUSED_RESULT;
166 /** Copies a secp256k1 context object.
168 * Returns: a newly created context object.
169 * Args: ctx: an existing context to copy (cannot be NULL)
171 SECP256K1_API secp256k1_context* secp256k1_context_clone(
172 const secp256k1_context* ctx
173 ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
175 /** Destroy a secp256k1 context object.
177 * The context pointer may not be used afterwards.
178 * Args: ctx: an existing context to destroy (cannot be NULL)
180 SECP256K1_API void secp256k1_context_destroy(
181 secp256k1_context* ctx
184 /** Set a callback function to be called when an illegal argument is passed to
185 * an API call. It will only trigger for violations that are mentioned
186 * explicitly in the header.
188 * The philosophy is that these shouldn't be dealt with through a
189 * specific return value, as calling code should not have branches to deal with
190 * the case that this code itself is broken.
192 * On the other hand, during debug stage, one would want to be informed about
193 * such mistakes, and the default (crashing) may be inadvisable.
194 * When this callback is triggered, the API function called is guaranteed not
195 * to cause a crash, though its return value and output arguments are
198 * Args: ctx: an existing context object (cannot be NULL)
199 * In: fun: a pointer to a function to call when an illegal argument is
200 * passed to the API, taking a message and an opaque pointer
201 * (NULL restores a default handler that calls abort).
202 * data: the opaque pointer to pass to fun above.
204 SECP256K1_API void secp256k1_context_set_illegal_callback(
205 secp256k1_context* ctx,
206 void (*fun)(const char* message, void* data),
208 ) SECP256K1_ARG_NONNULL(1);
210 /** Set a callback function to be called when an internal consistency check
211 * fails. The default is crashing.
213 * This can only trigger in case of a hardware failure, miscompilation,
214 * memory corruption, serious bug in the library, or other error would can
215 * otherwise result in undefined behaviour. It will not trigger due to mere
216 * incorrect usage of the API (see secp256k1_context_set_illegal_callback
217 * for that). After this callback returns, anything may happen, including
220 * Args: ctx: an existing context object (cannot be NULL)
221 * In: fun: a pointer to a function to call when an interal error occurs,
222 * taking a message and an opaque pointer (NULL restores a default
223 * handler that calls abort).
224 * data: the opaque pointer to pass to fun above.
226 SECP256K1_API void secp256k1_context_set_error_callback(
227 secp256k1_context* ctx,
228 void (*fun)(const char* message, void* data),
230 ) SECP256K1_ARG_NONNULL(1);
232 /** Parse a variable-length public key into the pubkey object.
234 * Returns: 1 if the public key was fully valid.
235 * 0 if the public key could not be parsed or is invalid.
236 * Args: ctx: a secp256k1 context object.
237 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
238 * parsed version of input. If not, its value is undefined.
239 * In: input: pointer to a serialized public key
240 * inputlen: length of the array pointed to by input
242 * This function supports parsing compressed (33 bytes, header byte 0x02 or
243 * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
244 * byte 0x06 or 0x07) format public keys.
246 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
247 const secp256k1_context* ctx,
248 secp256k1_pubkey* pubkey,
249 const unsigned char *input,
251 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
253 /** Serialize a pubkey object into a serialized byte sequence.
256 * Args: ctx: a secp256k1 context object.
257 * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
258 * compressed==1) byte array to place the serialized key in.
259 * outputlen: a pointer to an integer which will contain the serialized
261 * In: pubkey: a pointer to a secp256k1_pubkey containing an initialized
263 * flags: SECP256K1_EC_COMPRESSED if serialization should be in
266 SECP256K1_API int secp256k1_ec_pubkey_serialize(
267 const secp256k1_context* ctx,
268 unsigned char *output,
270 const secp256k1_pubkey* pubkey,
272 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
274 /** Parse a DER ECDSA signature.
276 * Returns: 1 when the signature could be parsed, 0 otherwise.
277 * Args: ctx: a secp256k1 context object
278 * Out: sig: a pointer to a signature object
279 * In: input: a pointer to the signature to be parsed
280 * inputlen: the length of the array pointed to be input
282 * Note that this function also supports some violations of DER and even BER.
284 SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
285 const secp256k1_context* ctx,
286 secp256k1_ecdsa_signature* sig,
287 const unsigned char *input,
289 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
291 /** Serialize an ECDSA signature in DER format.
293 * Returns: 1 if enough space was available to serialize, 0 otherwise
294 * Args: ctx: a secp256k1 context object
295 * Out: output: a pointer to an array to store the DER serialization
296 * In/Out: outputlen: a pointer to a length integer. Initially, this integer
297 * should be set to the length of output. After the call
298 * it will be set to the length of the serialization (even
299 * if 0 was returned).
300 * In: sig: a pointer to an initialized signature object
302 SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
303 const secp256k1_context* ctx,
304 unsigned char *output,
306 const secp256k1_ecdsa_signature* sig
307 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
309 /** Verify an ECDSA signature.
311 * Returns: 1: correct signature
312 * 0: incorrect or unparseable signature
313 * Args: ctx: a secp256k1 context object, initialized for verification.
314 * In: sig: the signature being verified (cannot be NULL)
315 * msg32: the 32-byte message hash being verified (cannot be NULL)
316 * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
318 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
319 const secp256k1_context* ctx,
320 const secp256k1_ecdsa_signature *sig,
321 const unsigned char *msg32,
322 const secp256k1_pubkey *pubkey
323 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
325 /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
326 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
329 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
331 /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
332 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
334 /** Create an ECDSA signature.
336 * Returns: 1: signature created
337 * 0: the nonce generation function failed, or the private key was invalid.
338 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
339 * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
340 * In: msg32: the 32-byte message hash being signed (cannot be NULL)
341 * seckey: pointer to a 32-byte secret key (cannot be NULL)
342 * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
343 * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
345 * The sig always has an s value in the lower half of the range (From 0x1
346 * to 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
347 * inclusive), unlike many other implementations.
349 * With ECDSA a third-party can can forge a second distinct signature
350 * of the same message given a single initial signature without knowing
351 * the key by setting s to its additive inverse mod-order, 'flipping' the
352 * sign of the random point R which is not included in the signature.
353 * Since the forgery is of the same message this isn't universally
354 * problematic, but in systems where message malleability or uniqueness
355 * of signatures is important this can cause issues. This forgery can be
356 * blocked by all verifiers forcing signers to use a canonical form. The
357 * lower-S form reduces the size of signatures slightly on average when
358 * variable length encodings (such as DER) are used and is cheap to
359 * verify, making it a good choice. Security of always using lower-S is
360 * assured because anyone can trivially modify a signature after the
361 * fact to enforce this property. Adjusting it inside the signing
362 * function avoids the need to re-serialize or have curve specific
363 * constants outside of the library. By always using a canonical form
364 * even in applications where it isn't needed it becomes possible to
365 * impose a requirement later if a need is discovered.
366 * No other forms of ECDSA malleability are known and none seem likely,
367 * but there is no formal proof that ECDSA, even with this additional
368 * restriction, is free of other malleability. Commonly used serialization
369 * schemes will also accept various non-unique encodings, so care should
370 * be taken when this property is required for an application.
372 SECP256K1_API int secp256k1_ecdsa_sign(
373 const secp256k1_context* ctx,
374 secp256k1_ecdsa_signature *sig,
375 const unsigned char *msg32,
376 const unsigned char *seckey,
377 secp256k1_nonce_function noncefp,
379 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
381 /** Verify an ECDSA secret key.
383 * Returns: 1: secret key is valid
384 * 0: secret key is invalid
385 * Args: ctx: pointer to a context object (cannot be NULL)
386 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
388 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
389 const secp256k1_context* ctx,
390 const unsigned char *seckey
391 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
393 /** Compute the public key for a secret key.
395 * Returns: 1: secret was valid, public key stores
396 * 0: secret was invalid, try again
397 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
398 * Out: pubkey: pointer to the created public key (cannot be NULL)
399 * In: seckey: pointer to a 32-byte private key (cannot be NULL)
401 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
402 const secp256k1_context* ctx,
403 secp256k1_pubkey *pubkey,
404 const unsigned char *seckey
405 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
407 /** Export a private key in BER format.
409 * Returns: 1 if the private key was valid.
410 * Args: ctx: pointer to a context object, initialized for signing (cannot
412 * Out: privkey: pointer to an array for storing the private key in BER.
413 * Should have space for 279 bytes, and cannot be NULL.
414 * privkeylen: Pointer to an int where the length of the private key in
415 * privkey will be stored.
416 * In: seckey: pointer to a 32-byte secret key to export.
417 * flags: SECP256K1_EC_COMPRESSED if the key should be exported in
420 * This function is purely meant for compatibility with applications that
421 * require BER encoded keys. When working with secp256k1-specific code, the
422 * simple 32-byte private keys are sufficient.
424 * Note that this function does not guarantee correct DER output. It is
425 * guaranteed to be parsable by secp256k1_ec_privkey_import.
427 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_export(
428 const secp256k1_context* ctx,
429 unsigned char *privkey,
431 const unsigned char *seckey,
433 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
435 /** Import a private key in DER format.
436 * Returns: 1 if a private key was extracted.
437 * Args: ctx: pointer to a context object (cannot be NULL).
438 * Out: seckey: pointer to a 32-byte array for storing the private key.
440 * In: privkey: pointer to a private key in DER format (cannot be NULL).
441 * privkeylen: length of the DER private key pointed to be privkey.
443 * This function will accept more than just strict DER, and even allow some BER
444 * violations. The public key stored inside the DER-encoded private key is not
445 * verified for correctness, nor are the curve parameters. Use this function
446 * only if you know in advance it is supposed to contain a secp256k1 private
449 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_import(
450 const secp256k1_context* ctx,
451 unsigned char *seckey,
452 const unsigned char *privkey,
454 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
456 /** Tweak a private key by adding tweak to it.
457 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
458 * uniformly random 32-byte arrays, or if the resulting private key
459 * would be invalid (only when the tweak is the complement of the
460 * private key). 1 otherwise.
461 * Args: ctx: pointer to a context object (cannot be NULL).
462 * In/Out: seckey: pointer to a 32-byte private key.
463 * In: tweak: pointer to a 32-byte tweak.
465 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
466 const secp256k1_context* ctx,
467 unsigned char *seckey,
468 const unsigned char *tweak
469 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
471 /** Tweak a public key by adding tweak times the generator to it.
472 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
473 * uniformly random 32-byte arrays, or if the resulting public key
474 * would be invalid (only when the tweak is the complement of the
475 * corresponding private key). 1 otherwise.
476 * Args: ctx: pointer to a context object initialized for validation
478 * In/Out: pubkey: pointer to a public key object.
479 * In: tweak: pointer to a 32-byte tweak.
481 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
482 const secp256k1_context* ctx,
483 secp256k1_pubkey *pubkey,
484 const unsigned char *tweak
485 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
487 /** Tweak a private key by multiplying it by a tweak.
488 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
489 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
490 * Args: ctx: pointer to a context object (cannot be NULL).
491 * In/Out: seckey: pointer to a 32-byte private key.
492 * In: tweak: pointer to a 32-byte tweak.
494 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
495 const secp256k1_context* ctx,
496 unsigned char *seckey,
497 const unsigned char *tweak
498 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
500 /** Tweak a public key by multiplying it by a tweak value.
501 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
502 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
503 * Args: ctx: pointer to a context object initialized for validation
505 * In/Out: pubkey: pointer to a public key obkect.
506 * In: tweak: pointer to a 32-byte tweak.
508 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
509 const secp256k1_context* ctx,
510 secp256k1_pubkey *pubkey,
511 const unsigned char *tweak
512 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
514 /** Updates the context randomization.
515 * Returns: 1: randomization successfully updated
517 * Args: ctx: pointer to a context object (cannot be NULL)
518 * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
520 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
521 secp256k1_context* ctx,
522 const unsigned char *seed32
523 ) SECP256K1_ARG_NONNULL(1);
525 /** Add a number of public keys together.
526 * Returns: 1: the sum of the public keys is valid.
527 * 0: the sum of the public keys is not valid.
528 * Args: ctx: pointer to a context object
529 * Out: out: pointer to pubkey for placing the resulting public key
531 * In: ins: pointer to array of pointers to public keys (cannot be NULL)
532 * n: the number of public keys to add together (must be at least 1)
533 * Use secp256k1_ec_pubkey_compress and secp256k1_ec_pubkey_decompress if the
534 * uncompressed format is needed.
536 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
537 const secp256k1_context* ctx,
538 secp256k1_pubkey *out,
539 const secp256k1_pubkey * const * ins,
541 ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);