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 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, secret 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 calls 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, secp256k1_context_preallocated_destroy,
39 * and secp256k1_context_randomize.
41 * Regarding randomization, either do it once at creation time (in which case
42 * you do not need any locking for the other calls), or use a read-write lock.
44 typedef struct secp256k1_context_struct secp256k1_context;
46 /** Opaque data structure that holds rewriteable "scratch space"
48 * The purpose of this structure is to replace dynamic memory allocations,
49 * because we target architectures where this may not be available. It is
50 * essentially a resizable (within specified parameters) block of bytes,
51 * which is initially created either by memory allocation or TODO as a pointer
52 * into some fixed rewritable space.
54 * Unlike the context object, this cannot safely be shared between threads
55 * without additional synchronization logic.
57 typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
59 /** Opaque data structure that holds a parsed and valid public key.
61 * The exact representation of data inside is implementation defined and not
62 * guaranteed to be portable between different platforms or versions. It is
63 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
64 * If you need to convert to a format suitable for storage, transmission, or
65 * comparison, use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse.
68 unsigned char data[64];
71 /** Opaque data structured that holds a parsed ECDSA signature.
73 * The exact representation of data inside is implementation defined and not
74 * guaranteed to be portable between different platforms or versions. It is
75 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
76 * If you need to convert to a format suitable for storage, transmission, or
77 * comparison, use the secp256k1_ecdsa_signature_serialize_* and
78 * secp256k1_ecdsa_signature_parse_* functions.
81 unsigned char data[64];
82 } secp256k1_ecdsa_signature;
84 /** A pointer to a function to deterministically generate a nonce.
86 * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
87 * Out: nonce32: pointer to a 32-byte array to be filled by the function.
88 * In: msg32: the 32-byte message hash being verified (will not be NULL)
89 * key32: pointer to a 32-byte secret key (will not be NULL)
90 * algo16: pointer to a 16-byte array describing the signature
91 * algorithm (will be NULL for ECDSA for compatibility).
92 * data: Arbitrary data pointer that is passed through.
93 * attempt: how many iterations we have tried to find a nonce.
94 * This will almost always be 0, but different attempt values
95 * are required to result in a different nonce.
97 * Except for test cases, this function should compute some cryptographic hash of
98 * the message, the algorithm, the key and the attempt.
100 typedef int (*secp256k1_nonce_function)(
101 unsigned char *nonce32,
102 const unsigned char *msg32,
103 const unsigned char *key32,
104 const unsigned char *algo16,
109 # if !defined(SECP256K1_GNUC_PREREQ)
110 # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
111 # define SECP256K1_GNUC_PREREQ(_maj,_min) \
112 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
114 # define SECP256K1_GNUC_PREREQ(_maj,_min) 0
118 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
119 # if SECP256K1_GNUC_PREREQ(2,7)
120 # define SECP256K1_INLINE __inline__
121 # elif (defined(_MSC_VER))
122 # define SECP256K1_INLINE __inline
124 # define SECP256K1_INLINE
127 # define SECP256K1_INLINE inline
130 /** When this header is used at build-time the SECP256K1_BUILD define needs to be set
131 * to correctly setup export attributes and nullness checks. This is normally done
132 * by secp256k1.c but to guard against this header being included before secp256k1.c
133 * has had a chance to set the define (e.g. via test harnesses that just includes
134 * secp256k1.c) we set SECP256K1_NO_BUILD when this header is processed without the
135 * BUILD define so this condition can be caught.
137 #ifndef SECP256K1_BUILD
138 # define SECP256K1_NO_BUILD
141 #ifndef SECP256K1_API
143 # ifdef SECP256K1_BUILD
144 # define SECP256K1_API __declspec(dllexport)
146 # define SECP256K1_API
148 # elif defined(__GNUC__) && (__GNUC__ >= 4) && defined(SECP256K1_BUILD)
149 # define SECP256K1_API __attribute__ ((visibility ("default")))
151 # define SECP256K1_API
155 /**Warning attributes
156 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
157 * some paranoid null checks. */
158 # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
159 # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
161 # define SECP256K1_WARN_UNUSED_RESULT
163 # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
164 # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
166 # define SECP256K1_ARG_NONNULL(_x)
169 /** All flags' lower 8 bits indicate what they're for. Do not use directly. */
170 #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
171 #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
172 #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
173 /** The higher bits contain the actual data. Do not use directly. */
174 #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
175 #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
176 #define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
177 #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
179 /** Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
180 * secp256k1_context_preallocated_create. */
181 #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
182 #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
183 #define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
184 #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
186 /** Flag to pass to secp256k1_ec_pubkey_serialize. */
187 #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
188 #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
190 /** Prefix byte used to tag various encoded curvepoints for specific purposes */
191 #define SECP256K1_TAG_PUBKEY_EVEN 0x02
192 #define SECP256K1_TAG_PUBKEY_ODD 0x03
193 #define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
194 #define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
195 #define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
197 /** A simple secp256k1 context object with no precomputed tables. These are useful for
198 * type serialization/parsing functions which require a context object to maintain
199 * API consistency, but currently do not require expensive precomputations or dynamic
202 SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
204 /** Create a secp256k1 context object (in dynamically allocated memory).
206 * This function uses malloc to allocate memory. It is guaranteed that malloc is
207 * called at most once for every call of this function. If you need to avoid dynamic
208 * memory allocation entirely, see the functions in secp256k1_preallocated.h.
210 * Returns: a newly created context object.
211 * In: flags: which parts of the context to initialize.
213 * See also secp256k1_context_randomize.
215 SECP256K1_API secp256k1_context* secp256k1_context_create(
217 ) SECP256K1_WARN_UNUSED_RESULT;
219 /** Copy a secp256k1 context object (into dynamically allocated memory).
221 * This function uses malloc to allocate memory. It is guaranteed that malloc is
222 * called at most once for every call of this function. If you need to avoid dynamic
223 * memory allocation entirely, see the functions in secp256k1_preallocated.h.
225 * Returns: a newly created context object.
226 * Args: ctx: an existing context to copy (cannot be NULL)
228 SECP256K1_API secp256k1_context* secp256k1_context_clone(
229 const secp256k1_context* ctx
230 ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
232 /** Destroy a secp256k1 context object (created in dynamically allocated memory).
234 * The context pointer may not be used afterwards.
236 * The context to destroy must have been created using secp256k1_context_create
237 * or secp256k1_context_clone. If the context has instead been created using
238 * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
239 * behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
242 * Args: ctx: an existing context to destroy, constructed using
243 * secp256k1_context_create or secp256k1_context_clone
245 SECP256K1_API void secp256k1_context_destroy(
246 secp256k1_context* ctx
249 /** Set a callback function to be called when an illegal argument is passed to
250 * an API call. It will only trigger for violations that are mentioned
251 * explicitly in the header.
253 * The philosophy is that these shouldn't be dealt with through a
254 * specific return value, as calling code should not have branches to deal with
255 * the case that this code itself is broken.
257 * On the other hand, during debug stage, one would want to be informed about
258 * such mistakes, and the default (crashing) may be inadvisable.
259 * When this callback is triggered, the API function called is guaranteed not
260 * to cause a crash, though its return value and output arguments are
263 * When this function has not been called (or called with fn==NULL), then the
264 * default handler will be used. The library provides a default handler which
265 * writes the message to stderr and calls abort. This default handler can be
266 * replaced at link time if the preprocessor macro
267 * USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build
268 * has been configured with --enable-external-default-callbacks. Then the
269 * following two symbols must be provided to link against:
270 * - void secp256k1_default_illegal_callback_fn(const char* message, void* data);
271 * - void secp256k1_default_error_callback_fn(const char* message, void* data);
272 * The library can call these default handlers even before a proper callback data
273 * pointer could have been set using secp256k1_context_set_illegal_callback or
274 * secp256k1_context_set_error_callback, e.g., when the creation of a context
275 * fails. In this case, the corresponding default handler will be called with
276 * the data pointer argument set to NULL.
278 * Args: ctx: an existing context object (cannot be NULL)
279 * In: fun: a pointer to a function to call when an illegal argument is
280 * passed to the API, taking a message and an opaque pointer.
281 * (NULL restores the default handler.)
282 * data: the opaque pointer to pass to fun above.
284 * See also secp256k1_context_set_error_callback.
286 SECP256K1_API void secp256k1_context_set_illegal_callback(
287 secp256k1_context* ctx,
288 void (*fun)(const char* message, void* data),
290 ) SECP256K1_ARG_NONNULL(1);
292 /** Set a callback function to be called when an internal consistency check
293 * fails. The default is crashing.
295 * This can only trigger in case of a hardware failure, miscompilation,
296 * memory corruption, serious bug in the library, or other error would can
297 * otherwise result in undefined behaviour. It will not trigger due to mere
298 * incorrect usage of the API (see secp256k1_context_set_illegal_callback
299 * for that). After this callback returns, anything may happen, including
302 * Args: ctx: an existing context object (cannot be NULL)
303 * In: fun: a pointer to a function to call when an internal error occurs,
304 * taking a message and an opaque pointer (NULL restores the
305 * default handler, see secp256k1_context_set_illegal_callback
307 * data: the opaque pointer to pass to fun above.
309 * See also secp256k1_context_set_illegal_callback.
311 SECP256K1_API void secp256k1_context_set_error_callback(
312 secp256k1_context* ctx,
313 void (*fun)(const char* message, void* data),
315 ) SECP256K1_ARG_NONNULL(1);
317 /** Create a secp256k1 scratch space object.
319 * Returns: a newly created scratch space.
320 * Args: ctx: an existing context object (cannot be NULL)
321 * In: size: amount of memory to be available as scratch space. Some extra
322 * (<100 bytes) will be allocated for extra accounting.
324 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
325 const secp256k1_context* ctx,
327 ) SECP256K1_ARG_NONNULL(1);
329 /** Destroy a secp256k1 scratch space.
331 * The pointer may not be used afterwards.
332 * Args: ctx: a secp256k1 context object.
333 * scratch: space to destroy
335 SECP256K1_API void secp256k1_scratch_space_destroy(
336 const secp256k1_context* ctx,
337 secp256k1_scratch_space* scratch
338 ) SECP256K1_ARG_NONNULL(1);
340 /** Parse a variable-length public key into the pubkey object.
342 * Returns: 1 if the public key was fully valid.
343 * 0 if the public key could not be parsed or is invalid.
344 * Args: ctx: a secp256k1 context object.
345 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
346 * parsed version of input. If not, its value is undefined.
347 * In: input: pointer to a serialized public key
348 * inputlen: length of the array pointed to by input
350 * This function supports parsing compressed (33 bytes, header byte 0x02 or
351 * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
352 * byte 0x06 or 0x07) format public keys.
354 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
355 const secp256k1_context* ctx,
356 secp256k1_pubkey* pubkey,
357 const unsigned char *input,
359 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
361 /** Serialize a pubkey object into a serialized byte sequence.
364 * Args: ctx: a secp256k1 context object.
365 * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
366 * compressed==1) byte array to place the serialized key
368 * In/Out: outputlen: a pointer to an integer which is initially set to the
369 * size of output, and is overwritten with the written
371 * In: pubkey: a pointer to a secp256k1_pubkey containing an
372 * initialized public key.
373 * flags: SECP256K1_EC_COMPRESSED if serialization should be in
374 * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
376 SECP256K1_API int secp256k1_ec_pubkey_serialize(
377 const secp256k1_context* ctx,
378 unsigned char *output,
380 const secp256k1_pubkey* pubkey,
382 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
384 /** Parse an ECDSA signature in compact (64 bytes) format.
386 * Returns: 1 when the signature could be parsed, 0 otherwise.
387 * Args: ctx: a secp256k1 context object
388 * Out: sig: a pointer to a signature object
389 * In: input64: a pointer to the 64-byte array to parse
391 * The signature must consist of a 32-byte big endian R value, followed by a
392 * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
393 * encoding is invalid. R and S with value 0 are allowed in the encoding.
395 * After the call, sig will always be initialized. If parsing failed or R or
396 * S are zero, the resulting sig value is guaranteed to fail validation for any
397 * message and public key.
399 SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
400 const secp256k1_context* ctx,
401 secp256k1_ecdsa_signature* sig,
402 const unsigned char *input64
403 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
405 /** Parse a DER ECDSA signature.
407 * Returns: 1 when the signature could be parsed, 0 otherwise.
408 * Args: ctx: a secp256k1 context object
409 * Out: sig: a pointer to a signature object
410 * In: input: a pointer to the signature to be parsed
411 * inputlen: the length of the array pointed to be input
413 * This function will accept any valid DER encoded signature, even if the
414 * encoded numbers are out of range.
416 * After the call, sig will always be initialized. If parsing failed or the
417 * encoded numbers are out of range, signature validation with it is
418 * guaranteed to fail for every message and public key.
420 SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
421 const secp256k1_context* ctx,
422 secp256k1_ecdsa_signature* sig,
423 const unsigned char *input,
425 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
427 /** Serialize an ECDSA signature in DER format.
429 * Returns: 1 if enough space was available to serialize, 0 otherwise
430 * Args: ctx: a secp256k1 context object
431 * Out: output: a pointer to an array to store the DER serialization
432 * In/Out: outputlen: a pointer to a length integer. Initially, this integer
433 * should be set to the length of output. After the call
434 * it will be set to the length of the serialization (even
435 * if 0 was returned).
436 * In: sig: a pointer to an initialized signature object
438 SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
439 const secp256k1_context* ctx,
440 unsigned char *output,
442 const secp256k1_ecdsa_signature* sig
443 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
445 /** Serialize an ECDSA signature in compact (64 byte) format.
448 * Args: ctx: a secp256k1 context object
449 * Out: output64: a pointer to a 64-byte array to store the compact serialization
450 * In: sig: a pointer to an initialized signature object
452 * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
454 SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
455 const secp256k1_context* ctx,
456 unsigned char *output64,
457 const secp256k1_ecdsa_signature* sig
458 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
460 /** Verify an ECDSA signature.
462 * Returns: 1: correct signature
463 * 0: incorrect or unparseable signature
464 * Args: ctx: a secp256k1 context object, initialized for verification.
465 * In: sig: the signature being verified (cannot be NULL)
466 * msghash32: the 32-byte message hash being verified (cannot be NULL).
467 * The verifier must make sure to apply a cryptographic
468 * hash function to the message by itself and not accept an
469 * msghash32 value directly. Otherwise, it would be easy to
470 * create a "valid" signature without knowledge of the
471 * secret key. See also
472 * https://bitcoin.stackexchange.com/a/81116/35586 for more
473 * background on this topic.
474 * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
476 * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
479 * If you need to accept ECDSA signatures from sources that do not obey this
480 * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
481 * validation, but be aware that doing so results in malleable signatures.
483 * For details, see the comments for that function.
485 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
486 const secp256k1_context* ctx,
487 const secp256k1_ecdsa_signature *sig,
488 const unsigned char *msghash32,
489 const secp256k1_pubkey *pubkey
490 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
492 /** Convert a signature to a normalized lower-S form.
494 * Returns: 1 if sigin was not normalized, 0 if it already was.
495 * Args: ctx: a secp256k1 context object
496 * Out: sigout: a pointer to a signature to fill with the normalized form,
497 * or copy if the input was already normalized. (can be NULL if
498 * you're only interested in whether the input was already
500 * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
501 * can be identical to sigout)
503 * With ECDSA a third-party can forge a second distinct signature of the same
504 * message, given a single initial signature, but without knowing the key. This
505 * is done by negating the S value modulo the order of the curve, 'flipping'
506 * the sign of the random point R which is not included in the signature.
508 * Forgery of the same message isn't universally problematic, but in systems
509 * where message malleability or uniqueness of signatures is important this can
510 * cause issues. This forgery can be blocked by all verifiers forcing signers
511 * to use a normalized form.
513 * The lower-S form reduces the size of signatures slightly on average when
514 * variable length encodings (such as DER) are used and is cheap to verify,
515 * making it a good choice. Security of always using lower-S is assured because
516 * anyone can trivially modify a signature after the fact to enforce this
519 * The lower S value is always between 0x1 and
520 * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
523 * No other forms of ECDSA malleability are known and none seem likely, but
524 * there is no formal proof that ECDSA, even with this additional restriction,
525 * is free of other malleability. Commonly used serialization schemes will also
526 * accept various non-unique encodings, so care should be taken when this
527 * property is required for an application.
529 * The secp256k1_ecdsa_sign function will by default create signatures in the
530 * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
531 * signatures come from a system that cannot enforce this property,
532 * secp256k1_ecdsa_signature_normalize must be called before verification.
534 SECP256K1_API int secp256k1_ecdsa_signature_normalize(
535 const secp256k1_context* ctx,
536 secp256k1_ecdsa_signature *sigout,
537 const secp256k1_ecdsa_signature *sigin
538 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
540 /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
541 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
544 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
546 /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
547 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
549 /** Create an ECDSA signature.
551 * Returns: 1: signature created
552 * 0: the nonce generation function failed, or the secret key was invalid.
553 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
554 * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
555 * In: msghash32: the 32-byte message hash being signed (cannot be NULL)
556 * seckey: pointer to a 32-byte secret key (cannot be NULL)
557 * noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
558 * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
560 * The created signature is always in lower-S form. See
561 * secp256k1_ecdsa_signature_normalize for more details.
563 SECP256K1_API int secp256k1_ecdsa_sign(
564 const secp256k1_context* ctx,
565 secp256k1_ecdsa_signature *sig,
566 const unsigned char *msghash32,
567 const unsigned char *seckey,
568 secp256k1_nonce_function noncefp,
570 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
572 /** Verify an ECDSA secret key.
574 * A secret key is valid if it is not 0 and less than the secp256k1 curve order
575 * when interpreted as an integer (most significant byte first). The
576 * probability of choosing a 32-byte string uniformly at random which is an
577 * invalid secret key is negligible.
579 * Returns: 1: secret key is valid
580 * 0: secret key is invalid
581 * Args: ctx: pointer to a context object (cannot be NULL)
582 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
584 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
585 const secp256k1_context* ctx,
586 const unsigned char *seckey
587 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
589 /** Compute the public key for a secret key.
591 * Returns: 1: secret was valid, public key stores
592 * 0: secret was invalid, try again
593 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
594 * Out: pubkey: pointer to the created public key (cannot be NULL)
595 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
597 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
598 const secp256k1_context* ctx,
599 secp256k1_pubkey *pubkey,
600 const unsigned char *seckey
601 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
603 /** Negates a secret key in place.
605 * Returns: 0 if the given secret key is invalid according to
606 * secp256k1_ec_seckey_verify. 1 otherwise
607 * Args: ctx: pointer to a context object
608 * In/Out: seckey: pointer to the 32-byte secret key to be negated. If the
609 * secret key is invalid according to
610 * secp256k1_ec_seckey_verify, this function returns 0 and
611 * seckey will be set to some unspecified value. (cannot be
614 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(
615 const secp256k1_context* ctx,
616 unsigned char *seckey
617 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
619 /** Same as secp256k1_ec_seckey_negate, but DEPRECATED. Will be removed in
620 * future versions. */
621 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
622 const secp256k1_context* ctx,
623 unsigned char *seckey
624 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
626 /** Negates a public key in place.
629 * Args: ctx: pointer to a context object
630 * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
632 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
633 const secp256k1_context* ctx,
634 secp256k1_pubkey *pubkey
635 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
637 /** Tweak a secret key by adding tweak to it.
639 * Returns: 0 if the arguments are invalid or the resulting secret key would be
640 * invalid (only when the tweak is the negation of the secret key). 1
642 * Args: ctx: pointer to a context object (cannot be NULL).
643 * In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
644 * invalid according to secp256k1_ec_seckey_verify, this
645 * function returns 0. seckey will be set to some unspecified
646 * value if this function returns 0. (cannot be NULL)
647 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
648 * secp256k1_ec_seckey_verify, this function returns 0. For
649 * uniformly random 32-byte arrays the chance of being invalid
650 * is negligible (around 1 in 2^128) (cannot be NULL).
652 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(
653 const secp256k1_context* ctx,
654 unsigned char *seckey,
655 const unsigned char *tweak32
656 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
658 /** Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED. Will be removed in
659 * future versions. */
660 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
661 const secp256k1_context* ctx,
662 unsigned char *seckey,
663 const unsigned char *tweak32
664 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
666 /** Tweak a public key by adding tweak times the generator to it.
668 * Returns: 0 if the arguments are invalid or the resulting public key would be
669 * invalid (only when the tweak is the negation of the corresponding
670 * secret key). 1 otherwise.
671 * Args: ctx: pointer to a context object initialized for validation
673 * In/Out: pubkey: pointer to a public key object. pubkey will be set to an
674 * invalid value if this function returns 0 (cannot be NULL).
675 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
676 * secp256k1_ec_seckey_verify, this function returns 0. For
677 * uniformly random 32-byte arrays the chance of being invalid
678 * is negligible (around 1 in 2^128) (cannot be NULL).
680 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
681 const secp256k1_context* ctx,
682 secp256k1_pubkey *pubkey,
683 const unsigned char *tweak32
684 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
686 /** Tweak a secret key by multiplying it by a tweak.
688 * Returns: 0 if the arguments are invalid. 1 otherwise.
689 * Args: ctx: pointer to a context object (cannot be NULL).
690 * In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
691 * invalid according to secp256k1_ec_seckey_verify, this
692 * function returns 0. seckey will be set to some unspecified
693 * value if this function returns 0. (cannot be NULL)
694 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
695 * secp256k1_ec_seckey_verify, this function returns 0. For
696 * uniformly random 32-byte arrays the chance of being invalid
697 * is negligible (around 1 in 2^128) (cannot be NULL).
699 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(
700 const secp256k1_context* ctx,
701 unsigned char *seckey,
702 const unsigned char *tweak32
703 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
705 /** Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED. Will be removed in
706 * future versions. */
707 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
708 const secp256k1_context* ctx,
709 unsigned char *seckey,
710 const unsigned char *tweak32
711 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
713 /** Tweak a public key by multiplying it by a tweak value.
715 * Returns: 0 if the arguments are invalid. 1 otherwise.
716 * Args: ctx: pointer to a context object initialized for validation
718 * In/Out: pubkey: pointer to a public key object. pubkey will be set to an
719 * invalid value if this function returns 0 (cannot be NULL).
720 * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
721 * secp256k1_ec_seckey_verify, this function returns 0. For
722 * uniformly random 32-byte arrays the chance of being invalid
723 * is negligible (around 1 in 2^128) (cannot be NULL).
725 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
726 const secp256k1_context* ctx,
727 secp256k1_pubkey *pubkey,
728 const unsigned char *tweak32
729 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
731 /** Updates the context randomization to protect against side-channel leakage.
732 * Returns: 1: randomization successfully updated or nothing to randomize
734 * Args: ctx: pointer to a context object (cannot be NULL)
735 * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
737 * While secp256k1 code is written to be constant-time no matter what secret
738 * values are, it's possible that a future compiler may output code which isn't,
739 * and also that the CPU may not emit the same radio frequencies or draw the same
740 * amount power for all values.
742 * This function provides a seed which is combined into the blinding value: that
743 * blinding value is added before each multiplication (and removed afterwards) so
744 * that it does not affect function results, but shields against attacks which
745 * rely on any input-dependent behaviour.
747 * This function has currently an effect only on contexts initialized for signing
748 * because randomization is currently used only for signing. However, this is not
749 * guaranteed and may change in the future. It is safe to call this function on
750 * contexts not initialized for signing; then it will have no effect and return 1.
752 * You should call this after secp256k1_context_create or
753 * secp256k1_context_clone (and secp256k1_context_preallocated_create or
754 * secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
756 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
757 secp256k1_context* ctx,
758 const unsigned char *seed32
759 ) SECP256K1_ARG_NONNULL(1);
761 /** Add a number of public keys together.
763 * Returns: 1: the sum of the public keys is valid.
764 * 0: the sum of the public keys is not valid.
765 * Args: ctx: pointer to a context object
766 * Out: out: pointer to a public key object for placing the resulting public key
768 * In: ins: pointer to array of pointers to public keys (cannot be NULL)
769 * n: the number of public keys to add together (must be at least 1)
771 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
772 const secp256k1_context* ctx,
773 secp256k1_pubkey *out,
774 const secp256k1_pubkey * const * ins,
776 ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
782 #endif /* SECP256K1_H */