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 34KB

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