Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#ifndef SECP256K1_H2#define SECP256K1_H4#ifdef __cplusplus5extern "C" {6#endif8#include <stddef.h>9#include <stdint.h>11/** Unless explicitly stated all pointer arguments must not be NULL.12*13* The following rules specify the order of arguments in API calls:14*15* 1. Context pointers go first, followed by output arguments, combined16* output/input arguments, and finally input-only arguments.17* 2. Array lengths always immediately follow the argument whose length18* they describe, even if this violates rule 1.19* 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated20* later go first. This means: signatures, public nonces, secret nonces,21* messages, public keys, secret keys, tweaks.22* 4. Arguments that are not data pointers go last, from more complex to less23* complex: function pointers, algorithm names, messages, void pointers,24* counts, flags, booleans.25* 5. Opaque data pointers follow the function pointer they are to be passed to.26*/28/** Opaque data structure that holds context information29*30* The primary purpose of context objects is to store randomization data for31* enhanced protection against side-channel leakage. This protection is only32* effective if the context is randomized after its creation. See33* secp256k1_context_create for creation of contexts and34* secp256k1_context_randomize for randomization.35*36* A secondary purpose of context objects is to store pointers to callback37* functions that the library will call when certain error states arise. See38* secp256k1_context_set_error_callback as well as39* secp256k1_context_set_illegal_callback for details. Future library versions40* may use context objects for additional purposes.41*42* A constructed context can safely be used from multiple threads43* simultaneously, but API calls that take a non-const pointer to a context44* need exclusive access to it. In particular this is the case for45* secp256k1_context_destroy, secp256k1_context_preallocated_destroy,46* and secp256k1_context_randomize.47*48* Regarding randomization, either do it once at creation time (in which case49* you do not need any locking for the other calls), or use a read-write lock.50*/51typedef struct secp256k1_context_struct secp256k1_context;53/** Opaque data structure that holds a parsed and valid public key.54*55* The exact representation of data inside is implementation defined and not56* guaranteed to be portable between different platforms or versions. It is57* however guaranteed to be 64 bytes in size, and can be safely copied/moved.58* If you need to convert to a format suitable for storage or transmission,59* use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse. To60* compare keys, use secp256k1_ec_pubkey_cmp.61*/62typedef struct secp256k1_pubkey {63unsigned char data[64];64} secp256k1_pubkey;66/** Opaque data structure that holds a parsed ECDSA signature.67*68* The exact representation of data inside is implementation defined and not69* guaranteed to be portable between different platforms or versions. It is70* however guaranteed to be 64 bytes in size, and can be safely copied/moved.71* If you need to convert to a format suitable for storage, transmission, or72* comparison, use the secp256k1_ecdsa_signature_serialize_* and73* secp256k1_ecdsa_signature_parse_* functions.74*/75typedef struct secp256k1_ecdsa_signature {76unsigned char data[64];77} secp256k1_ecdsa_signature;79/** A pointer to a function to deterministically generate a nonce.80*81* Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.82* Out: nonce32: pointer to a 32-byte array to be filled by the function.83* In: msg32: the 32-byte message hash being verified (will not be NULL)84* key32: pointer to a 32-byte secret key (will not be NULL)85* algo16: pointer to a 16-byte array describing the signature86* algorithm (will be NULL for ECDSA for compatibility).87* data: Arbitrary data pointer that is passed through.88* attempt: how many iterations we have tried to find a nonce.89* This will almost always be 0, but different attempt values90* are required to result in a different nonce.91*92* Except for test cases, this function should compute some cryptographic hash of93* the message, the algorithm, the key and the attempt.94*/95typedef int (*secp256k1_nonce_function)(96unsigned char *nonce32,97const unsigned char *msg32,98const unsigned char *key32,99const unsigned char *algo16,100void *data,101unsigned int attempt102);104/* When this header is used at build-time the SECP256K1_BUILD define needs to be set105* to correctly setup export attributes and nullness checks. This is normally done106* by secp256k1.c but to guard against this header being included before secp256k1.c107* has had a chance to set the define (e.g. via test harnesses that just includes108* secp256k1.c) we set SECP256K1_NO_BUILD when this header is processed without the109* BUILD define so this condition can be caught.110*/111#ifndef SECP256K1_BUILD112# define SECP256K1_NO_BUILD113#endif115/* Symbol visibility. */116#if !defined(SECP256K1_API) && defined(SECP256K1_NO_API_VISIBILITY_ATTRIBUTES)117/* The user has requested that we don't specify visibility attributes in118* the public API.119*120* Since all our non-API declarations use the static qualifier, this means121* that the user can use -fvisibility=<value> to set the visibility of the122* API symbols. For instance, -fvisibility=hidden can be useful *even for123* the API symbols*, e.g., when building a static library which is linked124* into a shared library, and the latter should not re-export the125* libsecp256k1 API.126*127* While visibility is a concept that applies only to shared libraries,128* setting visibility will still make a difference when building a static129* library: the visibility settings will be stored in the static library,130* solely for the potential case that the static library will be linked into131* a shared library. In that case, the stored visibility settings will132* resurface and be honored for the shared library. */133# define SECP256K1_API extern134#endif135#if !defined(SECP256K1_API)136# if defined(SECP256K1_BUILD)137/* On Windows, assume a shared library only if explicitly requested.138* 1. If using Libtool, it defines DLL_EXPORT automatically.139* 2. In other cases, SECP256K1_DLL_EXPORT must be defined. */140# if defined(_WIN32) && (defined(SECP256K1_DLL_EXPORT) || defined(DLL_EXPORT))141/* GCC for Windows (e.g., MinGW) accepts the __declspec syntax for142* MSVC compatibility. A __declspec declaration implies (but is not143* exactly equivalent to) __attribute__ ((visibility("default"))),144* and so we actually want __declspec even on GCC, see "Microsoft145* Windows Function Attributes" in the GCC manual and the146* recommendations in https://gcc.gnu.org/wiki/Visibility . */147# define SECP256K1_API extern __declspec(dllexport)148/* Avoid __attribute__ ((visibility("default"))) on Windows to get rid149* of warnings when compiling with -flto due to a bug in GCC, see150* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116478 . */151# elif !defined(_WIN32) && defined (__GNUC__) && (__GNUC__ >= 4)152# define SECP256K1_API extern __attribute__ ((visibility("default")))153# else154# define SECP256K1_API extern155# endif156# else157/* On Windows, SECP256K1_STATIC must be defined when consuming158* libsecp256k1 as a static library. Note that SECP256K1_STATIC is a159* "consumer-only" macro, and it has no meaning when building160* libsecp256k1. */161# if defined(_WIN32) && !defined(SECP256K1_STATIC)162# define SECP256K1_API extern __declspec(dllimport)163# else164# define SECP256K1_API extern165# endif166# endif167#endif169/* Warning attributes170* NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out171* some paranoid null checks. */172# if defined(__GNUC__)173# define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))174# else175# define SECP256K1_WARN_UNUSED_RESULT176# endif177# if !defined(SECP256K1_BUILD) && defined(__GNUC__)178# define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))179# else180# define SECP256K1_ARG_NONNULL(_x)181# endif183/* Attribute for marking functions, types, and variables as deprecated */184#if !defined(SECP256K1_BUILD) && defined(__has_attribute)185# if __has_attribute(__deprecated__)186# define SECP256K1_DEPRECATED(_msg) __attribute__ ((__deprecated__(_msg)))187# else188# define SECP256K1_DEPRECATED(_msg)189# endif190#else191# define SECP256K1_DEPRECATED(_msg)192#endif194/* All flags' lower 8 bits indicate what they're for. Do not use directly. */195#define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)196#define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)197#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)198/* The higher bits contain the actual data. Do not use directly. */199#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)200#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)201#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY (1 << 10)202#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)204/** Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and205* secp256k1_context_preallocated_create. */206#define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)208/** Deprecated context flags. These flags are treated equivalent to SECP256K1_CONTEXT_NONE. */209#define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)210#define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)212/* Testing flag. Do not use. */213#define SECP256K1_CONTEXT_DECLASSIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY)215/** Flag to pass to secp256k1_ec_pubkey_serialize. */216#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)217#define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)219/** Prefix byte used to tag various encoded curvepoints for specific purposes */220#define SECP256K1_TAG_PUBKEY_EVEN 0x02221#define SECP256K1_TAG_PUBKEY_ODD 0x03222#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04223#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06224#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07226/** A built-in constant secp256k1 context object with static storage duration, to be227* used in conjunction with secp256k1_selftest.228*229* This context object offers *only limited functionality* , i.e., it cannot be used230* for API functions that perform computations involving secret keys, e.g., signing231* and public key generation. If this restriction applies to a specific API function,232* it is mentioned in its documentation. See secp256k1_context_create if you need a233* full context object that supports all functionality offered by the library.234*235* It is highly recommended to call secp256k1_selftest before using this context.236*/237SECP256K1_API const secp256k1_context * const secp256k1_context_static;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 and242* 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 are244* very rudimentary and are not intended as a replacement for running the test245* 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 with249* secp256k1_context_create (or secp256k1_context_preallocated_create), which will250* take care of performing the self tests.
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.