Skip to content

Commit cd9ded4

Browse files
committed
silentpayments: add shared secret creation routine for receiver (A*b)
1 parent 2290e80 commit cd9ded4

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

include/secp256k1_silentpayments.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_p
123123
const unsigned char *outpoint_smallest36
124124
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(7);
125125

126+
/** Create Silent Payment shared secret for the receiver side.
127+
*
128+
* Given public input tweak data A_tweaked and a recipient's scan private key
129+
* b_scan, compute the corresponding shared secret using ECDH:
130+
*
131+
* shared_secret = A_tweaked * b_scan
132+
* (where A_tweaked = (A_1 + A_2 + ... + A_n) * input_hash)
133+
*
134+
* The resulting data is needed as input for creating silent payments outputs
135+
* belonging to the same receiver scan public key.
136+
*
137+
* Returns: 1 if shared secret creation was successful. 0 if an error occured.
138+
* Args: ctx: pointer to a context object
139+
* Out: shared_secret33: pointer to the resulting 33-byte shared secret
140+
* In: public_tweak_data: pointer to the public tweak data
141+
* receiver_scan_seckey: pointer to the receiver's scan private key
142+
*/
143+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_receive_create_shared_secret(
144+
const secp256k1_context *ctx,
145+
unsigned char *shared_secret33,
146+
const secp256k1_pubkey *public_tweak_data,
147+
const unsigned char *receiver_scan_seckey
148+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
149+
126150
#ifdef __cplusplus
127151
}
128152
#endif

src/modules/silentpayments/main_impl.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ int secp256k1_silentpayments_create_public_tweak_data(const secp256k1_context *c
187187
return 1;
188188
}
189189

190-
/* TODO: implement functions for receiver side. */
190+
int secp256k1_silentpayments_receive_create_shared_secret(const secp256k1_context *ctx, unsigned char *shared_secret33, const secp256k1_pubkey *public_tweak_data, const unsigned char *receiver_scan_seckey) {
191+
/* Sanity check inputs. */
192+
VERIFY_CHECK(ctx != NULL);
193+
ARG_CHECK(shared_secret33 != NULL);
194+
memset(shared_secret33, 0, 33);
195+
ARG_CHECK(public_tweak_data != NULL);
196+
ARG_CHECK(receiver_scan_seckey != NULL);
197+
198+
/* Compute shared_secret = A_tweaked * b_scan */
199+
if (!secp256k1_ecdh(ctx, shared_secret33, public_tweak_data, receiver_scan_seckey, secp256k1_silentpayments_ecdh_return_pubkey, NULL)) {
200+
return 0;
201+
}
202+
203+
return 1;
204+
}
191205

192206
#endif

0 commit comments

Comments
 (0)