|
7 | 7 | #define SECP256K1_MODULE_SILENTPAYMENTS_MAIN_H
|
8 | 8 |
|
9 | 9 | #include "../../../include/secp256k1.h"
|
| 10 | +#include "../../../include/secp256k1_ecdh.h" |
| 11 | +#include "../../../include/secp256k1_extrakeys.h" |
10 | 12 | #include "../../../include/secp256k1_silentpayments.h"
|
11 | 13 |
|
| 14 | +/* secp256k1_ecdh expects a hash function to be passed in or uses its default |
| 15 | + * hashing function. We don't want to hash the ECDH result, so we define a |
| 16 | + * custom function which simply returns the pubkey without hashing. |
| 17 | + */ |
| 18 | +static int ecdh_return_pubkey(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) { |
| 19 | + secp256k1_pubkey pubkey; |
| 20 | + unsigned char uncompressed_pubkey[65]; |
| 21 | + size_t outputlen = 33; |
| 22 | + (void)data; |
| 23 | + |
| 24 | + uncompressed_pubkey[0] = 0x04; |
| 25 | + memcpy(uncompressed_pubkey + 1, x32, 32); |
| 26 | + memcpy(uncompressed_pubkey + 33, y32, 32); |
| 27 | + |
| 28 | + if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, uncompressed_pubkey, 65)) { |
| 29 | + return 0; |
| 30 | + } |
| 31 | + |
| 32 | + if (!secp256k1_ec_pubkey_serialize(secp256k1_context_static, output, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED)) { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + |
| 36 | + return 1; |
| 37 | +} |
| 38 | + |
12 | 39 | int secp256k1_silentpayments_create_private_tweak_data(const secp256k1_context *ctx, unsigned char *tweak_data32, const unsigned char *plain_seckeys, size_t n_plain_seckeys, const unsigned char *taproot_seckeys, size_t n_taproot_seckeys, const unsigned char *outpoints_hash32) {
|
13 | 40 | size_t i;
|
14 | 41 | unsigned char a_tweaked[32];
|
@@ -70,6 +97,20 @@ int secp256k1_silentpayments_create_private_tweak_data(const secp256k1_context *
|
70 | 97 | return 1;
|
71 | 98 | }
|
72 | 99 |
|
| 100 | +int secp256k1_silentpayments_send_create_shared_secret(const secp256k1_context *ctx, unsigned char *shared_secret33, const unsigned char *tweak_data32, const secp256k1_pubkey *receiver_scan_pubkey) { |
| 101 | + /* Sanity check inputs */ |
| 102 | + ARG_CHECK(shared_secret33 != NULL); |
| 103 | + memset(shared_secret33, 0, 33); |
| 104 | + ARG_CHECK(receiver_scan_pubkey != NULL); |
| 105 | + |
| 106 | + /* Compute shared_secret = a_tweaked * B_scan */ |
| 107 | + if (!secp256k1_ecdh(ctx, shared_secret33, receiver_scan_pubkey, tweak_data32, ecdh_return_pubkey, NULL)) { |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + return 1; |
| 112 | +} |
| 113 | + |
73 | 114 | /* TODO: implement functions for receiver side. */
|
74 | 115 |
|
75 | 116 | #endif
|
0 commit comments