Skip to content

Commit c70d0e5

Browse files
committed
add secp256k1_silentpayments_verify_proof
1 parent c5db922 commit c70d0e5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

include/secp256k1_silentpayments.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,29 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipien
487487
const uint32_t k
488488
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
489489

490+
/** Verifies the Silent Payment proof. If the following algorithm succeeds, the points A and C were both generated from
491+
* the same scalar. The former from multiplying by G, and the latter from multiplying by B.
492+
*
493+
* Here, A refers to input public key sum (present in prevouts_summary)
494+
* B refers to recipient's scan pubkey
495+
* C refers to shared_secret point
496+
*
497+
* Returns: 1 if verification of proof was successful. 0 if an error occurred.
498+
* Args: ctx: pointer to a context object
499+
* In: shared_secret: 33 bytes shared secret
500+
* proof: 64 bytes DLEQ proof
501+
* recipient_scan_pubkey: pointer to the recipient's scan pubkey
502+
* prevouts_summary: pointer to the input public key sum (optionally, with the `input_hash` multiplied in,
503+
* see `_recipient_prevouts_summary_create`).
504+
*/
505+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_verify_proof(
506+
const secp256k1_context *ctx,
507+
const unsigned char *shared_secret33,
508+
const unsigned char *proof64,
509+
const secp256k1_pubkey *recipient_scan_pubkey,
510+
const secp256k1_silentpayments_prevouts_summary *prevouts_summary
511+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
512+
490513
/** Serialize a secp256k1_silentpayments_dleq_data object into a 101-byte sequence.
491514
* 101-byte sequence = 33 bytes shared secret + 64 bytes proof + 4 bytes index
492515
* where index is position in an array of pointers to silent payment recipients

src/modules/silentpayments/main_impl.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,35 @@ int secp256k1_silentpayments_recipient_create_output_pubkey(const secp256k1_cont
831831
return secp256k1_silentpayments_create_output_pubkey(ctx, output_xonly, shared_secret33, spend_pubkey, k);
832832
}
833833

834+
int secp256k1_silentpayments_verify_proof(const secp256k1_context *ctx, const unsigned char *shared_secret33, const unsigned char *proof64, const secp256k1_pubkey *recipient_scan_pubkey, const secp256k1_silentpayments_prevouts_summary *prevouts_summary)
835+
{
836+
secp256k1_scalar s;
837+
secp256k1_scalar e;
838+
secp256k1_pubkey pk;
839+
secp256k1_ge pubkey_sum;
840+
secp256k1_ge scan_pubkey;
841+
secp256k1_ge shared_secret;
842+
size_t pubkeylen = 33;
843+
unsigned char pubkey33[33];
844+
int ret = 1;
845+
846+
VERIFY_CHECK(ctx != NULL);
847+
ARG_CHECK(shared_secret33 != NULL);
848+
ARG_CHECK(proof64 != NULL);
849+
ARG_CHECK(recipient_scan_pubkey != NULL);
850+
ARG_CHECK(prevouts_summary != NULL);
851+
852+
ret &= secp256k1_silentpayments_recipient_prevouts_summary_serialize(ctx, pubkey33, prevouts_summary);
853+
ret &= secp256k1_ec_pubkey_parse(ctx, &pk, pubkey33, pubkeylen);
854+
ret &= secp256k1_pubkey_load(ctx, &pubkey_sum, &pk);
855+
ret &= secp256k1_pubkey_load(ctx, &scan_pubkey, recipient_scan_pubkey);
856+
ret &= secp256k1_ec_pubkey_parse(ctx, &pk, shared_secret33, pubkeylen);
857+
ret &= secp256k1_pubkey_load(ctx, &shared_secret, &pk);
858+
secp256k1_scalar_set_b32(&s, proof64, NULL);
859+
secp256k1_scalar_set_b32(&e, proof64 + 32, NULL);
860+
ret &= secp256k1_dleq_verify(&s, &e, &pubkey_sum, &scan_pubkey, &shared_secret, NULL);
861+
return ret;
862+
}
834863

835864
void secp256k1_silentpayments_dleq_data_serialize(unsigned char *output, const secp256k1_silentpayments_dleq_data *dleq_data) {
836865
memcpy(output, dleq_data->shared_secret, 33);

0 commit comments

Comments
 (0)