|
7 | 7 | #define SECP256K1_MODULE_SILENTPAYMENTS_MAIN_H
|
8 | 8 |
|
9 | 9 | #include "../../../include/secp256k1.h"
|
| 10 | +#include "../../../include/secp256k1_extrakeys.h" |
10 | 11 | #include "../../../include/secp256k1_silentpayments.h"
|
11 | 12 |
|
12 |
| -/* TODO: implement functions for sender side. */ |
| 13 | +/** Sort an array of silent payment recipients. This is used to group recipients by scan pubkey to |
| 14 | + * ensure the correct values of k are used when creating multiple outputs for a recipient. */ |
| 15 | +static int secp256k1_silentpayments_recipient_sort_cmp(const void* pk1, const void* pk2, void *ctx) { |
| 16 | + return secp256k1_ec_pubkey_cmp((secp256k1_context *)ctx, |
| 17 | + &(*(const secp256k1_silentpayments_recipient **)pk1)->scan_pubkey, |
| 18 | + &(*(const secp256k1_silentpayments_recipient **)pk2)->scan_pubkey |
| 19 | + ); |
| 20 | +} |
13 | 21 |
|
14 |
| -/* TODO: implement functions for receiver side. */ |
| 22 | +static void secp256k1_silentpayments_recipient_sort(const secp256k1_context* ctx, const secp256k1_silentpayments_recipient **recipients, size_t n_recipients) { |
| 23 | + |
| 24 | + /* Suppress wrong warning (fixed in MSVC 19.33) */ |
| 25 | + #if defined(_MSC_VER) && (_MSC_VER < 1933) |
| 26 | + #pragma warning(push) |
| 27 | + #pragma warning(disable: 4090) |
| 28 | + #endif |
| 29 | + |
| 30 | + secp256k1_hsort(recipients, n_recipients, sizeof(*recipients), secp256k1_silentpayments_recipient_sort_cmp, (void *)ctx); |
| 31 | + |
| 32 | + #if defined(_MSC_VER) && (_MSC_VER < 1933) |
| 33 | + #pragma warning(pop) |
| 34 | + #endif |
| 35 | +} |
| 36 | + |
| 37 | +/** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Inputs". */ |
| 38 | +static void secp256k1_silentpayments_sha256_init_inputs(secp256k1_sha256* hash) { |
| 39 | + secp256k1_sha256_initialize(hash); |
| 40 | + hash->s[0] = 0xd4143ffcul; |
| 41 | + hash->s[1] = 0x012ea4b5ul; |
| 42 | + hash->s[2] = 0x36e21c8ful; |
| 43 | + hash->s[3] = 0xf7ec7b54ul; |
| 44 | + hash->s[4] = 0x4dd4e2acul; |
| 45 | + hash->s[5] = 0x9bcaa0a4ul; |
| 46 | + hash->s[6] = 0xe244899bul; |
| 47 | + hash->s[7] = 0xcd06903eul; |
| 48 | + |
| 49 | + hash->bytes = 64; |
| 50 | +} |
| 51 | + |
| 52 | +static void secp256k1_silentpayments_calculate_input_hash(unsigned char *input_hash, const unsigned char *outpoint_smallest36, secp256k1_ge *pubkey_sum) { |
| 53 | + secp256k1_sha256 hash; |
| 54 | + unsigned char pubkey_sum_ser[33]; |
| 55 | + size_t len; |
| 56 | + int ret; |
| 57 | + |
| 58 | + secp256k1_silentpayments_sha256_init_inputs(&hash); |
| 59 | + secp256k1_sha256_write(&hash, outpoint_smallest36, 36); |
| 60 | + ret = secp256k1_eckey_pubkey_serialize(pubkey_sum, pubkey_sum_ser, &len, 1); |
| 61 | + VERIFY_CHECK(ret && len == sizeof(pubkey_sum_ser)); |
| 62 | + (void)ret; |
| 63 | + secp256k1_sha256_write(&hash, pubkey_sum_ser, sizeof(pubkey_sum_ser)); |
| 64 | + secp256k1_sha256_finalize(&hash, input_hash); |
| 65 | +} |
| 66 | + |
| 67 | +static void secp256k1_silentpayments_create_shared_secret(const secp256k1_context *ctx, unsigned char *shared_secret33, const secp256k1_scalar *secret_component, const secp256k1_ge *public_component) { |
| 68 | + secp256k1_gej ss_j; |
| 69 | + secp256k1_ge ss; |
| 70 | + size_t len; |
| 71 | + int ret; |
| 72 | + |
| 73 | + /* Compute shared_secret = tweaked_secret_component * Public_component */ |
| 74 | + secp256k1_ecmult_const(&ss_j, public_component, secret_component); |
| 75 | + secp256k1_ge_set_gej(&ss, &ss_j); |
| 76 | + secp256k1_declassify(ctx, &ss, sizeof(ss)); |
| 77 | + /* This can only fail if the shared secret is the point at infinity, which should be |
| 78 | + * impossible at this point, considering we have already validated the public key and |
| 79 | + * the secret key being used |
| 80 | + */ |
| 81 | + ret = secp256k1_eckey_pubkey_serialize(&ss, shared_secret33, &len, 1); |
| 82 | + VERIFY_CHECK(ret && len == 33); |
| 83 | + (void)ret; |
| 84 | + /* While not technically "secret" data, explicitly clear the shared secret since leaking this would allow an attacker |
| 85 | + * to identify the resulting transaction as a silent payments transaction and potentially link the transaction |
| 86 | + * back to the silent payment address |
| 87 | + */ |
| 88 | + secp256k1_ge_clear(&ss); |
| 89 | + secp256k1_gej_clear(&ss_j); |
| 90 | +} |
| 91 | + |
| 92 | +/** Set hash state to the BIP340 tagged hash midstate for "BIP0352/SharedSecret". */ |
| 93 | +static void secp256k1_silentpayments_sha256_init_sharedsecret(secp256k1_sha256* hash) { |
| 94 | + secp256k1_sha256_initialize(hash); |
| 95 | + hash->s[0] = 0x88831537ul; |
| 96 | + hash->s[1] = 0x5127079bul; |
| 97 | + hash->s[2] = 0x69c2137bul; |
| 98 | + hash->s[3] = 0xab0303e6ul; |
| 99 | + hash->s[4] = 0x98fa21faul; |
| 100 | + hash->s[5] = 0x4a888523ul; |
| 101 | + hash->s[6] = 0xbd99daabul; |
| 102 | + hash->s[7] = 0xf25e5e0aul; |
| 103 | + |
| 104 | + hash->bytes = 64; |
| 105 | +} |
| 106 | + |
| 107 | +static void secp256k1_silentpayments_create_t_k(secp256k1_scalar *t_k_scalar, const unsigned char *shared_secret33, unsigned int k) { |
| 108 | + secp256k1_sha256 hash; |
| 109 | + unsigned char hash_ser[32]; |
| 110 | + unsigned char k_serialized[4]; |
| 111 | + |
| 112 | + /* Compute t_k = hash(shared_secret || ser_32(k)) [sha256 with tag "BIP0352/SharedSecret"] */ |
| 113 | + secp256k1_silentpayments_sha256_init_sharedsecret(&hash); |
| 114 | + secp256k1_sha256_write(&hash, shared_secret33, 33); |
| 115 | + secp256k1_write_be32(k_serialized, k); |
| 116 | + secp256k1_sha256_write(&hash, k_serialized, sizeof(k_serialized)); |
| 117 | + secp256k1_sha256_finalize(&hash, hash_ser); |
| 118 | + secp256k1_scalar_set_b32(t_k_scalar, hash_ser, NULL); |
| 119 | + /* While not technically "secret" data, explicitly clear hash_ser since leaking this would allow an attacker |
| 120 | + * to identify the resulting transaction as a silent payments transaction and potentially link the transaction |
| 121 | + * back to the silent payment address |
| 122 | + */ |
| 123 | + memset(hash_ser, 0, sizeof(hash_ser)); |
| 124 | +} |
| 125 | + |
| 126 | +static int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *P_output_xonly, const unsigned char *shared_secret33, const secp256k1_pubkey *recipient_spend_pubkey, unsigned int k) { |
| 127 | + secp256k1_ge P_output_ge; |
| 128 | + secp256k1_scalar t_k_scalar; |
| 129 | + int ret; |
| 130 | + |
| 131 | + /* Calculate and return P_output_xonly = B_spend + t_k * G |
| 132 | + * This will fail if B_spend is the point at infinity or if |
| 133 | + * B_spend + t_k*G is the point at infinity. |
| 134 | + */ |
| 135 | + secp256k1_silentpayments_create_t_k(&t_k_scalar, shared_secret33, k); |
| 136 | + if (!secp256k1_pubkey_load(ctx, &P_output_ge, recipient_spend_pubkey)) { |
| 137 | + secp256k1_scalar_clear(&t_k_scalar); |
| 138 | + return 0; |
| 139 | + } |
| 140 | + ret = secp256k1_eckey_pubkey_tweak_add(&P_output_ge, &t_k_scalar); |
| 141 | + /* tweak add only fails if t_k_scalar is equal to the dlog of P_output_ge, but t_k_scalar is the output of a collision resistant hash function. */ |
| 142 | + /* TODO: consider declassify ret */ |
| 143 | + /* TODO: but we don't want to imply this can never happen */ |
| 144 | + VERIFY_CHECK(ret); |
| 145 | +#ifndef VERIFY |
| 146 | + (void) ret; |
| 147 | +#endif |
| 148 | + secp256k1_xonly_pubkey_save(P_output_xonly, &P_output_ge); |
| 149 | + |
| 150 | + /* While not technically "secret" data, explicitly clear t_k since leaking this would allow an attacker |
| 151 | + * to identify the resulting transaction as a silent payments transaction and potentially link the transaction |
| 152 | + * back to the silent payment address |
| 153 | + */ |
| 154 | + secp256k1_scalar_clear(&t_k_scalar); |
| 155 | + return 1; |
| 156 | +} |
| 157 | + |
| 158 | +int secp256k1_silentpayments_sender_create_outputs( |
| 159 | + const secp256k1_context *ctx, |
| 160 | + secp256k1_xonly_pubkey **generated_outputs, |
| 161 | + const secp256k1_silentpayments_recipient **recipients, |
| 162 | + size_t n_recipients, |
| 163 | + const unsigned char *outpoint_smallest36, |
| 164 | + const secp256k1_keypair * const *taproot_seckeys, |
| 165 | + size_t n_taproot_seckeys, |
| 166 | + const unsigned char * const *plain_seckeys, |
| 167 | + size_t n_plain_seckeys |
| 168 | +) { |
| 169 | + size_t i, k; |
| 170 | + secp256k1_scalar a_sum_scalar, addend, input_hash_scalar; |
| 171 | + secp256k1_ge A_sum_ge; |
| 172 | + secp256k1_gej A_sum_gej; |
| 173 | + unsigned char input_hash[32]; |
| 174 | + unsigned char shared_secret[33]; |
| 175 | + secp256k1_silentpayments_recipient last_recipient; |
| 176 | + int overflow = 0; |
| 177 | + int ret; |
| 178 | + |
| 179 | + /* Sanity check inputs. */ |
| 180 | + VERIFY_CHECK(ctx != NULL); |
| 181 | + ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); |
| 182 | + ARG_CHECK(generated_outputs != NULL); |
| 183 | + ARG_CHECK(recipients != NULL); |
| 184 | + ARG_CHECK(n_recipients > 0); |
| 185 | + ARG_CHECK((plain_seckeys != NULL) || (taproot_seckeys != NULL)); |
| 186 | + if (taproot_seckeys != NULL) { |
| 187 | + ARG_CHECK(n_taproot_seckeys > 0); |
| 188 | + } else { |
| 189 | + ARG_CHECK(n_taproot_seckeys == 0); |
| 190 | + } |
| 191 | + if (plain_seckeys != NULL) { |
| 192 | + ARG_CHECK(n_plain_seckeys > 0); |
| 193 | + } else { |
| 194 | + ARG_CHECK(n_plain_seckeys == 0); |
| 195 | + } |
| 196 | + ARG_CHECK(outpoint_smallest36 != NULL); |
| 197 | + /* ensure the index field is set correctly */ |
| 198 | + for (i = 0; i < n_recipients; i++) { |
| 199 | + ARG_CHECK(recipients[i]->index == i); |
| 200 | + } |
| 201 | + |
| 202 | + /* Compute input private keys sum: a_sum = a_1 + a_2 + ... + a_n */ |
| 203 | + a_sum_scalar = secp256k1_scalar_zero; |
| 204 | + for (i = 0; i < n_plain_seckeys; i++) { |
| 205 | + ret = secp256k1_scalar_set_b32_seckey(&addend, plain_seckeys[i]); |
| 206 | + /* TODO: We can declassify return value, because scalar set only fails if the seckey is invalid */ |
| 207 | + secp256k1_declassify(ctx, &ret, sizeof(ret)); |
| 208 | + if (!ret) { |
| 209 | + /* TODO: clear a_sum_scalar */ |
| 210 | + return 0; |
| 211 | + } |
| 212 | + secp256k1_scalar_add(&a_sum_scalar, &a_sum_scalar, &addend); |
| 213 | + } |
| 214 | + /* private keys used for taproot outputs have to be negated if they resulted in an odd point */ |
| 215 | + for (i = 0; i < n_taproot_seckeys; i++) { |
| 216 | + secp256k1_ge addend_point; |
| 217 | + ret = secp256k1_keypair_load(ctx, &addend, &addend_point, taproot_seckeys[i]); |
| 218 | + /* TODO: we can declassify return value */ |
| 219 | + if (!ret) { |
| 220 | + /* TODO: clear a_sum_scalar */ |
| 221 | + return 0; |
| 222 | + } |
| 223 | + secp256k1_declassify(ctx, &ret, sizeof(ret)); |
| 224 | + if (secp256k1_fe_is_odd(&addend_point.y)) { |
| 225 | + secp256k1_scalar_negate(&addend, &addend); |
| 226 | + } |
| 227 | + secp256k1_scalar_add(&a_sum_scalar, &a_sum_scalar, &addend); |
| 228 | + } |
| 229 | + /* If there are any failures in loading/summing up the secret keys, fail early */ |
| 230 | + /* TODO: can we declassify this? */ |
| 231 | + /* Yes: We assume the adversary has access to a_sum_scalar*G */ |
| 232 | + ret = secp256k1_scalar_is_zero(&a_sum_scalar); |
| 233 | + secp256k1_declassify(ctx, &ret, sizeof(ret)); |
| 234 | + if (ret) { |
| 235 | + return 0; |
| 236 | + } |
| 237 | + /* Compute input_hash = hash(outpoint_L || (a_sum * G)) */ |
| 238 | + secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &A_sum_gej, &a_sum_scalar); |
| 239 | + secp256k1_ge_set_gej(&A_sum_ge, &A_sum_gej); |
| 240 | + /* TODO: comment */ |
| 241 | + secp256k1_declassify(ctx, &A_sum_ge, sizeof(A_sum_ge)); |
| 242 | + |
| 243 | + /* Calculate the input hash and tweak a_sum, i.e., a_sum_tweaked = a_sum * input_hash */ |
| 244 | + secp256k1_silentpayments_calculate_input_hash(input_hash, outpoint_smallest36, &A_sum_ge); |
| 245 | + secp256k1_scalar_set_b32(&input_hash_scalar, input_hash, &overflow); |
| 246 | + /* TODO: consider VERIFY_CHECK ??? */ |
| 247 | + if (overflow) { |
| 248 | + return 0; |
| 249 | + } |
| 250 | + secp256k1_scalar_mul(&a_sum_scalar, &a_sum_scalar, &input_hash_scalar); |
| 251 | + secp256k1_silentpayments_recipient_sort(ctx, recipients, n_recipients); |
| 252 | + last_recipient = *recipients[0]; |
| 253 | + k = 0; |
| 254 | + for (i = 0; i < n_recipients; i++) { |
| 255 | + if ((i == 0) || (secp256k1_ec_pubkey_cmp(ctx, &last_recipient.scan_pubkey, &recipients[i]->scan_pubkey) != 0)) { |
| 256 | + /* If we are on a different scan pubkey, its time to recreate the the shared secret and reset k to 0. |
| 257 | + * It's very unlikely the scan public key is invalid by this point, since this means the caller would |
| 258 | + * have created the _silentpayments_recipient object incorrectly, but just to be sure we still check that |
| 259 | + * the public key is valid. |
| 260 | + */ |
| 261 | + secp256k1_ge pk; |
| 262 | + if (!secp256k1_pubkey_load(ctx, &pk, &recipients[i]->scan_pubkey)) { |
| 263 | + /* TODO: clean up */ |
| 264 | + return 0; |
| 265 | + } |
| 266 | + secp256k1_silentpayments_create_shared_secret(ctx, shared_secret, &a_sum_scalar, &pk); |
| 267 | + k = 0; |
| 268 | + } |
| 269 | + if (!secp256k1_silentpayments_create_output_pubkey(ctx, generated_outputs[recipients[i]->index], shared_secret, &recipients[i]->spend_pubkey, k)) { |
| 270 | + /* TODO: clean up */ |
| 271 | + return 0; |
| 272 | + } |
| 273 | + k++; |
| 274 | + last_recipient = *recipients[i]; |
| 275 | + } |
| 276 | + /* Explicitly clear variables containing secret data */ |
| 277 | + secp256k1_scalar_clear(&addend); |
| 278 | + secp256k1_scalar_clear(&a_sum_scalar); |
| 279 | + |
| 280 | + /* While technically not "secret data," explicitly clear the shared secret since leaking this |
| 281 | + * could result in a third party being able to identify the transaction as a silent payments transaction |
| 282 | + * and potentially link the transaction back to a silent payment address |
| 283 | + */ |
| 284 | + memset(&shared_secret, 0, sizeof(shared_secret)); |
| 285 | + return 1; |
| 286 | +} |
15 | 287 |
|
16 | 288 | #endif
|
0 commit comments