@@ -753,6 +753,42 @@ void test_schnorrsig_sign_verify(void) {
753753}
754754#undef N_SIGS
755755
756+ void test_schnorrsig_taproot (void ) {
757+ unsigned char sk [32 ];
758+ secp256k1_keypair keypair ;
759+ secp256k1_xonly_pubkey internal_pk ;
760+ unsigned char internal_pk_bytes [32 ];
761+ secp256k1_xonly_pubkey output_pk ;
762+ unsigned char output_pk_bytes [32 ];
763+ unsigned char tweak [32 ];
764+ int pk_parity ;
765+ unsigned char msg [32 ];
766+ unsigned char sig [64 ];
767+
768+ /* Create output key */
769+ secp256k1_rand256 (sk );
770+ CHECK (secp256k1_keypair_create (ctx , & keypair , sk ) == 1 );
771+ CHECK (secp256k1_keypair_xonly_pub (ctx , & internal_pk , NULL , & keypair ) == 1 );
772+ /* In actual taproot the tweak would be hash of internal_pk */
773+ CHECK (secp256k1_xonly_pubkey_serialize (ctx , tweak , & internal_pk ) == 1 );
774+ CHECK (secp256k1_keypair_xonly_tweak_add (ctx , & keypair , tweak ) == 1 );
775+ CHECK (secp256k1_keypair_xonly_pub (ctx , & output_pk , & pk_parity , & keypair ) == 1 );
776+ CHECK (secp256k1_xonly_pubkey_serialize (ctx , output_pk_bytes , & output_pk ) == 1 );
777+
778+ /* Key spend */
779+ secp256k1_rand256 (msg );
780+ CHECK (secp256k1_schnorrsig_sign (ctx , sig , msg , & keypair , NULL , NULL ) == 1 );
781+ /* Verify key spend */
782+ CHECK (secp256k1_xonly_pubkey_parse (ctx , & output_pk , output_pk_bytes ) == 1 );
783+ CHECK (secp256k1_schnorrsig_verify (ctx , sig , msg , & output_pk ) == 1 );
784+
785+ /* Script spend */
786+ CHECK (secp256k1_xonly_pubkey_serialize (ctx , internal_pk_bytes , & internal_pk ) == 1 );
787+ /* Verify script spend */
788+ CHECK (secp256k1_xonly_pubkey_parse (ctx , & internal_pk , internal_pk_bytes ) == 1 );
789+ CHECK (secp256k1_xonly_pubkey_tweak_add_check (ctx , output_pk_bytes , pk_parity , & internal_pk , tweak ) == 1 );
790+ }
791+
756792void run_schnorrsig_tests (void ) {
757793 int i ;
758794 run_nonce_function_bip340_tests ();
@@ -764,6 +800,7 @@ void run_schnorrsig_tests(void) {
764800 test_schnorrsig_sign ();
765801 test_schnorrsig_sign_verify ();
766802 }
803+ test_schnorrsig_taproot ();
767804}
768805
769806#endif
0 commit comments