|
10 | 10 | #include "../../../include/secp256k1_ecdh.h"
|
11 | 11 | #include "../../../include/secp256k1_extrakeys.h"
|
12 | 12 | #include "../../../include/secp256k1_silentpayments.h"
|
| 13 | +#include "../../hash.h" |
13 | 14 |
|
14 | 15 | /** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Inputs". */
|
15 | 16 | static void secp256k1_silentpayments_sha256_init_inputs(secp256k1_sha256* hash) {
|
@@ -263,4 +264,60 @@ int secp256k1_silentpayments_create_address_spend_pubkey(const secp256k1_context
|
263 | 264 | return 1;
|
264 | 265 | }
|
265 | 266 |
|
| 267 | +/** Set hash state to the BIP340 tagged hash midstate for "BIP0352/SharedSecret". */ |
| 268 | +static void secp256k1_silentpayments_sha256_init_sharedsecret(secp256k1_sha256* hash) { |
| 269 | + secp256k1_sha256_initialize(hash); |
| 270 | + hash->s[0] = 0x88831537ul; |
| 271 | + hash->s[1] = 0x5127079bul; |
| 272 | + hash->s[2] = 0x69c2137bul; |
| 273 | + hash->s[3] = 0xab0303e6ul; |
| 274 | + hash->s[4] = 0x98fa21faul; |
| 275 | + hash->s[5] = 0x4a888523ul; |
| 276 | + hash->s[6] = 0xbd99daabul; |
| 277 | + hash->s[7] = 0xf25e5e0aul; |
| 278 | + |
| 279 | + hash->bytes = 64; |
| 280 | +} |
| 281 | + |
| 282 | +static void secp256k1_silentpayments_create_t_k(secp256k1_scalar *t_k_scalar, const unsigned char *shared_secret33, unsigned int k) { |
| 283 | + secp256k1_sha256 hash; |
| 284 | + unsigned char hash_ser[32]; |
| 285 | + unsigned char k_serialized[4]; |
| 286 | + |
| 287 | + /* Compute t_k = hash(shared_secret || ser_32(k)) [sha256 with tag "BIP0352/SharedSecret"] */ |
| 288 | + secp256k1_silentpayments_sha256_init_sharedsecret(&hash); |
| 289 | + secp256k1_sha256_write(&hash, shared_secret33, 33); |
| 290 | + secp256k1_write_be32(k_serialized, k); |
| 291 | + secp256k1_sha256_write(&hash, k_serialized, sizeof(k_serialized)); |
| 292 | + secp256k1_sha256_finalize(&hash, hash_ser); |
| 293 | + secp256k1_scalar_set_b32(t_k_scalar, hash_ser, NULL); |
| 294 | +} |
| 295 | + |
| 296 | +int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *output_xonly_pubkey, const unsigned char *shared_secret33, const secp256k1_pubkey *receiver_spend_pubkey, unsigned int k, const unsigned char *label_tweak32) { |
| 297 | + secp256k1_ge P_output; |
| 298 | + secp256k1_scalar t_k_scalar; |
| 299 | + |
| 300 | + /* Sanity check inputs */ |
| 301 | + VERIFY_CHECK(ctx != NULL); |
| 302 | + ARG_CHECK(output_xonly_pubkey != NULL); |
| 303 | + ARG_CHECK(receiver_spend_pubkey != NULL); |
| 304 | + |
| 305 | + /* Apply label tweak if provided: B_m = B_spend + label_tweak * G */ |
| 306 | + secp256k1_pubkey_load(ctx, &P_output, receiver_spend_pubkey); |
| 307 | + if (label_tweak32 != NULL) { |
| 308 | + if (!secp256k1_ec_pubkey_tweak_add_helper(&P_output, label_tweak32)) { |
| 309 | + return 0; |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + /* Calculate and return P_output = B_m + t_k * G */ |
| 314 | + secp256k1_silentpayments_create_t_k(&t_k_scalar, shared_secret33, k); |
| 315 | + if (!secp256k1_eckey_pubkey_tweak_add(&P_output, &t_k_scalar)) { |
| 316 | + return 0; |
| 317 | + } |
| 318 | + secp256k1_xonly_pubkey_save(output_xonly_pubkey, &P_output); |
| 319 | + |
| 320 | + return 1; |
| 321 | +} |
| 322 | + |
266 | 323 | #endif
|
0 commit comments