Skip to content

Commit d725050

Browse files
committed
silentpayments: add private tweak data creation routine
1 parent 6e3ed2d commit d725050

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

include/secp256k1_silentpayments.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,41 @@ extern "C" {
2828
* operations.
2929
*/
3030

31-
/* TODO: add function API for sender side. */
31+
/** Create Silent Payment tweak data from input private keys.
32+
*
33+
* Given a list of n private keys a_1...a_n (one for each silent payment
34+
* eligible input to spend) and a serialized outpoint_smallest, compute
35+
* the corresponding input private keys tweak data:
36+
*
37+
* a_tweaked = (a_1 + a_2 + ... + a_n) * hash(outpoint_smallest || A)
38+
*
39+
* (where A = A_1 + A_2 + ... + A_n)
40+
*
41+
* If necessary, the private keys are negated to enforce the right y-parity.
42+
* For that reason, the private keys have to be passed in via two different parameter
43+
* pairs, depending on whether they were used for creating taproot outputs or not.
44+
* The resulting data is needed to create a shared secret for the sender side.
45+
*
46+
* Returns: 1 if shared secret creation was successful. 0 if an error occured.
47+
* Args: ctx: pointer to a context object
48+
* Out: private_tweak_data32: pointer to the resulting 32-byte private tweak data
49+
* In: plain_seckeys: pointer to an array of 32-byte private keys of non-taproot inputs
50+
* (can be NULL if no private keys of non-taproot inputs are used)
51+
* n_plain_seckeys: the number of sender's non-taproot input private keys
52+
* taproot_seckeys: pointer to an array of 32-byte private keys of taproot inputs
53+
* (can be NULL if no private keys of taproot inputs are used)
54+
* n_taproot_seckeys: the number of sender's taproot input private keys
55+
* outpoint_smallest36: serialized smallest outpoint
56+
*/
57+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_private_tweak_data(
58+
const secp256k1_context *ctx,
59+
unsigned char *private_tweak_data32,
60+
const unsigned char *plain_seckeys,
61+
size_t n_plain_seckeys,
62+
const unsigned char *taproot_seckeys,
63+
size_t n_taproot_seckeys,
64+
const unsigned char *outpoint_smallest36
65+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(7);
3266

3367
/* TODO: add function API for receiver side. */
3468

src/modules/silentpayments/main_impl.h

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,98 @@
99
#include "../../../include/secp256k1.h"
1010
#include "../../../include/secp256k1_silentpayments.h"
1111

12-
/* TODO: implement functions for sender side. */
12+
/** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Inputs". */
13+
static void secp256k1_silentpayments_sha256_init_inputs(secp256k1_sha256* hash) {
14+
secp256k1_sha256_initialize(hash);
15+
hash->s[0] = 0xd4143ffcul;
16+
hash->s[1] = 0x012ea4b5ul;
17+
hash->s[2] = 0x36e21c8ful;
18+
hash->s[3] = 0xf7ec7b54ul;
19+
hash->s[4] = 0x4dd4e2acul;
20+
hash->s[5] = 0x9bcaa0a4ul;
21+
hash->s[6] = 0xe244899bul;
22+
hash->s[7] = 0xcd06903eul;
23+
24+
hash->bytes = 64;
25+
}
26+
27+
static void secp256k1_silentpayments_calculate_input_hash(secp256k1_scalar *input_hash_scalar, const unsigned char *outpoint_smallest36, secp256k1_ge *pubkey_sum) {
28+
secp256k1_sha256 hash;
29+
unsigned char hash_ser[32];
30+
unsigned char pubkey_sum_ser[33];
31+
size_t ser_size;
32+
int ser_ret;
33+
34+
secp256k1_silentpayments_sha256_init_inputs(&hash);
35+
secp256k1_sha256_write(&hash, outpoint_smallest36, 36);
36+
ser_ret = secp256k1_eckey_pubkey_serialize(pubkey_sum, pubkey_sum_ser, &ser_size, 1);
37+
VERIFY_CHECK(ser_ret && ser_size == sizeof(pubkey_sum_ser));
38+
(void)ser_ret;
39+
secp256k1_sha256_write(&hash, pubkey_sum_ser, sizeof(pubkey_sum_ser));
40+
secp256k1_sha256_finalize(&hash, hash_ser);
41+
secp256k1_scalar_set_b32(input_hash_scalar, hash_ser, NULL);
42+
}
43+
44+
int secp256k1_silentpayments_create_private_tweak_data(const secp256k1_context *ctx, unsigned char *private_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 *outpoint_smallest36) {
45+
size_t i;
46+
secp256k1_scalar a_sum, addend;
47+
secp256k1_ge A_sum_ge;
48+
secp256k1_gej A_sum_gej;
49+
secp256k1_scalar input_hash_scalar;
50+
51+
/* Sanity check inputs. */
52+
VERIFY_CHECK(ctx != NULL);
53+
ARG_CHECK(private_tweak_data32 != NULL);
54+
memset(private_tweak_data32, 0, 32);
55+
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
56+
ARG_CHECK(plain_seckeys == NULL || n_plain_seckeys >= 1);
57+
ARG_CHECK(taproot_seckeys == NULL || n_taproot_seckeys >= 1);
58+
ARG_CHECK((plain_seckeys != NULL) || (taproot_seckeys != NULL));
59+
ARG_CHECK((n_plain_seckeys + n_taproot_seckeys) >= 1);
60+
ARG_CHECK(outpoint_smallest36 != NULL);
61+
62+
/* Compute input private keys sum: a_sum = a_1 + a_2 + ... + a_n */
63+
a_sum = secp256k1_scalar_zero;
64+
for (i = 0; i < n_plain_seckeys; i++) {
65+
int ret = secp256k1_scalar_set_b32_seckey(&addend, &plain_seckeys[i*32]);
66+
VERIFY_CHECK(ret);
67+
(void)ret;
68+
69+
secp256k1_scalar_add(&a_sum, &a_sum, &addend);
70+
VERIFY_CHECK(!secp256k1_scalar_is_zero(&a_sum));
71+
}
72+
/* private keys used for taproot outputs have to be negated if they resulted in an odd point */
73+
for (i = 0; i < n_taproot_seckeys; i++) {
74+
secp256k1_ge addend_point;
75+
int ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &addend, &addend_point, &taproot_seckeys[i*32]);
76+
VERIFY_CHECK(ret);
77+
(void)ret;
78+
/* declassify addend_point to allow using it as a branch point (this is fine because addend_point is not a secret) */
79+
secp256k1_declassify(ctx, &addend_point, sizeof(addend_point));
80+
secp256k1_fe_normalize_var(&addend_point.y);
81+
if (secp256k1_fe_is_odd(&addend_point.y)) {
82+
secp256k1_scalar_negate(&addend, &addend);
83+
}
84+
85+
secp256k1_scalar_add(&a_sum, &a_sum, &addend);
86+
VERIFY_CHECK(!secp256k1_scalar_is_zero(&a_sum));
87+
}
88+
if (secp256k1_scalar_is_zero(&a_sum)) {
89+
/* TODO: do we need a special error return code for this case? */
90+
return 0;
91+
}
92+
93+
/* Compute input_hash = hash(outpoint_L || A_sum) */
94+
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &A_sum_gej, &a_sum);
95+
secp256k1_ge_set_gej(&A_sum_ge, &A_sum_gej);
96+
secp256k1_silentpayments_calculate_input_hash(&input_hash_scalar, outpoint_smallest36, &A_sum_ge);
97+
98+
/* Compute a_tweaked = a_sum * input_hash */
99+
secp256k1_scalar_mul(&a_sum, &a_sum, &input_hash_scalar);
100+
secp256k1_scalar_get_b32(private_tweak_data32, &a_sum);
101+
102+
return 1;
103+
}
13104

14105
/* TODO: implement functions for receiver side. */
15106

0 commit comments

Comments
 (0)