|
| 1 | +#ifndef SECP256K1_SCHNORRSIG_H |
| 2 | +#define SECP256K1_SCHNORRSIG_H |
| 3 | + |
| 4 | +/** This module implements a variant of Schnorr signatures compliant with |
| 5 | + * BIP-schnorr |
| 6 | + * (https://github.com/sipa/bips/blob/bip-schnorr/bip-schnorr.mediawiki). |
| 7 | + */ |
| 8 | + |
| 9 | +/** Opaque data structure that holds a parsed Schnorr signature. |
| 10 | + * |
| 11 | + * The exact representation of data inside is implementation defined and not |
| 12 | + * guaranteed to be portable between different platforms or versions. It is |
| 13 | + * however guaranteed to be 64 bytes in size, and can be safely copied/moved. |
| 14 | + * If you need to convert to a format suitable for storage, transmission, or |
| 15 | + * comparison, use the `secp256k1_schnorrsig_serialize` and |
| 16 | + * `secp256k1_schnorrsig_parse` functions. |
| 17 | + */ |
| 18 | +typedef struct { |
| 19 | + unsigned char data[64]; |
| 20 | +} secp256k1_schnorrsig; |
| 21 | + |
| 22 | +/** Serialize a Schnorr signature. |
| 23 | + * |
| 24 | + * Returns: 1 |
| 25 | + * Args: ctx: a secp256k1 context object |
| 26 | + * Out: out64: pointer to a 64-byte array to store the serialized signature |
| 27 | + * In: sig: pointer to the signature |
| 28 | + * |
| 29 | + * See secp256k1_schnorrsig_parse for details about the encoding. |
| 30 | + */ |
| 31 | +SECP256K1_API int secp256k1_schnorrsig_serialize( |
| 32 | + const secp256k1_context* ctx, |
| 33 | + unsigned char *out64, |
| 34 | + const secp256k1_schnorrsig* sig |
| 35 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
| 36 | + |
| 37 | +/** Parse a Schnorr signature. |
| 38 | + * |
| 39 | + * Returns: 1 when the signature could be parsed, 0 otherwise. |
| 40 | + * Args: ctx: a secp256k1 context object |
| 41 | + * Out: sig: pointer to a signature object |
| 42 | + * In: in64: pointer to the 64-byte signature to be parsed |
| 43 | + * |
| 44 | + * The signature is serialized in the form R||s, where R is a 32-byte public |
| 45 | + * key (x-coordinate only; the y-coordinate is considered to be the unique |
| 46 | + * y-coordinate satisfying the curve equation that is a quadratic residue) |
| 47 | + * and s is a 32-byte big-endian scalar. |
| 48 | + * |
| 49 | + * After the call, sig will always be initialized. If parsing failed or the |
| 50 | + * encoded numbers are out of range, signature validation with it is |
| 51 | + * guaranteed to fail for every message and public key. |
| 52 | + */ |
| 53 | +SECP256K1_API int secp256k1_schnorrsig_parse( |
| 54 | + const secp256k1_context* ctx, |
| 55 | + secp256k1_schnorrsig* sig, |
| 56 | + const unsigned char *in64 |
| 57 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3); |
| 58 | + |
| 59 | +/** Create a Schnorr signature. |
| 60 | + * |
| 61 | + * Returns 1 on success, 0 on failure. |
| 62 | + * Args: ctx: pointer to a context object, initialized for signing (cannot be NULL) |
| 63 | + * Out: sig: pointer to the returned signature (cannot be NULL) |
| 64 | + * negated_nonce: a pointer to an integer indicates if signing algorithm negated the |
| 65 | + * nonce (can be NULL) |
| 66 | + * In: msg32: the 32-byte message hash being signed (cannot be NULL) |
| 67 | + * seckey: pointer to a 32-byte secret key (cannot be NULL) |
| 68 | + * noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_bipschnorr is used |
| 69 | + * ndata: pointer to arbitrary data used by the nonce generation function (can be NULL) |
| 70 | + */ |
| 71 | +SECP256K1_API int secp256k1_schnorrsig_sign( |
| 72 | + const secp256k1_context* ctx, |
| 73 | + secp256k1_schnorrsig *sig, |
| 74 | + int *negated_nonce, |
| 75 | + const unsigned char *msg32, |
| 76 | + const unsigned char *seckey, |
| 77 | + secp256k1_nonce_function noncefp, |
| 78 | + void *ndata |
| 79 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); |
| 80 | + |
| 81 | +/** Verify a Schnorr signature. |
| 82 | + * |
| 83 | + * Returns: 1: correct signature |
| 84 | + * 0: incorrect or unparseable signature |
| 85 | + * Args: ctx: a secp256k1 context object, initialized for verification. |
| 86 | + * In: sig: the signature being verified (cannot be NULL) |
| 87 | + * msg32: the 32-byte message hash being verified (cannot be NULL) |
| 88 | + * pubkey: pointer to a public key to verify with (cannot be NULL) |
| 89 | + */ |
| 90 | +SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify( |
| 91 | + const secp256k1_context* ctx, |
| 92 | + const secp256k1_schnorrsig *sig, |
| 93 | + const unsigned char *msg32, |
| 94 | + const secp256k1_pubkey *pubkey |
| 95 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
| 96 | + |
| 97 | +/** Verifies a set of Schnorr signatures. |
| 98 | + * |
| 99 | + * Returns 1 if all succeeded, 0 otherwise. In particular, returns 1 if n_sigs is 0. |
| 100 | + * |
| 101 | + * Args: ctx: a secp256k1 context object, initialized for verification. |
| 102 | + * scratch: scratch space used for the multiexponentiation |
| 103 | + * In: sig: array of signatures, or NULL if there are no signatures |
| 104 | + * msg32: array of messages, or NULL if there are no signatures |
| 105 | + * pk: array of public keys, or NULL if there are no signatures |
| 106 | + * n_sigs: number of signatures in above arrays. Must be smaller than |
| 107 | + * 2^31 and smaller than half the maximum size_t value. Must be 0 |
| 108 | + * if above arrays are NULL. |
| 109 | + */ |
| 110 | +SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify_batch( |
| 111 | + const secp256k1_context* ctx, |
| 112 | + secp256k1_scratch_space *scratch, |
| 113 | + const secp256k1_schnorrsig *const *sig, |
| 114 | + const unsigned char *const *msg32, |
| 115 | + const secp256k1_pubkey *const *pk, |
| 116 | + size_t n_sigs |
| 117 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); |
| 118 | +#endif |
0 commit comments