|
9 | 9 | #include "../../../include/secp256k1.h"
|
10 | 10 | #include "../../../include/secp256k1_silentpayments.h"
|
11 | 11 |
|
12 |
| -/* TODO: implement functions for sender side. */ |
| 12 | +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 | + size_t i; |
| 14 | + unsigned char a_tweaked[32]; |
| 15 | + |
| 16 | + /* Sanity check inputs. */ |
| 17 | + VERIFY_CHECK(ctx != NULL); |
| 18 | + ARG_CHECK(tweak_data32 != NULL); |
| 19 | + memset(tweak_data32, 0, 32); |
| 20 | + ARG_CHECK(plain_seckeys == NULL || n_plain_seckeys >= 1); |
| 21 | + ARG_CHECK(taproot_seckeys == NULL || n_taproot_seckeys >= 1); |
| 22 | + ARG_CHECK((plain_seckeys != NULL) || (taproot_seckeys != NULL)); |
| 23 | + ARG_CHECK((n_plain_seckeys + n_taproot_seckeys) >= 1); |
| 24 | + ARG_CHECK(outpoints_hash32 != NULL); |
| 25 | + |
| 26 | + /* Compute input private keys tweak: a_tweaked = (a_0 + a_1 + ... + a_(n-1)) * outpoints_hash */ |
| 27 | + for (i = 0; i < n_plain_seckeys; i++) { |
| 28 | + const unsigned char *seckey_to_add = &plain_seckeys[i*32]; |
| 29 | + if (i == 0) { |
| 30 | + memcpy(a_tweaked, seckey_to_add, 32); |
| 31 | + continue; |
| 32 | + } |
| 33 | + if (!secp256k1_ec_seckey_tweak_add(ctx, a_tweaked, seckey_to_add)) { |
| 34 | + return 0; |
| 35 | + } |
| 36 | + } |
| 37 | + /* private keys used for taproot outputs have to be negated if they resulted in an odd point */ |
| 38 | + for (i = 0; i < n_taproot_seckeys; i++) { |
| 39 | + unsigned char seckey_to_add[32]; |
| 40 | + secp256k1_pubkey pubkey; |
| 41 | + secp256k1_ge ge; |
| 42 | + |
| 43 | + memcpy(seckey_to_add, &taproot_seckeys[32*i], 32); |
| 44 | + if (!secp256k1_ec_pubkey_create(ctx, &pubkey, seckey_to_add)) { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + if (!secp256k1_pubkey_load(ctx, &ge, &pubkey)) { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + if (secp256k1_fe_is_odd(&ge.y)) { |
| 51 | + if (!secp256k1_ec_seckey_negate(ctx, seckey_to_add)) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (i == 0 && n_plain_seckeys == 0) { |
| 57 | + memcpy(a_tweaked, seckey_to_add, 32); |
| 58 | + continue; |
| 59 | + } |
| 60 | + if (!secp256k1_ec_seckey_tweak_add(ctx, a_tweaked, seckey_to_add)) { |
| 61 | + return 0; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (!secp256k1_ec_seckey_tweak_mul(ctx, a_tweaked, outpoints_hash32)) { |
| 66 | + return 0; |
| 67 | + } |
| 68 | + |
| 69 | + memcpy(tweak_data32, a_tweaked, 32); |
| 70 | + return 1; |
| 71 | +} |
13 | 72 |
|
14 | 73 | /* TODO: implement functions for receiver side. */
|
15 | 74 |
|
|
0 commit comments