|
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 |
|
12 | 14 | /** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Inputs". */
|
@@ -100,6 +102,45 @@ int secp256k1_silentpayments_create_private_tweak_data(const secp256k1_context *
|
100 | 102 | return 1;
|
101 | 103 | }
|
102 | 104 |
|
| 105 | +/* secp256k1_ecdh expects a hash function to be passed in or uses its default |
| 106 | + * hashing function. We don't want to hash the ECDH result, so we define a |
| 107 | + * custom function which simply returns the pubkey without hashing. |
| 108 | + */ |
| 109 | +static int secp256k1_silentpayments_ecdh_return_pubkey(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) { |
| 110 | + secp256k1_ge point; |
| 111 | + secp256k1_fe x, y; |
| 112 | + size_t ser_size; |
| 113 | + int ser_ret; |
| 114 | + |
| 115 | + (void)data; |
| 116 | + /* Parse point as group element */ |
| 117 | + if (!secp256k1_fe_set_b32_limit(&x, x32) || !secp256k1_fe_set_b32_limit(&y, y32)) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + secp256k1_ge_set_xy(&point, &x, &y); |
| 121 | + |
| 122 | + /* Serialize as compressed pubkey */ |
| 123 | + ser_ret = secp256k1_eckey_pubkey_serialize(&point, output, &ser_size, 1); |
| 124 | + VERIFY_CHECK(ser_ret && ser_size == 33); |
| 125 | + (void)ser_ret; |
| 126 | + |
| 127 | + return 1; |
| 128 | +} |
| 129 | + |
| 130 | +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) { |
| 131 | + /* Sanity check inputs */ |
| 132 | + ARG_CHECK(shared_secret33 != NULL); |
| 133 | + memset(shared_secret33, 0, 33); |
| 134 | + ARG_CHECK(receiver_scan_pubkey != NULL); |
| 135 | + |
| 136 | + /* Compute shared_secret = a_tweaked * B_scan */ |
| 137 | + if (!secp256k1_ecdh(ctx, shared_secret33, receiver_scan_pubkey, tweak_data32, secp256k1_silentpayments_ecdh_return_pubkey, NULL)) { |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + return 1; |
| 142 | +} |
| 143 | + |
103 | 144 | /* TODO: implement functions for receiver side. */
|
104 | 145 |
|
105 | 146 | #endif
|
0 commit comments