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