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.
501* pubkey2: second public key to compare502*/503SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(504const secp256k1_context *ctx,505const secp256k1_pubkey *pubkey1,506const secp256k1_pubkey *pubkey2507) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);509/** Sort public keys using lexicographic (of compressed serialization) order510*511* Returns: 0 if the arguments are invalid. 1 otherwise.512*513* Args: ctx: pointer to a context object514* In: pubkeys: array of pointers to pubkeys to sort515* n_pubkeys: number of elements in the pubkeys array516*/517SECP256K1_API int secp256k1_ec_pubkey_sort(518const secp256k1_context *ctx,519const secp256k1_pubkey **pubkeys,520size_t n_pubkeys521) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);523/** Parse an ECDSA signature in compact (64 bytes) format.524*525* Returns: 1 when the signature could be parsed, 0 otherwise.526* Args: ctx: pointer to a context object527* Out: sig: pointer to a signature object528* In: input64: pointer to the 64-byte array to parse529*530* The signature must consist of a 32-byte big endian R value, followed by a531* 32-byte big endian S value. If R or S fall outside of [0..order-1], the532* encoding is invalid. R and S with value 0 are allowed in the encoding.533*534* After the call, sig will always be initialized. If parsing failed or R or535* S are zero, the resulting sig value is guaranteed to fail verification for536* any message and public key.537*/538SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(539const secp256k1_context *ctx,540secp256k1_ecdsa_signature *sig,541const unsigned char *input64542) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);544/** Parse a DER ECDSA signature.545*546* Returns: 1 when the signature could be parsed, 0 otherwise.547* Args: ctx: pointer to a context object548* Out: sig: pointer to a signature object549* In: input: pointer to the signature to be parsed550* inputlen: the length of the array pointed to be input551*552* This function will accept any valid DER encoded signature, even if the553* encoded numbers are out of range.554*555* After the call, sig will always be initialized. If parsing failed or the556* encoded numbers are out of range, signature verification with it is557* guaranteed to fail for every message and public key.558*/559SECP256K1_API int secp256k1_ecdsa_signature_parse_der(560const secp256k1_context *ctx,561secp256k1_ecdsa_signature *sig,562const unsigned char *input,563size_t inputlen564) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);566/** Serialize an ECDSA signature in DER format.567*568* Returns: 1 if enough space was available to serialize, 0 otherwise569* Args: ctx: pointer to a context object570* Out: output: pointer to an array to store the DER serialization571* In/Out: outputlen: pointer to a length integer. Initially, this integer572* should be set to the length of output. After the call573* it will be set to the length of the serialization (even574* if 0 was returned).575* In: sig: pointer to an initialized signature object576*/577SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(578const secp256k1_context *ctx,579unsigned char *output,580size_t *outputlen,581const secp256k1_ecdsa_signature *sig582) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);584/** Serialize an ECDSA signature in compact (64 byte) format.585*586* Returns: 1587* Args: ctx: pointer to a context object588* Out: output64: pointer to a 64-byte array to store the compact serialization589* In: sig: pointer to an initialized signature object590*591* See secp256k1_ecdsa_signature_parse_compact for details about the encoding.592*/593SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(594const secp256k1_context *ctx,595unsigned char *output64,596const secp256k1_ecdsa_signature *sig597) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);599/** Verify an ECDSA signature.600*601* Returns: 1: correct signature602* 0: incorrect or unparseable signature603* Args: ctx: pointer to a context object604* In: sig: the signature being verified.605* msghash32: the 32-byte message hash being verified.606* The verifier must make sure to apply a cryptographic607* hash function to the message by itself and not accept an608* msghash32 value directly. Otherwise, it would be easy to609* create a "valid" signature without knowledge of the610* secret key. See also611* https://bitcoin.stackexchange.com/a/81116/35586 for more612* background on this topic.613* pubkey: pointer to an initialized public key to verify with.614*615* To avoid accepting malleable signatures, only ECDSA signatures in lower-S616* form are accepted.617*618* If you need to accept ECDSA signatures from sources that do not obey this619* rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to620* verification, but be aware that doing so results in malleable signatures.621*622* For details, see the comments for that function.623*/624SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(625const secp256k1_context *ctx,626const secp256k1_ecdsa_signature *sig,627const unsigned char *msghash32,628const secp256k1_pubkey *pubkey629) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);631/** Convert a signature to a normalized lower-S form.632*633* Returns: 1 if sigin was not normalized, 0 if it already was.634* Args: ctx: pointer to a context object635* Out: sigout: pointer to a signature to fill with the normalized form,636* or copy if the input was already normalized. (can be NULL if637* you're only interested in whether the input was already638* normalized).639* In: sigin: pointer to a signature to check/normalize (can be identical to sigout)640*641* With ECDSA a third-party can forge a second distinct signature of the same642* message, given a single initial signature, but without knowing the key. This643* is done by negating the S value modulo the order of the curve, 'flipping'644* the sign of the random point R which is not included in the signature.645*646* Forgery of the same message isn't universally problematic, but in systems647* where message malleability or uniqueness of signatures is important this can648* cause issues. This forgery can be blocked by all verifiers forcing signers649* to use a normalized form.650*651* The lower-S form reduces the size of signatures slightly on average when652* variable length encodings (such as DER) are used and is cheap to verify,653* making it a good choice. Security of always using lower-S is assured because654* anyone can trivially modify a signature after the fact to enforce this655* property anyway.656*657* The lower S value is always between 0x1 and658* 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,659* inclusive.660*661* No other forms of ECDSA malleability are known and none seem likely, but662* there is no formal proof that ECDSA, even with this additional restriction,663* is free of other malleability. Commonly used serialization schemes will also664* accept various non-unique encodings, so care should be taken when this665* property is required for an application.666*667* The secp256k1_ecdsa_sign function will by default create signatures in the668* lower-S form, and secp256k1_ecdsa_verify will not accept others. In case669* signatures come from a system that cannot enforce this property,670* secp256k1_ecdsa_signature_normalize must be called before verification.671*/672SECP256K1_API int secp256k1_ecdsa_signature_normalize(673const secp256k1_context *ctx,674secp256k1_ecdsa_signature *sigout,675const secp256k1_ecdsa_signature *sigin676) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);678/** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.679* If a data pointer is passed, it is assumed to be a pointer to 32 bytes of680* extra entropy.681*/682SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;684/** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */685SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_default;687/** Create an ECDSA signature.688*689* Returns: 1: signature created690* 0: the nonce generation function failed, or the secret key was invalid.691* Args: ctx: pointer to a context object (not secp256k1_context_static).692* Out: sig: pointer to a signature object.693* In: msghash32: the 32-byte message hash being signed.694* seckey: pointer to a 32-byte secret key.695* noncefp: pointer to a nonce generation function. If NULL,696* secp256k1_nonce_function_default is used.697* ndata: pointer to arbitrary data used by the nonce generation function698* (can be NULL). If it is non-NULL and699* secp256k1_nonce_function_default is used, then ndata must be a700* pointer to 32-bytes of additional data.701*702* The created signature is always in lower-S form. See703* secp256k1_ecdsa_signature_normalize for more details.704*/705SECP256K1_API int secp256k1_ecdsa_sign(706const secp256k1_context *ctx,707secp256k1_ecdsa_signature *sig,708const unsigned char *msghash32,709const unsigned char *seckey,710secp256k1_nonce_function noncefp,711const void *ndata712) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);714/** Verify an elliptic curve secret key.715*716* A secret key is valid if it is not 0 and less than the secp256k1 curve order717* when interpreted as an integer (most significant byte first). The718* probability of choosing a 32-byte string uniformly at random which is an719* invalid secret key is negligible. However, if it does happen it should720* be assumed that the randomness source is severely broken and there should721* be no retry.722*723* Returns: 1: secret key is valid724* 0: secret key is invalid725* Args: ctx: pointer to a context object.726* In: seckey: pointer to a 32-byte secret key.727*/728SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(729const secp256k1_context *ctx,730const unsigned char *seckey731) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);733/** Compute the public key for a secret key.734*735* Returns: 1: secret was valid, public key stores.736* 0: secret was invalid, try again.737* Args: ctx: pointer to a context object (not secp256k1_context_static).738* Out: pubkey: pointer to the created public key.739* In: seckey: pointer to a 32-byte secret key.740*/741SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(742const secp256k1_context *ctx,743secp256k1_pubkey *pubkey,744const unsigned char *seckey745) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);747/** Negates a secret key in place.748*749* Returns: 0 if the given secret key is invalid according to750* secp256k1_ec_seckey_verify. 1 otherwise
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.