Skip to content

Commit 8ba10b7

Browse files
committed
silentpayments: add label tweak calculation routine
1 parent cd9ded4 commit 8ba10b7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

include/secp256k1_silentpayments.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_receive_
147147
const unsigned char *receiver_scan_seckey
148148
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
149149

150+
/** Create Silent Payment label tweak.
151+
*
152+
* Given a recipient's scan private key b_scan and a label integer m, calculate
153+
* the corresponding label tweak:
154+
*
155+
* label_tweak = hash(b_scan || m)
156+
*
157+
* Returns: 1 if label tweak creation was successful. 0 if an error occured.
158+
* Args: ctx: pointer to a context object
159+
* Out: label_tweak: pointer to the resulting label tweak
160+
* In: receiver_scan_seckey: pointer to the receiver's scan private key
161+
* m: label integer (0 is used for change outputs)
162+
*/
163+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_label_tweak(
164+
const secp256k1_context *ctx,
165+
unsigned char *label_tweak32,
166+
const unsigned char *receiver_scan_seckey,
167+
unsigned int m
168+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
169+
150170
#ifdef __cplusplus
151171
}
152172
#endif

src/modules/silentpayments/main_impl.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,39 @@ int secp256k1_silentpayments_receive_create_shared_secret(const secp256k1_contex
203203
return 1;
204204
}
205205

206+
/** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Label". */
207+
static void secp256k1_silentpayments_sha256_init_label(secp256k1_sha256* hash) {
208+
secp256k1_sha256_initialize(hash);
209+
hash->s[0] = 0x26b95d63ul;
210+
hash->s[1] = 0x8bf1b740ul;
211+
hash->s[2] = 0x10a5986ful;
212+
hash->s[3] = 0x06a387a5ul;
213+
hash->s[4] = 0x2d1c1c30ul;
214+
hash->s[5] = 0xd035951aul;
215+
hash->s[6] = 0x2d7f0f96ul;
216+
hash->s[7] = 0x29e3e0dbul;
217+
218+
hash->bytes = 64;
219+
}
220+
221+
int secp256k1_silentpayments_create_label_tweak(const secp256k1_context *ctx, unsigned char *label_tweak32, const unsigned char *receiver_scan_seckey, unsigned int m) {
222+
secp256k1_sha256 hash;
223+
unsigned char m_serialized[4];
224+
225+
/* Sanity check inputs. */
226+
VERIFY_CHECK(ctx != NULL);
227+
(void)ctx;
228+
VERIFY_CHECK(label_tweak32 != NULL);
229+
VERIFY_CHECK(receiver_scan_seckey != NULL);
230+
231+
/* Compute label_tweak = hash(ser_256(b_scan) || ser_32(m)) [sha256 with tag "BIP0352/Label"] */
232+
secp256k1_silentpayments_sha256_init_label(&hash);
233+
secp256k1_sha256_write(&hash, receiver_scan_seckey, 32);
234+
secp256k1_write_be32(m_serialized, m);
235+
secp256k1_sha256_write(&hash, m_serialized, sizeof(m_serialized));
236+
secp256k1_sha256_finalize(&hash, label_tweak32);
237+
238+
return 1;
239+
}
240+
206241
#endif

0 commit comments

Comments
 (0)