secp256k1.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #ifndef _SECP256K1_
  2. # define _SECP256K1_
  3. # ifdef __cplusplus
  4. extern "C" {
  5. # endif
  6. #include <stddef.h>
  7. /* These rules specify the order of arguments in API calls:
  8. *
  9. * 1. Context pointers go first, followed by output arguments, combined
  10. * output/input arguments, and finally input-only arguments.
  11. * 2. Array lengths always immediately the follow the argument whose length
  12. * they describe, even if this violates rule 1.
  13. * 3. Within the OUT/OUTIN/IN groups, pointers to data that is typically generated
  14. * later go first. This means: signatures, public nonces, private nonces,
  15. * messages, public keys, secret keys, tweaks.
  16. * 4. Arguments that are not data pointers go last, from more complex to less
  17. * complex: function pointers, algorithm names, messages, void pointers,
  18. * counts, flags, booleans.
  19. * 5. Opaque data pointers follow the function pointer they are to be passed to.
  20. */
  21. /** Opaque data structure that holds context information (precomputed tables etc.).
  22. *
  23. * The purpose of context structures is to cache large precomputed data tables
  24. * that are expensive to construct, and also to maintain the randomization data
  25. * for blinding.
  26. *
  27. * Do not create a new context object for each operation, as construction is
  28. * far slower than all other API calls (~100 times slower than an ECDSA
  29. * verification).
  30. *
  31. * A constructed context can safely be used from multiple threads
  32. * simultaneously, but API call that take a non-const pointer to a context
  33. * need exclusive access to it. In particular this is the case for
  34. * secp256k1_context_destroy and secp256k1_context_randomize.
  35. *
  36. * Regarding randomization, either do it once at creation time (in which case
  37. * you do not need any locking for the other calls), or use a read-write lock.
  38. */
  39. typedef struct secp256k1_context_struct secp256k1_context;
  40. /** Opaque data structure that holds a parsed and valid public key.
  41. *
  42. * The exact representation of data inside is implementation defined and not
  43. * guaranteed to be portable between different platforms or versions. It is
  44. * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
  45. * If you need to convert to a format suitable for storage, transmission, or
  46. * comparison, use secp256k1_ec_pubkey_serialize and secp256k1_ec_pubkey_parse.
  47. */
  48. typedef struct {
  49. unsigned char data[64];
  50. } secp256k1_pubkey;
  51. /** Opaque data structured that holds a parsed ECDSA signature.
  52. *
  53. * The exact representation of data inside is implementation defined and not
  54. * guaranteed to be portable between different platforms or versions. It is
  55. * however guaranteed to be 64 bytes in size, and can be safely copied/moved.
  56. * If you need to convert to a format suitable for storage, transmission, or
  57. * comparison, use the secp256k1_ecdsa_signature_serialize_* and
  58. * secp256k1_ecdsa_signature_serialize_* functions.
  59. */
  60. typedef struct {
  61. unsigned char data[64];
  62. } secp256k1_ecdsa_signature;
  63. /** A pointer to a function to deterministically generate a nonce.
  64. *
  65. * Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
  66. * Out: nonce32: pointer to a 32-byte array to be filled by the function.
  67. * In: msg32: the 32-byte message hash being verified (will not be NULL)
  68. * key32: pointer to a 32-byte secret key (will not be NULL)
  69. * algo16: pointer to a 16-byte array describing the signature
  70. * algorithm (will be NULL for ECDSA for compatibility).
  71. * data: Arbitrary data pointer that is passed through.
  72. * attempt: how many iterations we have tried to find a nonce.
  73. * This will almost always be 0, but different attempt values
  74. * are required to result in a different nonce.
  75. *
  76. * Except for test cases, this function should compute some cryptographic hash of
  77. * the message, the algorithm, the key and the attempt.
  78. */
  79. typedef int (*secp256k1_nonce_function)(
  80. unsigned char *nonce32,
  81. const unsigned char *msg32,
  82. const unsigned char *key32,
  83. const unsigned char *algo16,
  84. void *data,
  85. unsigned int attempt
  86. );
  87. # if !defined(SECP256K1_GNUC_PREREQ)
  88. # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
  89. # define SECP256K1_GNUC_PREREQ(_maj,_min) \
  90. ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
  91. # else
  92. # define SECP256K1_GNUC_PREREQ(_maj,_min) 0
  93. # endif
  94. # endif
  95. # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
  96. # if SECP256K1_GNUC_PREREQ(2,7)
  97. # define SECP256K1_INLINE __inline__
  98. # elif (defined(_MSC_VER))
  99. # define SECP256K1_INLINE __inline
  100. # else
  101. # define SECP256K1_INLINE
  102. # endif
  103. # else
  104. # define SECP256K1_INLINE inline
  105. # endif
  106. #ifndef SECP256K1_API
  107. # if defined(_WIN32)
  108. # ifdef SECP256K1_BUILD
  109. # define SECP256K1_API __declspec(dllexport)
  110. # else
  111. # define SECP256K1_API
  112. # endif
  113. # elif defined(__GNUC__) && defined(SECP256K1_BUILD)
  114. # define SECP256K1_API __attribute__ ((visibility ("default")))
  115. # else
  116. # define SECP256K1_API
  117. # endif
  118. #endif
  119. /**Warning attributes
  120. * NONNULL is not used if SECP256K1_BUILD is set to avoid the compiler optimizing out
  121. * some paranoid null checks. */
  122. # if defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
  123. # define SECP256K1_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
  124. # else
  125. # define SECP256K1_WARN_UNUSED_RESULT
  126. # endif
  127. # if !defined(SECP256K1_BUILD) && defined(__GNUC__) && SECP256K1_GNUC_PREREQ(3, 4)
  128. # define SECP256K1_ARG_NONNULL(_x) __attribute__ ((__nonnull__(_x)))
  129. # else
  130. # define SECP256K1_ARG_NONNULL(_x)
  131. # endif
  132. /** All flags' lower 8 bits indicate what they're for. Do not use directly. */
  133. #define SECP256K1_FLAGS_TYPE_MASK ((1 << 8) - 1)
  134. #define SECP256K1_FLAGS_TYPE_CONTEXT (1 << 0)
  135. #define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
  136. /** The higher bits contain the actual data. Do not use directly. */
  137. #define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY (1 << 8)
  138. #define SECP256K1_FLAGS_BIT_CONTEXT_SIGN (1 << 9)
  139. #define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
  140. /** Flags to pass to secp256k1_context_create. */
  141. #define SECP256K1_CONTEXT_VERIFY (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_VERIFY)
  142. #define SECP256K1_CONTEXT_SIGN (SECP256K1_FLAGS_TYPE_CONTEXT | SECP256K1_FLAGS_BIT_CONTEXT_SIGN)
  143. #define SECP256K1_CONTEXT_NONE (SECP256K1_FLAGS_TYPE_CONTEXT)
  144. /** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */
  145. #define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
  146. #define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
  147. /** Create a secp256k1 context object.
  148. *
  149. * Returns: a newly created context object.
  150. * In: flags: which parts of the context to initialize.
  151. */
  152. SECP256K1_API secp256k1_context* secp256k1_context_create(
  153. unsigned int flags
  154. ) SECP256K1_WARN_UNUSED_RESULT;
  155. /** Copies a secp256k1 context object.
  156. *
  157. * Returns: a newly created context object.
  158. * Args: ctx: an existing context to copy (cannot be NULL)
  159. */
  160. SECP256K1_API secp256k1_context* secp256k1_context_clone(
  161. const secp256k1_context* ctx
  162. ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
  163. /** Destroy a secp256k1 context object.
  164. *
  165. * The context pointer may not be used afterwards.
  166. * Args: ctx: an existing context to destroy (cannot be NULL)
  167. */
  168. SECP256K1_API void secp256k1_context_destroy(
  169. secp256k1_context* ctx
  170. );
  171. /** Set a callback function to be called when an illegal argument is passed to
  172. * an API call. It will only trigger for violations that are mentioned
  173. * explicitly in the header.
  174. *
  175. * The philosophy is that these shouldn't be dealt with through a
  176. * specific return value, as calling code should not have branches to deal with
  177. * the case that this code itself is broken.
  178. *
  179. * On the other hand, during debug stage, one would want to be informed about
  180. * such mistakes, and the default (crashing) may be inadvisable.
  181. * When this callback is triggered, the API function called is guaranteed not
  182. * to cause a crash, though its return value and output arguments are
  183. * undefined.
  184. *
  185. * Args: ctx: an existing context object (cannot be NULL)
  186. * In: fun: a pointer to a function to call when an illegal argument is
  187. * passed to the API, taking a message and an opaque pointer
  188. * (NULL restores a default handler that calls abort).
  189. * data: the opaque pointer to pass to fun above.
  190. */
  191. SECP256K1_API void secp256k1_context_set_illegal_callback(
  192. secp256k1_context* ctx,
  193. void (*fun)(const char* message, void* data),
  194. const void* data
  195. ) SECP256K1_ARG_NONNULL(1);
  196. /** Set a callback function to be called when an internal consistency check
  197. * fails. The default is crashing.
  198. *
  199. * This can only trigger in case of a hardware failure, miscompilation,
  200. * memory corruption, serious bug in the library, or other error would can
  201. * otherwise result in undefined behaviour. It will not trigger due to mere
  202. * incorrect usage of the API (see secp256k1_context_set_illegal_callback
  203. * for that). After this callback returns, anything may happen, including
  204. * crashing.
  205. *
  206. * Args: ctx: an existing context object (cannot be NULL)
  207. * In: fun: a pointer to a function to call when an internal error occurs,
  208. * taking a message and an opaque pointer (NULL restores a default
  209. * handler that calls abort).
  210. * data: the opaque pointer to pass to fun above.
  211. */
  212. SECP256K1_API void secp256k1_context_set_error_callback(
  213. secp256k1_context* ctx,
  214. void (*fun)(const char* message, void* data),
  215. const void* data
  216. ) SECP256K1_ARG_NONNULL(1);
  217. /** Parse a variable-length public key into the pubkey object.
  218. *
  219. * Returns: 1 if the public key was fully valid.
  220. * 0 if the public key could not be parsed or is invalid.
  221. * Args: ctx: a secp256k1 context object.
  222. * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
  223. * parsed version of input. If not, its value is undefined.
  224. * In: input: pointer to a serialized public key
  225. * inputlen: length of the array pointed to by input
  226. *
  227. * This function supports parsing compressed (33 bytes, header byte 0x02 or
  228. * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
  229. * byte 0x06 or 0x07) format public keys.
  230. */
  231. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
  232. const secp256k1_context* ctx,
  233. secp256k1_pubkey* pubkey,
  234. const unsigned char *input,
  235. size_t inputlen
  236. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  237. /** Serialize a pubkey object into a serialized byte sequence.
  238. *
  239. * Returns: 1 always.
  240. * Args: ctx: a secp256k1 context object.
  241. * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
  242. * compressed==1) byte array to place the serialized key
  243. * in.
  244. * In/Out: outputlen: a pointer to an integer which is initially set to the
  245. * size of output, and is overwritten with the written
  246. * size.
  247. * In: pubkey: a pointer to a secp256k1_pubkey containing an
  248. * initialized public key.
  249. * flags: SECP256K1_EC_COMPRESSED if serialization should be in
  250. * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
  251. */
  252. SECP256K1_API int secp256k1_ec_pubkey_serialize(
  253. const secp256k1_context* ctx,
  254. unsigned char *output,
  255. size_t *outputlen,
  256. const secp256k1_pubkey* pubkey,
  257. unsigned int flags
  258. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  259. /** Parse an ECDSA signature in compact (64 bytes) format.
  260. *
  261. * Returns: 1 when the signature could be parsed, 0 otherwise.
  262. * Args: ctx: a secp256k1 context object
  263. * Out: sig: a pointer to a signature object
  264. * In: input64: a pointer to the 64-byte array to parse
  265. *
  266. * The signature must consist of a 32-byte big endian R value, followed by a
  267. * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
  268. * encoding is invalid. R and S with value 0 are allowed in the encoding.
  269. *
  270. * After the call, sig will always be initialized. If parsing failed or R or
  271. * S are zero, the resulting sig value is guaranteed to fail validation for any
  272. * message and public key.
  273. */
  274. SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
  275. const secp256k1_context* ctx,
  276. secp256k1_ecdsa_signature* sig,
  277. const unsigned char *input64
  278. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  279. /** Parse a DER ECDSA signature.
  280. *
  281. * Returns: 1 when the signature could be parsed, 0 otherwise.
  282. * Args: ctx: a secp256k1 context object
  283. * Out: sig: a pointer to a signature object
  284. * In: input: a pointer to the signature to be parsed
  285. * inputlen: the length of the array pointed to be input
  286. *
  287. * This function will accept any valid DER encoded signature, even if the
  288. * encoded numbers are out of range.
  289. *
  290. * After the call, sig will always be initialized. If parsing failed or the
  291. * encoded numbers are out of range, signature validation with it is
  292. * guaranteed to fail for every message and public key.
  293. */
  294. SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
  295. const secp256k1_context* ctx,
  296. secp256k1_ecdsa_signature* sig,
  297. const unsigned char *input,
  298. size_t inputlen
  299. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  300. /** Serialize an ECDSA signature in DER format.
  301. *
  302. * Returns: 1 if enough space was available to serialize, 0 otherwise
  303. * Args: ctx: a secp256k1 context object
  304. * Out: output: a pointer to an array to store the DER serialization
  305. * In/Out: outputlen: a pointer to a length integer. Initially, this integer
  306. * should be set to the length of output. After the call
  307. * it will be set to the length of the serialization (even
  308. * if 0 was returned).
  309. * In: sig: a pointer to an initialized signature object
  310. */
  311. SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
  312. const secp256k1_context* ctx,
  313. unsigned char *output,
  314. size_t *outputlen,
  315. const secp256k1_ecdsa_signature* sig
  316. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  317. /** Serialize an ECDSA signature in compact (64 byte) format.
  318. *
  319. * Returns: 1
  320. * Args: ctx: a secp256k1 context object
  321. * Out: output64: a pointer to a 64-byte array to store the compact serialization
  322. * In: sig: a pointer to an initialized signature object
  323. *
  324. * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
  325. */
  326. SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
  327. const secp256k1_context* ctx,
  328. unsigned char *output64,
  329. const secp256k1_ecdsa_signature* sig
  330. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  331. /** Verify an ECDSA signature.
  332. *
  333. * Returns: 1: correct signature
  334. * 0: incorrect or unparseable signature
  335. * Args: ctx: a secp256k1 context object, initialized for verification.
  336. * In: sig: the signature being verified (cannot be NULL)
  337. * msg32: the 32-byte message hash being verified (cannot be NULL)
  338. * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
  339. *
  340. * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
  341. * form are accepted.
  342. *
  343. * If you need to accept ECDSA signatures from sources that do not obey this
  344. * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
  345. * validation, but be aware that doing so results in malleable signatures.
  346. *
  347. * For details, see the comments for that function.
  348. */
  349. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
  350. const secp256k1_context* ctx,
  351. const secp256k1_ecdsa_signature *sig,
  352. const unsigned char *msg32,
  353. const secp256k1_pubkey *pubkey
  354. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  355. /** Convert a signature to a normalized lower-S form.
  356. *
  357. * Returns: 1 if sigin was not normalized, 0 if it already was.
  358. * Args: ctx: a secp256k1 context object
  359. * Out: sigout: a pointer to a signature to fill with the normalized form,
  360. * or copy if the input was already normalized. (can be NULL if
  361. * you're only interested in whether the input was already
  362. * normalized).
  363. * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
  364. * can be identical to sigout)
  365. *
  366. * With ECDSA a third-party can forge a second distinct signature of the same
  367. * message, given a single initial signature, but without knowing the key. This
  368. * is done by negating the S value modulo the order of the curve, 'flipping'
  369. * the sign of the random point R which is not included in the signature.
  370. *
  371. * Forgery of the same message isn't universally problematic, but in systems
  372. * where message malleability or uniqueness of signatures is important this can
  373. * cause issues. This forgery can be blocked by all verifiers forcing signers
  374. * to use a normalized form.
  375. *
  376. * The lower-S form reduces the size of signatures slightly on average when
  377. * variable length encodings (such as DER) are used and is cheap to verify,
  378. * making it a good choice. Security of always using lower-S is assured because
  379. * anyone can trivially modify a signature after the fact to enforce this
  380. * property anyway.
  381. *
  382. * The lower S value is always between 0x1 and
  383. * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
  384. * inclusive.
  385. *
  386. * No other forms of ECDSA malleability are known and none seem likely, but
  387. * there is no formal proof that ECDSA, even with this additional restriction,
  388. * is free of other malleability. Commonly used serialization schemes will also
  389. * accept various non-unique encodings, so care should be taken when this
  390. * property is required for an application.
  391. *
  392. * The secp256k1_ecdsa_sign function will by default create signatures in the
  393. * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
  394. * signatures come from a system that cannot enforce this property,
  395. * secp256k1_ecdsa_signature_normalize must be called before verification.
  396. */
  397. SECP256K1_API int secp256k1_ecdsa_signature_normalize(
  398. const secp256k1_context* ctx,
  399. secp256k1_ecdsa_signature *sigout,
  400. const secp256k1_ecdsa_signature *sigin
  401. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
  402. /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
  403. * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
  404. * extra entropy.
  405. */
  406. SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
  407. /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
  408. SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
  409. /** Create an ECDSA signature.
  410. *
  411. * Returns: 1: signature created
  412. * 0: the nonce generation function failed, or the private key was invalid.
  413. * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
  414. * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
  415. * In: msg32: the 32-byte message hash being signed (cannot be NULL)
  416. * seckey: pointer to a 32-byte secret key (cannot be NULL)
  417. * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
  418. * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
  419. *
  420. * The created signature is always in lower-S form. See
  421. * secp256k1_ecdsa_signature_normalize for more details.
  422. */
  423. SECP256K1_API int secp256k1_ecdsa_sign(
  424. const secp256k1_context* ctx,
  425. secp256k1_ecdsa_signature *sig,
  426. const unsigned char *msg32,
  427. const unsigned char *seckey,
  428. secp256k1_nonce_function noncefp,
  429. const void *ndata
  430. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  431. /** Verify an ECDSA secret key.
  432. *
  433. * Returns: 1: secret key is valid
  434. * 0: secret key is invalid
  435. * Args: ctx: pointer to a context object (cannot be NULL)
  436. * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
  437. */
  438. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
  439. const secp256k1_context* ctx,
  440. const unsigned char *seckey
  441. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
  442. /** Compute the public key for a secret key.
  443. *
  444. * Returns: 1: secret was valid, public key stores
  445. * 0: secret was invalid, try again
  446. * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
  447. * Out: pubkey: pointer to the created public key (cannot be NULL)
  448. * In: seckey: pointer to a 32-byte private key (cannot be NULL)
  449. */
  450. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
  451. const secp256k1_context* ctx,
  452. secp256k1_pubkey *pubkey,
  453. const unsigned char *seckey
  454. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  455. /** Tweak a private key by adding tweak to it.
  456. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  457. * uniformly random 32-byte arrays, or if the resulting private key
  458. * would be invalid (only when the tweak is the complement of the
  459. * private key). 1 otherwise.
  460. * Args: ctx: pointer to a context object (cannot be NULL).
  461. * In/Out: seckey: pointer to a 32-byte private key.
  462. * In: tweak: pointer to a 32-byte tweak.
  463. */
  464. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
  465. const secp256k1_context* ctx,
  466. unsigned char *seckey,
  467. const unsigned char *tweak
  468. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  469. /** Tweak a public key by adding tweak times the generator to it.
  470. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  471. * uniformly random 32-byte arrays, or if the resulting public key
  472. * would be invalid (only when the tweak is the complement of the
  473. * corresponding private key). 1 otherwise.
  474. * Args: ctx: pointer to a context object initialized for validation
  475. * (cannot be NULL).
  476. * In/Out: pubkey: pointer to a public key object.
  477. * In: tweak: pointer to a 32-byte tweak.
  478. */
  479. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
  480. const secp256k1_context* ctx,
  481. secp256k1_pubkey *pubkey,
  482. const unsigned char *tweak
  483. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  484. /** Tweak a private key by multiplying it by a tweak.
  485. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  486. * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
  487. * Args: ctx: pointer to a context object (cannot be NULL).
  488. * In/Out: seckey: pointer to a 32-byte private key.
  489. * In: tweak: pointer to a 32-byte tweak.
  490. */
  491. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
  492. const secp256k1_context* ctx,
  493. unsigned char *seckey,
  494. const unsigned char *tweak
  495. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  496. /** Tweak a public key by multiplying it by a tweak value.
  497. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  498. * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
  499. * Args: ctx: pointer to a context object initialized for validation
  500. * (cannot be NULL).
  501. * In/Out: pubkey: pointer to a public key obkect.
  502. * In: tweak: pointer to a 32-byte tweak.
  503. */
  504. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
  505. const secp256k1_context* ctx,
  506. secp256k1_pubkey *pubkey,
  507. const unsigned char *tweak
  508. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  509. /** Updates the context randomization.
  510. * Returns: 1: randomization successfully updated
  511. * 0: error
  512. * Args: ctx: pointer to a context object (cannot be NULL)
  513. * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
  514. */
  515. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
  516. secp256k1_context* ctx,
  517. const unsigned char *seed32
  518. ) SECP256K1_ARG_NONNULL(1);
  519. /** Add a number of public keys together.
  520. * Returns: 1: the sum of the public keys is valid.
  521. * 0: the sum of the public keys is not valid.
  522. * Args: ctx: pointer to a context object
  523. * Out: out: pointer to a public key object for placing the resulting public key
  524. * (cannot be NULL)
  525. * In: ins: pointer to array of pointers to public keys (cannot be NULL)
  526. * n: the number of public keys to add together (must be at least 1)
  527. */
  528. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
  529. const secp256k1_context* ctx,
  530. secp256k1_pubkey *out,
  531. const secp256k1_pubkey * const * ins,
  532. size_t n
  533. ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  534. # ifdef __cplusplus
  535. }
  536. # endif
  537. #endif