10 /* These rules specify the order of arguments in API calls:
12 * 1. Context pointers go first, followed by output arguments, combined
13 * output/input arguments, and finally input-only arguments.
14 * 2. Array lengths always immediately the follow the argument whose length
15 * they describe, even if this violates rule 1.
16 * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated
17 * later go first. This means: signatures, public nonces, private nonces,
18 * messages, public keys, secret keys, tweaks.
19 * 4. Arguments that are not data pointers go last, from more complex to less
20 * complex: function pointers, algorithm names, messages, void pointers,
21 * counts, flags, booleans.
22 * 5. Opaque data pointers follow the function pointer they are to be passed to.
25 /** Opaque data structure that holds context information (precomputed tables etc.).
27 * The purpose of context structures is to cache large precomputed data tables
28 * that are expensive to construct, and also to maintain the randomization data
31 * Do not create a new context object for each operation, as construction is
32 * far slower than all other API calls (~100 times slower than an ECDSA
35 * A constructed context can safely be used from multiple threads
36 * simultaneously, but API call that take a non-const pointer to a context
37 * need exclusive access to it. In particular this is the case for
38 * secp256k1_context_destroy and secp256k1_context_randomize.
40 * Regarding randomization, either do it once at creation time (in which case
41 * you do not need any locking for the other calls), or use a read-write lock.
43 typedef struct secp256k1_context_struct secp256k1_context;
45 /** Opaque data structure that holds rewriteable "scratch space"
47 * The purpose of this structure is to replace dynamic memory allocations,
48 * because we target architectures where this may not be available. It is
49 * essentially a resizable (within specified parameters) block of bytes,
50 * which is initially created either by memory allocation or TODO as a pointer
51 * into some fixed rewritable space.
53 * Unlike the context object, this cannot safely be shared between threads
54 * without additional synchronization logic.
56 typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
58 /** Opaque data structure that holds a parsed and valid public key.
60 * The exact representation of data inside is implementation defined and not
61 * guaranteed to be portable between different platforms or versions. It is
62 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
63 * If you need to convert to a format suitable for storage, transmission, or
64 * comparison, use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse.
67 unsigned char data[64];
70 /** Opaque data structured that holds a parsed ECDSA signature.
72 * The exact representation of data inside is implementation defined and not
73 * guaranteed to be portable between different platforms or versions. It is
74 * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
75 * If you need to convert to a format suitable for storage, transmission, or
76 * comparison, use the secp256k1_ecdsa_signature_serialize_* and
77 * secp256k1_ecdsa_signature_parse_* functions.
80 unsigned char data[64];
81 } secp256k1_ecdsa_signature;
83 /** A pointer to a function to deterministically generate a nonce.
85 * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
86 * Out: nonce32: pointer to a 32-byte array to be filled by the function.
87 * In: msg32: the 32-byte message hash being verified (will not be NULL)
88 * key32: pointer to a 32-byte secret key (will not be NULL)
89 * algo16: pointer to a 16-byte array describing the signature
90 * algorithm (will be NULL for ECDSA for compatibility).
91 * data: Arbitrary data pointer that is passed through.
92 * attempt: how many iterations we have tried to find a nonce.
93 * This will almost always be 0, but different attempt values
94 * are required to result in a different nonce.
96 * Except for test cases, this function should compute some cryptographic hash of
97 * the message, the algorithm, the key and the attempt.
99 typedef int (*secp256k1_nonce_function)(
100 unsigned char *nonce32,
101 const unsigned char *msg32,
102 const unsigned char *key32,
103 const unsigned char *algo16,
108 # if !defined(SECP256K1_GNUC_PREREQ)
109 # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
110 # define SECP256K1_GNUC_PREREQ(_maj,_min) \
111 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
113 # define SECP256K1_GNUC_PREREQ(_maj,_min) 0
117 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
118 # if SECP256K1_GNUC_PREREQ(2,7)
119 # define SECP256K1_INLINE __inline__
120 # elif (defined(_MSC_VER))
121 # define SECP256K1_INLINE __inline
123 # define SECP256K1_INLINE
126 # define SECP256K1_INLINE inline
129 #ifndef SECP256K1_API
131 # ifdef SECP256K1_BUILD
132 # define SECP256K1_API __declspec(dllexport)
134 # define SECP256K1_API
136 # elif defined(__GNUC__) && defined(SECP256K1_BUILD)
137 # define SECP256K1_API __attribute__ ((visibility ("default")))
139 # define SECP256K1_API
143 /**Warning attributes
144 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
145 * some paranoid null checks. */
146 # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
147 # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
149 # define SECP256K1_WARN_UNUSED_RESULT
151 # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
152 # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
154 # define SECP256K1_ARG_NONNULL(_x)
157 /** All flags' lower 8 bits indicate what they're for. Do not use directly. */
158 #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
159 #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
160 #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
161 /** The higher bits contain the actual data. Do not use directly. */
162 #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
163 #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
164 #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
166 /** Flags to pass to secp256k1_context_create. */
167 #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
168 #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
169 #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
171 /** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */
172 #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
173 #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
175 /** Prefix byte used to tag various encoded curvepoints for specific purposes */
176 #define SECP256K1_TAG_PUBKEY_EVEN 0x02
177 #define SECP256K1_TAG_PUBKEY_ODD 0x03
178 #define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
179 #define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
180 #define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
182 /** Create a secp256k1 context object.
184 * Returns: a newly created context object.
185 * In: flags: which parts of the context to initialize.
187 * See also secp256k1_context_randomize.
189 SECP256K1_API secp256k1_context* secp256k1_context_create(
191 ) SECP256K1_WARN_UNUSED_RESULT;
193 /** Copies a secp256k1 context object.
195 * Returns: a newly created context object.
196 * Args: ctx: an existing context to copy (cannot be NULL)
198 SECP256K1_API secp256k1_context* secp256k1_context_clone(
199 const secp256k1_context* ctx
200 ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
202 /** Destroy a secp256k1 context object.
204 * The context pointer may not be used afterwards.
205 * Args: ctx: an existing context to destroy (cannot be NULL)
207 SECP256K1_API void secp256k1_context_destroy(
208 secp256k1_context* ctx
211 /** Set a callback function to be called when an illegal argument is passed to
212 * an API call. It will only trigger for violations that are mentioned
213 * explicitly in the header.
215 * The philosophy is that these shouldn't be dealt with through a
216 * specific return value, as calling code should not have branches to deal with
217 * the case that this code itself is broken.
219 * On the other hand, during debug stage, one would want to be informed about
220 * such mistakes, and the default (crashing) may be inadvisable.
221 * When this callback is triggered, the API function called is guaranteed not
222 * to cause a crash, though its return value and output arguments are
225 * Args: ctx: an existing context object (cannot be NULL)
226 * In: fun: a pointer to a function to call when an illegal argument is
227 * passed to the API, taking a message and an opaque pointer
228 * (NULL restores a default handler that calls abort).
229 * data: the opaque pointer to pass to fun above.
231 SECP256K1_API void secp256k1_context_set_illegal_callback(
232 secp256k1_context* ctx,
233 void (*fun)(const char* message, void* data),
235 ) SECP256K1_ARG_NONNULL(1);
237 /** Set a callback function to be called when an internal consistency check
238 * fails. The default is crashing.
240 * This can only trigger in case of a hardware failure, miscompilation,
241 * memory corruption, serious bug in the library, or other error would can
242 * otherwise result in undefined behaviour. It will not trigger due to mere
243 * incorrect usage of the API (see secp256k1_context_set_illegal_callback
244 * for that). After this callback returns, anything may happen, including
247 * Args: ctx: an existing context object (cannot be NULL)
248 * In: fun: a pointer to a function to call when an internal error occurs,
249 * taking a message and an opaque pointer (NULL restores a default
250 * handler that calls abort).
251 * data: the opaque pointer to pass to fun above.
253 SECP256K1_API void secp256k1_context_set_error_callback(
254 secp256k1_context* ctx,
255 void (*fun)(const char* message, void* data),
257 ) SECP256K1_ARG_NONNULL(1);
259 /** Create a secp256k1 scratch space object.
261 * Returns: a newly created scratch space.
262 * Args: ctx: an existing context object (cannot be NULL)
263 * In: init_size: initial amount of memory to allocate
264 * max_size: maximum amount of memory to allocate
266 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
267 const secp256k1_context* ctx,
270 ) SECP256K1_ARG_NONNULL(1);
272 /** Destroy a secp256k1 scratch space.
274 * The pointer may not be used afterwards.
275 * Args: scratch: space to destroy
277 SECP256K1_API void secp256k1_scratch_space_destroy(
278 secp256k1_scratch_space* scratch
281 /** Parse a variable-length public key into the pubkey object.
283 * Returns: 1 if the public key was fully valid.
284 * 0 if the public key could not be parsed or is invalid.
285 * Args: ctx: a secp256k1 context object.
286 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
287 * parsed version of input. If not, its value is undefined.
288 * In: input: pointer to a serialized public key
289 * inputlen: length of the array pointed to by input
291 * This function supports parsing compressed (33 bytes, header byte 0x02 or
292 * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
293 * byte 0x06 or 0x07) format public keys.
295 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
296 const secp256k1_context* ctx,
297 secp256k1_pubkey* pubkey,
298 const unsigned char *input,
300 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
302 /** Serialize a pubkey object into a serialized byte sequence.
305 * Args: ctx: a secp256k1 context object.
306 * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
307 * compressed==1) byte array to place the serialized key
309 * In/Out: outputlen: a pointer to an integer which is initially set to the
310 * size of output, and is overwritten with the written
312 * In: pubkey: a pointer to a secp256k1_pubkey containing an
313 * initialized public key.
314 * flags: SECP256K1_EC_COMPRESSED if serialization should be in
315 * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
317 SECP256K1_API int secp256k1_ec_pubkey_serialize(
318 const secp256k1_context* ctx,
319 unsigned char *output,
321 const secp256k1_pubkey* pubkey,
323 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
325 /** Parse an ECDSA signature in compact (64 bytes) format.
327 * Returns: 1 when the signature could be parsed, 0 otherwise.
328 * Args: ctx: a secp256k1 context object
329 * Out: sig: a pointer to a signature object
330 * In: input64: a pointer to the 64-byte array to parse
332 * The signature must consist of a 32-byte big endian R value, followed by a
333 * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
334 * encoding is invalid. R and S with value 0 are allowed in the encoding.
336 * After the call, sig will always be initialized. If parsing failed or R or
337 * S are zero, the resulting sig value is guaranteed to fail validation for any
338 * message and public key.
340 SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
341 const secp256k1_context* ctx,
342 secp256k1_ecdsa_signature* sig,
343 const unsigned char *input64
344 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
346 /** Parse a DER ECDSA signature.
348 * Returns: 1 when the signature could be parsed, 0 otherwise.
349 * Args: ctx: a secp256k1 context object
350 * Out: sig: a pointer to a signature object
351 * In: input: a pointer to the signature to be parsed
352 * inputlen: the length of the array pointed to be input
354 * This function will accept any valid DER encoded signature, even if the
355 * encoded numbers are out of range.
357 * After the call, sig will always be initialized. If parsing failed or the
358 * encoded numbers are out of range, signature validation with it is
359 * guaranteed to fail for every message and public key.
361 SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
362 const secp256k1_context* ctx,
363 secp256k1_ecdsa_signature* sig,
364 const unsigned char *input,
366 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
368 /** Serialize an ECDSA signature in DER format.
370 * Returns: 1 if enough space was available to serialize, 0 otherwise
371 * Args: ctx: a secp256k1 context object
372 * Out: output: a pointer to an array to store the DER serialization
373 * In/Out: outputlen: a pointer to a length integer. Initially, this integer
374 * should be set to the length of output. After the call
375 * it will be set to the length of the serialization (even
376 * if 0 was returned).
377 * In: sig: a pointer to an initialized signature object
379 SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
380 const secp256k1_context* ctx,
381 unsigned char *output,
383 const secp256k1_ecdsa_signature* sig
384 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
386 /** Serialize an ECDSA signature in compact (64 byte) format.
389 * Args: ctx: a secp256k1 context object
390 * Out: output64: a pointer to a 64-byte array to store the compact serialization
391 * In: sig: a pointer to an initialized signature object
393 * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
395 SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
396 const secp256k1_context* ctx,
397 unsigned char *output64,
398 const secp256k1_ecdsa_signature* sig
399 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
401 /** Verify an ECDSA signature.
403 * Returns: 1: correct signature
404 * 0: incorrect or unparseable signature
405 * Args: ctx: a secp256k1 context object, initialized for verification.
406 * In: sig: the signature being verified (cannot be NULL)
407 * msg32: the 32-byte message hash being verified (cannot be NULL)
408 * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
410 * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
413 * If you need to accept ECDSA signatures from sources that do not obey this
414 * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
415 * validation, but be aware that doing so results in malleable signatures.
417 * For details, see the comments for that function.
419 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
420 const secp256k1_context* ctx,
421 const secp256k1_ecdsa_signature *sig,
422 const unsigned char *msg32,
423 const secp256k1_pubkey *pubkey
424 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
426 /** Convert a signature to a normalized lower-S form.
428 * Returns: 1 if sigin was not normalized, 0 if it already was.
429 * Args: ctx: a secp256k1 context object
430 * Out: sigout: a pointer to a signature to fill with the normalized form,
431 * or copy if the input was already normalized. (can be NULL if
432 * you're only interested in whether the input was already
434 * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
435 * can be identical to sigout)
437 * With ECDSA a third-party can forge a second distinct signature of the same
438 * message, given a single initial signature, but without knowing the key. This
439 * is done by negating the S value modulo the order of the curve, 'flipping'
440 * the sign of the random point R which is not included in the signature.
442 * Forgery of the same message isn't universally problematic, but in systems
443 * where message malleability or uniqueness of signatures is important this can
444 * cause issues. This forgery can be blocked by all verifiers forcing signers
445 * to use a normalized form.
447 * The lower-S form reduces the size of signatures slightly on average when
448 * variable length encodings (such as DER) are used and is cheap to verify,
449 * making it a good choice. Security of always using lower-S is assured because
450 * anyone can trivially modify a signature after the fact to enforce this
453 * The lower S value is always between 0x1 and
454 * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
457 * No other forms of ECDSA malleability are known and none seem likely, but
458 * there is no formal proof that ECDSA, even with this additional restriction,
459 * is free of other malleability. Commonly used serialization schemes will also
460 * accept various non-unique encodings, so care should be taken when this
461 * property is required for an application.
463 * The secp256k1_ecdsa_sign function will by default create signatures in the
464 * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
465 * signatures come from a system that cannot enforce this property,
466 * secp256k1_ecdsa_signature_normalize must be called before verification.
468 SECP256K1_API int secp256k1_ecdsa_signature_normalize(
469 const secp256k1_context* ctx,
470 secp256k1_ecdsa_signature *sigout,
471 const secp256k1_ecdsa_signature *sigin
472 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
474 /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
475 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
478 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
480 /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
481 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
483 /** Create an ECDSA signature.
485 * Returns: 1: signature created
486 * 0: the nonce generation function failed, or the private key was invalid.
487 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
488 * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
489 * In: msg32: the 32-byte message hash being signed (cannot be NULL)
490 * seckey: pointer to a 32-byte secret key (cannot be NULL)
491 * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
492 * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
494 * The created signature is always in lower-S form. See
495 * secp256k1_ecdsa_signature_normalize for more details.
497 SECP256K1_API int secp256k1_ecdsa_sign(
498 const secp256k1_context* ctx,
499 secp256k1_ecdsa_signature *sig,
500 const unsigned char *msg32,
501 const unsigned char *seckey,
502 secp256k1_nonce_function noncefp,
504 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
506 /** Verify an ECDSA secret key.
508 * Returns: 1: secret key is valid
509 * 0: secret key is invalid
510 * Args: ctx: pointer to a context object (cannot be NULL)
511 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
513 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
514 const secp256k1_context* ctx,
515 const unsigned char *seckey
516 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
518 /** Compute the public key for a secret key.
520 * Returns: 1: secret was valid, public key stores
521 * 0: secret was invalid, try again
522 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
523 * Out: pubkey: pointer to the created public key (cannot be NULL)
524 * In: seckey: pointer to a 32-byte private key (cannot be NULL)
526 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
527 const secp256k1_context* ctx,
528 secp256k1_pubkey *pubkey,
529 const unsigned char *seckey
530 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
532 /** Negates a private key in place.
535 * Args: ctx: pointer to a context object
536 * In/Out: seckey: pointer to the 32-byte private key to be negated (cannot be NULL)
538 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
539 const secp256k1_context* ctx,
540 unsigned char *seckey
541 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
543 /** Negates a public key in place.
546 * Args: ctx: pointer to a context object
547 * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
549 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
550 const secp256k1_context* ctx,
551 secp256k1_pubkey *pubkey
552 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
554 /** Tweak a private key by adding tweak to it.
555 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
556 * uniformly random 32-byte arrays, or if the resulting private key
557 * would be invalid (only when the tweak is the complement of the
558 * private key). 1 otherwise.
559 * Args: ctx: pointer to a context object (cannot be NULL).
560 * In/Out: seckey: pointer to a 32-byte private key.
561 * In: tweak: pointer to a 32-byte tweak.
563 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
564 const secp256k1_context* ctx,
565 unsigned char *seckey,
566 const unsigned char *tweak
567 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
569 /** Tweak a public key by adding tweak times the generator to it.
570 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
571 * uniformly random 32-byte arrays, or if the resulting public key
572 * would be invalid (only when the tweak is the complement of the
573 * corresponding private key). 1 otherwise.
574 * Args: ctx: pointer to a context object initialized for validation
576 * In/Out: pubkey: pointer to a public key object.
577 * In: tweak: pointer to a 32-byte tweak.
579 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
580 const secp256k1_context* ctx,
581 secp256k1_pubkey *pubkey,
582 const unsigned char *tweak
583 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
585 /** Tweak a private key by multiplying it by a tweak.
586 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
587 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
588 * Args: ctx: pointer to a context object (cannot be NULL).
589 * In/Out: seckey: pointer to a 32-byte private key.
590 * In: tweak: pointer to a 32-byte tweak.
592 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
593 const secp256k1_context* ctx,
594 unsigned char *seckey,
595 const unsigned char *tweak
596 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
598 /** Tweak a public key by multiplying it by a tweak value.
599 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
600 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
601 * Args: ctx: pointer to a context object initialized for validation
603 * In/Out: pubkey: pointer to a public key obkect.
604 * In: tweak: pointer to a 32-byte tweak.
606 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
607 const secp256k1_context* ctx,
608 secp256k1_pubkey *pubkey,
609 const unsigned char *tweak
610 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
612 /** Updates the context randomization to protect against side-channel leakage.
613 * Returns: 1: randomization successfully updated
615 * Args: ctx: pointer to a context object (cannot be NULL)
616 * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
618 * While secp256k1 code is written to be constant-time no matter what secret
619 * values are, it's possible that a future compiler may output code which isn't,
620 * and also that the CPU may not emit the same radio frequencies or draw the same
621 * amount power for all values.
623 * This function provides a seed which is combined into the blinding value: that
624 * blinding value is added before each multiplication (and removed afterwards) so
625 * that it does not affect function results, but shields against attacks which
626 * rely on any input-dependent behaviour.
628 * You should call this after secp256k1_context_create or
629 * secp256k1_context_clone, and may call this repeatedly afterwards.
631 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
632 secp256k1_context* ctx,
633 const unsigned char *seed32
634 ) SECP256K1_ARG_NONNULL(1);
636 /** Add a number of public keys together.
637 * Returns: 1: the sum of the public keys is valid.
638 * 0: the sum of the public keys is not valid.
639 * Args: ctx: pointer to a context object
640 * Out: out: pointer to a public key object for placing the resulting public key
642 * In: ins: pointer to array of pointers to public keys (cannot be NULL)
643 * n: the number of public keys to add together (must be at least 1)
645 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
646 const secp256k1_context* ctx,
647 secp256k1_pubkey *out,
648 const secp256k1_pubkey * const * ins,
650 ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
656 #endif /* SECP256K1_H */