You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

secp256k1_schnorrsig.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef SECP256K1_SCHNORRSIG_H
  2. #define SECP256K1_SCHNORRSIG_H
  3. #include "secp256k1.h"
  4. #include "secp256k1_extrakeys.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /** This module implements a variant of Schnorr signatures compliant with
  9. * Bitcoin Improvement Proposal 340 "Schnorr Signatures for secp256k1"
  10. * (https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).
  11. */
  12. /** A pointer to a function to deterministically generate a nonce.
  13. *
  14. * Same as secp256k1_nonce function with the exception of accepting an
  15. * additional pubkey argument and not requiring an attempt argument. The pubkey
  16. * argument can protect signature schemes with key-prefixed challenge hash
  17. * inputs against reusing the nonce when signing with the wrong precomputed
  18. * pubkey.
  19. *
  20. * Returns: 1 if a nonce was successfully generated. 0 will cause signing to
  21. * return an error.
  22. * Out: nonce32: pointer to a 32-byte array to be filled by the function.
  23. * In: msg32: the 32-byte message hash being verified (will not be NULL)
  24. * key32: pointer to a 32-byte secret key (will not be NULL)
  25. * xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32
  26. * (will not be NULL)
  27. * algo16: pointer to a 16-byte array describing the signature
  28. * algorithm (will not be NULL).
  29. * data: Arbitrary data pointer that is passed through.
  30. *
  31. * Except for test cases, this function should compute some cryptographic hash of
  32. * the message, the key, the pubkey, the algorithm description, and data.
  33. */
  34. typedef int (*secp256k1_nonce_function_hardened)(
  35. unsigned char *nonce32,
  36. const unsigned char *msg32,
  37. const unsigned char *key32,
  38. const unsigned char *xonly_pk32,
  39. const unsigned char *algo16,
  40. void *data
  41. );
  42. /** An implementation of the nonce generation function as defined in Bitcoin
  43. * Improvement Proposal 340 "Schnorr Signatures for secp256k1"
  44. * (https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki).
  45. *
  46. * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
  47. * auxiliary random data as defined in BIP-340. If the data pointer is NULL,
  48. * schnorrsig_sign does not produce BIP-340 compliant signatures. The algo16
  49. * argument must be non-NULL, otherwise the function will fail and return 0.
  50. * The hash will be tagged with algo16 after removing all terminating null
  51. * bytes. Therefore, to create BIP-340 compliant signatures, algo16 must be set
  52. * to "BIP0340/nonce\0\0\0"
  53. */
  54. SECP256K1_API extern const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340;
  55. /** Create a Schnorr signature.
  56. *
  57. * Does _not_ strictly follow BIP-340 because it does not verify the resulting
  58. * signature. Instead, you can manually use secp256k1_schnorrsig_verify and
  59. * abort if it fails.
  60. *
  61. * Otherwise BIP-340 compliant if the noncefp argument is NULL or
  62. * secp256k1_nonce_function_bip340 and the ndata argument is 32-byte auxiliary
  63. * randomness.
  64. *
  65. * Returns 1 on success, 0 on failure.
  66. * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
  67. * Out: sig64: pointer to a 64-byte array to store the serialized signature (cannot be NULL)
  68. * In: msg32: the 32-byte message being signed (cannot be NULL)
  69. * keypair: pointer to an initialized keypair (cannot be NULL)
  70. * noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_bip340 is used
  71. * ndata: pointer to arbitrary data used by the nonce generation
  72. * function (can be NULL). If it is non-NULL and
  73. * secp256k1_nonce_function_bip340 is used, then ndata must be a
  74. * pointer to 32-byte auxiliary randomness as per BIP-340.
  75. */
  76. SECP256K1_API int secp256k1_schnorrsig_sign(
  77. const secp256k1_context* ctx,
  78. unsigned char *sig64,
  79. const unsigned char *msg32,
  80. const secp256k1_keypair *keypair,
  81. secp256k1_nonce_function_hardened noncefp,
  82. void *ndata
  83. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  84. /** Verify a Schnorr signature.
  85. *
  86. * Returns: 1: correct signature
  87. * 0: incorrect signature
  88. * Args: ctx: a secp256k1 context object, initialized for verification.
  89. * In: sig64: pointer to the 64-byte signature to verify (cannot be NULL)
  90. * msg32: the 32-byte message being verified (cannot be NULL)
  91. * pubkey: pointer to an x-only public key to verify with (cannot be NULL)
  92. */
  93. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(
  94. const secp256k1_context* ctx,
  95. const unsigned char *sig64,
  96. const unsigned char *msg32,
  97. const secp256k1_xonly_pubkey *pubkey
  98. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /* SECP256K1_SCHNORRSIG_H */