Skip to content

Commit 8b1f06b

Browse files
committed
silentpayments: add shared secret creation routine for receiver (A*b)
1 parent adff4de commit 8b1f06b

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
@@ -119,6 +119,30 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_p
119119
const unsigned char *outpoints_hash32
120120
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(7);
121121

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

src/modules/silentpayments/main_impl.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ int secp256k1_silentpayments_create_public_tweak_data(const secp256k1_context *c
179179
return 1;
180180
}
181181

182-
/* TODO: implement functions for receiver side. */
182+
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) {
183+
secp256k1_pubkey A_tweaked;
184+
185+
/* Sanity check inputs. */
186+
VERIFY_CHECK(ctx != NULL);
187+
ARG_CHECK(shared_secret33 != NULL);
188+
memset(shared_secret33, 0, 33);
189+
ARG_CHECK(tweak_data33 != NULL);
190+
ARG_CHECK(receiver_scan_seckey != NULL);
191+
192+
/* Parse tweak data into pubkey object */
193+
if (!secp256k1_ec_pubkey_parse(ctx, &A_tweaked, tweak_data33, 33)) {
194+
return 0;
195+
}
196+
197+
/* Compute shared_secret = A_tweaked * b_scan */
198+
if (!secp256k1_ecdh(ctx, shared_secret33, &A_tweaked, receiver_scan_seckey, ecdh_return_pubkey, NULL)) {
199+
return 0;
200+
}
201+
202+
return 1;
203+
}
183204

184205
#endif

0 commit comments

Comments
 (0)