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_SCHNORRSIG_H2#define SECP256K1_SCHNORRSIG_H4#include "secp256k1.h"5#include "secp256k1_extrakeys.h"7#ifdef __cplusplus8extern "C" {9#endif11/** This module implements a variant of Schnorr signatures compliant with12* Bitcoin Improvement Proposal 340 "Schnorr Signatures for secp256k1"13* (https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).14*/16/** A pointer to a function to deterministically generate a nonce.17*18* Same as secp256k1_nonce function with the exception of accepting an19* additional pubkey argument and not requiring an attempt argument. The pubkey20* argument can protect signature schemes with key-prefixed challenge hash21* inputs against reusing the nonce when signing with the wrong precomputed22* pubkey.23*24* Returns: 1 if a nonce was successfully generated. 0 will cause signing to25* return an error.26* Out: nonce32: pointer to a 32-byte array to be filled by the function27* In: msg: the message being verified. Is NULL if and only if msglen28* is 0.29* msglen: the length of the message30* key32: pointer to a 32-byte secret key (will not be NULL)31* xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key3232* (will not be NULL)33* algo: pointer to an array describing the signature34* algorithm (will not be NULL)35* algolen: the length of the algo array36* data: arbitrary data pointer that is passed through37*38* Except for test cases, this function should compute some cryptographic hash of39* the message, the key, the pubkey, the algorithm description, and data.40*/41typedef int (*secp256k1_nonce_function_hardened)(42unsigned char *nonce32,43const unsigned char *msg,44size_t msglen,45const unsigned char *key32,46const unsigned char *xonly_pk32,47const unsigned char *algo,48size_t algolen,49void *data50);52/** An implementation of the nonce generation function as defined in Bitcoin53* Improvement Proposal 340 "Schnorr Signatures for secp256k1"54* (https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).55*56* If a data pointer is passed, it is assumed to be a pointer to 32 bytes of57* auxiliary random data as defined in BIP-340. If the data pointer is NULL,58* the nonce derivation procedure follows BIP-340 by setting the auxiliary59* random data to zero. The algo argument must be non-NULL, otherwise the60* function will fail and return 0. The hash will be tagged with algo.61* Therefore, to create BIP-340 compliant signatures, algo must be set to62* "BIP0340/nonce" and algolen to 13.63*/64SECP256K1_API const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340;66/** Data structure that contains additional arguments for schnorrsig_sign_custom.67*68* A schnorrsig_extraparams structure object can be initialized correctly by69* setting it to SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT.70*71* Members:72* magic: set to SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC at initialization73* and has no other function than making sure the object is74* initialized.75* noncefp: pointer to a nonce generation function. If NULL,76* secp256k1_nonce_function_bip340 is used77* ndata: pointer to arbitrary data used by the nonce generation function78* (can be NULL). If it is non-NULL and79* secp256k1_nonce_function_bip340 is used, then ndata must be a80* pointer to 32-byte auxiliary randomness as per BIP-340.81*/82typedef struct secp256k1_schnorrsig_extraparams {83unsigned char magic[4];84secp256k1_nonce_function_hardened noncefp;85void *ndata;86} secp256k1_schnorrsig_extraparams;88#define SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC { 0xda, 0x6f, 0xb3, 0x8c }89#define SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT {\90SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC,\91NULL,\92NULL\93}95/** Create a Schnorr signature.96*97* Does _not_ strictly follow BIP-340 because it does not verify the resulting98* signature. Instead, you can manually use secp256k1_schnorrsig_verify and99* abort if it fails.100*101* This function only signs 32-byte messages. If you have messages of a102* different size (or the same size but without a context-specific tag103* prefix), it is recommended to create a 32-byte message hash with104* secp256k1_tagged_sha256 and then sign the hash. Tagged hashing allows105* providing an context-specific tag for domain separation. This prevents106* signatures from being valid in multiple contexts by accident.107*108* Returns 1 on success, 0 on failure.109* Args: ctx: pointer to a context object (not secp256k1_context_static).110* Out: sig64: pointer to a 64-byte array to store the serialized signature.111* In: msg32: the 32-byte message being signed.112* keypair: pointer to an initialized keypair.113* aux_rand32: 32 bytes of fresh randomness. While recommended to provide114* this, it is only supplemental to security and can be NULL. A115* NULL argument is treated the same as an all-zero one. See116* BIP-340 "Default Signing" for a full explanation of this117* argument and for guidance if randomness is expensive.118*/119SECP256K1_API int secp256k1_schnorrsig_sign32(120const secp256k1_context *ctx,121unsigned char *sig64,122const unsigned char *msg32,123const secp256k1_keypair *keypair,124const unsigned char *aux_rand32125) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);127/** Create a Schnorr signature with a more flexible API.128*129* Same arguments as secp256k1_schnorrsig_sign32 except that it allows signing130* variable length messages and accepts a pointer to an extraparams object that131* allows customizing signing by passing additional arguments.132*133* Equivalent to secp256k1_schnorrsig_sign32(..., aux_rand32) if msglen is 32134* and extraparams is initialized as follows:135* ```136* secp256k1_schnorrsig_extraparams extraparams = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;137* extraparams.ndata = (unsigned char*)aux_rand32;138* ```139*140* Returns 1 on success, 0 on failure.141* Args: ctx: pointer to a context object (not secp256k1_context_static).142* Out: sig64: pointer to a 64-byte array to store the serialized signature.143* In: msg: the message being signed. Can only be NULL if msglen is 0.144* msglen: length of the message.145* keypair: pointer to an initialized keypair.146* extraparams: pointer to an extraparams object (can be NULL).147*/148SECP256K1_API int secp256k1_schnorrsig_sign_custom(149const secp256k1_context *ctx,150unsigned char *sig64,151const unsigned char *msg,152size_t msglen,153const secp256k1_keypair *keypair,154secp256k1_schnorrsig_extraparams *extraparams155) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5);157/** Verify a Schnorr signature.158*159* Returns: 1: correct signature160* 0: incorrect signature161* Args: ctx: pointer to a context object.162* In: sig64: pointer to the 64-byte signature to verify.163* msg: the message being verified. Can only be NULL if msglen is 0.164* msglen: length of the message165* pubkey: pointer to an x-only public key to verify with166*/167SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(168const secp256k1_context *ctx,169const unsigned char *sig64,170const unsigned char *msg,171size_t msglen,172const secp256k1_xonly_pubkey *pubkey173) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5);175#ifdef __cplusplus176}177#endif179#endif /* SECP256K1_SCHNORRSIG_H */
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.