1 module include.secp256k1_recovery;
2 
3 import include.secp256k1;
4 
5 extern (C):
6 
7 /** Opaque data structured that holds a parsed ECDSA signature,
8  *  supporting pubkey recovery.
9  *
10  *  The exact representation of data inside is implementation defined and not
11  *  guaranteed to be portable between different platforms or versions. It is
12  *  however guaranteed to be 65 bytes in size, and can be safely copied/moved.
13  *  If you need to convert to a format suitable for storage or transmission, use
14  *  the secp256k1_ecdsa_signature_serialize_* and
15  *  secp256k1_ecdsa_signature_parse_* functions.
16  *
17  *  Furthermore, it is guaranteed that identical signatures (including their
18  *  recoverability) will have identical representation, so they can be
19  *  memcmp'ed.
20  */
21 struct secp256k1_ecdsa_recoverable_signature
22 {
23     ubyte[65] data;
24 }
25 
26 /** Parse a compact ECDSA signature (64 bytes + recovery id).
27  *
28  *  Returns: 1 when the signature could be parsed, 0 otherwise
29  *  Args: ctx:     a secp256k1 context object
30  *  Out:  sig:     a pointer to a signature object
31  *  In:   input64: a pointer to a 64-byte compact signature
32  *        recid:   the recovery id (0, 1, 2 or 3)
33  */
34 int secp256k1_ecdsa_recoverable_signature_parse_compact(const(secp256k1_context)* ctx,
35         secp256k1_ecdsa_recoverable_signature* sig, const(ubyte)* input64, int recid);
36 
37 /** Convert a recoverable signature into a normal signature.
38  *
39  *  Returns: 1
40  *  Args: ctx:    a secp256k1 context object.
41  *  Out:  sig:    a pointer to a normal signature.
42  *  In:   sigin:  a pointer to a recoverable signature.
43  */
44 int secp256k1_ecdsa_recoverable_signature_convert(const(secp256k1_context)* ctx,
45         secp256k1_ecdsa_signature* sig, const(secp256k1_ecdsa_recoverable_signature)* sigin);
46 
47 /** Serialize an ECDSA signature in compact format (64 bytes + recovery id).
48  *
49  *  Returns: 1
50  *  Args: ctx:      a secp256k1 context object.
51  *  Out:  output64: a pointer to a 64-byte array of the compact signature.
52  *        recid:    a pointer to an integer to hold the recovery id.
53  *  In:   sig:      a pointer to an initialized signature object.
54  */
55 int secp256k1_ecdsa_recoverable_signature_serialize_compact(const(secp256k1_context)* ctx,
56         ubyte* output64, int* recid, const(secp256k1_ecdsa_recoverable_signature)* sig);
57 
58 /** Create a recoverable ECDSA signature.
59  *
60  *  Returns: 1: signature created
61  *           0: the nonce generation function failed, or the secret key was invalid.
62  *  Args:    ctx:       pointer to a context object, initialized for signing.
63  *  Out:     sig:       pointer to an array where the signature will be placed.
64  *  In:      msghash32: the 32-byte message hash being signed.
65  *           seckey:    pointer to a 32-byte secret key.
66  *           noncefp:   pointer to a nonce generation function. If NULL,
67  *                      secp256k1_nonce_function_default is used.
68  *           ndata:     pointer to arbitrary data used by the nonce generation function
69  *                      (can be NULL for secp256k1_nonce_function_default).
70  */
71 int secp256k1_ecdsa_sign_recoverable(const(secp256k1_context)* ctx, secp256k1_ecdsa_recoverable_signature* sig,
72         const(ubyte)* msghash32, const(ubyte)* seckey,
73         secp256k1_nonce_function noncefp, const(void)* ndata) pure nothrow @nogc @safe;
74 
75 /** Recover an ECDSA public key from a signature.
76  *
77  *  Returns: 1: public key successfully recovered (which guarantees a correct signature).
78  *           0: otherwise.
79  *  Args:    ctx:       pointer to a context object, initialized for verification.
80  *  Out:     pubkey:    pointer to the recovered public key.
81  *  In:      sig:       pointer to initialized signature that supports pubkey recovery.
82  *           msghash32: the 32-byte message hash assumed to be signed.
83  */
84 int secp256k1_ecdsa_recover(const(secp256k1_context)* ctx, secp256k1_pubkey* pubkey,
85         const(secp256k1_ecdsa_recoverable_signature)* sig, const(ubyte)* msghash32) pure nothrow @nogc @safe;
86 
87 /* SECP256K1_RECOVERY_H */