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 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 #ifndef SECP256K1_API
132 # ifdef SECP256K1_BUILD
133 # define SECP256K1_API __declspec(dllexport)
135 # define SECP256K1_API
137 # elif defined(__GNUC__) && defined(SECP256K1_BUILD)
138 # define SECP256K1_API __attribute__ ((visibility ("default")))
140 # define SECP256K1_API
144 /**Warning attributes
145 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
146 * some paranoid null checks. */
147 # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
148 # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
150 # define SECP256K1_WARN_UNUSED_RESULT
152 # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
153 # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
155 # define SECP256K1_ARG_NONNULL(_x)
158 /** All flags' lower 8 bits indicate what they're for. Do not use directly. */
159 #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
160 #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
161 #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
162 /** The higher bits contain the actual data. Do not use directly. */
163 #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
164 #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
165 #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
167 /** Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
168 * secp256k1_context_preallocated_create. */
169 #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
170 #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
171 #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
173 /** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */
174 #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
175 #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
177 /** Prefix byte used to tag various encoded curvepoints for specific purposes */
178 #define SECP256K1_TAG_PUBKEY_EVEN 0x02
179 #define SECP256K1_TAG_PUBKEY_ODD 0x03
180 #define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
181 #define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
182 #define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
184 /** A simple secp256k1 context object with no precomputed tables. These are useful for
185 * type serialization/parsing functions which require a context object to maintain
186 * API consistency, but currently do not require expensive precomputations or dynamic
189 SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
191 /** Create a secp256k1 context object (in dynamically allocated memory).
193 * This function uses malloc to allocate memory. It is guaranteed that malloc is
194 * called at most once for every call of this function. If you need to avoid dynamic
195 * memory allocation entirely, see the functions in secp256k1_preallocated.h.
197 * Returns: a newly created context object.
198 * In: flags: which parts of the context to initialize.
200 * See also secp256k1_context_randomize.
202 SECP256K1_API secp256k1_context* secp256k1_context_create(
204 ) SECP256K1_WARN_UNUSED_RESULT;
206 /** Copy a secp256k1 context object (into dynamically allocated memory).
208 * This function uses malloc to allocate memory. It is guaranteed that malloc is
209 * called at most once for every call of this function. If you need to avoid dynamic
210 * memory allocation entirely, see the functions in secp256k1_preallocated.h.
212 * Returns: a newly created context object.
213 * Args: ctx: an existing context to copy (cannot be NULL)
215 SECP256K1_API secp256k1_context* secp256k1_context_clone(
216 const secp256k1_context* ctx
217 ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
219 /** Destroy a secp256k1 context object (created in dynamically allocated memory).
221 * The context pointer may not be used afterwards.
223 * The context to destroy must have been created using secp256k1_context_create
224 * or secp256k1_context_clone. If the context has instead been created using
225 * secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
226 * behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
229 * Args: ctx: an existing context to destroy, constructed using
230 * secp256k1_context_create or secp256k1_context_clone
232 SECP256K1_API void secp256k1_context_destroy(
233 secp256k1_context* ctx
236 /** Set a callback function to be called when an illegal argument is passed to
237 * an API call. It will only trigger for violations that are mentioned
238 * explicitly in the header.
240 * The philosophy is that these shouldn't be dealt with through a
241 * specific return value, as calling code should not have branches to deal with
242 * the case that this code itself is broken.
244 * On the other hand, during debug stage, one would want to be informed about
245 * such mistakes, and the default (crashing) may be inadvisable.
246 * When this callback is triggered, the API function called is guaranteed not
247 * to cause a crash, though its return value and output arguments are
250 * When this function has not been called (or called with fn==NULL), then the
251 * default handler will be used. The library provides a default handler which
252 * writes the message to stderr and calls abort. This default handler can be
253 * replaced at link time if the preprocessor macro
254 * USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build
255 * has been configured with --enable-external-default-callbacks. Then the
256 * following two symbols must be provided to link against:
257 * - void secp256k1_default_illegal_callback_fn(const char* message, void* data);
258 * - void secp256k1_default_error_callback_fn(const char* message, void* data);
259 * The library can call these default handlers even before a proper callback data
260 * pointer could have been set using secp256k1_context_set_illegal_callback or
261 * secp256k1_context_set_illegal_callback, e.g., when the creation of a context
262 * fails. In this case, the corresponding default handler will be called with
263 * the data pointer argument set to NULL.
265 * Args: ctx: an existing context object (cannot be NULL)
266 * In: fun: a pointer to a function to call when an illegal argument is
267 * passed to the API, taking a message and an opaque pointer.
268 * (NULL restores the default handler.)
269 * data: the opaque pointer to pass to fun above.
271 * See also secp256k1_context_set_error_callback.
273 SECP256K1_API void secp256k1_context_set_illegal_callback(
274 secp256k1_context* ctx,
275 void (*fun)(const char* message, void* data),
277 ) SECP256K1_ARG_NONNULL(1);
279 /** Set a callback function to be called when an internal consistency check
280 * fails. The default is crashing.
282 * This can only trigger in case of a hardware failure, miscompilation,
283 * memory corruption, serious bug in the library, or other error would can
284 * otherwise result in undefined behaviour. It will not trigger due to mere
285 * incorrect usage of the API (see secp256k1_context_set_illegal_callback
286 * for that). After this callback returns, anything may happen, including
289 * Args: ctx: an existing context object (cannot be NULL)
290 * In: fun: a pointer to a function to call when an internal error occurs,
291 * taking a message and an opaque pointer (NULL restores the
292 * default handler, see secp256k1_context_set_illegal_callback
294 * data: the opaque pointer to pass to fun above.
296 * See also secp256k1_context_set_illegal_callback.
298 SECP256K1_API void secp256k1_context_set_error_callback(
299 secp256k1_context* ctx,
300 void (*fun)(const char* message, void* data),
302 ) SECP256K1_ARG_NONNULL(1);
304 /** Create a secp256k1 scratch space object.
306 * Returns: a newly created scratch space.
307 * Args: ctx: an existing context object (cannot be NULL)
308 * In: size: amount of memory to be available as scratch space. Some extra
309 * (<100 bytes) will be allocated for extra accounting.
311 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
312 const secp256k1_context* ctx,
314 ) SECP256K1_ARG_NONNULL(1);
316 /** Destroy a secp256k1 scratch space.
318 * The pointer may not be used afterwards.
319 * Args: ctx: a secp256k1 context object.
320 * scratch: space to destroy
322 SECP256K1_API void secp256k1_scratch_space_destroy(
323 const secp256k1_context* ctx,
324 secp256k1_scratch_space* scratch
325 ) SECP256K1_ARG_NONNULL(1);
327 /** Parse a variable-length public key into the pubkey object.
329 * Returns: 1 if the public key was fully valid.
330 * 0 if the public key could not be parsed or is invalid.
331 * Args: ctx: a secp256k1 context object.
332 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
333 * parsed version of input. If not, its value is undefined.
334 * In: input: pointer to a serialized public key
335 * inputlen: length of the array pointed to by input
337 * This function supports parsing compressed (33 bytes, header byte 0x02 or
338 * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
339 * byte 0x06 or 0x07) format public keys.
341 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
342 const secp256k1_context* ctx,
343 secp256k1_pubkey* pubkey,
344 const unsigned char *input,
346 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
348 /** Serialize a pubkey object into a serialized byte sequence.
351 * Args: ctx: a secp256k1 context object.
352 * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
353 * compressed==1) byte array to place the serialized key
355 * In/Out: outputlen: a pointer to an integer which is initially set to the
356 * size of output, and is overwritten with the written
358 * In: pubkey: a pointer to a secp256k1_pubkey containing an
359 * initialized public key.
360 * flags: SECP256K1_EC_COMPRESSED if serialization should be in
361 * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
363 SECP256K1_API int secp256k1_ec_pubkey_serialize(
364 const secp256k1_context* ctx,
365 unsigned char *output,
367 const secp256k1_pubkey* pubkey,
369 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
371 /** Parse an ECDSA signature in compact (64 bytes) format.
373 * Returns: 1 when the signature could be parsed, 0 otherwise.
374 * Args: ctx: a secp256k1 context object
375 * Out: sig: a pointer to a signature object
376 * In: input64: a pointer to the 64-byte array to parse
378 * The signature must consist of a 32-byte big endian R value, followed by a
379 * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
380 * encoding is invalid. R and S with value 0 are allowed in the encoding.
382 * After the call, sig will always be initialized. If parsing failed or R or
383 * S are zero, the resulting sig value is guaranteed to fail validation for any
384 * message and public key.
386 SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
387 const secp256k1_context* ctx,
388 secp256k1_ecdsa_signature* sig,
389 const unsigned char *input64
390 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
392 /** Parse a DER ECDSA signature.
394 * Returns: 1 when the signature could be parsed, 0 otherwise.
395 * Args: ctx: a secp256k1 context object
396 * Out: sig: a pointer to a signature object
397 * In: input: a pointer to the signature to be parsed
398 * inputlen: the length of the array pointed to be input
400 * This function will accept any valid DER encoded signature, even if the
401 * encoded numbers are out of range.
403 * After the call, sig will always be initialized. If parsing failed or the
404 * encoded numbers are out of range, signature validation with it is
405 * guaranteed to fail for every message and public key.
407 SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
408 const secp256k1_context* ctx,
409 secp256k1_ecdsa_signature* sig,
410 const unsigned char *input,
412 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
414 /** Serialize an ECDSA signature in DER format.
416 * Returns: 1 if enough space was available to serialize, 0 otherwise
417 * Args: ctx: a secp256k1 context object
418 * Out: output: a pointer to an array to store the DER serialization
419 * In/Out: outputlen: a pointer to a length integer. Initially, this integer
420 * should be set to the length of output. After the call
421 * it will be set to the length of the serialization (even
422 * if 0 was returned).
423 * In: sig: a pointer to an initialized signature object
425 SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
426 const secp256k1_context* ctx,
427 unsigned char *output,
429 const secp256k1_ecdsa_signature* sig
430 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
432 /** Serialize an ECDSA signature in compact (64 byte) format.
435 * Args: ctx: a secp256k1 context object
436 * Out: output64: a pointer to a 64-byte array to store the compact serialization
437 * In: sig: a pointer to an initialized signature object
439 * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
441 SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
442 const secp256k1_context* ctx,
443 unsigned char *output64,
444 const secp256k1_ecdsa_signature* sig
445 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
447 /** Verify an ECDSA signature.
449 * Returns: 1: correct signature
450 * 0: incorrect or unparseable signature
451 * Args: ctx: a secp256k1 context object, initialized for verification.
452 * In: sig: the signature being verified (cannot be NULL)
453 * msg32: the 32-byte message hash being verified (cannot be NULL)
454 * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
456 * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
459 * If you need to accept ECDSA signatures from sources that do not obey this
460 * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
461 * validation, but be aware that doing so results in malleable signatures.
463 * For details, see the comments for that function.
465 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
466 const secp256k1_context* ctx,
467 const secp256k1_ecdsa_signature *sig,
468 const unsigned char *msg32,
469 const secp256k1_pubkey *pubkey
470 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
472 /** Convert a signature to a normalized lower-S form.
474 * Returns: 1 if sigin was not normalized, 0 if it already was.
475 * Args: ctx: a secp256k1 context object
476 * Out: sigout: a pointer to a signature to fill with the normalized form,
477 * or copy if the input was already normalized. (can be NULL if
478 * you're only interested in whether the input was already
480 * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
481 * can be identical to sigout)
483 * With ECDSA a third-party can forge a second distinct signature of the same
484 * message, given a single initial signature, but without knowing the key. This
485 * is done by negating the S value modulo the order of the curve, 'flipping'
486 * the sign of the random point R which is not included in the signature.
488 * Forgery of the same message isn't universally problematic, but in systems
489 * where message malleability or uniqueness of signatures is important this can
490 * cause issues. This forgery can be blocked by all verifiers forcing signers
491 * to use a normalized form.
493 * The lower-S form reduces the size of signatures slightly on average when
494 * variable length encodings (such as DER) are used and is cheap to verify,
495 * making it a good choice. Security of always using lower-S is assured because
496 * anyone can trivially modify a signature after the fact to enforce this
499 * The lower S value is always between 0x1 and
500 * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
503 * No other forms of ECDSA malleability are known and none seem likely, but
504 * there is no formal proof that ECDSA, even with this additional restriction,
505 * is free of other malleability. Commonly used serialization schemes will also
506 * accept various non-unique encodings, so care should be taken when this
507 * property is required for an application.
509 * The secp256k1_ecdsa_sign function will by default create signatures in the
510 * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
511 * signatures come from a system that cannot enforce this property,
512 * secp256k1_ecdsa_signature_normalize must be called before verification.
514 SECP256K1_API int secp256k1_ecdsa_signature_normalize(
515 const secp256k1_context* ctx,
516 secp256k1_ecdsa_signature *sigout,
517 const secp256k1_ecdsa_signature *sigin
518 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
520 /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
521 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
524 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
526 /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
527 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
529 /** Create an ECDSA signature.
531 * Returns: 1: signature created
532 * 0: the nonce generation function failed, or the private key was invalid.
533 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
534 * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
535 * In: msg32: the 32-byte message hash being signed (cannot be NULL)
536 * seckey: pointer to a 32-byte secret key (cannot be NULL)
537 * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
538 * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
540 * The created signature is always in lower-S form. See
541 * secp256k1_ecdsa_signature_normalize for more details.
543 SECP256K1_API int secp256k1_ecdsa_sign(
544 const secp256k1_context* ctx,
545 secp256k1_ecdsa_signature *sig,
546 const unsigned char *msg32,
547 const unsigned char *seckey,
548 secp256k1_nonce_function noncefp,
550 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
552 /** Verify an ECDSA secret key.
554 * Returns: 1: secret key is valid
555 * 0: secret key is invalid
556 * Args: ctx: pointer to a context object (cannot be NULL)
557 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
559 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
560 const secp256k1_context* ctx,
561 const unsigned char *seckey
562 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
564 /** Compute the public key for a secret key.
566 * Returns: 1: secret was valid, public key stores
567 * 0: secret was invalid, try again
568 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
569 * Out: pubkey: pointer to the created public key (cannot be NULL)
570 * In: seckey: pointer to a 32-byte private key (cannot be NULL)
572 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
573 const secp256k1_context* ctx,
574 secp256k1_pubkey *pubkey,
575 const unsigned char *seckey
576 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
578 /** Negates a private key in place.
581 * Args: ctx: pointer to a context object
582 * In/Out: seckey: pointer to the 32-byte private key to be negated (cannot be NULL)
584 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
585 const secp256k1_context* ctx,
586 unsigned char *seckey
587 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
589 /** Negates a public key in place.
592 * Args: ctx: pointer to a context object
593 * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
595 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
596 const secp256k1_context* ctx,
597 secp256k1_pubkey *pubkey
598 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
600 /** Tweak a private key by adding tweak to it.
601 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
602 * uniformly random 32-byte arrays, or if the resulting private key
603 * would be invalid (only when the tweak is the complement of the
604 * private key). 1 otherwise.
605 * Args: ctx: pointer to a context object (cannot be NULL).
606 * In/Out: seckey: pointer to a 32-byte private key.
607 * In: tweak: pointer to a 32-byte tweak.
609 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
610 const secp256k1_context* ctx,
611 unsigned char *seckey,
612 const unsigned char *tweak
613 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
615 /** Tweak a public key by adding tweak times the generator to it.
616 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
617 * uniformly random 32-byte arrays, or if the resulting public key
618 * would be invalid (only when the tweak is the complement of the
619 * corresponding private key). 1 otherwise.
620 * Args: ctx: pointer to a context object initialized for validation
622 * In/Out: pubkey: pointer to a public key object.
623 * In: tweak: pointer to a 32-byte tweak.
625 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
626 const secp256k1_context* ctx,
627 secp256k1_pubkey *pubkey,
628 const unsigned char *tweak
629 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
631 /** Tweak a private key by multiplying it by a tweak.
632 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
633 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
634 * Args: ctx: pointer to a context object (cannot be NULL).
635 * In/Out: seckey: pointer to a 32-byte private key.
636 * In: tweak: pointer to a 32-byte tweak.
638 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
639 const secp256k1_context* ctx,
640 unsigned char *seckey,
641 const unsigned char *tweak
642 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
644 /** Tweak a public key by multiplying it by a tweak value.
645 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
646 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
647 * Args: ctx: pointer to a context object initialized for validation
649 * In/Out: pubkey: pointer to a public key obkect.
650 * In: tweak: pointer to a 32-byte tweak.
652 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
653 const secp256k1_context* ctx,
654 secp256k1_pubkey *pubkey,
655 const unsigned char *tweak
656 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
658 /** Updates the context randomization to protect against side-channel leakage.
659 * Returns: 1: randomization successfully updated or nothing to randomize
661 * Args: ctx: pointer to a context object (cannot be NULL)
662 * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
664 * While secp256k1 code is written to be constant-time no matter what secret
665 * values are, it's possible that a future compiler may output code which isn't,
666 * and also that the CPU may not emit the same radio frequencies or draw the same
667 * amount power for all values.
669 * This function provides a seed which is combined into the blinding value: that
670 * blinding value is added before each multiplication (and removed afterwards) so
671 * that it does not affect function results, but shields against attacks which
672 * rely on any input-dependent behaviour.
674 * This function has currently an effect only on contexts initialized for signing
675 * because randomization is currently used only for signing. However, this is not
676 * guaranteed and may change in the future. It is safe to call this function on
677 * contexts not initialized for signing; then it will have no effect and return 1.
679 * You should call this after secp256k1_context_create or
680 * secp256k1_context_clone (and secp256k1_context_preallocated_create or
681 * secp256k1_context_clone, resp.), and you may call this repeatedly afterwards.
683 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
684 secp256k1_context* ctx,
685 const unsigned char *seed32
686 ) SECP256K1_ARG_NONNULL(1);
688 /** Add a number of public keys together.
689 * Returns: 1: the sum of the public keys is valid.
690 * 0: the sum of the public keys is not valid.
691 * Args: ctx: pointer to a context object
692 * Out: out: pointer to a public key object for placing the resulting public key
694 * In: ins: pointer to array of pointers to public keys (cannot be NULL)
695 * n: the number of public keys to add together (must be at least 1)
697 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
698 const secp256k1_context* ctx,
699 secp256k1_pubkey *out,
700 const secp256k1_pubkey * const * ins,
702 ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
708 #endif /* SECP256K1_H */