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