Bitcoin Core 31.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#include <stdint.h>
10
52
62typedef struct secp256k1_pubkey {
63 unsigned char data[64];
65
76 unsigned char data[64];
78
96 unsigned char *nonce32,
97 const unsigned char *msg32,
98 const unsigned char *key32,
99 const unsigned char *algo16,
100 void *data,
101 unsigned int attempt
102);
103
104# if !defined(SECP256K1_GNUC_PREREQ)
105# if defined(__GNUC__)&&defined(__GNUC_MINOR__)
106# define SECP256K1_GNUC_PREREQ(_maj,_min) \
107 ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
108# else
109# define SECP256K1_GNUC_PREREQ(_maj,_min) 0
110# endif
111# endif
112
113/* When this header is used at build-time the SECP256K1_BUILD define needs to be set
114 * to correctly setup export attributes and nullness checks. This is normally done
115 * by secp256k1.c but to guard against this header being included before secp256k1.c
116 * has had a chance to set the define (e.g. via test harnesses that just includes
117 * secp256k1.c) we set SECP256K1_NO_BUILD when this header is processed without the
118 * BUILD define so this condition can be caught.
119 */
120#ifndef SECP256K1_BUILD
121# define SECP256K1_NO_BUILD
122#endif
123
124/* Symbol visibility. */
125#if !defined(SECP256K1_API) && defined(SECP256K1_NO_API_VISIBILITY_ATTRIBUTES)
126 /* The user has requested that we don't specify visibility attributes in
127 * the public API.
128 *
129 * Since all our non-API declarations use the static qualifier, this means
130 * that the user can use -fvisibility=<value> to set the visibility of the
131 * API symbols. For instance, -fvisibility=hidden can be useful *even for
132 * the API symbols*, e.g., when building a static library which is linked
133 * into a shared library, and the latter should not re-export the
134 * libsecp256k1 API.
135 *
136 * While visibility is a concept that applies only to shared libraries,
137 * setting visibility will still make a difference when building a static
138 * library: the visibility settings will be stored in the static library,
139 * solely for the potential case that the static library will be linked into
140 * a shared library. In that case, the stored visibility settings will
141 * resurface and be honored for the shared library. */
142# define SECP256K1_API extern
143#endif
144#if !defined(SECP256K1_API)
145# if defined(SECP256K1_BUILD)
146 /* On Windows, assume a shared library only if explicitly requested.
147 * 1. If using Libtool, it defines DLL_EXPORT automatically.
148 * 2. In other cases, SECP256K1_DLL_EXPORT must be defined. */
149# if defined(_WIN32) && (defined(SECP256K1_DLL_EXPORT) || defined(DLL_EXPORT))
150 /* GCC for Windows (e.g., MinGW) accepts the __declspec syntax for
151 * MSVC compatibility. A __declspec declaration implies (but is not
152 * exactly equivalent to) __attribute__ ((visibility("default"))),
153 * and so we actually want __declspec even on GCC, see "Microsoft
154 * Windows Function Attributes" in the GCC manual and the
155 * recommendations in https://gcc.gnu.org/wiki/Visibility . */
156# define SECP256K1_API extern __declspec(dllexport)
157 /* Avoid __attribute__ ((visibility("default"))) on Windows to get rid
158 * of warnings when compiling with -flto due to a bug in GCC, see
159 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116478 . */
160# elif !defined(_WIN32) && defined (__GNUC__) && (__GNUC__ >= 4)
161# define SECP256K1_API extern __attribute__ ((visibility("default")))
162# else
163# define SECP256K1_API extern
164# endif
165# else
166 /* On Windows, SECP256K1_STATIC must be defined when consuming
167 * libsecp256k1 as a static library. Note that SECP256K1_STATIC is a
168 * "consumer-only" macro, and it has no meaning when building
169 * libsecp256k1. */
170# if defined(_WIN32) && !defined(SECP256K1_STATIC)
171# define SECP256K1_API extern __declspec(dllimport)
172# else
173# define SECP256K1_API extern
174# endif
175# endif
176#endif
177
178/* Warning attributes
179 * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
180 * some paranoid null checks. */
181# if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
182# define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
183# else
184# define SECP256K1_WARN_UNUSED_RESULT
185# endif
186# if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
187# define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
188# else
189# define SECP256K1_ARG_NONNULL(_x)
190# endif
191
192/* Attribute for marking functions, types, and variables as deprecated */
193#if !defined(SECP256K1_BUILD) && defined(__has_attribute)
194# if __has_attribute(__deprecated__)
195# define SECP256K1_DEPRECATED(_msg) __attribute__ ((__deprecated__(_msg)))
196# else
197# define SECP256K1_DEPRECATED(_msg)
198# endif
199#else
200# define SECP256K1_DEPRECATED(_msg)
201#endif
202
203/* All flags' lower 8 bits indicate what they're for. Do not use directly. */
204#define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
205#define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
206#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
207/* The higher bits contain the actual data. Do not use directly. */
208#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
209#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
210#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)
211#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
212
215#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
216
218#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
219#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
220
221/* Testing flag. Do not use. */
222#define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)
223
225#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
226#define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
227
229#define SECP256K1_TAG_PUBKEY_EVEN 0x02
230#define SECP256K1_TAG_PUBKEY_ODD 0x03
231#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
232#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
233#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
234
247
250SECP256K1_DEPRECATED("Use secp256k1_context_static instead");
251
269
270
297 unsigned int flags
299
313 const secp256k1_context *ctx
315
333
375 void (*fun)(const char *message, void *data),
376 const void *data
378
404 void (*fun)(const char *message, void *data),
405 const void *data
407
421 uint32_t *state,
422 const unsigned char *blocks64,
423 size_t n_blocks
424);
425
447
463 const secp256k1_context *ctx,
464 secp256k1_pubkey *pubkey,
465 const unsigned char *input,
466 size_t inputlen
468
485 const secp256k1_context *ctx,
486 unsigned char *output,
487 size_t *outputlen,
488 const secp256k1_pubkey *pubkey,
489 unsigned int flags
491
502 const secp256k1_context *ctx,
503 const secp256k1_pubkey *pubkey1,
504 const secp256k1_pubkey *pubkey2
506
516 const secp256k1_context *ctx,
517 const secp256k1_pubkey **pubkeys,
518 size_t n_pubkeys
520
537 const secp256k1_context *ctx,
539 const unsigned char *input64
541
558 const secp256k1_context *ctx,
560 const unsigned char *input,
561 size_t inputlen
563
576 const secp256k1_context *ctx,
577 unsigned char *output,
578 size_t *outputlen,
581
592 const secp256k1_context *ctx,
593 unsigned char *output64,
596
623 const secp256k1_context *ctx,
624 const secp256k1_ecdsa_signature *sig,
625 const unsigned char *msghash32,
626 const secp256k1_pubkey *pubkey
628
671 const secp256k1_context *ctx,
673 const secp256k1_ecdsa_signature *sigin
675
681
684
704 const secp256k1_context *ctx,
706 const unsigned char *msghash32,
707 const unsigned char *seckey,
709 const void *ndata
711
727 const secp256k1_context *ctx,
728 const unsigned char *seckey
730
740 const secp256k1_context *ctx,
741 secp256k1_pubkey *pubkey,
742 const unsigned char *seckey
744
756 const secp256k1_context *ctx,
757 unsigned char *seckey
759
767 const secp256k1_context *ctx,
768 secp256k1_pubkey *pubkey
770
787 const secp256k1_context *ctx,
788 unsigned char *seckey,
789 const unsigned char *tweak32
791
806 const secp256k1_context *ctx,
807 secp256k1_pubkey *pubkey,
808 const unsigned char *tweak32
810
825 const secp256k1_context *ctx,
826 unsigned char *seckey,
827 const unsigned char *tweak32
829
842 const secp256k1_context *ctx,
843 secp256k1_pubkey *pubkey,
844 const unsigned char *tweak32
846
881 const unsigned char *seed32
883
894 const secp256k1_context *ctx,
896 const secp256k1_pubkey * const *ins,
897 size_t n
899
917 const secp256k1_context *ctx,
918 unsigned char *hash32,
919 const unsigned char *tag,
920 size_t taglen,
921 const unsigned char *msg,
922 size_t msglen
924
925#ifdef __cplusplus
926}
927#endif
928
929#endif /* SECP256K1_H */
int flags
Definition: bitcoin-tx.cpp:530
const secp256k1_context *const secp256k1_context_no_precomp
Definition: secp256k1.c:77
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:190
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:739
#define SECP256K1_ARG_NONNULL(_x)
Definition: secp256k1.h:189
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:782
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:792
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:657
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:683
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:412
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:287
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:214
SECP256K1_API 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:672
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:313
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:615
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:144
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:202
#define SECP256K1_API
Definition: secp256k1.h:173
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:601
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:269
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:95
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:396
SECP256K1_API void secp256k1_selftest(void)
Perform basic self tests (to be used in conjunction with secp256k1_context_static)
Definition: secp256k1.c:88
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:639
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:477
void(* secp256k1_sha256_compression_function)(uint32_t *state, const unsigned char *blocks64, size_t n_blocks)
A pointer to a function implementing SHA256's internal compression function.
Definition: secp256k1.h:420
#define SECP256K1_DEPRECATED(_msg)
Definition: secp256k1.h:200
SECP256K1_API 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:818
SECP256K1_API const secp256k1_context *const secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition: secp256k1.h:246
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:458
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:166
SECP256K1_API void secp256k1_context_set_sha256_compression(secp256k1_context *ctx, secp256k1_sha256_compression_function fn_compression) SECP256K1_ARG_NONNULL(1)
Set a callback function to override the internal SHA256 compression function.
Definition: secp256k1.c:226
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:722
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.h:680
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:433
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:345
#define SECP256K1_WARN_UNUSED_RESULT
Definition: secp256k1.h:184
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:699
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:759
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:445
Opaque data structure that holds a parsed ECDSA signature.
Definition: secp256k1.h:75
unsigned char data[64]
Definition: secp256k1.h:76
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:62
unsigned char data[64]
Definition: secp256k1.h:63