10 /* Unless explicitly stated all pointer arguments must not be NULL.
12 * The following rules specify the order of arguments in API calls:
14 * 1. Context pointers go first, followed by output arguments, combined
15 * output/input arguments, and finally input-only arguments.
16 * 2. Array lengths always immediately follow the argument whose length
17 * they describe, even if this violates rule 1.
18 * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated
19 * later go first. This means: signatures, public nonces, secret nonces,
20 * messages, public keys, secret keys, tweaks.
21 * 4. Arguments that are not data pointers go last, from more complex to less
22 * complex: function pointers, algorithm names, messages, void pointers,
23 * counts, flags, booleans.
24 * 5. Opaque data pointers follow the function pointer they are to be passed to.
27 /** Opaque data structure that holds context information (precomputed tables etc.).
29 * The purpose of context structures is to cache large precomputed data tables
30 * that are expensive to construct, and also to maintain the randomization data
33 * Do not create a new context object for each operation, as construction is
34 * far slower than all other API calls (~100 times slower than an ECDSA
37 * A constructed context can safely be used from multiple threads
38 * simultaneously, but API calls that take a non-const pointer to a context
39 * need exclusive access to it. In particular this is the case for
40 * secp256k1_context_destroy, secp256k1_context_preallocated_destroy,
41 * and secp256k1_context_randomize.
43 * Regarding randomization, either do it once at creation time (in which case
44 * you do not need any locking for the other calls), or use a read-write lock.
46 typedef struct secp256k1_context_struct secp256k1_context;
48 /** Opaque data structure that holds rewriteable "scratch space"
50 * The purpose of this structure is to replace dynamic memory allocations,
51 * because we target architectures where this may not be available. It is
52 * essentially a resizable (within specified parameters) block of bytes,
53 * which is initially created either by memory allocation or TODO as a pointer
54 * into some fixed rewritable space.
56 * Unlike the context object, this cannot safely be shared between threads
57 * without additional synchronization logic.
59 typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
61 /** Opaque data structure that holds a parsed and valid public key.
63 * The exact representation of data inside is implementation defined and not
64 * guaranteed to be portable between different platforms or versions. It is
65 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
66 * If you need to convert to a format suitable for storage or transmission,
67 * use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse. To
68 * compare keys, use secp256k1_ec_pubkey_cmp.
71 unsigned char data[64];
74 /** Opaque data structured that holds a parsed ECDSA signature.
76 * The exact representation of data inside is implementation defined and not
77 * guaranteed to be portable between different platforms or versions. It is
78 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
79 * If you need to convert to a format suitable for storage, transmission, or
80 * comparison, use the secp256k1_ecdsa_signature_serialize_* and
81 * secp256k1_ecdsa_signature_parse_* functions.
84 unsigned char data[64];
85 } secp256k1_ecdsa_signature;
87 /** A pointer to a function to deterministically generate a nonce.
89 * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
90 * Out: nonce32: pointer to a 32-byte array to be filled by the function.
91 * In: msg32: the 32-byte message hash being verified (will not be NULL)
92 * key32: pointer to a 32-byte secret key (will not be NULL)
93 * algo16: pointer to a 16-byte array describing the signature
94 * algorithm (will be NULL for ECDSA for compatibility).
95 * data: Arbitrary data pointer that is passed through.
96 * attempt: how many iterations we have tried to find a nonce.
97 * This will almost always be 0, but different attempt values
98 * are required to result in a different nonce.
100 * Except for test cases, this function should compute some cryptographic hash of
101 * the message, the algorithm, the key and the attempt.
103 typedef int (*secp256k1_nonce_function)(
104 unsigned char *nonce32,
105 const unsigned char *msg32,
106 const unsigned char *key32,
107 const unsigned char *algo16,
112 # if !defined(SECP256K1_GNUC_PREREQ)
113 # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
114 # define SECP256K1_GNUC_PREREQ(_maj,_min) \
115 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
117 # define SECP256K1_GNUC_PREREQ(_maj,_min) 0
121 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
122 # if SECP256K1_GNUC_PREREQ(2,7)
123 # define SECP256K1_INLINE __inline__
124 # elif (defined(_MSC_VER))
125 # define SECP256K1_INLINE __inline
127 # define SECP256K1_INLINE
130 # define SECP256K1_INLINE inline
133 /** When this header is used at build-time the SECP256K1_BUILD define needs to be set
134 * to correctly setup export attributes and nullness checks. This is normally done
135 * by secp256k1.c but to guard against this header being included before secp256k1.c
136 * has had a chance to set the define (e.g. via test harnesses that just includes
137 * secp256k1.c) we set SECP256K1_NO_BUILD when this header is processed without the
138 * BUILD define so this condition can be caught.
140 #ifndef SECP256K1_BUILD
141 # define SECP256K1_NO_BUILD
144 #ifndef SECP256K1_API
146 # ifdef SECP256K1_BUILD
147 # define SECP256K1_API __declspec(dllexport)
149 # define SECP256K1_API
151 # elif defined(__GNUC__) && (__GNUC__ >= 4) && defined(SECP256K1_BUILD)
152 # define SECP256K1_API __attribute__ ((visibility ("default")))
154 # define SECP256K1_API
158 /**Warning attributes
159 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
160 * some paranoid null checks. */
161 # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
162 # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
164 # define SECP256K1_WARN_UNUSED_RESULT
166 # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
167 # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
169 # define SECP256K1_ARG_NONNULL(_x)
172 /** All flags' lower 8 bits indicate what they're for. Do not use directly. */
173 #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
174 #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
175 #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
176 /** The higher bits contain the actual data. Do not use directly. */
177 #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
178 #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
179 #define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
180 #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
182 /** Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
183 * secp256k1_context_preallocated_create. */
184 #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
185 #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
186 #define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
187 #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
189 /** Flag to pass to secp256k1_ec_pubkey_serialize. */
190 #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
191 #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
193 /** Prefix byte used to tag various encoded curvepoints for specific purposes */
194 #define SECP256K1_TAG_PUBKEY_EVEN 0x02
195 #define SECP256K1_TAG_PUBKEY_ODD 0x03
196 #define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
197 #define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
198 #define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
200 /** A simple secp256k1 context object with no precomputed tables. These are useful for
201 * type serialization/parsing functions which require a context object to maintain
202 * API consistency, but currently do not require expensive precomputations or dynamic
205 SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
207 /** Create a secp256k1 context object (in dynamically allocated memory).
209 * This function uses malloc to allocate memory. It is guaranteed that malloc is
210 * called at most once for every call of this function. If you need to avoid dynamic
211 * memory allocation entirely, see the functions in secp256k1_preallocated.h.
213 * Returns: a newly created context object.
214 * In: flags: which parts of the context to initialize.
216 * See also secp256k1_context_randomize.
218 SECP256K1_API secp256k1_context* secp256k1_context_create(
220 ) SECP256K1_WARN_UNUSED_RESULT;
222 /** Copy a secp256k1 context object (into dynamically allocated memory).
224 * This function uses malloc to allocate memory. It is guaranteed that malloc is
225 * called at most once for every call of this function. If you need to avoid dynamic
226 * memory allocation entirely, see the functions in secp256k1_preallocated.h.
228 * Returns: a newly created context object.
229 * Args: ctx: an existing context to copy (cannot be NULL)
231 SECP256K1_API secp256k1_context* secp256k1_context_clone(
232 const secp256k1_context* ctx
233 ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
235 /** Destroy a secp256k1 context object (created in dynamically allocated memory).
237 * The context pointer may not be used afterwards.
239 * The context to destroy must have been created using secp256k1_context_create
240 * or secp256k1_context_clone. If the context has instead been created using
241 * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
242 * behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
245 * Args: ctx: an existing context to destroy, constructed using
246 * secp256k1_context_create or secp256k1_context_clone
248 SECP256K1_API void secp256k1_context_destroy(
249 secp256k1_context* ctx
252 /** Set a callback function to be called when an illegal argument is passed to
253 * an API call. It will only trigger for violations that are mentioned
254 * explicitly in the header.
256 * The philosophy is that these shouldn't be dealt with through a
257 * specific return value, as calling code should not have branches to deal with
258 * the case that this code itself is broken.
260 * On the other hand, during debug stage, one would want to be informed about
261 * such mistakes, and the default (crashing) may be inadvisable.
262 * When this callback is triggered, the API function called is guaranteed not
263 * to cause a crash, though its return value and output arguments are
266 * When this function has not been called (or called with fn==NULL), then the
267 * default handler will be used. The library provides a default handler which
268 * writes the message to stderr and calls abort. This default handler can be
269 * replaced at link time if the preprocessor macro
270 * USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build
271 * has been configured with --enable-external-default-callbacks. Then the
272 * following two symbols must be provided to link against:
273 * - void secp256k1_default_illegal_callback_fn(const char* message, void* data);
274 * - void secp256k1_default_error_callback_fn(const char* message, void* data);
275 * The library can call these default handlers even before a proper callback data
276 * pointer could have been set using secp256k1_context_set_illegal_callback or
277 * secp256k1_context_set_error_callback, e.g., when the creation of a context
278 * fails. In this case, the corresponding default handler will be called with
279 * the data pointer argument set to NULL.
281 * Args: ctx: an existing context object (cannot be NULL)
282 * In: fun: a pointer to a function to call when an illegal argument is
283 * passed to the API, taking a message and an opaque pointer.
284 * (NULL restores the default handler.)
285 * data: the opaque pointer to pass to fun above.
287 * See also secp256k1_context_set_error_callback.
289 SECP256K1_API void secp256k1_context_set_illegal_callback(
290 secp256k1_context* ctx,
291 void (*fun)(const char* message, void* data),
293 ) SECP256K1_ARG_NONNULL(1);
295 /** Set a callback function to be called when an internal consistency check
296 * fails. The default is crashing.
298 * This can only trigger in case of a hardware failure, miscompilation,
299 * memory corruption, serious bug in the library, or other error would can
300 * otherwise result in undefined behaviour. It will not trigger due to mere
301 * incorrect usage of the API (see secp256k1_context_set_illegal_callback
302 * for that). After this callback returns, anything may happen, including
305 * Args: ctx: an existing context object (cannot be NULL)
306 * In: fun: a pointer to a function to call when an internal error occurs,
307 * taking a message and an opaque pointer (NULL restores the
308 * default handler, see secp256k1_context_set_illegal_callback
310 * data: the opaque pointer to pass to fun above.
312 * See also secp256k1_context_set_illegal_callback.
314 SECP256K1_API void secp256k1_context_set_error_callback(
315 secp256k1_context* ctx,
316 void (*fun)(const char* message, void* data),
318 ) SECP256K1_ARG_NONNULL(1);
320 /** Create a secp256k1 scratch space object.
322 * Returns: a newly created scratch space.
323 * Args: ctx: an existing context object (cannot be NULL)
324 * In: size: amount of memory to be available as scratch space. Some extra
325 * (<100 bytes) will be allocated for extra accounting.
327 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
328 const secp256k1_context* ctx,
330 ) SECP256K1_ARG_NONNULL(1);
332 /** Destroy a secp256k1 scratch space.
334 * The pointer may not be used afterwards.
335 * Args: ctx: a secp256k1 context object.
336 * scratch: space to destroy
338 SECP256K1_API void secp256k1_scratch_space_destroy(
339 const secp256k1_context* ctx,
340 secp256k1_scratch_space* scratch
341 ) SECP256K1_ARG_NONNULL(1);
343 /** Parse a variable-length public key into the pubkey object.
345 * Returns: 1 if the public key was fully valid.
346 * 0 if the public key could not be parsed or is invalid.
347 * Args: ctx: a secp256k1 context object.
348 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
349 * parsed version of input. If not, its value is undefined.
350 * In: input: pointer to a serialized public key
351 * inputlen: length of the array pointed to by input
353 * This function supports parsing compressed (33 bytes, header byte 0x02 or
354 * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
355 * byte 0x06 or 0x07) format public keys.
357 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
358 const secp256k1_context* ctx,
359 secp256k1_pubkey* pubkey,
360 const unsigned char *input,
362 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
364 /** Serialize a pubkey object into a serialized byte sequence.
367 * Args: ctx: a secp256k1 context object.
368 * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
369 * compressed==1) byte array to place the serialized key
371 * In/Out: outputlen: a pointer to an integer which is initially set to the
372 * size of output, and is overwritten with the written
374 * In: pubkey: a pointer to a secp256k1_pubkey containing an
375 * initialized public key.
376 * flags: SECP256K1_EC_COMPRESSED if serialization should be in
377 * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
379 SECP256K1_API int secp256k1_ec_pubkey_serialize(
380 const secp256k1_context* ctx,
381 unsigned char *output,
383 const secp256k1_pubkey* pubkey,
385 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
387 /** Compare two public keys using lexicographic (of compressed serialization) order
389 * Returns: <0 if the first public key is less than the second
390 * >0 if the first public key is greater than the second
391 * 0 if the two public keys are equal
392 * Args: ctx: a secp256k1 context object.
393 * In: pubkey1: first public key to compare
394 * pubkey2: second public key to compare
396 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(
397 const secp256k1_context* ctx,
398 const secp256k1_pubkey* pubkey1,
399 const secp256k1_pubkey* pubkey2
400 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
402 /** Parse an ECDSA signature in compact (64 bytes) format.
404 * Returns: 1 when the signature could be parsed, 0 otherwise.
405 * Args: ctx: a secp256k1 context object
406 * Out: sig: a pointer to a signature object
407 * In: input64: a pointer to the 64-byte array to parse
409 * The signature must consist of a 32-byte big endian R value, followed by a
410 * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
411 * encoding is invalid. R and S with value 0 are allowed in the encoding.
413 * After the call, sig will always be initialized. If parsing failed or R or
414 * S are zero, the resulting sig value is guaranteed to fail validation for any
415 * message and public key.
417 SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
418 const secp256k1_context* ctx,
419 secp256k1_ecdsa_signature* sig,
420 const unsigned char *input64
421 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
423 /** Parse a DER ECDSA signature.
425 * Returns: 1 when the signature could be parsed, 0 otherwise.
426 * Args: ctx: a secp256k1 context object
427 * Out: sig: a pointer to a signature object
428 * In: input: a pointer to the signature to be parsed
429 * inputlen: the length of the array pointed to be input
431 * This function will accept any valid DER encoded signature, even if the
432 * encoded numbers are out of range.
434 * After the call, sig will always be initialized. If parsing failed or the
435 * encoded numbers are out of range, signature validation with it is
436 * guaranteed to fail for every message and public key.
438 SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
439 const secp256k1_context* ctx,
440 secp256k1_ecdsa_signature* sig,
441 const unsigned char *input,
443 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
445 /** Serialize an ECDSA signature in DER format.
447 * Returns: 1 if enough space was available to serialize, 0 otherwise
448 * Args: ctx: a secp256k1 context object
449 * Out: output: a pointer to an array to store the DER serialization
450 * In/Out: outputlen: a pointer to a length integer. Initially, this integer
451 * should be set to the length of output. After the call
452 * it will be set to the length of the serialization (even
453 * if 0 was returned).
454 * In: sig: a pointer to an initialized signature object
456 SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
457 const secp256k1_context* ctx,
458 unsigned char *output,
460 const secp256k1_ecdsa_signature* sig
461 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
463 /** Serialize an ECDSA signature in compact (64 byte) format.
466 * Args: ctx: a secp256k1 context object
467 * Out: output64: a pointer to a 64-byte array to store the compact serialization
468 * In: sig: a pointer to an initialized signature object
470 * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
472 SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
473 const secp256k1_context* ctx,
474 unsigned char *output64,
475 const secp256k1_ecdsa_signature* sig
476 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
478 /** Verify an ECDSA signature.
480 * Returns: 1: correct signature
481 * 0: incorrect or unparseable signature
482 * Args: ctx: a secp256k1 context object, initialized for verification.
483 * In: sig: the signature being verified (cannot be NULL)
484 * msghash32: the 32-byte message hash being verified (cannot be NULL).
485 * The verifier must make sure to apply a cryptographic
486 * hash function to the message by itself and not accept an
487 * msghash32 value directly. Otherwise, it would be easy to
488 * create a "valid" signature without knowledge of the
489 * secret key. See also
490 * https://bitcoin.stackexchange.com/a/81116/35586 for more
491 * background on this topic.
492 * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
494 * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
497 * If you need to accept ECDSA signatures from sources that do not obey this
498 * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
499 * validation, but be aware that doing so results in malleable signatures.
501 * For details, see the comments for that function.
503 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
504 const secp256k1_context* ctx,
505 const secp256k1_ecdsa_signature *sig,
506 const unsigned char *msghash32,
507 const secp256k1_pubkey *pubkey
508 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
510 /** Convert a signature to a normalized lower-S form.
512 * Returns: 1 if sigin was not normalized, 0 if it already was.
513 * Args: ctx: a secp256k1 context object
514 * Out: sigout: a pointer to a signature to fill with the normalized form,
515 * or copy if the input was already normalized. (can be NULL if
516 * you're only interested in whether the input was already
518 * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
519 * can be identical to sigout)
521 * With ECDSA a third-party can forge a second distinct signature of the same
522 * message, given a single initial signature, but without knowing the key. This
523 * is done by negating the S value modulo the order of the curve, 'flipping'
524 * the sign of the random point R which is not included in the signature.
526 * Forgery of the same message isn't universally problematic, but in systems
527 * where message malleability or uniqueness of signatures is important this can
528 * cause issues. This forgery can be blocked by all verifiers forcing signers
529 * to use a normalized form.
531 * The lower-S form reduces the size of signatures slightly on average when
532 * variable length encodings (such as DER) are used and is cheap to verify,
533 * making it a good choice. Security of always using lower-S is assured because
534 * anyone can trivially modify a signature after the fact to enforce this
537 * The lower S value is always between 0x1 and
538 * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
541 * No other forms of ECDSA malleability are known and none seem likely, but
542 * there is no formal proof that ECDSA, even with this additional restriction,
543 * is free of other malleability. Commonly used serialization schemes will also
544 * accept various non-unique encodings, so care should be taken when this
545 * property is required for an application.
547 * The secp256k1_ecdsa_sign function will by default create signatures in the
548 * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
549 * signatures come from a system that cannot enforce this property,
550 * secp256k1_ecdsa_signature_normalize must be called before verification.
552 SECP256K1_API int secp256k1_ecdsa_signature_normalize(
553 const secp256k1_context* ctx,
554 secp256k1_ecdsa_signature *sigout,
555 const secp256k1_ecdsa_signature *sigin
556 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
558 /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
559 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
562 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
564 /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
565 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
567 /** Create an ECDSA signature.
569 * Returns: 1: signature created
570 * 0: the nonce generation function failed, or the secret key was invalid.
571 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
572 * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
573 * In: msghash32: the 32-byte message hash being signed (cannot be NULL)
574 * seckey: pointer to a 32-byte secret key (cannot be NULL)
575 * noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
576 * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
578 * The created signature is always in lower-S form. See
579 * secp256k1_ecdsa_signature_normalize for more details.
581 SECP256K1_API int secp256k1_ecdsa_sign(
582 const secp256k1_context* ctx,
583 secp256k1_ecdsa_signature *sig,
584 const unsigned char *msghash32,
585 const unsigned char *seckey,
586 secp256k1_nonce_function noncefp,
588 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
590 /** Verify an ECDSA secret key.
592 * A secret key is valid if it is not 0 and less than the secp256k1 curve order
593 * when interpreted as an integer (most significant byte first). The
594 * probability of choosing a 32-byte string uniformly at random which is an
595 * invalid secret key is negligible.
597 * Returns: 1: secret key is valid
598 * 0: secret key is invalid
599 * Args: ctx: pointer to a context object (cannot be NULL)
600 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
602 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
603 const secp256k1_context* ctx,
604 const unsigned char *seckey
605 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
607 /** Compute the public key for a secret key.
609 * Returns: 1: secret was valid, public key stores
610 * 0: secret was invalid, try again
611 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
612 * Out: pubkey: pointer to the created public key (cannot be NULL)
613 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
615 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
616 const secp256k1_context* ctx,
617 secp256k1_pubkey *pubkey,
618 const unsigned char *seckey
619 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
621 /** Negates a secret key in place.
623 * Returns: 0 if the given secret key is invalid according to
624 * secp256k1_ec_seckey_verify. 1 otherwise
625 * Args: ctx: pointer to a context object
626 * In/Out: seckey: pointer to the 32-byte secret key to be negated. If the
627 * secret key is invalid according to
628 * secp256k1_ec_seckey_verify, this function returns 0 and
629 * seckey will be set to some unspecified value. (cannot be
632 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(
633 const secp256k1_context* ctx,
634 unsigned char *seckey
635 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
637 /** Same as secp256k1_ec_seckey_negate, but DEPRECATED. Will be removed in
638 * future versions. */
639 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
640 const secp256k1_context* ctx,
641 unsigned char *seckey
642 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
644 /** Negates a public key in place.
647 * Args: ctx: pointer to a context object
648 * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
650 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
651 const secp256k1_context* ctx,
652 secp256k1_pubkey *pubkey
653 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
655 /** Tweak a secret key by adding tweak to it.
657 * Returns: 0 if the arguments are invalid or the resulting secret key would be
658 * invalid (only when the tweak is the negation of the secret key). 1
660 * Args: ctx: pointer to a context object (cannot be NULL).
661 * In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
662 * invalid according to secp256k1_ec_seckey_verify, this
663 * function returns 0. seckey will be set to some unspecified
664 * value if this function returns 0. (cannot be NULL)
665 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
666 * secp256k1_ec_seckey_verify, this function returns 0. For
667 * uniformly random 32-byte arrays the chance of being invalid
668 * is negligible (around 1 in 2^128) (cannot be NULL).
670 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(
671 const secp256k1_context* ctx,
672 unsigned char *seckey,
673 const unsigned char *tweak32
674 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
676 /** Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED. Will be removed in
677 * future versions. */
678 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
679 const secp256k1_context* ctx,
680 unsigned char *seckey,
681 const unsigned char *tweak32
682 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
684 /** Tweak a public key by adding tweak times the generator to it.
686 * Returns: 0 if the arguments are invalid or the resulting public key would be
687 * invalid (only when the tweak is the negation of the corresponding
688 * secret key). 1 otherwise.
689 * Args: ctx: pointer to a context object initialized for validation
691 * In/Out: pubkey: pointer to a public key object. pubkey will be set to an
692 * invalid value if this function returns 0 (cannot be NULL).
693 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
694 * secp256k1_ec_seckey_verify, this function returns 0. For
695 * uniformly random 32-byte arrays the chance of being invalid
696 * is negligible (around 1 in 2^128) (cannot be NULL).
698 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
699 const secp256k1_context* ctx,
700 secp256k1_pubkey *pubkey,
701 const unsigned char *tweak32
702 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
704 /** Tweak a secret key by multiplying it by a tweak.
706 * Returns: 0 if the arguments are invalid. 1 otherwise.
707 * Args: ctx: pointer to a context object (cannot be NULL).
708 * In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
709 * invalid according to secp256k1_ec_seckey_verify, this
710 * function returns 0. seckey will be set to some unspecified
711 * value if this function returns 0. (cannot be NULL)
712 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
713 * secp256k1_ec_seckey_verify, this function returns 0. For
714 * uniformly random 32-byte arrays the chance of being invalid
715 * is negligible (around 1 in 2^128) (cannot be NULL).
717 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(
718 const secp256k1_context* ctx,
719 unsigned char *seckey,
720 const unsigned char *tweak32
721 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
723 /** Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED. Will be removed in
724 * future versions. */
725 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
726 const secp256k1_context* ctx,
727 unsigned char *seckey,
728 const unsigned char *tweak32
729 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
731 /** Tweak a public key by multiplying it by a tweak value.
733 * Returns: 0 if the arguments are invalid. 1 otherwise.
734 * Args: ctx: pointer to a context object initialized for validation
736 * In/Out: pubkey: pointer to a public key object. pubkey will be set to an
737 * invalid value if this function returns 0 (cannot be NULL).
738 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
739 * secp256k1_ec_seckey_verify, this function returns 0. For
740 * uniformly random 32-byte arrays the chance of being invalid
741 * is negligible (around 1 in 2^128) (cannot be NULL).
743 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
744 const secp256k1_context* ctx,
745 secp256k1_pubkey *pubkey,
746 const unsigned char *tweak32
747 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
749 /** Updates the context randomization to protect against side-channel leakage.
750 * Returns: 1: randomization successfully updated or nothing to randomize
752 * Args: ctx: pointer to a context object (cannot be NULL)
753 * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
755 * While secp256k1 code is written to be constant-time no matter what secret
756 * values are, it's possible that a future compiler may output code which isn't,
757 * and also that the CPU may not emit the same radio frequencies or draw the same
758 * amount power for all values.
760 * This function provides a seed which is combined into the blinding value: that
761 * blinding value is added before each multiplication (and removed afterwards) so
762 * that it does not affect function results, but shields against attacks which
763 * rely on any input-dependent behaviour.
765 * This function has currently an effect only on contexts initialized for signing
766 * because randomization is currently used only for signing. However, this is not
767 * guaranteed and may change in the future. It is safe to call this function on
768 * contexts not initialized for signing; then it will have no effect and return 1.
770 * You should call this after secp256k1_context_create or
771 * secp256k1_context_clone (and secp256k1_context_preallocated_create or
772 * secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
774 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
775 secp256k1_context* ctx,
776 const unsigned char *seed32
777 ) SECP256K1_ARG_NONNULL(1);
779 /** Add a number of public keys together.
781 * Returns: 1: the sum of the public keys is valid.
782 * 0: the sum of the public keys is not valid.
783 * Args: ctx: pointer to a context object
784 * Out: out: pointer to a public key object for placing the resulting public key
786 * In: ins: pointer to array of pointers to public keys (cannot be NULL)
787 * n: the number of public keys to add together (must be at least 1)
789 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
790 const secp256k1_context* ctx,
791 secp256k1_pubkey *out,
792 const secp256k1_pubkey * const * ins,
794 ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
796 /** Compute a tagged hash as defined in BIP-340.
798 * This is useful for creating a message hash and achieving domain separation
799 * through an application-specific tag. This function returns
800 * SHA256(SHA256(tag)||SHA256(tag)||msg). Therefore, tagged hash
801 * implementations optimized for a specific tag can precompute the SHA256 state
802 * after hashing the tag hashes.
804 * Returns 0 if the arguments are invalid and 1 otherwise.
805 * Args: ctx: pointer to a context object
806 * Out: hash32: pointer to a 32-byte array to store the resulting hash
807 * In: tag: pointer to an array containing the tag
808 * taglen: length of the tag array
809 * msg: pointer to an array containing the message
810 * msglen: length of the message array
812 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_tagged_sha256(
813 const secp256k1_context* ctx,
814 unsigned char *hash32,
815 const unsigned char *tag,
817 const unsigned char *msg,
819 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5);
825 #endif /* SECP256K1_H */