-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add BIP352 module (take 3) #1698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bb2c5bf
6fe3a7a
28917fa
55dce8c
14b1bf0
a0b77dc
9c6d949
e759094
568e3ae
c7d3827
29bf593
a0d2a33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,60 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_sender_c | |
| size_t n_plain_seckeys | ||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5); | ||
|
|
||
| /** Create Silent Payment label tweak and label. | ||
| * | ||
| * Given a recipient's 32 byte scan key and a label integer m, calculate the | ||
| * corresponding label tweak and label: | ||
| * | ||
| * label_tweak = hash(scan_key || m) | ||
| * label = label_tweak * G | ||
| * | ||
| * Returns: 1 if label tweak and label creation was successful. | ||
| * 0 if hash output label_tweak32 is not valid scalar (negligible | ||
| * probability per hash evaluation). | ||
| * | ||
| * Args: ctx: pointer to a context object | ||
| * (not secp256k1_context_static) | ||
| * Out: label: pointer to the resulting label public key | ||
| * label_tweak32: pointer to the 32 byte label tweak | ||
| * In: scan_key32: pointer to the recipient's 32 byte scan key | ||
| * m: integer for the m-th label (0 is used for change outputs) | ||
| */ | ||
| SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_create_label( | ||
| const secp256k1_context *ctx, | ||
| secp256k1_pubkey *label, | ||
| unsigned char *label_tweak32, | ||
| const unsigned char *scan_key32, | ||
| const uint32_t m | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: not saying it's wrong, but it seems unnecessary and at least very unusual to use |
||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); | ||
|
|
||
| /** Create Silent Payment labeled spend public key. | ||
| * | ||
| * Given a recipient's spend public key and a label, calculate the | ||
| * corresponding labeled spend public key: | ||
| * | ||
| * labeled_spend_pubkey = unlabeled_spend_pubkey + label | ||
| * | ||
| * The result is used by the recipient to create a Silent Payment address, | ||
| * consisting of the serialized and concatenated scan public key and | ||
| * (labeled) spend public key. | ||
| * | ||
| * Returns: 1 if labeled spend public key creation was successful. | ||
| * 0 if spend pubkey and label sum to zero (negligible probability for | ||
| * labels created according to BIP352). | ||
| * | ||
| * Args: ctx: pointer to a context object | ||
| * Out: labeled_spend_pubkey: pointer to the resulting labeled spend public key | ||
| * In: unlabeled_spend_pubkey: pointer to the recipient's unlabeled spend public key | ||
| * label: pointer to the recipient's label public key | ||
| */ | ||
| SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipient_create_labeled_spend_pubkey( | ||
| const secp256k1_context *ctx, | ||
| secp256k1_pubkey *labeled_spend_pubkey, | ||
| const secp256k1_pubkey *unlabeled_spend_pubkey, | ||
| const secp256k1_pubkey *label | ||
| ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,4 +333,74 @@ int secp256k1_silentpayments_sender_create_outputs( | |
| return 1; | ||
| } | ||
|
|
||
| /** Set hash state to the BIP340 tagged hash midstate for "BIP0352/Label". */ | ||
| static void secp256k1_silentpayments_sha256_init_label(secp256k1_sha256* hash) { | ||
| secp256k1_sha256_initialize(hash); | ||
| hash->s[0] = 0x26b95d63ul; | ||
| hash->s[1] = 0x8bf1b740ul; | ||
| hash->s[2] = 0x10a5986ful; | ||
| hash->s[3] = 0x06a387a5ul; | ||
| hash->s[4] = 0x2d1c1c30ul; | ||
| hash->s[5] = 0xd035951aul; | ||
| hash->s[6] = 0x2d7f0f96ul; | ||
| hash->s[7] = 0x29e3e0dbul; | ||
|
|
||
| hash->bytes = 64; | ||
| } | ||
|
|
||
| int secp256k1_silentpayments_recipient_create_label(const secp256k1_context *ctx, secp256k1_pubkey *label, unsigned char *label_tweak32, const unsigned char *scan_key32, const uint32_t m) { | ||
| secp256k1_sha256 hash; | ||
| unsigned char m_serialized[4]; | ||
|
|
||
| /* Sanity check inputs. */ | ||
| VERIFY_CHECK(ctx != NULL); | ||
| ARG_CHECK(label != NULL); | ||
| ARG_CHECK(label_tweak32 != NULL); | ||
| ARG_CHECK(scan_key32 != NULL); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in _recipient_create_label: I think it would make sense to also error if the passed scan key is invalid, to prevent the user of creating unspendable labels |
||
|
|
||
| /* Compute hash(ser_256(b_scan) || ser_32(m)) [sha256 with tag "BIP0352/Label"] */ | ||
| secp256k1_silentpayments_sha256_init_label(&hash); | ||
| secp256k1_sha256_write(&hash, scan_key32, 32); | ||
| secp256k1_write_be32(m_serialized, m); | ||
| secp256k1_sha256_write(&hash, m_serialized, sizeof(m_serialized)); | ||
| secp256k1_sha256_finalize(&hash, label_tweak32); | ||
|
|
||
josibake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| secp256k1_memclear_explicit(m_serialized, sizeof(m_serialized)); | ||
| secp256k1_sha256_clear(&hash); | ||
| return secp256k1_ec_pubkey_create(ctx, label, label_tweak32); | ||
| } | ||
|
|
||
| int secp256k1_silentpayments_recipient_create_labeled_spend_pubkey(const secp256k1_context *ctx, secp256k1_pubkey *labeled_spend_pubkey, const secp256k1_pubkey *unlabeled_spend_pubkey, const secp256k1_pubkey *label) { | ||
| secp256k1_ge labeled_spend_pubkey_ge, label_addend; | ||
| secp256k1_gej result_gej; | ||
| secp256k1_ge result_ge; | ||
| int ret; | ||
|
|
||
| /* Sanity check inputs. */ | ||
| VERIFY_CHECK(ctx != NULL); | ||
| ARG_CHECK(labeled_spend_pubkey != NULL); | ||
| ARG_CHECK(unlabeled_spend_pubkey != NULL); | ||
| ARG_CHECK(label != NULL); | ||
|
|
||
| /* Calculate labeled_spend_pubkey = spend_pubkey + label. | ||
| * If either the label or spend public key is an invalid public key, | ||
| * return early | ||
| */ | ||
| ret = secp256k1_pubkey_load(ctx, &labeled_spend_pubkey_ge, unlabeled_spend_pubkey); | ||
| ret &= secp256k1_pubkey_load(ctx, &label_addend, label); | ||
| if (!ret) { | ||
| return 0; | ||
| } | ||
| secp256k1_gej_set_ge(&result_gej, &labeled_spend_pubkey_ge); | ||
| secp256k1_gej_add_ge_var(&result_gej, &result_gej, &label_addend, NULL); | ||
| if (secp256k1_gej_is_infinity(&result_gej)) { | ||
| return 0; | ||
| } | ||
|
|
||
| secp256k1_ge_set_gej_var(&result_ge, &result_gej); | ||
| secp256k1_pubkey_save(labeled_spend_pubkey, &result_ge); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.