Skip to content

Commit ce9c2b4

Browse files
committed
silentpayments: add shared secret creation routine for receiver (A*b)
1 parent 0acde7b commit ce9c2b4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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_lowest36
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: tweak_data33: pointer to 33-byte public input 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 unsigned char *tweak_data33,
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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,27 @@ int secp256k1_silentpayments_create_public_tweak_data(const secp256k1_context *c
195195
return 1;
196196
}
197197

198-
/* TODO: implement functions for receiver side. */
198+
int secp256k1_silentpayments_receive_create_shared_secret(const secp256k1_context *ctx, unsigned char *shared_secret33, const unsigned char *tweak_data33, const unsigned char *receiver_scan_seckey) {
199+
secp256k1_pubkey A_tweaked;
200+
201+
/* Sanity check inputs. */
202+
VERIFY_CHECK(ctx != NULL);
203+
ARG_CHECK(shared_secret33 != NULL);
204+
memset(shared_secret33, 0, 33);
205+
ARG_CHECK(tweak_data33 != NULL);
206+
ARG_CHECK(receiver_scan_seckey != NULL);
207+
208+
/* Parse tweak data into pubkey object */
209+
if (!secp256k1_ec_pubkey_parse(ctx, &A_tweaked, tweak_data33, 33)) {
210+
return 0;
211+
}
212+
213+
/* Compute shared_secret = A_tweaked * b_scan */
214+
if (!secp256k1_ecdh(ctx, shared_secret33, &A_tweaked, receiver_scan_seckey, secp256k1_silentpayments_ecdh_return_pubkey, NULL)) {
215+
return 0;
216+
}
217+
218+
return 1;
219+
}
199220

200221
#endif

0 commit comments

Comments
 (0)