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 /** A simple secp256k1 context object with no precomputed tables. These are useful for
183 * type serialization/parsing functions which require a context object to maintain
184 * API consistency, but currently do not require expensive precomputations or dynamic
187 SECP256K1_API extern const secp256k1_context *secp256k1_context_no_precomp;
189 /** Create a secp256k1 context object.
191 * Returns: a newly created context object.
192 * In: flags: which parts of the context to initialize.
194 * See also secp256k1_context_randomize.
196 SECP256K1_API secp256k1_context* secp256k1_context_create(
198 ) SECP256K1_WARN_UNUSED_RESULT;
200 /** Copies a secp256k1 context object.
202 * Returns: a newly created context object.
203 * Args: ctx: an existing context to copy (cannot be NULL)
205 SECP256K1_API secp256k1_context* secp256k1_context_clone(
206 const secp256k1_context* ctx
207 ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
209 /** Destroy a secp256k1 context object.
211 * The context pointer may not be used afterwards.
212 * Args: ctx: an existing context to destroy (cannot be NULL)
214 SECP256K1_API void secp256k1_context_destroy(
215 secp256k1_context* ctx
218 /** Set a callback function to be called when an illegal argument is passed to
219 * an API call. It will only trigger for violations that are mentioned
220 * explicitly in the header.
222 * The philosophy is that these shouldn't be dealt with through a
223 * specific return value, as calling code should not have branches to deal with
224 * the case that this code itself is broken.
226 * On the other hand, during debug stage, one would want to be informed about
227 * such mistakes, and the default (crashing) may be inadvisable.
228 * When this callback is triggered, the API function called is guaranteed not
229 * to cause a crash, though its return value and output arguments are
232 * Args: ctx: an existing context object (cannot be NULL)
233 * In: fun: a pointer to a function to call when an illegal argument is
234 * passed to the API, taking a message and an opaque pointer
235 * (NULL restores a default handler that calls abort).
236 * data: the opaque pointer to pass to fun above.
238 SECP256K1_API void secp256k1_context_set_illegal_callback(
239 secp256k1_context* ctx,
240 void (*fun)(const char* message, void* data),
242 ) SECP256K1_ARG_NONNULL(1);
244 /** Set a callback function to be called when an internal consistency check
245 * fails. The default is crashing.
247 * This can only trigger in case of a hardware failure, miscompilation,
248 * memory corruption, serious bug in the library, or other error would can
249 * otherwise result in undefined behaviour. It will not trigger due to mere
250 * incorrect usage of the API (see secp256k1_context_set_illegal_callback
251 * for that). After this callback returns, anything may happen, including
254 * Args: ctx: an existing context object (cannot be NULL)
255 * In: fun: a pointer to a function to call when an internal error occurs,
256 * taking a message and an opaque pointer (NULL restores a default
257 * handler that calls abort).
258 * data: the opaque pointer to pass to fun above.
260 SECP256K1_API void secp256k1_context_set_error_callback(
261 secp256k1_context* ctx,
262 void (*fun)(const char* message, void* data),
264 ) SECP256K1_ARG_NONNULL(1);
266 /** Create a secp256k1 scratch space object.
268 * Returns: a newly created scratch space.
269 * Args: ctx: an existing context object (cannot be NULL)
270 * In: max_size: maximum amount of memory to allocate
272 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT secp256k1_scratch_space* secp256k1_scratch_space_create(
273 const secp256k1_context* ctx,
275 ) SECP256K1_ARG_NONNULL(1);
277 /** Destroy a secp256k1 scratch space.
279 * The pointer may not be used afterwards.
280 * Args: scratch: space to destroy
282 SECP256K1_API void secp256k1_scratch_space_destroy(
283 secp256k1_scratch_space* scratch
286 /** Parse a variable-length public key into the pubkey object.
288 * Returns: 1 if the public key was fully valid.
289 * 0 if the public key could not be parsed or is invalid.
290 * Args: ctx: a secp256k1 context object.
291 * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
292 * parsed version of input. If not, its value is undefined.
293 * In: input: pointer to a serialized public key
294 * inputlen: length of the array pointed to by input
296 * This function supports parsing compressed (33 bytes, header byte 0x02 or
297 * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
298 * byte 0x06 or 0x07) format public keys.
300 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
301 const secp256k1_context* ctx,
302 secp256k1_pubkey* pubkey,
303 const unsigned char *input,
305 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
307 /** Serialize a pubkey object into a serialized byte sequence.
310 * Args: ctx: a secp256k1 context object.
311 * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
312 * compressed==1) byte array to place the serialized key
314 * In/Out: outputlen: a pointer to an integer which is initially set to the
315 * size of output, and is overwritten with the written
317 * In: pubkey: a pointer to a secp256k1_pubkey containing an
318 * initialized public key.
319 * flags: SECP256K1_EC_COMPRESSED if serialization should be in
320 * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
322 SECP256K1_API int secp256k1_ec_pubkey_serialize(
323 const secp256k1_context* ctx,
324 unsigned char *output,
326 const secp256k1_pubkey* pubkey,
328 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
330 /** Parse an ECDSA signature in compact (64 bytes) format.
332 * Returns: 1 when the signature could be parsed, 0 otherwise.
333 * Args: ctx: a secp256k1 context object
334 * Out: sig: a pointer to a signature object
335 * In: input64: a pointer to the 64-byte array to parse
337 * The signature must consist of a 32-byte big endian R value, followed by a
338 * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
339 * encoding is invalid. R and S with value 0 are allowed in the encoding.
341 * After the call, sig will always be initialized. If parsing failed or R or
342 * S are zero, the resulting sig value is guaranteed to fail validation for any
343 * message and public key.
345 SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
346 const secp256k1_context* ctx,
347 secp256k1_ecdsa_signature* sig,
348 const unsigned char *input64
349 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
351 /** Parse a DER ECDSA signature.
353 * Returns: 1 when the signature could be parsed, 0 otherwise.
354 * Args: ctx: a secp256k1 context object
355 * Out: sig: a pointer to a signature object
356 * In: input: a pointer to the signature to be parsed
357 * inputlen: the length of the array pointed to be input
359 * This function will accept any valid DER encoded signature, even if the
360 * encoded numbers are out of range.
362 * After the call, sig will always be initialized. If parsing failed or the
363 * encoded numbers are out of range, signature validation with it is
364 * guaranteed to fail for every message and public key.
366 SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
367 const secp256k1_context* ctx,
368 secp256k1_ecdsa_signature* sig,
369 const unsigned char *input,
371 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
373 /** Serialize an ECDSA signature in DER format.
375 * Returns: 1 if enough space was available to serialize, 0 otherwise
376 * Args: ctx: a secp256k1 context object
377 * Out: output: a pointer to an array to store the DER serialization
378 * In/Out: outputlen: a pointer to a length integer. Initially, this integer
379 * should be set to the length of output. After the call
380 * it will be set to the length of the serialization (even
381 * if 0 was returned).
382 * In: sig: a pointer to an initialized signature object
384 SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
385 const secp256k1_context* ctx,
386 unsigned char *output,
388 const secp256k1_ecdsa_signature* sig
389 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
391 /** Serialize an ECDSA signature in compact (64 byte) format.
394 * Args: ctx: a secp256k1 context object
395 * Out: output64: a pointer to a 64-byte array to store the compact serialization
396 * In: sig: a pointer to an initialized signature object
398 * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
400 SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
401 const secp256k1_context* ctx,
402 unsigned char *output64,
403 const secp256k1_ecdsa_signature* sig
404 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
406 /** Verify an ECDSA signature.
408 * Returns: 1: correct signature
409 * 0: incorrect or unparseable signature
410 * Args: ctx: a secp256k1 context object, initialized for verification.
411 * In: sig: the signature being verified (cannot be NULL)
412 * msg32: the 32-byte message hash being verified (cannot be NULL)
413 * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
415 * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
418 * If you need to accept ECDSA signatures from sources that do not obey this
419 * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
420 * validation, but be aware that doing so results in malleable signatures.
422 * For details, see the comments for that function.
424 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
425 const secp256k1_context* ctx,
426 const secp256k1_ecdsa_signature *sig,
427 const unsigned char *msg32,
428 const secp256k1_pubkey *pubkey
429 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
431 /** Convert a signature to a normalized lower-S form.
433 * Returns: 1 if sigin was not normalized, 0 if it already was.
434 * Args: ctx: a secp256k1 context object
435 * Out: sigout: a pointer to a signature to fill with the normalized form,
436 * or copy if the input was already normalized. (can be NULL if
437 * you're only interested in whether the input was already
439 * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
440 * can be identical to sigout)
442 * With ECDSA a third-party can forge a second distinct signature of the same
443 * message, given a single initial signature, but without knowing the key. This
444 * is done by negating the S value modulo the order of the curve, 'flipping'
445 * the sign of the random point R which is not included in the signature.
447 * Forgery of the same message isn't universally problematic, but in systems
448 * where message malleability or uniqueness of signatures is important this can
449 * cause issues. This forgery can be blocked by all verifiers forcing signers
450 * to use a normalized form.
452 * The lower-S form reduces the size of signatures slightly on average when
453 * variable length encodings (such as DER) are used and is cheap to verify,
454 * making it a good choice. Security of always using lower-S is assured because
455 * anyone can trivially modify a signature after the fact to enforce this
458 * The lower S value is always between 0x1 and
459 * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
462 * No other forms of ECDSA malleability are known and none seem likely, but
463 * there is no formal proof that ECDSA, even with this additional restriction,
464 * is free of other malleability. Commonly used serialization schemes will also
465 * accept various non-unique encodings, so care should be taken when this
466 * property is required for an application.
468 * The secp256k1_ecdsa_sign function will by default create signatures in the
469 * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
470 * signatures come from a system that cannot enforce this property,
471 * secp256k1_ecdsa_signature_normalize must be called before verification.
473 SECP256K1_API int secp256k1_ecdsa_signature_normalize(
474 const secp256k1_context* ctx,
475 secp256k1_ecdsa_signature *sigout,
476 const secp256k1_ecdsa_signature *sigin
477 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
479 /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
480 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
483 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
485 /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
486 SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
488 /** Create an ECDSA signature.
490 * Returns: 1: signature created
491 * 0: the nonce generation function failed, or the private key was invalid.
492 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
493 * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
494 * In: msg32: the 32-byte message hash being signed (cannot be NULL)
495 * seckey: pointer to a 32-byte secret key (cannot be NULL)
496 * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
497 * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
499 * The created signature is always in lower-S form. See
500 * secp256k1_ecdsa_signature_normalize for more details.
502 SECP256K1_API int secp256k1_ecdsa_sign(
503 const secp256k1_context* ctx,
504 secp256k1_ecdsa_signature *sig,
505 const unsigned char *msg32,
506 const unsigned char *seckey,
507 secp256k1_nonce_function noncefp,
509 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
511 /** Verify an ECDSA secret key.
513 * Returns: 1: secret key is valid
514 * 0: secret key is invalid
515 * Args: ctx: pointer to a context object (cannot be NULL)
516 * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
518 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
519 const secp256k1_context* ctx,
520 const unsigned char *seckey
521 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
523 /** Compute the public key for a secret key.
525 * Returns: 1: secret was valid, public key stores
526 * 0: secret was invalid, try again
527 * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
528 * Out: pubkey: pointer to the created public key (cannot be NULL)
529 * In: seckey: pointer to a 32-byte private key (cannot be NULL)
531 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
532 const secp256k1_context* ctx,
533 secp256k1_pubkey *pubkey,
534 const unsigned char *seckey
535 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
537 /** Negates a private key in place.
540 * Args: ctx: pointer to a context object
541 * In/Out: seckey: pointer to the 32-byte private key to be negated (cannot be NULL)
543 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
544 const secp256k1_context* ctx,
545 unsigned char *seckey
546 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
548 /** Negates a public key in place.
551 * Args: ctx: pointer to a context object
552 * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
554 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
555 const secp256k1_context* ctx,
556 secp256k1_pubkey *pubkey
557 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
559 /** Tweak a private key by adding tweak to it.
560 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
561 * uniformly random 32-byte arrays, or if the resulting private key
562 * would be invalid (only when the tweak is the complement of the
563 * private key). 1 otherwise.
564 * Args: ctx: pointer to a context object (cannot be NULL).
565 * In/Out: seckey: pointer to a 32-byte private key.
566 * In: tweak: pointer to a 32-byte tweak.
568 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
569 const secp256k1_context* ctx,
570 unsigned char *seckey,
571 const unsigned char *tweak
572 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
574 /** Tweak a public key by adding tweak times the generator to it.
575 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
576 * uniformly random 32-byte arrays, or if the resulting public key
577 * would be invalid (only when the tweak is the complement of the
578 * corresponding private key). 1 otherwise.
579 * Args: ctx: pointer to a context object initialized for validation
581 * In/Out: pubkey: pointer to a public key object.
582 * In: tweak: pointer to a 32-byte tweak.
584 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
585 const secp256k1_context* ctx,
586 secp256k1_pubkey *pubkey,
587 const unsigned char *tweak
588 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
590 /** Tweak a private key by multiplying it by a tweak.
591 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
592 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
593 * Args: ctx: pointer to a context object (cannot be NULL).
594 * In/Out: seckey: pointer to a 32-byte private key.
595 * In: tweak: pointer to a 32-byte tweak.
597 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
598 const secp256k1_context* ctx,
599 unsigned char *seckey,
600 const unsigned char *tweak
601 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
603 /** Tweak a public key by multiplying it by a tweak value.
604 * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
605 * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
606 * Args: ctx: pointer to a context object initialized for validation
608 * In/Out: pubkey: pointer to a public key obkect.
609 * In: tweak: pointer to a 32-byte tweak.
611 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
612 const secp256k1_context* ctx,
613 secp256k1_pubkey *pubkey,
614 const unsigned char *tweak
615 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
617 /** Updates the context randomization to protect against side-channel leakage.
618 * Returns: 1: randomization successfully updated or nothing to randomize
620 * Args: ctx: pointer to a context object (cannot be NULL)
621 * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
623 * While secp256k1 code is written to be constant-time no matter what secret
624 * values are, it's possible that a future compiler may output code which isn't,
625 * and also that the CPU may not emit the same radio frequencies or draw the same
626 * amount power for all values.
628 * This function provides a seed which is combined into the blinding value: that
629 * blinding value is added before each multiplication (and removed afterwards) so
630 * that it does not affect function results, but shields against attacks which
631 * rely on any input-dependent behaviour.
633 * This function has currently an effect only on contexts initialized for signing
634 * because randomization is currently used only for signing. However, this is not
635 * guaranteed and may change in the future. It is safe to call this function on
636 * contexts not initialized for signing; then it will have no effect and return 1.
638 * You should call this after secp256k1_context_create or
639 * secp256k1_context_clone, and may call this repeatedly afterwards.
641 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
642 secp256k1_context* ctx,
643 const unsigned char *seed32
644 ) SECP256K1_ARG_NONNULL(1);
646 /** Add a number of public keys together.
647 * Returns: 1: the sum of the public keys is valid.
648 * 0: the sum of the public keys is not valid.
649 * Args: ctx: pointer to a context object
650 * Out: out: pointer to a public key object for placing the resulting public key
652 * In: ins: pointer to array of pointers to public keys (cannot be NULL)
653 * n: the number of public keys to add together (must be at least 1)
655 SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
656 const secp256k1_context* ctx,
657 secp256k1_pubkey *out,
658 const secp256k1_pubkey * const * ins,
660 ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
666 #endif /* SECP256K1_H */