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.h 28KB

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