Bitcoin Core 28.99.0
P2P Digital Currency
secp256k1.h
Go to the documentation of this file.
1#ifndef SECP256K1_H
2#define SECP256K1_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <stddef.h>
9
51
61typedef struct secp256k1_pubkey {
62 unsigned char data[64];
64
75 unsigned char data[64];
77
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
202#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
203
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
212#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
213#define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
214
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
234
237SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
238
256
257
284 unsigned int flags
286
300 const secp256k1_context *ctx
302
320
360 void (*fun)(const char *message, void *data),
361 const void *data
363
388 void (*fun)(const char *message, void *data),
389 const void *data
391
407 const secp256k1_context *ctx,
408 secp256k1_pubkey *pubkey,
409 const unsigned char *input,
410 size_t inputlen
412
429 const secp256k1_context *ctx,
430 unsigned char *output,
431 size_t *outputlen,
432 const secp256k1_pubkey *pubkey,
433 unsigned int flags
435
446 const secp256k1_context *ctx,
447 const secp256k1_pubkey *pubkey1,
448 const secp256k1_pubkey *pubkey2
450
460 const secp256k1_context *ctx,
461 const secp256k1_pubkey **pubkeys,
462 size_t n_pubkeys
464
481 const secp256k1_context *ctx,
483 const unsigned char *input64
485
502 const secp256k1_context *ctx,
504 const unsigned char *input,
505 size_t inputlen
507
520 const secp256k1_context *ctx,
521 unsigned char *output,
522 size_t *outputlen,
525
536 const secp256k1_context *ctx,
537 unsigned char *output64,
540
567 const secp256k1_context *ctx,
568 const secp256k1_ecdsa_signature *sig,
569 const unsigned char *msghash32,
570 const secp256k1_pubkey *pubkey
572
615 const secp256k1_context *ctx,
617 const secp256k1_ecdsa_signature *sigin
619
625
628
648 const secp256k1_context *ctx,
650 const unsigned char *msghash32,
651 const unsigned char *seckey,
653 const void *ndata
655
671 const secp256k1_context *ctx,
672 const unsigned char *seckey
674
684 const secp256k1_context *ctx,
685 secp256k1_pubkey *pubkey,
686 const unsigned char *seckey
688
700 const secp256k1_context *ctx,
701 unsigned char *seckey
703
707 const secp256k1_context *ctx,
708 unsigned char *seckey
711
719 const secp256k1_context *ctx,
720 secp256k1_pubkey *pubkey
722
739 const secp256k1_context *ctx,
740 unsigned char *seckey,
741 const unsigned char *tweak32
743
747 const secp256k1_context *ctx,
748 unsigned char *seckey,
749 const unsigned char *tweak32
752
767 const secp256k1_context *ctx,
768 secp256k1_pubkey *pubkey,
769 const unsigned char *tweak32
771
786 const secp256k1_context *ctx,
787 unsigned char *seckey,
788 const unsigned char *tweak32
790
794 const secp256k1_context *ctx,
795 unsigned char *seckey,
796 const unsigned char *tweak32
799
812 const secp256k1_context *ctx,
813 secp256k1_pubkey *pubkey,
814 const unsigned char *tweak32
816
851 const unsigned char *seed32
853
864 const secp256k1_context *ctx,
866 const secp256k1_pubkey * const *ins,
867 size_t n
869
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
894
895#ifdef __cplusplus
896}
897#endif
898
899#endif /* SECP256K1_H */
int flags
Definition: bitcoin-tx.cpp:536
const secp256k1_context * secp256k1_context_no_precomp
Definition: secp256k1.c:75
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:187
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by multiplying it by a tweak.
Definition: secp256k1.c:712
#define SECP256K1_ARG_NONNULL(_x)
Definition: secp256k1.h:176
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition: secp256k1.c:759
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(const secp256k1_context *ctx, secp256k1_pubkey *out, const secp256k1_pubkey *const *ins, size_t n) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Add a number of public keys together.
Definition: secp256k1.c:769
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a secret key in place.
Definition: secp256k1.c:622
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_default
A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979).
Definition: secp256k1.h:627
SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input64) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse an ECDSA signature in compact (64 bytes) format.
Definition: secp256k1.c:385
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:268
SECP256K1_API void secp256k1_context_set_error_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data) SECP256K1_ARG_NONNULL(1)
Set a callback function to be called when an internal consistency check fails.
Definition: secp256k1.c:211
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_pubkey *pubkey1, const secp256k1_pubkey *pubkey2) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compare two public keys using lexicographic (of compressed serialization) order.
Definition: secp256k1.c:291
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an elliptic curve secret key.
Definition: secp256k1.c:580
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:141
SECP256K1_API void secp256k1_context_set_illegal_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data) SECP256K1_ARG_NONNULL(1)
Set a callback function to be called when an illegal argument is passed to an API call.
Definition: secp256k1.c:199
#define SECP256K1_API
Definition: secp256k1.h:161
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
Definition: secp256k1.c:566
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:250
int(* secp256k1_nonce_function)(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
A pointer to a function to deterministically generate a nonce.
Definition: secp256k1.h:94
SECP256K1_API int secp256k1_ecdsa_signature_parse_der(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a DER ECDSA signature.
Definition: secp256k1.c:369
SECP256K1_API void secp256k1_selftest(void)
Perform basic self tests (to be used in conjunction with secp256k1_context_static)
Definition: secp256k1.c:86
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition: secp256k1.c:604
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_tagged_sha256(const secp256k1_context *ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5)
Compute a tagged hash as defined in BIP-340.
Definition: secp256k1.c:795
struct secp256k1_pubkey secp256k1_pubkey
Opaque data structure that holds a parsed and valid public key.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify an ECDSA signature.
Definition: secp256k1.c:450
#define SECP256K1_DEPRECATED(_msg)
Definition: secp256k1.h:187
SECP256K1_API int secp256k1_ecdsa_signature_normalize(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3)
Convert a signature to a normalized lower-S form.
Definition: secp256k1.c:431
SECP256K1_API secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT
Copy a secp256k1 context object (into dynamically allocated memory).
Definition: secp256k1.c:163
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a public key by adding tweak times the generator to it.
Definition: secp256k1.c:695
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.h:624
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:406
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a public key in place.
Definition: secp256k1.c:641
struct secp256k1_ecdsa_signature secp256k1_ecdsa_signature
Opaque data structure that holds a parsed ECDSA signature.
SECP256K1_API int secp256k1_ec_pubkey_sort(const secp256k1_context *ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Sort public keys using lexicographic (of compressed serialization) order.
Definition: secp256k1.c:323
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_DEPRECATED("Use secp256k1_ec_seckey_tweak_add instead")
Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED.
Definition: secp256k1.c:684
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_DEPRECATED("Use secp256k1_ec_seckey_negate instead")
Same as secp256k1_ec_seckey_negate, but DEPRECATED.
Definition: secp256k1.c:637
#define SECP256K1_WARN_UNUSED_RESULT
Definition: secp256k1.h:171
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:668
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a public key by multiplying it by a tweak value.
Definition: secp256k1.c:736
SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an ECDSA signature in compact (64 byte) format.
Definition: secp256k1.c:418
SECP256K1_API const secp256k1_context * secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition: secp256k1.h:233
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_DEPRECATED("Use secp256k1_ec_seckey_tweak_mul instead")
Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED.
Definition: secp256k1.c:732
Opaque data structure that holds a parsed ECDSA signature.
Definition: secp256k1.h:74
unsigned char data[64]
Definition: secp256k1.h:75
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61
unsigned char data[64]
Definition: secp256k1.h:62