Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. * See also secp256k1_context_randomize.
  153. */
  154. SECP256K1_API secp256k1_context* secp256k1_context_create(
  155. unsigned int flags
  156. ) SECP256K1_WARN_UNUSED_RESULT;
  157. /** Copies a secp256k1 context object.
  158. *
  159. * Returns: a newly created context object.
  160. * Args: ctx: an existing context to copy (cannot be NULL)
  161. */
  162. SECP256K1_API secp256k1_context* secp256k1_context_clone(
  163. const secp256k1_context* ctx
  164. ) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
  165. /** Destroy a secp256k1 context object.
  166. *
  167. * The context pointer may not be used afterwards.
  168. * Args: ctx: an existing context to destroy (cannot be NULL)
  169. */
  170. SECP256K1_API void secp256k1_context_destroy(
  171. secp256k1_context* ctx
  172. );
  173. /** Set a callback function to be called when an illegal argument is passed to
  174. * an API call. It will only trigger for violations that are mentioned
  175. * explicitly in the header.
  176. *
  177. * The philosophy is that these shouldn't be dealt with through a
  178. * specific return value, as calling code should not have branches to deal with
  179. * the case that this code itself is broken.
  180. *
  181. * On the other hand, during debug stage, one would want to be informed about
  182. * such mistakes, and the default (crashing) may be inadvisable.
  183. * When this callback is triggered, the API function called is guaranteed not
  184. * to cause a crash, though its return value and output arguments are
  185. * undefined.
  186. *
  187. * Args: ctx: an existing context object (cannot be NULL)
  188. * In: fun: a pointer to a function to call when an illegal argument is
  189. * passed to the API, taking a message and an opaque pointer
  190. * (NULL restores a default handler that calls abort).
  191. * data: the opaque pointer to pass to fun above.
  192. */
  193. SECP256K1_API void secp256k1_context_set_illegal_callback(
  194. secp256k1_context* ctx,
  195. void (*fun)(const char* message, void* data),
  196. const void* data
  197. ) SECP256K1_ARG_NONNULL(1);
  198. /** Set a callback function to be called when an internal consistency check
  199. * fails. The default is crashing.
  200. *
  201. * This can only trigger in case of a hardware failure, miscompilation,
  202. * memory corruption, serious bug in the library, or other error would can
  203. * otherwise result in undefined behaviour. It will not trigger due to mere
  204. * incorrect usage of the API (see secp256k1_context_set_illegal_callback
  205. * for that). After this callback returns, anything may happen, including
  206. * crashing.
  207. *
  208. * Args: ctx: an existing context object (cannot be NULL)
  209. * In: fun: a pointer to a function to call when an internal error occurs,
  210. * taking a message and an opaque pointer (NULL restores a default
  211. * handler that calls abort).
  212. * data: the opaque pointer to pass to fun above.
  213. */
  214. SECP256K1_API void secp256k1_context_set_error_callback(
  215. secp256k1_context* ctx,
  216. void (*fun)(const char* message, void* data),
  217. const void* data
  218. ) SECP256K1_ARG_NONNULL(1);
  219. /** Parse a variable-length public key into the pubkey object.
  220. *
  221. * Returns: 1 if the public key was fully valid.
  222. * 0 if the public key could not be parsed or is invalid.
  223. * Args: ctx: a secp256k1 context object.
  224. * Out: pubkey: pointer to a pubkey object. If 1 is returned, it is set to a
  225. * parsed version of input. If not, its value is undefined.
  226. * In: input: pointer to a serialized public key
  227. * inputlen: length of the array pointed to by input
  228. *
  229. * This function supports parsing compressed (33 bytes, header byte 0x02 or
  230. * 0x03), uncompressed (65 bytes, header byte 0x04), or hybrid (65 bytes, header
  231. * byte 0x06 or 0x07) format public keys.
  232. */
  233. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
  234. const secp256k1_context* ctx,
  235. secp256k1_pubkey* pubkey,
  236. const unsigned char *input,
  237. size_t inputlen
  238. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  239. /** Serialize a pubkey object into a serialized byte sequence.
  240. *
  241. * Returns: 1 always.
  242. * Args: ctx: a secp256k1 context object.
  243. * Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
  244. * compressed==1) byte array to place the serialized key
  245. * in.
  246. * In/Out: outputlen: a pointer to an integer which is initially set to the
  247. * size of output, and is overwritten with the written
  248. * size.
  249. * In: pubkey: a pointer to a secp256k1_pubkey containing an
  250. * initialized public key.
  251. * flags: SECP256K1_EC_COMPRESSED if serialization should be in
  252. * compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
  253. */
  254. SECP256K1_API int secp256k1_ec_pubkey_serialize(
  255. const secp256k1_context* ctx,
  256. unsigned char *output,
  257. size_t *outputlen,
  258. const secp256k1_pubkey* pubkey,
  259. unsigned int flags
  260. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  261. /** Parse an ECDSA signature in compact (64 bytes) format.
  262. *
  263. * Returns: 1 when the signature could be parsed, 0 otherwise.
  264. * Args: ctx: a secp256k1 context object
  265. * Out: sig: a pointer to a signature object
  266. * In: input64: a pointer to the 64-byte array to parse
  267. *
  268. * The signature must consist of a 32-byte big endian R value, followed by a
  269. * 32-byte big endian S value. If R or S fall outside of [0..order-1], the
  270. * encoding is invalid. R and S with value 0 are allowed in the encoding.
  271. *
  272. * After the call, sig will always be initialized. If parsing failed or R or
  273. * S are zero, the resulting sig value is guaranteed to fail validation for any
  274. * message and public key.
  275. */
  276. SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(
  277. const secp256k1_context* ctx,
  278. secp256k1_ecdsa_signature* sig,
  279. const unsigned char *input64
  280. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  281. /** Parse a DER ECDSA signature.
  282. *
  283. * Returns: 1 when the signature could be parsed, 0 otherwise.
  284. * Args: ctx: a secp256k1 context object
  285. * Out: sig: a pointer to a signature object
  286. * In: input: a pointer to the signature to be parsed
  287. * inputlen: the length of the array pointed to be input
  288. *
  289. * This function will accept any valid DER encoded signature, even if the
  290. * encoded numbers are out of range.
  291. *
  292. * After the call, sig will always be initialized. If parsing failed or the
  293. * encoded numbers are out of range, signature validation with it is
  294. * guaranteed to fail for every message and public key.
  295. */
  296. SECP256K1_API int secp256k1_ecdsa_signature_parse_der(
  297. const secp256k1_context* ctx,
  298. secp256k1_ecdsa_signature* sig,
  299. const unsigned char *input,
  300. size_t inputlen
  301. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  302. /** Serialize an ECDSA signature in DER format.
  303. *
  304. * Returns: 1 if enough space was available to serialize, 0 otherwise
  305. * Args: ctx: a secp256k1 context object
  306. * Out: output: a pointer to an array to store the DER serialization
  307. * In/Out: outputlen: a pointer to a length integer. Initially, this integer
  308. * should be set to the length of output. After the call
  309. * it will be set to the length of the serialization (even
  310. * if 0 was returned).
  311. * In: sig: a pointer to an initialized signature object
  312. */
  313. SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(
  314. const secp256k1_context* ctx,
  315. unsigned char *output,
  316. size_t *outputlen,
  317. const secp256k1_ecdsa_signature* sig
  318. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  319. /** Serialize an ECDSA signature in compact (64 byte) format.
  320. *
  321. * Returns: 1
  322. * Args: ctx: a secp256k1 context object
  323. * Out: output64: a pointer to a 64-byte array to store the compact serialization
  324. * In: sig: a pointer to an initialized signature object
  325. *
  326. * See secp256k1_ecdsa_signature_parse_compact for details about the encoding.
  327. */
  328. SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(
  329. const secp256k1_context* ctx,
  330. unsigned char *output64,
  331. const secp256k1_ecdsa_signature* sig
  332. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  333. /** Verify an ECDSA signature.
  334. *
  335. * Returns: 1: correct signature
  336. * 0: incorrect or unparseable signature
  337. * Args: ctx: a secp256k1 context object, initialized for verification.
  338. * In: sig: the signature being verified (cannot be NULL)
  339. * msg32: the 32-byte message hash being verified (cannot be NULL)
  340. * pubkey: pointer to an initialized public key to verify with (cannot be NULL)
  341. *
  342. * To avoid accepting malleable signatures, only ECDSA signatures in lower-S
  343. * form are accepted.
  344. *
  345. * If you need to accept ECDSA signatures from sources that do not obey this
  346. * rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
  347. * validation, but be aware that doing so results in malleable signatures.
  348. *
  349. * For details, see the comments for that function.
  350. */
  351. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(
  352. const secp256k1_context* ctx,
  353. const secp256k1_ecdsa_signature *sig,
  354. const unsigned char *msg32,
  355. const secp256k1_pubkey *pubkey
  356. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  357. /** Convert a signature to a normalized lower-S form.
  358. *
  359. * Returns: 1 if sigin was not normalized, 0 if it already was.
  360. * Args: ctx: a secp256k1 context object
  361. * Out: sigout: a pointer to a signature to fill with the normalized form,
  362. * or copy if the input was already normalized. (can be NULL if
  363. * you're only interested in whether the input was already
  364. * normalized).
  365. * In: sigin: a pointer to a signature to check/normalize (cannot be NULL,
  366. * can be identical to sigout)
  367. *
  368. * With ECDSA a third-party can forge a second distinct signature of the same
  369. * message, given a single initial signature, but without knowing the key. This
  370. * is done by negating the S value modulo the order of the curve, 'flipping'
  371. * the sign of the random point R which is not included in the signature.
  372. *
  373. * Forgery of the same message isn't universally problematic, but in systems
  374. * where message malleability or uniqueness of signatures is important this can
  375. * cause issues. This forgery can be blocked by all verifiers forcing signers
  376. * to use a normalized form.
  377. *
  378. * The lower-S form reduces the size of signatures slightly on average when
  379. * variable length encodings (such as DER) are used and is cheap to verify,
  380. * making it a good choice. Security of always using lower-S is assured because
  381. * anyone can trivially modify a signature after the fact to enforce this
  382. * property anyway.
  383. *
  384. * The lower S value is always between 0x1 and
  385. * 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
  386. * inclusive.
  387. *
  388. * No other forms of ECDSA malleability are known and none seem likely, but
  389. * there is no formal proof that ECDSA, even with this additional restriction,
  390. * is free of other malleability. Commonly used serialization schemes will also
  391. * accept various non-unique encodings, so care should be taken when this
  392. * property is required for an application.
  393. *
  394. * The secp256k1_ecdsa_sign function will by default create signatures in the
  395. * lower-S form, and secp256k1_ecdsa_verify will not accept others. In case
  396. * signatures come from a system that cannot enforce this property,
  397. * secp256k1_ecdsa_signature_normalize must be called before verification.
  398. */
  399. SECP256K1_API int secp256k1_ecdsa_signature_normalize(
  400. const secp256k1_context* ctx,
  401. secp256k1_ecdsa_signature *sigout,
  402. const secp256k1_ecdsa_signature *sigin
  403. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3);
  404. /** An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
  405. * If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
  406. * extra entropy.
  407. */
  408. SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_rfc6979;
  409. /** A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). */
  410. SECP256K1_API extern const secp256k1_nonce_function secp256k1_nonce_function_default;
  411. /** Create an ECDSA signature.
  412. *
  413. * Returns: 1: signature created
  414. * 0: the nonce generation function failed, or the private key was invalid.
  415. * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
  416. * Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
  417. * In: msg32: the 32-byte message hash being signed (cannot be NULL)
  418. * seckey: pointer to a 32-byte secret key (cannot be NULL)
  419. * noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
  420. * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
  421. *
  422. * The created signature is always in lower-S form. See
  423. * secp256k1_ecdsa_signature_normalize for more details.
  424. */
  425. SECP256K1_API int secp256k1_ecdsa_sign(
  426. const secp256k1_context* ctx,
  427. secp256k1_ecdsa_signature *sig,
  428. const unsigned char *msg32,
  429. const unsigned char *seckey,
  430. secp256k1_nonce_function noncefp,
  431. const void *ndata
  432. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  433. /** Verify an ECDSA secret key.
  434. *
  435. * Returns: 1: secret key is valid
  436. * 0: secret key is invalid
  437. * Args: ctx: pointer to a context object (cannot be NULL)
  438. * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
  439. */
  440. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(
  441. const secp256k1_context* ctx,
  442. const unsigned char *seckey
  443. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
  444. /** Compute the public key for a secret key.
  445. *
  446. * Returns: 1: secret was valid, public key stores
  447. * 0: secret was invalid, try again
  448. * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
  449. * Out: pubkey: pointer to the created public key (cannot be NULL)
  450. * In: seckey: pointer to a 32-byte private key (cannot be NULL)
  451. */
  452. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
  453. const secp256k1_context* ctx,
  454. secp256k1_pubkey *pubkey,
  455. const unsigned char *seckey
  456. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  457. /** Negates a private key in place.
  458. *
  459. * Returns: 1 always
  460. * Args: ctx: pointer to a context object
  461. * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
  462. */
  463. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
  464. const secp256k1_context* ctx,
  465. unsigned char *seckey
  466. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
  467. /** Negates a public key in place.
  468. *
  469. * Returns: 1 always
  470. * Args: ctx: pointer to a context object
  471. * In/Out: pubkey: pointer to the public key to be negated (cannot be NULL)
  472. */
  473. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
  474. const secp256k1_context* ctx,
  475. secp256k1_pubkey *pubkey
  476. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
  477. /** Tweak a private key by adding tweak to it.
  478. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  479. * uniformly random 32-byte arrays, or if the resulting private key
  480. * would be invalid (only when the tweak is the complement of the
  481. * private key). 1 otherwise.
  482. * Args: ctx: pointer to a context object (cannot be NULL).
  483. * In/Out: seckey: pointer to a 32-byte private key.
  484. * In: tweak: pointer to a 32-byte tweak.
  485. */
  486. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
  487. const secp256k1_context* ctx,
  488. unsigned char *seckey,
  489. const unsigned char *tweak
  490. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  491. /** Tweak a public key by adding tweak times the generator to it.
  492. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  493. * uniformly random 32-byte arrays, or if the resulting public key
  494. * would be invalid (only when the tweak is the complement of the
  495. * corresponding private key). 1 otherwise.
  496. * Args: ctx: pointer to a context object initialized for validation
  497. * (cannot be NULL).
  498. * In/Out: pubkey: pointer to a public key object.
  499. * In: tweak: pointer to a 32-byte tweak.
  500. */
  501. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
  502. const secp256k1_context* ctx,
  503. secp256k1_pubkey *pubkey,
  504. const unsigned char *tweak
  505. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  506. /** Tweak a private key by multiplying it by a tweak.
  507. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  508. * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
  509. * Args: ctx: pointer to a context object (cannot be NULL).
  510. * In/Out: seckey: pointer to a 32-byte private key.
  511. * In: tweak: pointer to a 32-byte tweak.
  512. */
  513. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
  514. const secp256k1_context* ctx,
  515. unsigned char *seckey,
  516. const unsigned char *tweak
  517. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  518. /** Tweak a public key by multiplying it by a tweak value.
  519. * Returns: 0 if the tweak was out of range (chance of around 1 in 2^128 for
  520. * uniformly random 32-byte arrays, or equal to zero. 1 otherwise.
  521. * Args: ctx: pointer to a context object initialized for validation
  522. * (cannot be NULL).
  523. * In/Out: pubkey: pointer to a public key obkect.
  524. * In: tweak: pointer to a 32-byte tweak.
  525. */
  526. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
  527. const secp256k1_context* ctx,
  528. secp256k1_pubkey *pubkey,
  529. const unsigned char *tweak
  530. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  531. /** Updates the context randomization to protect against side-channel leakage.
  532. * Returns: 1: randomization successfully updated
  533. * 0: error
  534. * Args: ctx: pointer to a context object (cannot be NULL)
  535. * In: seed32: pointer to a 32-byte random seed (NULL resets to initial state)
  536. *
  537. * While secp256k1 code is written to be constant-time no matter what secret
  538. * values are, it's possible that a future compiler may output code which isn't,
  539. * and also that the CPU may not emit the same radio frequencies or draw the same
  540. * amount power for all values.
  541. *
  542. * This function provides a seed which is combined into the blinding value: that
  543. * blinding value is added before each multiplication (and removed afterwards) so
  544. * that it does not affect function results, but shields against attacks which
  545. * rely on any input-dependent behaviour.
  546. *
  547. * You should call this after secp256k1_context_create or
  548. * secp256k1_context_clone, and may call this repeatedly afterwards.
  549. */
  550. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(
  551. secp256k1_context* ctx,
  552. const unsigned char *seed32
  553. ) SECP256K1_ARG_NONNULL(1);
  554. /** Add a number of public keys together.
  555. * Returns: 1: the sum of the public keys is valid.
  556. * 0: the sum of the public keys is not valid.
  557. * Args: ctx: pointer to a context object
  558. * Out: out: pointer to a public key object for placing the resulting public key
  559. * (cannot be NULL)
  560. * In: ins: pointer to array of pointers to public keys (cannot be NULL)
  561. * n: the number of public keys to add together (must be at least 1)
  562. */
  563. SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_combine(
  564. const secp256k1_context* ctx,
  565. secp256k1_pubkey *out,
  566. const secp256k1_pubkey * const * ins,
  567. size_t n
  568. ) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  569. # ifdef __cplusplus
  570. }
  571. # endif
  572. #endif