github.com/ethereum/go-ethereum@v1.16.1/crypto/secp256k1/libsecp256k1/include/secp256k1.h (about)

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