Skip to content

Commit c9882bc

Browse files
committed
test: add simple BIP-352 test with mixed plain/taproot inputs
1 parent 6ee1131 commit c9882bc

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/***********************************************************************
2+
* Distributed under the MIT software license, see the accompanying *
3+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4+
***********************************************************************/
5+
6+
#ifndef SECP256K1_MODULE_SILENTPAYMENTS_TESTS_H
7+
#define SECP256K1_MODULE_SILENTPAYMENTS_TESTS_H
8+
9+
#include "../../../include/secp256k1_silentpayments.h"
10+
11+
void run_silentpayments_tests(void) {
12+
/* BIP-352 test vector
13+
* "Single recipient: taproot input with odd y-value and non-taproot input" */
14+
unsigned char outpoints_hash[32] = {
15+
0x21,0x0f,0xef,0x5d,0x62,0x4d,0xb1,0x7c,0x96,0x5c,0x75,0x97,0xe2,0xc6,0xc9,0xf6,
16+
0x0e,0xf4,0x40,0xc8,0x31,0xd1,0x49,0xc4,0x35,0x67,0xc5,0x01,0x58,0x55,0x7f,0x12
17+
};
18+
unsigned char input_privkeys_plain[32] = {
19+
0x8d,0x47,0x51,0xf6,0xe8,0xa3,0x58,0x68,0x80,0xfb,0x66,0xc1,0x9a,0xe2,0x77,0x96,
20+
0x9b,0xd5,0xaa,0x06,0xf6,0x1c,0x4e,0xe2,0xf1,0xe2,0x48,0x6e,0xfd,0xf6,0x66,0xd3
21+
};
22+
unsigned char input_privkeys_taproot[32] = {
23+
0x03,0x78,0xe9,0x56,0x85,0xb7,0x45,0x65,0xfa,0x56,0x75,0x1b,0x84,0xa3,0x2d,0xfd,
24+
0x18,0x54,0x5d,0x10,0xd6,0x91,0x64,0x1b,0x83,0x72,0xe3,0x21,0x64,0xfa,0xd6,0x6a
25+
};
26+
unsigned char input_pubkeys_plain[33] = {0x03,
27+
0xe0,0xec,0x4f,0x64,0xb3,0xfa,0x2e,0x46,0x3c,0xcf,0xcf,0x4e,0x85,0x6e,0x37,0xd5,
28+
0xe1,0xe2,0x02,0x75,0xbc,0x89,0xec,0x1d,0xef,0x9e,0xb0,0x98,0xef,0xf1,0xf8,0x5d
29+
};
30+
unsigned char input_pubkeys_xonly[32] = {
31+
0x78,0x2e,0xeb,0x91,0x34,0x31,0xca,0x6e,0x9b,0x8c,0x2f,0xd8,0x0a,0x5f,0x72,0xed,
32+
0x20,0x24,0xef,0x72,0xa3,0xc6,0xfb,0x10,0x26,0x3c,0x37,0x99,0x37,0x32,0x33,0x38
33+
};
34+
unsigned char receiver_scan_privkey[32] = {
35+
0x0f,0x69,0x4e,0x06,0x80,0x28,0xa7,0x17,0xf8,0xaf,0x6b,0x94,0x11,0xf9,0xa1,0x33,
36+
0xdd,0x35,0x65,0x25,0x87,0x14,0xcc,0x22,0x65,0x94,0xb3,0x4d,0xb9,0x0c,0x1f,0x2c
37+
};
38+
unsigned char receiver_scan_pubkey[33] = {0x02,
39+
0x20,0xbc,0xfa,0xc5,0xb9,0x9e,0x04,0xad,0x1a,0x06,0xdd,0xfb,0x01,0x6e,0xe1,0x35,
40+
0x82,0x60,0x9d,0x60,0xb6,0x29,0x1e,0x98,0xd0,0x1a,0x9b,0xc9,0xa1,0x6c,0x96,0xd4
41+
};
42+
unsigned char receiver_spend_privkey[32] = {
43+
0x9d,0x6a,0xd8,0x55,0xce,0x34,0x17,0xef,0x84,0xe8,0x36,0x89,0x2e,0x5a,0x56,0x39,
44+
0x2b,0xfb,0xa0,0x5f,0xa5,0xd9,0x7c,0xce,0xa3,0x0e,0x26,0x6f,0x54,0x0e,0x08,0xb3
45+
};
46+
unsigned char receiver_spend_pubkey[33] = {0x02,
47+
0x5c,0xc9,0x85,0x6d,0x6f,0x83,0x75,0x35,0x0e,0x12,0x39,0x78,0xda,0xac,0x20,0x0c,
48+
0x26,0x0c,0xb5,0xb5,0xae,0x83,0x10,0x6c,0xab,0x90,0x48,0x4d,0xcd,0x8f,0xcf,0x36
49+
};
50+
unsigned char output_expected[32] = {
51+
0x75,0xf5,0x01,0xf3,0x19,0xdb,0x54,0x9a,0xaa,0x61,0x37,0x17,0xbd,0x7a,0xf4,0x4d,
52+
0xa5,0x66,0xd4,0xd8,0x59,0xb6,0x7f,0xe4,0x36,0x94,0x65,0x64,0xfa,0xfc,0x47,0xa3
53+
};
54+
unsigned char privkey_tweak_expected[32] = {
55+
0x61,0x9a,0x5a,0x59,0xa1,0x6d,0x4a,0x8e,0x85,0x7e,0xf4,0x8e,0x63,0xef,0x7c,0x81,
56+
0x95,0xc8,0x58,0x19,0x1d,0x4e,0x82,0x62,0x05,0xe8,0x43,0x8a,0xb7,0x0d,0x05,0x9e
57+
};
58+
59+
unsigned char shared_secret_sender[33];
60+
unsigned char shared_secret_receiver[33];
61+
unsigned char public_tweak_data[33];
62+
unsigned char private_tweak_data[32];
63+
secp256k1_xonly_pubkey output_expected_xonly_obj;
64+
secp256k1_xonly_pubkey output_calculated_xonly_obj;
65+
unsigned char output_calculated[32];
66+
unsigned char privkey_calculated[32];
67+
unsigned char privkey_expected[32];
68+
69+
/* convert raw key material into secp256k1 objects where necessary */
70+
secp256k1_pubkey input_pubkey_plain_obj, receiver_scan_pubkey_obj, receiver_spend_pubkey_obj;
71+
secp256k1_xonly_pubkey input_pubkey_xonly_obj;
72+
CHECK(secp256k1_ec_pubkey_parse(CTX, &input_pubkey_plain_obj, input_pubkeys_plain, 33));
73+
CHECK(secp256k1_ec_pubkey_parse(CTX, &receiver_scan_pubkey_obj, receiver_scan_pubkey, 33));
74+
CHECK(secp256k1_ec_pubkey_parse(CTX, &receiver_spend_pubkey_obj, receiver_spend_pubkey, 33));
75+
CHECK(secp256k1_xonly_pubkey_parse(CTX, &input_pubkey_xonly_obj, input_pubkeys_xonly));
76+
CHECK(secp256k1_xonly_pubkey_parse(CTX, &output_expected_xonly_obj, output_expected));
77+
78+
/* create shared secret from sender and receiver perspective, and check that they match */
79+
CHECK(secp256k1_silentpayments_create_private_tweak_data(CTX, private_tweak_data,
80+
input_privkeys_plain, 1, input_privkeys_taproot, 1, outpoints_hash));
81+
CHECK(secp256k1_silentpayments_send_create_shared_secret(CTX, shared_secret_sender,
82+
private_tweak_data, &receiver_scan_pubkey_obj));
83+
84+
CHECK(secp256k1_silentpayments_create_public_tweak_data(CTX, public_tweak_data,
85+
&input_pubkey_plain_obj, 1, &input_pubkey_xonly_obj, 1, outpoints_hash));
86+
CHECK(secp256k1_silentpayments_receive_create_shared_secret(CTX, shared_secret_receiver,
87+
public_tweak_data, receiver_scan_privkey));
88+
89+
CHECK(secp256k1_memcmp_var(shared_secret_sender, shared_secret_receiver, 33) == 0);
90+
91+
/* check that calculated silent payments output matches */
92+
CHECK(secp256k1_silentpayments_create_output_pubkey(CTX, &output_calculated_xonly_obj,
93+
shared_secret_sender, &receiver_spend_pubkey_obj, 0, NULL));
94+
CHECK(secp256k1_xonly_pubkey_serialize(CTX, output_calculated, &output_calculated_xonly_obj));
95+
CHECK(secp256k1_memcmp_var(output_calculated, output_expected, 32) == 0);
96+
97+
/* check that calculated silent payment output spending private key matches */
98+
memcpy(privkey_expected, receiver_spend_privkey, 32);
99+
CHECK(secp256k1_ec_seckey_tweak_add(CTX, privkey_expected, privkey_tweak_expected));
100+
CHECK(secp256k1_silentpayments_create_output_seckey(CTX, privkey_calculated,
101+
shared_secret_receiver, receiver_spend_privkey, 0, NULL));
102+
CHECK(secp256k1_memcmp_var(privkey_calculated, privkey_expected, 32) == 0);
103+
}
104+
105+
#endif

src/tests.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7287,6 +7287,10 @@ static void run_ecdsa_wycheproof(void) {
72877287
# include "modules/ellswift/tests_impl.h"
72887288
#endif
72897289

7290+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
7291+
# include "modules/silentpayments/tests_impl.h"
7292+
#endif
7293+
72907294
static void run_secp256k1_memczero_test(void) {
72917295
unsigned char buf1[6] = {1, 2, 3, 4, 5, 6};
72927296
unsigned char buf2[sizeof(buf1)];
@@ -7635,6 +7639,10 @@ int main(int argc, char **argv) {
76357639
run_ellswift_tests();
76367640
#endif
76377641

7642+
#ifdef ENABLE_MODULE_SILENTPAYMENTS
7643+
run_silentpayments_tests();
7644+
#endif
7645+
76387646
/* util tests */
76397647
run_secp256k1_memczero_test();
76407648
run_secp256k1_byteorder_tests();

0 commit comments

Comments
 (0)