Skip to content

Commit 6ee1131

Browse files
committed
silentpayments: implement output spending privkey creation (for receiver)
Labels are not supported yet, the corresponding parameter label_tweak32 must be set to NULL right now.
1 parent 64a451e commit 6ee1131

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

include/secp256k1_silentpayments.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,33 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_o
170170
const unsigned char *label_tweak32
171171
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
172172

173+
/** Create Silent Payment output private key (for spending receiver's funds).
174+
*
175+
* Given a shared_secret, a recipient's spend private key b_spend, and an
176+
* output counter k, calculate the corresponding output private key d:
177+
*
178+
* d = (b_spend + sha256(shared_secret || ser_32(k))) mod n
179+
*
180+
* Returns: 1 if private key creation was successful. 0 if an error occured.
181+
* Args: ctx: pointer to a context object
182+
* Out: output_seckey: pointer to the resulting spending private key
183+
* In: shared_secret33: shared secret, derived from either sender's
184+
* or receiver's perspective with routines from above
185+
* receiver_spend_seckey: pointer to the receiver's spend private key
186+
* k: output counter (usually set to 0, should be increased for
187+
* every additional output to the same recipient)
188+
* label_tweak32: an optional 32-byte label tweak
189+
* (not supported yet, must be set to NULL right now)
190+
*/
191+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_output_seckey(
192+
const secp256k1_context *ctx,
193+
unsigned char *output_seckey,
194+
const unsigned char *shared_secret33,
195+
const unsigned char *receiver_spend_seckey,
196+
unsigned int k,
197+
const unsigned char *label_tweak32
198+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
199+
173200
#ifdef __cplusplus
174201
}
175202
#endif

src/modules/silentpayments/main_impl.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,25 @@ int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context *ctx,
239239
return 1;
240240
}
241241

242+
int secp256k1_silentpayments_create_output_seckey(const secp256k1_context *ctx, unsigned char *output_seckey, const unsigned char *shared_secret33, const unsigned char *receiver_spend_seckey, unsigned int k, const unsigned char *label_tweak32) {
243+
unsigned char t_k[32];
244+
245+
/* Sanity check inputs */
246+
VERIFY_CHECK(ctx != NULL);
247+
ARG_CHECK(output_seckey != NULL);
248+
memset(output_seckey, 0, 32);
249+
ARG_CHECK(shared_secret33 != NULL);
250+
ARG_CHECK(receiver_spend_seckey != NULL);
251+
ARG_CHECK(label_tweak32 == NULL); /* label tweaks are not supported yet */
252+
253+
/* Compute and return d = (b_spend + t_k) mod n */
254+
memcpy(output_seckey, receiver_spend_seckey, 32);
255+
secp256k1_silentpayments_create_t_k(t_k, shared_secret33, k);
256+
if (!secp256k1_ec_seckey_tweak_add(ctx, output_seckey, t_k)) {
257+
return 0;
258+
}
259+
260+
return 1;
261+
}
262+
242263
#endif

0 commit comments

Comments
 (0)