|
| 1 | +#ifndef SECP256K1_ECDSA_ADAPTOR_H |
| 2 | +#define SECP256K1_ECDSA_ADAPTOR_H |
| 3 | + |
| 4 | +#ifdef __cplusplus |
| 5 | +extern "C" { |
| 6 | +#endif |
| 7 | + |
| 8 | +/** This module implements single signer ECDSA adaptor signatures following |
| 9 | + * "One-Time Verifiably Encrypted Signatures A.K.A. Adaptor Signatures" by |
| 10 | + * Lloyd Fournier |
| 11 | + * (https://lists.linuxfoundation.org/pipermail/lightning-dev/2019-November/002316.html |
| 12 | + * and https://github.com/LLFourn/one-time-VES/blob/master/main.pdf). |
| 13 | + * |
| 14 | + * WARNING! DANGER AHEAD! |
| 15 | + * As mentioned in Lloyd Fournier's paper, the adaptor signature leaks the |
| 16 | + * Elliptic-curve Diffie–Hellman (ECDH) key between the signing key and the |
| 17 | + * encryption key. This is not a problem for ECDSA adaptor signatures |
| 18 | + * themselves, but may result in a complete loss of security when they are |
| 19 | + * composed with other schemes. More specifically, let us refer to the |
| 20 | + * signer's public key as X = x*G, and to the encryption key as Y = y*G. |
| 21 | + * Given X, Y and the adaptor signature, it is trivial to compute Y^x = X^y. |
| 22 | + * |
| 23 | + * A defense is to not reuse the signing key of ECDSA adaptor signatures in |
| 24 | + * protocols that rely on the hardness of the CDH problem, e.g., Diffie-Hellman |
| 25 | + * key exchange and ElGamal encryption. In general, it is a well-established |
| 26 | + * cryptographic practice to seperate keys for different purposes whenever |
| 27 | + * possible. |
| 28 | + */ |
| 29 | + |
| 30 | +/** A pointer to a function to deterministically generate a nonce. |
| 31 | + * |
| 32 | + * Same as secp256k1_nonce_function_hardened with the exception of using the |
| 33 | + * compressed 33-byte encoding for the pubkey argument. |
| 34 | + * |
| 35 | + * Returns: 1 if a nonce was successfully generated. 0 will cause signing to |
| 36 | + * return an error. |
| 37 | + * Out: nonce32: pointer to a 32-byte array to be filled by the function |
| 38 | + * In: msg32: the 32-byte message hash being verified |
| 39 | + * key32: pointer to a 32-byte secret key |
| 40 | + * pk33: the 33-byte serialized pubkey corresponding to key32 |
| 41 | + * algo: pointer to an array describing the signature algorithm |
| 42 | + * algolen: the length of the algo array |
| 43 | + * data: arbitrary data pointer that is passed through |
| 44 | + * |
| 45 | + * Except for test cases, this function should compute some cryptographic hash of |
| 46 | + * the message, the key, the pubkey, the algorithm description, and data. |
| 47 | + */ |
| 48 | +typedef int (*secp256k1_nonce_function_hardened_ecdsa_adaptor)( |
| 49 | + unsigned char *nonce32, |
| 50 | + const unsigned char *msg32, |
| 51 | + const unsigned char *key32, |
| 52 | + const unsigned char *pk33, |
| 53 | + const unsigned char *algo, |
| 54 | + size_t algolen, |
| 55 | + void *data |
| 56 | +); |
| 57 | + |
| 58 | +/** A modified BIP-340 nonce generation function. If a data pointer is passed, it is |
| 59 | + * assumed to be a pointer to 32 bytes of auxiliary random data as defined in BIP-340. |
| 60 | + * The hash will be tagged with algo after removing all terminating null bytes. |
| 61 | + */ |
| 62 | +SECP256K1_API extern const secp256k1_nonce_function_hardened_ecdsa_adaptor secp256k1_nonce_function_ecdsa_adaptor; |
| 63 | + |
| 64 | +/** Encrypted Signing |
| 65 | + * |
| 66 | + * Creates an adaptor signature, which includes a proof to verify the adaptor |
| 67 | + * signature. |
| 68 | + * WARNING: Make sure you have read and understood the WARNING at the top of |
| 69 | + * this file and applied the suggested countermeasures. |
| 70 | + * |
| 71 | + * Returns: 1 on success, 0 on failure |
| 72 | + * Args: ctx: a secp256k1 context object, initialized for signing |
| 73 | + * Out: adaptor_sig162: pointer to 162 byte to store the returned signature |
| 74 | + * In: seckey32: pointer to 32 byte secret key that will be used for |
| 75 | + * signing |
| 76 | + * enckey: pointer to the encryption public key |
| 77 | + * msg32: pointer to the 32-byte message hash to sign |
| 78 | + * noncefp: pointer to a nonce generation function. If NULL, |
| 79 | + * secp256k1_nonce_function_ecdsa_adaptor is used |
| 80 | + * ndata: pointer to arbitrary data used by the nonce generation |
| 81 | + * function (can be NULL). If it is non-NULL and |
| 82 | + * secp256k1_nonce_function_ecdsa_adaptor is used, then |
| 83 | + * ndata must be a pointer to 32-byte auxiliary randomness |
| 84 | + * as per BIP-340. |
| 85 | + */ |
| 86 | +SECP256K1_API int secp256k1_ecdsa_adaptor_encrypt( |
| 87 | + const secp256k1_context* ctx, |
| 88 | + unsigned char *adaptor_sig162, |
| 89 | + unsigned char *seckey32, |
| 90 | + const secp256k1_pubkey *enckey, |
| 91 | + const unsigned char *msg32, |
| 92 | + secp256k1_nonce_function_hardened_ecdsa_adaptor noncefp, |
| 93 | + void *ndata |
| 94 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); |
| 95 | + |
| 96 | +/** Encryption Verification |
| 97 | + * |
| 98 | + * Verifies that the adaptor decryption key can be extracted from the adaptor signature |
| 99 | + * and the completed ECDSA signature. |
| 100 | + * |
| 101 | + * Returns: 1 on success, 0 on failure |
| 102 | + * Args: ctx: a secp256k1 context object, initialized for verification |
| 103 | + * In: adaptor_sig162: pointer to 162-byte signature to verify |
| 104 | + * pubkey: pointer to the public key corresponding to the secret key |
| 105 | + * used for signing |
| 106 | + * msg32: pointer to the 32-byte message hash being verified |
| 107 | + * enckey: pointer to the adaptor encryption public key |
| 108 | + */ |
| 109 | +SECP256K1_API int secp256k1_ecdsa_adaptor_verify( |
| 110 | + const secp256k1_context* ctx, |
| 111 | + const unsigned char *adaptor_sig162, |
| 112 | + const secp256k1_pubkey *pubkey, |
| 113 | + const unsigned char *msg32, |
| 114 | + const secp256k1_pubkey *enckey |
| 115 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); |
| 116 | + |
| 117 | +/** Signature Decryption |
| 118 | + * |
| 119 | + * Derives an ECDSA signature from an adaptor signature and an adaptor decryption key. |
| 120 | + * |
| 121 | + * Returns: 1 on success, 0 on failure |
| 122 | + * Args: ctx: a secp256k1 context object |
| 123 | + * Out: sig: pointer to the ECDSA signature to create |
| 124 | + * In: deckey32: pointer to 32-byte decryption secret key for the adaptor |
| 125 | + * encryption public key |
| 126 | + * adaptor_sig162: pointer to 162-byte adaptor sig |
| 127 | + */ |
| 128 | +SECP256K1_API int secp256k1_ecdsa_adaptor_decrypt( |
| 129 | + const secp256k1_context* ctx, |
| 130 | + secp256k1_ecdsa_signature *sig, |
| 131 | + const unsigned char *deckey32, |
| 132 | + const unsigned char *adaptor_sig162 |
| 133 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); |
| 134 | + |
| 135 | +/** Decryption Key Recovery |
| 136 | + * |
| 137 | + * Extracts the adaptor decryption key from the complete signature and the adaptor |
| 138 | + * signature. |
| 139 | + * |
| 140 | + * Returns: 1 on success, 0 on failure |
| 141 | + * Args: ctx: a secp256k1 context object, initialized for signing |
| 142 | + * Out: deckey32: pointer to 32-byte adaptor decryption key for the adaptor |
| 143 | + * encryption public key |
| 144 | + * In: sig: pointer to ECDSA signature to recover the adaptor decryption |
| 145 | + * key from |
| 146 | + * adaptor_sig162: pointer to adaptor signature to recover the adaptor |
| 147 | + * decryption key from |
| 148 | + * enckey: pointer to the adaptor encryption public key |
| 149 | + */ |
| 150 | +SECP256K1_API int secp256k1_ecdsa_adaptor_recover( |
| 151 | + const secp256k1_context* ctx, |
| 152 | + unsigned char *deckey32, |
| 153 | + const secp256k1_ecdsa_signature *sig, |
| 154 | + const unsigned char *adaptor_sig162, |
| 155 | + const secp256k1_pubkey *enckey |
| 156 | +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); |
| 157 | + |
| 158 | +#ifdef __cplusplus |
| 159 | +} |
| 160 | +#endif |
| 161 | + |
| 162 | +#endif /* SECP256K1_ECDSA_ADAPTOR_H */ |
0 commit comments