Coverage Report

Created: 2025-09-19 18:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/secp256k1/include/secp256k1.h
Line
Count
Source
1
#ifndef SECP256K1_H
2
#define SECP256K1_H
3
4
#ifdef __cplusplus
5
extern "C" {
6
#endif
7
8
#include <stddef.h>
9
10
/** Unless explicitly stated all pointer arguments must not be NULL.
11
 *
12
 * The following rules specify the order of arguments in API calls:
13
 *
14
 * 1. Context pointers go first, followed by output arguments, combined
15
 *    output/input arguments, and finally input-only arguments.
16
 * 2. Array lengths always immediately follow the argument whose length
17
 *    they describe, even if this violates rule 1.
18
 * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated
19
 *    later go first. This means: signatures, public nonces, secret nonces,
20
 *    messages, public keys, secret keys, tweaks.
21
 * 4. Arguments that are not data pointers go last, from more complex to less
22
 *    complex: function pointers, algorithm names, messages, void pointers,
23
 *    counts, flags, booleans.
24
 * 5. Opaque data pointers follow the function pointer they are to be passed to.
25
 */
26
27
/** Opaque data structure that holds context information
28
 *
29
 *  The primary purpose of context objects is to store randomization data for
30
 *  enhanced protection against side-channel leakage. This protection is only
31
 *  effective if the context is randomized after its creation. See
32
 *  secp256k1_context_create for creation of contexts and
33
 *  secp256k1_context_randomize for randomization.
34
 *
35
 *  A secondary purpose of context objects is to store pointers to callback
36
 *  functions that the library will call when certain error states arise. See
37
 *  secp256k1_context_set_error_callback as well as
38
 *  secp256k1_context_set_illegal_callback for details. Future library versions
39
 *  may use context objects for additional purposes.
40
 *
41
 *  A constructed context can safely be used from multiple threads
42
 *  simultaneously, but API calls that take a non-const pointer to a context
43
 *  need exclusive access to it. In particular this is the case for
44
 *  secp256k1_context_destroy, secp256k1_context_preallocated_destroy,
45
 *  and secp256k1_context_randomize.
46
 *
47
 *  Regarding randomization, either do it once at creation time (in which case
48
 *  you do not need any locking for the other calls), or use a read-write lock.
49
 */
50
typedef struct secp256k1_context_struct secp256k1_context;
51
52
/** Opaque data structure that holds a parsed and valid public key.
53
 *
54
 *  The exact representation of data inside is implementation defined and not
55
 *  guaranteed to be portable between different platforms or versions. It is
56
 *  however guaranteed to be 64 bytes in size, and can be safely copied/moved.
57
 *  If you need to convert to a format suitable for storage or transmission,
58
 *  use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse. To
59
 *  compare keys, use secp256k1_ec_pubkey_cmp.
60
 */
61
typedef struct secp256k1_pubkey {
62
    unsigned char data[64];
63
} secp256k1_pubkey;
64
65
/** Opaque data structure that holds a parsed ECDSA signature.
66
 *
67
 *  The exact representation of data inside is implementation defined and not
68
 *  guaranteed to be portable between different platforms or versions. It is
69
 *  however guaranteed to be 64 bytes in size, and can be safely copied/moved.
70
 *  If you need to convert to a format suitable for storage, transmission, or
71
 *  comparison, use the secp256k1_ecdsa_signature_serialize_* and
72
 *  secp256k1_ecdsa_signature_parse_* functions.
73
 */
74
typedef struct secp256k1_ecdsa_signature {
75
    unsigned char data[64];
76
} secp256k1_ecdsa_signature;
77
78
/** A pointer to a function to deterministically generate a nonce.
79
 *
80
 * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
81
 * Out:     nonce32:   pointer to a 32-byte array to be filled by the function.
82
 * In:      msg32:     the 32-byte message hash being verified (will not be NULL)
83
 *          key32:     pointer to a 32-byte secret key (will not be NULL)
84
 *          algo16:    pointer to a 16-byte array describing the signature
85
 *                     algorithm (will be NULL for ECDSA for compatibility).
86
 *          data:      Arbitrary data pointer that is passed through.
87
 *          attempt:   how many iterations we have tried to find a nonce.
88
 *                     This will almost always be 0, but different attempt values
89
 *                     are required to result in a different nonce.
90
 *
91
 * Except for test cases, this function should compute some cryptographic hash of
92
 * the message, the algorithm, the key and the attempt.
93
 */
94
typedef int (*secp256k1_nonce_function)(
95
    unsigned char *nonce32,
96
    const unsigned char *msg32,
97
    const unsigned char *key32,
98
    const unsigned char *algo16,
99
    void *data,
100
    unsigned int attempt
101
);
102
103
# if !defined(SECP256K1_GNUC_PREREQ)
104
#  if defined(__GNUC__)&&defined(__GNUC_MINOR__)
105
#   define SECP256K1_GNUC_PREREQ(_maj,_min) \
106
 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
107
#  else
108
#   define SECP256K1_GNUC_PREREQ(_maj,_min) 0
109
#  endif
110
# endif
111
112
/*  When this header is used at build-time the SECP256K1_BUILD define needs to be set
113
 *  to correctly setup export attributes and nullness checks.  This is normally done
114
 *  by secp256k1.c but to guard against this header being included before secp256k1.c
115
 *  has had a chance to set the define (e.g. via test harnesses that just includes
116
 *  secp256k1.c) we set SECP256K1_NO_BUILD when this header is processed without the
117
 *  BUILD define so this condition can be caught.
118
 */
119
#ifndef SECP256K1_BUILD
120
# define SECP256K1_NO_BUILD
121
#endif
122
123
/* Symbol visibility. */
124
#if !defined(SECP256K1_API) && defined(SECP256K1_NO_API_VISIBILITY_ATTRIBUTES)
125
     /* The user has requested that we don't specify visibility attributes in
126
      * the public API.
127
      *
128
      * Since all our non-API declarations use the static qualifier, this means
129
      * that the user can use -fvisibility=<value> to set the visibility of the
130
      * API symbols. For instance, -fvisibility=hidden can be useful *even for
131
      * the API symbols*, e.g., when building a static library which is linked
132
      * into a shared library, and the latter should not re-export the
133
      * libsecp256k1 API.
134
      *
135
      * While visibility is a concept that applies only to shared libraries,
136
      * setting visibility will still make a difference when building a static
137
      * library: the visibility settings will be stored in the static library,
138
      * solely for the potential case that the static library will be linked into
139
      * a shared library. In that case, the stored visibility settings will
140
      * resurface and be honored for the shared library. */
141
#    define SECP256K1_API extern
142
#endif
143
#if !defined(SECP256K1_API)
144
#    if defined(SECP256K1_BUILD)
145
         /* On Windows, assume a shared library only if explicitly requested.
146
          *   1. If using Libtool, it defines DLL_EXPORT automatically.
147
          *   2. In other cases, SECP256K1_DLL_EXPORT must be defined. */
148
#        if defined(_WIN32) && (defined(SECP256K1_DLL_EXPORT) || defined(DLL_EXPORT))
149
             /* GCC for Windows (e.g., MinGW) accepts the __declspec syntax for
150
              * MSVC compatibility. A __declspec declaration implies (but is not
151
              * exactly equivalent to) __attribute__ ((visibility("default"))),
152
              * and so we actually want __declspec even on GCC, see "Microsoft
153
              * Windows Function Attributes" in the GCC manual and the
154
              * recommendations in https://gcc.gnu.org/wiki/Visibility . */
155
#            define SECP256K1_API extern __declspec(dllexport)
156
         /* Avoid __attribute__ ((visibility("default"))) on Windows to get rid
157
          * of warnings when compiling with -flto due to a bug in GCC, see
158
          * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116478 . */
159
#        elif !defined(_WIN32) && defined (__GNUC__) && (__GNUC__ >= 4)
160
#            define SECP256K1_API extern __attribute__ ((visibility("default")))
161
#        else
162
#            define SECP256K1_API extern
163
#        endif
164
#    else
165
         /* On Windows, SECP256K1_STATIC must be defined when consuming
166
          * libsecp256k1 as a static library. Note that SECP256K1_STATIC is a
167
          * "consumer-only" macro, and it has no meaning when building
168
          * libsecp256k1. */
169
#        if defined(_WIN32) && !defined(SECP256K1_STATIC)
170
#            define SECP256K1_API extern __declspec(dllimport)
171
#        else
172
#            define SECP256K1_API extern
173
#        endif
174
#    endif
175
#endif
176
177
/* Warning attributes
178
 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
179
 * some paranoid null checks. */
180
# if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
181
#  define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
182
# else
183
#  define SECP256K1_WARN_UNUSED_RESULT
184
# endif
185
# if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
186
#  define SECP256K1_ARG_NONNULL(_x)  __attribute__ ((__nonnull__(_x)))
187
# else
188
#  define SECP256K1_ARG_NONNULL(_x)
189
# endif
190
191
/* Attribute for marking functions, types, and variables as deprecated */
192
#if !defined(SECP256K1_BUILD) && defined(__has_attribute)
193
# if __has_attribute(__deprecated__)
194
#  define SECP256K1_DEPRECATED(_msg) __attribute__ ((__deprecated__(_msg)))
195
# else
196
#  define SECP256K1_DEPRECATED(_msg)
197
# endif
198
#else
199
# define SECP256K1_DEPRECATED(_msg)
200
#endif
201
202
/* All flags' lower 8 bits indicate what they're for. Do not use directly. */
203
#define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
204
0
#define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
205
0
#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
206
/* The higher bits contain the actual data. Do not use directly. */
207
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
208
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
209
0
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
210
0
#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
211
212
/** Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and
213
 *  secp256k1_context_preallocated_create. */
214
0
#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
215
216
/** Deprecated context flags. These flags are treated equivalent to SECP256K1_CONTEXT_NONE. */
217
#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
218
#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
219
220
/* Testing flag. Do not use. */
221
#define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
222
223
/** Flag to pass to secp256k1_ec_pubkey_serialize. */
224
0
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
225
0
#define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
226
227
/** Prefix byte used to tag various encoded curvepoints for specific purposes */
228
0
#define SECP256K1_TAG_PUBKEY_EVEN 0x02
229
0
#define SECP256K1_TAG_PUBKEY_ODD 0x03
230
0
#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
231
0
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
232
0
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
233
234
/** A built-in constant secp256k1 context object with static storage duration, to be
235
 *  used in conjunction with secp256k1_selftest.
236
 *
237
 *  This context object offers *only limited functionality* , i.e., it cannot be used
238
 *  for API functions that perform computations involving secret keys, e.g., signing
239
 *  and public key generation. If this restriction applies to a specific API function,
240
 *  it is mentioned in its documentation. See secp256k1_context_create if you need a
241
 *  full context object that supports all functionality offered by the library.
242
 *
243
 *  It is highly recommended to call secp256k1_selftest before using this context.
244
 */
245
SECP256K1_API const secp256k1_context * const secp256k1_context_static;
246
247
/** Deprecated alias for secp256k1_context_static. */
248
SECP256K1_API const secp256k1_context * const secp256k1_context_no_precomp
249
SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
250
251
/** Perform basic self tests (to be used in conjunction with secp256k1_context_static)
252
 *
253
 *  This function performs self tests that detect some serious usage errors and
254
 *  similar conditions, e.g., when the library is compiled for the wrong endianness.
255
 *  This is a last resort measure to be used in production. The performed tests are
256
 *  very rudimentary and are not intended as a replacement for running the test
257
 *  binaries.
258
 *
259
 *  It is highly recommended to call this before using secp256k1_context_static.
260
 *  It is not necessary to call this function before using a context created with
261
 *  secp256k1_context_create (or secp256k1_context_preallocated_create), which will
262
 *  take care of performing the self tests.
263
 *
264
 *  If the tests fail, this function will call the default error handler to abort the
265
 *  program (see secp256k1_context_set_error_callback).
266
 */
267
SECP256K1_API void secp256k1_selftest(void);
268
269
270
/** Create a secp256k1 context object (in dynamically allocated memory).
271
 *
272
 *  This function uses malloc to allocate memory. It is guaranteed that malloc is
273
 *  called at most once for every call of this function. If you need to avoid dynamic
274
 *  memory allocation entirely, see secp256k1_context_static and the functions in
275
 *  secp256k1_preallocated.h.
276
 *
277
 *  Returns: pointer to a newly created context object.
278
 *  In:      flags: Always set to SECP256K1_CONTEXT_NONE (see below).
279
 *
280
 *  The only valid non-deprecated flag in recent library versions is
281
 *  SECP256K1_CONTEXT_NONE, which will create a context sufficient for all functionality
282
 *  offered by the library. All other (deprecated) flags will be treated as equivalent
283
 *  to the SECP256K1_CONTEXT_NONE flag. Though the flags parameter primarily exists for
284
 *  historical reasons, future versions of the library may introduce new flags.
285
 *
286
 *  If the context is intended to be used for API functions that perform computations
287
 *  involving secret keys, e.g., signing and public key generation, then it is highly
288
 *  recommended to call secp256k1_context_randomize on the context before calling
289
 *  those API functions. This will provide enhanced protection against side-channel
290
 *  leakage, see secp256k1_context_randomize for details.
291
 *
292
 *  Do not create a new context object for each operation, as construction and
293
 *  randomization can take non-negligible time.
294
 */
295
SECP256K1_API secp256k1_context *secp256k1_context_create(
296
    unsigned int flags
297
) SECP256K1_WARN_UNUSED_RESULT;
298
299
/** Copy a secp256k1 context object (into dynamically allocated memory).
300
 *
301
 *  This function uses malloc to allocate memory. It is guaranteed that malloc is
302
 *  called at most once for every call of this function. If you need to avoid dynamic
303
 *  memory allocation entirely, see the functions in secp256k1_preallocated.h.
304
 *
305
 *  Cloning secp256k1_context_static is not possible, and should not be emulated by
306
 *  the caller (e.g., using memcpy). Create a new context instead.
307
 *
308
 *  Returns: pointer to a newly created context object.
309
 *  Args:    ctx: pointer to a context to copy (not secp256k1_context_static).
310
 */
311
SECP256K1_API secp256k1_context *secp256k1_context_clone(
312
    const secp256k1_context *ctx
313
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
314
315
/** Destroy a secp256k1 context object (created in dynamically allocated memory).
316
 *
317
 *  The context pointer may not be used afterwards.
318
 *
319
 *  The context to destroy must have been created using secp256k1_context_create
320
 *  or secp256k1_context_clone. If the context has instead been created using
321
 *  secp256k1_context_preallocated_create or secp256k1_context_preallocated_clone, the
322
 *  behaviour is undefined. In that case, secp256k1_context_preallocated_destroy must
323
 *  be used instead.
324
 *
325
 *  Args:   ctx: pointer to a context to destroy, constructed using
326
 *               secp256k1_context_create or secp256k1_context_clone
327
 *               (i.e., not secp256k1_context_static).
328
 */
329
SECP256K1_API void secp256k1_context_destroy(
330
    secp256k1_context *ctx
331
) SECP256K1_ARG_NONNULL(1);
332
333
/** Set a callback function to be called when an illegal argument is passed to
334
 *  an API call. It will only trigger for violations that are mentioned
335
 *  explicitly in the header.
336
 *
337
 *  The philosophy is that these shouldn't be dealt with through a
338
 *  specific return value, as calling code should not have branches to deal with
339
 *  the case that this code itself is broken.
340
 *
341
 *  On the other hand, during debug stage, one would want to be informed about
342
 *  such mistakes, and the default (crashing) may be inadvisable.
343
 *  When this callback is triggered, the API function called is guaranteed not
344
 *  to cause a crash, though its return value and output arguments are
345
 *  undefined.
346
 *
347
 *  When this function has not been called (or called with fn==NULL), then the
348
 *  default handler will be used. The library provides a default handler which
349
 *  writes the message to stderr and calls abort. This default handler can be
350
 *  replaced at link time if the preprocessor macro
351
 *  USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build
352
 *  has been configured with --enable-external-default-callbacks. Then the
353
 *  following two symbols must be provided to link against:
354
 *   - void secp256k1_default_illegal_callback_fn(const char *message, void *data);
355
 *   - void secp256k1_default_error_callback_fn(const char *message, void *data);
356
 *  The library can call these default handlers even before a proper callback data
357
 *  pointer could have been set using secp256k1_context_set_illegal_callback or
358
 *  secp256k1_context_set_error_callback, e.g., when the creation of a context
359
 *  fails. In this case, the corresponding default handler will be called with
360
 *  the data pointer argument set to NULL.
361
 *
362
 *  Args: ctx:  pointer to a context object.
363
 *  In:   fun:  pointer to a function to call when an illegal argument is
364
 *              passed to the API, taking a message and an opaque pointer.
365
 *              (NULL restores the default handler.)
366
 *        data: the opaque pointer to pass to fun above, must be NULL for the default handler.
367
 *
368
 *  See also secp256k1_context_set_error_callback.
369
 */
370
SECP256K1_API void secp256k1_context_set_illegal_callback(
371
    secp256k1_context *ctx,
372
    void (*fun)(const char *message, void *data),
373
    const void *data
374
) SECP256K1_ARG_NONNULL(1);
375
376
/** Set a callback function to be called when an internal consistency check
377
 *  fails.
378
 *
379
 *  The default callback writes an error message to stderr and calls abort
380
 *  to abort the program.
381
 *
382
 *  This can only trigger in case of a hardware failure, miscompilation,
383
 *  memory corruption, serious bug in the library, or other error would can
384
 *  otherwise result in undefined behaviour. It will not trigger due to mere
385
 *  incorrect usage of the API (see secp256k1_context_set_illegal_callback
386
 *  for that). After this callback returns, anything may happen, including
387
 *  crashing.
388
 *
389
 *  Args: ctx:  pointer to a context object.
390
 *  In:   fun:  pointer to a function to call when an internal error occurs,
391
 *              taking a message and an opaque pointer (NULL restores the
392
 *              default handler, see secp256k1_context_set_illegal_callback
393
 *              for details).
394
 *        data: the opaque pointer to pass to fun above, must be NULL for the default handler.
395
 *
396
 *  See also secp256k1_context_set_illegal_callback.
397
 */
398
SECP256K1_API void secp256k1_context_set_error_callback(
399
    secp256k1_context *ctx,
400
    void (*fun)(const char *message, void *data),
401
    const void *data
402
) SECP256K1_ARG_NONNULL(1);
403
404
/** Parse a variable-length public key into the pubkey object.
405
 *
406
 *  Returns: 1 if the public key was fully valid.
407
 *           0 if the public key could not be parsed or is invalid.
408
 *  Args: ctx:      pointer to a context object.
409
 *  Out:  pubkey:   pointer to a pubkey object. If 1 is returned, it is set to a
410
 *                  parsed version of input. If not, its value is undefined.
411
 *  In:   input:    pointer to a serialized public key
412
 *        inputlen: length of the array pointed to by input
413
 *
414
 *  This function supports parsing compressed (33 bytes, header byte 0x02 or
415
 *  0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
416
 *  byte 0x06 or 0x07) format public keys.
417
 */
418
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
419
    const secp256k1_context *ctx,
420
    secp256k1_pubkey *pubkey,
421
    const unsigned char *input,
422
    size_t inputlen
423
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
424
425
/** Serialize a pubkey object into a serialized byte sequence.
426
 *
427
 *  Returns: 1 always.
428
 *  Args:   ctx:        pointer to a context object.
429
 *  Out:    output:     pointer to a 65-byte (if compressed==0) or 33-byte (if
430
 *                      compressed==1) byte array to place the serialized key
431
 *                      in.
432
 *  In/Out: outputlen:  pointer to an integer which is initially set to the
433
 *                      size of output, and is overwritten with the written
434
 *                      size.
435
 *  In:     pubkey:     pointer to a secp256k1_pubkey containing an
436
 *                      initialized public key.
437
 *          flags:      SECP256K1_EC_COMPRESSED if serialization should be in
438
 *                      compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
439
 */
440
SECP256K1_API int secp256k1_ec_pubkey_serialize(
441
    const secp256k1_context *ctx,
442
    unsigned char *output,
443
    size_t *outputlen,
444
    const secp256k1_pubkey *pubkey,
445
    unsigned int flags
446
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
447
448
/** Compare two public keys using lexicographic (of compressed serialization) order
449
 *
450
 *  Returns: <0 if the first public key is less than the second
451
 *           >0 if the first public key is greater than the second
452
 *           0 if the two public keys are equal
453
 *  Args: ctx:      pointer to a context object
454
 *  In:   pubkey1:  first public key to compare
455
 *        pubkey2:  second public key to compare
456
 */
457
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(
458
    const secp256k1_context *ctx,
459
    const secp256k1_pubkey *pubkey1,
460
    const secp256k1_pubkey *pubkey2
461
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
462
463
/** Sort public keys using lexicographic (of compressed serialization) order
464
 *
465
 *  Returns: 0 if the arguments are invalid. 1 otherwise.
466
 *
467
 *  Args:     ctx: pointer to a context object
468
 *  In:   pubkeys: array of pointers to pubkeys to sort
469
 *      n_pubkeys: number of elements in the pubkeys array
470
 */
471
SECP256K1_API int secp256k1_ec_pubkey_sort(
472
    const secp256k1_context *ctx,
473
    const secp256k1_pubkey **pubkeys,
474
    size_t n_pubkeys
475
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
476
477
/** Parse an ECDSA signature in compact (64 bytes) format.
478
 *
479
 *  Returns: 1 when the signature could be parsed, 0 otherwise.
480
 *  Args: ctx:      pointer to a context object
481
 *  Out:  sig:      pointer to a signature object
482
 *  In:   input64:  pointer to the 64-byte array to parse
483
 *
484
 *  The signature must consist of a 32-byte big endian R value, followed by a
485
 *  32-byte big endian S value. If R or S fall outside of [0..order-1], the
486
 *  encoding is invalid. R and S with value 0 are allowed in the encoding.
487
 *
488
 *  After the call, sig will always be initialized. If parsing failed or R or
489
 *  S are zero, the resulting sig value is guaranteed to fail verification for
490
 *  any message and public key.
491
 */
492
SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
493
    const secp256k1_context *ctx,
494
    secp256k1_ecdsa_signature *sig,
495
    const unsigned char *input64
496
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
497
498
/** Parse a DER ECDSA signature.
499
 *
500
 *  Returns: 1 when the signature could be parsed, 0 otherwise.
501
 *  Args: ctx:      pointer to a context object
502
 *  Out:  sig:      pointer to a signature object
503
 *  In:   input:    pointer to the signature to be parsed
504
 *        inputlen: the length of the array pointed to be input
505
 *
506
 *  This function will accept any valid DER encoded signature, even if the
507
 *  encoded numbers are out of range.
508
 *
509
 *  After the call, sig will always be initialized. If parsing failed or the
510
 *  encoded numbers are out of range, signature verification with it is
511
 *  guaranteed to fail for every message and public key.
512
 */
513
SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
514
    const secp256k1_context *ctx,
515
    secp256k1_ecdsa_signature *sig,
516
    const unsigned char *input,
517
    size_t inputlen
518
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
519
520
/** Serialize an ECDSA signature in DER format.
521
 *
522
 *  Returns: 1 if enough space was available to serialize, 0 otherwise
523
 *  Args:   ctx:       pointer to a context object
524
 *  Out:    output:    pointer to an array to store the DER serialization
525
 *  In/Out: outputlen: pointer to a length integer. Initially, this integer
526
 *                     should be set to the length of output. After the call
527
 *                     it will be set to the length of the serialization (even
528
 *                     if 0 was returned).
529
 *  In:     sig:       pointer to an initialized signature object
530
 */
531
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
532
    const secp256k1_context *ctx,
533
    unsigned char *output,
534
    size_t *outputlen,
535
    const secp256k1_ecdsa_signature *sig
536
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
537
538
/** Serialize an ECDSA signature in compact (64 byte) format.
539
 *
540
 *  Returns: 1
541
 *  Args:   ctx:       pointer to a context object
542
 *  Out:    output64:  pointer to a 64-byte array to store the compact serialization
543
 *  In:     sig:       pointer to an initialized signature object
544
 *
545
 *  See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
546
 */
547
SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
548
    const secp256k1_context *ctx,
549
    unsigned char *output64,
550
    const secp256k1_ecdsa_signature *sig
551
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
552
553
/** Verify an ECDSA signature.
554
 *
555
 *  Returns: 1: correct signature
556
 *           0: incorrect or unparseable signature
557
 *  Args:    ctx:       pointer to a context object
558
 *  In:      sig:       the signature being verified.
559
 *           msghash32: the 32-byte message hash being verified.
560
 *                      The verifier must make sure to apply a cryptographic
561
 *                      hash function to the message by itself and not accept an
562
 *                      msghash32 value directly. Otherwise, it would be easy to
563
 *                      create a "valid" signature without knowledge of the
564
 *                      secret key. See also
565
 *                      https://bitcoin.stackexchange.com/a/81116/35586 for more
566
 *                      background on this topic.
567
 *           pubkey:    pointer to an initialized public key to verify with.
568
 *
569
 * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
570
 * form are accepted.
571
 *
572
 * If you need to accept ECDSA signatures from sources that do not obey this
573
 * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
574
 * verification, but be aware that doing so results in malleable signatures.
575
 *
576
 * For details, see the comments for that function.
577
 */
578
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
579
    const secp256k1_context *ctx,
580
    const secp256k1_ecdsa_signature *sig,
581
    const unsigned char *msghash32,
582
    const secp256k1_pubkey *pubkey
583
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
584
585
/** Convert a signature to a normalized lower-S form.
586
 *
587
 *  Returns: 1 if sigin was not normalized, 0 if it already was.
588
 *  Args: ctx:    pointer to a context object
589
 *  Out:  sigout: pointer to a signature to fill with the normalized form,
590
 *                or copy if the input was already normalized. (can be NULL if
591
 *                you're only interested in whether the input was already
592
 *                normalized).
593
 *  In:   sigin:  pointer to a signature to check/normalize (can be identical to sigout)
594
 *
595
 *  With ECDSA a third-party can forge a second distinct signature of the same
596
 *  message, given a single initial signature, but without knowing the key. This
597
 *  is done by negating the S value modulo the order of the curve, 'flipping'
598
 *  the sign of the random point R which is not included in the signature.
599
 *
600
 *  Forgery of the same message isn't universally problematic, but in systems
601
 *  where message malleability or uniqueness of signatures is important this can
602
 *  cause issues. This forgery can be blocked by all verifiers forcing signers
603
 *  to use a normalized form.
604
 *
605
 *  The lower-S form reduces the size of signatures slightly on average when
606
 *  variable length encodings (such as DER) are used and is cheap to verify,
607
 *  making it a good choice. Security of always using lower-S is assured because
608
 *  anyone can trivially modify a signature after the fact to enforce this
609
 *  property anyway.
610
 *
611
 *  The lower S value is always between 0x1 and
612
 *  0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
613
 *  inclusive.
614
 *
615
 *  No other forms of ECDSA malleability are known and none seem likely, but
616
 *  there is no formal proof that ECDSA, even with this additional restriction,
617
 *  is free of other malleability. Commonly used serialization schemes will also
618
 *  accept various non-unique encodings, so care should be taken when this
619
 *  property is required for an application.
620
 *
621
 *  The secp256k1_ecdsa_sign function will by default create signatures in the
622
 *  lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
623
 *  signatures come from a system that cannot enforce this property,
624
 *  secp256k1_ecdsa_signature_normalize must be called before verification.
625
 */
626
SECP256K1_API int secp256k1_ecdsa_signature_normalize(
627
    const secp256k1_context *ctx,
628
    secp256k1_ecdsa_signature *sigout,
629
    const secp256k1_ecdsa_signature *sigin
630
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
631
632
/** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
633
 * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
634
 * extra entropy.
635
 */
636
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
637
638
/** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
639
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_default;
640
641
/** Create an ECDSA signature.
642
 *
643
 *  Returns: 1: signature created
644
 *           0: the nonce generation function failed, or the secret key was invalid.
645
 *  Args:    ctx:       pointer to a context object (not secp256k1_context_static).
646
 *  Out:     sig:       pointer to an array where the signature will be placed.
647
 *  In:      msghash32: the 32-byte message hash being signed.
648
 *           seckey:    pointer to a 32-byte secret key.
649
 *           noncefp:   pointer to a nonce generation function. If NULL,
650
 *                      secp256k1_nonce_function_default is used.
651
 *           ndata:     pointer to arbitrary data used by the nonce generation function
652
 *                      (can be NULL). If it is non-NULL and
653
 *                      secp256k1_nonce_function_default is used, then ndata must be a
654
 *                      pointer to 32-bytes of additional data.
655
 *
656
 * The created signature is always in lower-S form. See
657
 * secp256k1_ecdsa_signature_normalize for more details.
658
 */
659
SECP256K1_API int secp256k1_ecdsa_sign(
660
    const secp256k1_context *ctx,
661
    secp256k1_ecdsa_signature *sig,
662
    const unsigned char *msghash32,
663
    const unsigned char *seckey,
664
    secp256k1_nonce_function noncefp,
665
    const void *ndata
666
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
667
668
/** Verify an elliptic curve secret key.
669
 *
670
 *  A secret key is valid if it is not 0 and less than the secp256k1 curve order
671
 *  when interpreted as an integer (most significant byte first). The
672
 *  probability of choosing a 32-byte string uniformly at random which is an
673
 *  invalid secret key is negligible. However, if it does happen it should
674
 *  be assumed that the randomness source is severely broken and there should
675
 *  be no retry.
676
 *
677
 *  Returns: 1: secret key is valid
678
 *           0: secret key is invalid
679
 *  Args:    ctx: pointer to a context object.
680
 *  In:      seckey: pointer to a 32-byte secret key.
681
 */
682
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
683
    const secp256k1_context *ctx,
684
    const unsigned char *seckey
685
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
686
687
/** Compute the public key for a secret key.
688
 *
689
 *  Returns: 1: secret was valid, public key stores.
690
 *           0: secret was invalid, try again.
691
 *  Args:    ctx:    pointer to a context object (not secp256k1_context_static).
692
 *  Out:     pubkey: pointer to the created public key.
693
 *  In:      seckey: pointer to a 32-byte secret key.
694
 */
695
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
696
    const secp256k1_context *ctx,
697
    secp256k1_pubkey *pubkey,
698
    const unsigned char *seckey
699
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
700
701
/** Negates a secret key in place.
702
 *
703
 *  Returns: 0 if the given secret key is invalid according to
704
 *           secp256k1_ec_seckey_verify. 1 otherwise
705
 *  Args:   ctx:    pointer to a context object
706
 *  In/Out: seckey: pointer to the 32-byte secret key to be negated. If the
707
 *                  secret key is invalid according to
708
 *                  secp256k1_ec_seckey_verify, this function returns 0 and
709
 *                  seckey will be set to some unspecified value.
710
 */
711
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(
712
    const secp256k1_context *ctx,
713
    unsigned char *seckey
714
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
715
716
/** Negates a public key in place.
717
 *
718
 *  Returns: 1 always
719
 *  Args:   ctx:        pointer to a context object
720
 *  In/Out: pubkey:     pointer to the public key to be negated.
721
 */
722
SECP256K1_API int secp256k1_ec_pubkey_negate(
723
    const secp256k1_context *ctx,
724
    secp256k1_pubkey *pubkey
725
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
726
727
/** Tweak a secret key by adding tweak to it.
728
 *
729
 *  Returns: 0 if the arguments are invalid or the resulting secret key would be
730
 *           invalid (only when the tweak is the negation of the secret key). 1
731
 *           otherwise.
732
 *  Args:    ctx:   pointer to a context object.
733
 *  In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
734
 *                  invalid according to secp256k1_ec_seckey_verify, this
735
 *                  function returns 0. seckey will be set to some unspecified
736
 *                  value if this function returns 0.
737
 *  In:    tweak32: pointer to a 32-byte tweak, which must be valid according to
738
 *                  secp256k1_ec_seckey_verify or 32 zero bytes. For uniformly
739
 *                  random 32-byte tweaks, the chance of being invalid is
740
 *                  negligible (around 1 in 2^128).
741
 */
742
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(
743
    const secp256k1_context *ctx,
744
    unsigned char *seckey,
745
    const unsigned char *tweak32
746
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
747
748
/** Tweak a public key by adding tweak times the generator to it.
749
 *
750
 *  Returns: 0 if the arguments are invalid or the resulting public key would be
751
 *           invalid (only when the tweak is the negation of the corresponding
752
 *           secret key). 1 otherwise.
753
 *  Args:    ctx:   pointer to a context object.
754
 *  In/Out: pubkey: pointer to a public key object. pubkey will be set to an
755
 *                  invalid value if this function returns 0.
756
 *  In:    tweak32: pointer to a 32-byte tweak, which must be valid according to
757
 *                  secp256k1_ec_seckey_verify or 32 zero bytes. For uniformly
758
 *                  random 32-byte tweaks, the chance of being invalid is
759
 *                  negligible (around 1 in 2^128).
760
 */
761
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
762
    const secp256k1_context *ctx,
763
    secp256k1_pubkey *pubkey,
764
    const unsigned char *tweak32
765
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
766
767
/** Tweak a secret key by multiplying it by a tweak.
768
 *
769
 *  Returns: 0 if the arguments are invalid. 1 otherwise.
770
 *  Args:   ctx:    pointer to a context object.
771
 *  In/Out: seckey: pointer to a 32-byte secret key. If the secret key is
772
 *                  invalid according to secp256k1_ec_seckey_verify, this
773
 *                  function returns 0. seckey will be set to some unspecified
774
 *                  value if this function returns 0.
775
 *  In:    tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
776
 *                  secp256k1_ec_seckey_verify, this function returns 0. For
777
 *                  uniformly random 32-byte arrays the chance of being invalid
778
 *                  is negligible (around 1 in 2^128).
779
 */
780
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(
781
    const secp256k1_context *ctx,
782
    unsigned char *seckey,
783
    const unsigned char *tweak32
784
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
785
786
/** Tweak a public key by multiplying it by a tweak value.
787
 *
788
 *  Returns: 0 if the arguments are invalid. 1 otherwise.
789
 *  Args:    ctx:   pointer to a context object.
790
 *  In/Out: pubkey: pointer to a public key object. pubkey will be set to an
791
 *                  invalid value if this function returns 0.
792
 *  In:    tweak32: pointer to a 32-byte tweak. If the tweak is invalid according to
793
 *                  secp256k1_ec_seckey_verify, this function returns 0. For
794
 *                  uniformly random 32-byte arrays the chance of being invalid
795
 *                  is negligible (around 1 in 2^128).
796
 */
797
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
798
    const secp256k1_context *ctx,
799
    secp256k1_pubkey *pubkey,
800
    const unsigned char *tweak32
801
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
802
803
/** Randomizes the context to provide enhanced protection against side-channel leakage.
804
 *
805
 *  Returns: 1: randomization successful
806
 *           0: error
807
 *  Args:    ctx:       pointer to a context object (not secp256k1_context_static).
808
 *  In:      seed32:    pointer to a 32-byte random seed (NULL resets to initial state).
809
 *
810
 * While secp256k1 code is written and tested to be constant-time no matter what
811
 * secret values are, it is possible that a compiler may output code which is not,
812
 * and also that the CPU may not emit the same radio frequencies or draw the same
813
 * amount of power for all values. Randomization of the context shields against
814
 * side-channel observations which aim to exploit secret-dependent behaviour in
815
 * certain computations which involve secret keys.
816
 *
817
 * It is highly recommended to call this function on contexts returned from
818
 * secp256k1_context_create or secp256k1_context_clone (or from the corresponding
819
 * functions in secp256k1_preallocated.h) before using these contexts to call API
820
 * functions that perform computations involving secret keys, e.g., signing and
821
 * public key generation. It is possible to call this function more than once on
822
 * the same context, and doing so before every few computations involving secret
823
 * keys is recommended as a defense-in-depth measure. Randomization of the static
824
 * context secp256k1_context_static is not supported.
825
 *
826
 * Currently, the random seed is mainly used for blinding multiplications of a
827
 * secret scalar with the elliptic curve base point. Multiplications of this
828
 * kind are performed by exactly those API functions which are documented to
829
 * require a context that is not secp256k1_context_static. As a rule of thumb,
830
 * these are all functions which take a secret key (or a keypair) as an input.
831
 * A notable exception to that rule is the ECDH module, which relies on a different
832
 * kind of elliptic curve point multiplication and thus does not benefit from
833
 * enhanced protection against side-channel leakage currently.
834
 */
835
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
836
    secp256k1_context *ctx,
837
    const unsigned char *seed32
838
) SECP256K1_ARG_NONNULL(1);
839
840
/** Add a number of public keys together.
841
 *
842
 *  Returns: 1: the sum of the public keys is valid.
843
 *           0: the sum of the public keys is not valid.
844
 *  Args:   ctx:        pointer to a context object.
845
 *  Out:    out:        pointer to a public key object for placing the resulting public key.
846
 *  In:     ins:        pointer to array of pointers to public keys.
847
 *          n:          the number of public keys to add together (must be at least 1).
848
 */
849
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
850
    const secp256k1_context *ctx,
851
    secp256k1_pubkey *out,
852
    const secp256k1_pubkey * const *ins,
853
    size_t n
854
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
855
856
/** Compute a tagged hash as defined in BIP-340.
857
 *
858
 *  This is useful for creating a message hash and achieving domain separation
859
 *  through an application-specific tag. This function returns
860
 *  SHA256(SHA256(tag)||SHA256(tag)||msg). Therefore, tagged hash
861
 *  implementations optimized for a specific tag can precompute the SHA256 state
862
 *  after hashing the tag hashes.
863
 *
864
 *  Returns: 1 always.
865
 *  Args:    ctx: pointer to a context object
866
 *  Out:  hash32: pointer to a 32-byte array to store the resulting hash
867
 *  In:      tag: pointer to an array containing the tag
868
 *        taglen: length of the tag array
869
 *           msg: pointer to an array containing the message
870
 *        msglen: length of the message array
871
 */
872
SECP256K1_API int secp256k1_tagged_sha256(
873
    const secp256k1_context *ctx,
874
    unsigned char *hash32,
875
    const unsigned char *tag,
876
    size_t taglen,
877
    const unsigned char *msg,
878
    size_t msglen
879
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5);
880
881
#ifdef __cplusplus
882
}
883
#endif
884
885
#endif /* SECP256K1_H */