Skip to content

Commit 4d04c4d

Browse files
committed
silentpayments: add shared secret creation routine for receiver (A*b)
1 parent 164bf8e commit 4d04c4d

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
@@ -125,6 +125,30 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_p
125125
const unsigned char *outpoint_smallest36
126126
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(7);
127127

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