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_extrakeys.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef SECP256K1_EXTRAKEYS_H
  2. #define SECP256K1_EXTRAKEYS_H
  3. #include "secp256k1.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /** Opaque data structure that holds a parsed and valid "x-only" public key.
  8. * An x-only pubkey encodes a point whose Y coordinate is even. It is
  9. * serialized using only its X coordinate (32 bytes). See BIP-340 for more
  10. * information about x-only pubkeys.
  11. *
  12. * The exact representation of data inside is implementation defined and not
  13. * guaranteed to be portable between different platforms or versions. It is
  14. * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
  15. * If you need to convert to a format suitable for storage, transmission, or
  16. * comparison, use secp256k1_xonly_pubkey_serialize and
  17. * secp256k1_xonly_pubkey_parse.
  18. */
  19. typedef struct {
  20. unsigned char data[64];
  21. } secp256k1_xonly_pubkey;
  22. /** Opaque data structure that holds a keypair consisting of a secret and a
  23. * public key.
  24. *
  25. * The exact representation of data inside is implementation defined and not
  26. * guaranteed to be portable between different platforms or versions. It is
  27. * however guaranteed to be 96 bytes in size, and can be safely copied/moved.
  28. */
  29. typedef struct {
  30. unsigned char data[96];
  31. } secp256k1_keypair;
  32. /** Parse a 32-byte sequence into a xonly_pubkey object.
  33. *
  34. * Returns: 1 if the public key was fully valid.
  35. * 0 if the public key could not be parsed or is invalid.
  36. *
  37. * Args: ctx: a secp256k1 context object (cannot be NULL).
  38. * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
  39. * parsed version of input. If not, it's set to an invalid value.
  40. * (cannot be NULL).
  41. * In: input32: pointer to a serialized xonly_pubkey (cannot be NULL)
  42. */
  43. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse(
  44. const secp256k1_context* ctx,
  45. secp256k1_xonly_pubkey* pubkey,
  46. const unsigned char *input32
  47. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  48. /** Serialize an xonly_pubkey object into a 32-byte sequence.
  49. *
  50. * Returns: 1 always.
  51. *
  52. * Args: ctx: a secp256k1 context object (cannot be NULL).
  53. * Out: output32: a pointer to a 32-byte array to place the serialized key in
  54. * (cannot be NULL).
  55. * In: pubkey: a pointer to a secp256k1_xonly_pubkey containing an
  56. * initialized public key (cannot be NULL).
  57. */
  58. SECP256K1_API int secp256k1_xonly_pubkey_serialize(
  59. const secp256k1_context* ctx,
  60. unsigned char *output32,
  61. const secp256k1_xonly_pubkey* pubkey
  62. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  63. /** Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
  64. *
  65. * Returns: 1 if the public key was successfully converted
  66. * 0 otherwise
  67. *
  68. * Args: ctx: pointer to a context object (cannot be NULL)
  69. * Out: xonly_pubkey: pointer to an x-only public key object for placing the
  70. * converted public key (cannot be NULL)
  71. * pk_parity: pointer to an integer that will be set to 1 if the point
  72. * encoded by xonly_pubkey is the negation of the pubkey and
  73. * set to 0 otherwise. (can be NULL)
  74. * In: pubkey: pointer to a public key that is converted (cannot be NULL)
  75. */
  76. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey(
  77. const secp256k1_context* ctx,
  78. secp256k1_xonly_pubkey *xonly_pubkey,
  79. int *pk_parity,
  80. const secp256k1_pubkey *pubkey
  81. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
  82. /** Tweak an x-only public key by adding the generator multiplied with tweak32
  83. * to it.
  84. *
  85. * Note that the resulting point can not in general be represented by an x-only
  86. * pubkey because it may have an odd Y coordinate. Instead, the output_pubkey
  87. * is a normal secp256k1_pubkey.
  88. *
  89. * Returns: 0 if the arguments are invalid or the resulting public key would be
  90. * invalid (only when the tweak is the negation of the corresponding
  91. * secret key). 1 otherwise.
  92. *
  93. * Args: ctx: pointer to a context object initialized for verification
  94. * (cannot be NULL)
  95. * Out: output_pubkey: pointer to a public key to store the result. Will be set
  96. * to an invalid value if this function returns 0 (cannot
  97. * be NULL)
  98. * In: internal_pubkey: pointer to an x-only pubkey to apply the tweak to.
  99. * (cannot be NULL).
  100. * tweak32: pointer to a 32-byte tweak. If the tweak is invalid
  101. * according to secp256k1_ec_seckey_verify, this function
  102. * returns 0. For uniformly random 32-byte arrays the
  103. * chance of being invalid is negligible (around 1 in
  104. * 2^128) (cannot be NULL).
  105. */
  106. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add(
  107. const secp256k1_context* ctx,
  108. secp256k1_pubkey *output_pubkey,
  109. const secp256k1_xonly_pubkey *internal_pubkey,
  110. const unsigned char *tweak32
  111. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  112. /** Checks that a tweaked pubkey is the result of calling
  113. * secp256k1_xonly_pubkey_tweak_add with internal_pubkey and tweak32.
  114. *
  115. * The tweaked pubkey is represented by its 32-byte x-only serialization and
  116. * its pk_parity, which can both be obtained by converting the result of
  117. * tweak_add to a secp256k1_xonly_pubkey.
  118. *
  119. * Note that this alone does _not_ verify that the tweaked pubkey is a
  120. * commitment. If the tweak is not chosen in a specific way, the tweaked pubkey
  121. * can easily be the result of a different internal_pubkey and tweak.
  122. *
  123. * Returns: 0 if the arguments are invalid or the tweaked pubkey is not the
  124. * result of tweaking the internal_pubkey with tweak32. 1 otherwise.
  125. * Args: ctx: pointer to a context object initialized for verification
  126. * (cannot be NULL)
  127. * In: tweaked_pubkey32: pointer to a serialized xonly_pubkey (cannot be NULL)
  128. * tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
  129. * is passed in as tweaked_pubkey32). This must match the
  130. * pk_parity value that is returned when calling
  131. * secp256k1_xonly_pubkey with the tweaked pubkey, or
  132. * this function will fail.
  133. * internal_pubkey: pointer to an x-only public key object to apply the
  134. * tweak to (cannot be NULL)
  135. * tweak32: pointer to a 32-byte tweak (cannot be NULL)
  136. */
  137. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_check(
  138. const secp256k1_context* ctx,
  139. const unsigned char *tweaked_pubkey32,
  140. int tweaked_pk_parity,
  141. const secp256k1_xonly_pubkey *internal_pubkey,
  142. const unsigned char *tweak32
  143. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
  144. /** Compute the keypair for a secret key.
  145. *
  146. * Returns: 1: secret was valid, keypair is ready to use
  147. * 0: secret was invalid, try again with a different secret
  148. * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
  149. * Out: keypair: pointer to the created keypair (cannot be NULL)
  150. * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
  151. */
  152. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(
  153. const secp256k1_context* ctx,
  154. secp256k1_keypair *keypair,
  155. const unsigned char *seckey
  156. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  157. /** Get the public key from a keypair.
  158. *
  159. * Returns: 0 if the arguments are invalid. 1 otherwise.
  160. * Args: ctx: pointer to a context object (cannot be NULL)
  161. * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to
  162. * the keypair public key. If not, it's set to an invalid value.
  163. * (cannot be NULL)
  164. * In: keypair: pointer to a keypair (cannot be NULL)
  165. */
  166. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub(
  167. const secp256k1_context* ctx,
  168. secp256k1_pubkey *pubkey,
  169. const secp256k1_keypair *keypair
  170. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  171. /** Get the x-only public key from a keypair.
  172. *
  173. * This is the same as calling secp256k1_keypair_pub and then
  174. * secp256k1_xonly_pubkey_from_pubkey.
  175. *
  176. * Returns: 0 if the arguments are invalid. 1 otherwise.
  177. * Args: ctx: pointer to a context object (cannot be NULL)
  178. * Out: pubkey: pointer to an xonly_pubkey object. If 1 is returned, it is set
  179. * to the keypair public key after converting it to an
  180. * xonly_pubkey. If not, it's set to an invalid value (cannot be
  181. * NULL).
  182. * pk_parity: pointer to an integer that will be set to the pk_parity
  183. * argument of secp256k1_xonly_pubkey_from_pubkey (can be NULL).
  184. * In: keypair: pointer to a keypair (cannot be NULL)
  185. */
  186. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub(
  187. const secp256k1_context* ctx,
  188. secp256k1_xonly_pubkey *pubkey,
  189. int *pk_parity,
  190. const secp256k1_keypair *keypair
  191. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4);
  192. /** Tweak a keypair by adding tweak32 to the secret key and updating the public
  193. * key accordingly.
  194. *
  195. * Calling this function and then secp256k1_keypair_pub results in the same
  196. * public key as calling secp256k1_keypair_xonly_pub and then
  197. * secp256k1_xonly_pubkey_tweak_add.
  198. *
  199. * Returns: 0 if the arguments are invalid or the resulting keypair would be
  200. * invalid (only when the tweak is the negation of the keypair's
  201. * secret key). 1 otherwise.
  202. *
  203. * Args: ctx: pointer to a context object initialized for verification
  204. * (cannot be NULL)
  205. * In/Out: keypair: pointer to a keypair to apply the tweak to. Will be set to
  206. * an invalid value if this function returns 0 (cannot be
  207. * NULL).
  208. * In: tweak32: pointer to a 32-byte tweak. If the tweak is invalid according
  209. * to secp256k1_ec_seckey_verify, this function returns 0. For
  210. * uniformly random 32-byte arrays the chance of being invalid
  211. * is negligible (around 1 in 2^128) (cannot be NULL).
  212. */
  213. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(
  214. const secp256k1_context* ctx,
  215. secp256k1_keypair *keypair,
  216. const unsigned char *tweak32
  217. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. #endif /* SECP256K1_EXTRAKEYS_H */