@@ -965,7 +965,63 @@ static void test_schnorrsig_taproot(void) {
965
965
CHECK (secp256k1_xonly_pubkey_tweak_add_check (CTX , output_pk_bytes , pk_parity , & internal_pk , tweak ) == 1 );
966
966
}
967
967
968
- static void run_schnorrsig_tests (void ) {
968
+ void test_s2c_opening (void ) {
969
+ int i = 0 ;
970
+ unsigned char output [33 ];
971
+ /* First byte 0x06 means that nonce_is_negated and EVEN tag for the
972
+ * following compressed pubkey (which is valid). */
973
+ unsigned char input [33 ] = {
974
+ 0x06 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
975
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
976
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
977
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
978
+ 0x02
979
+ };
980
+ secp256k1_schnorrsig_s2c_opening opening ;
981
+
982
+ /* Uninitialized opening can't be serialized. Actually testing that would be
983
+ * undefined behavior. Therefore we simulate it by setting the opening to 0. */
984
+ memset (& opening , 0 , sizeof (opening ));
985
+ CHECK_ILLEGAL (CTX , secp256k1_schnorrsig_s2c_opening_serialize (CTX , output , & opening ));
986
+
987
+ /* First parsing, then serializing works */
988
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input ) == 1 );
989
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (CTX , output , & opening ) == 1 );
990
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input ) == 1 );
991
+
992
+ {
993
+ /* Invalid pubkey makes parsing fail */
994
+ unsigned char input_tmp [33 ];
995
+ memcpy (input_tmp , input , sizeof (input_tmp ));
996
+ /* Pubkey oddness tag is invalid */
997
+ input_tmp [0 ] = 0 ;
998
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input_tmp ) == 0 );
999
+ /* nonce_is_negated bit is set but pubkey oddness tag is invalid */
1000
+ input_tmp [0 ] = 5 ;
1001
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input_tmp ) == 0 );
1002
+ /* Unknown bit is set */
1003
+ input_tmp [0 ] = 8 ;
1004
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input_tmp ) == 0 );
1005
+ }
1006
+
1007
+ /* Try parsing and serializing a bunch of openings */
1008
+ do {
1009
+ /* This is expected to fail in about 50% of iterations because the
1010
+ * points' x-coordinates are uniformly random */
1011
+ if (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input ) == 1 ) {
1012
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (CTX , output , & opening ) == 1 );
1013
+ CHECK (memcmp (output , input , sizeof (output )) == 0 );
1014
+ }
1015
+ testrand256 (& input [1 ]);
1016
+ /* Set pubkey oddness tag to first bit of input[1] */
1017
+ input [0 ] = (input [1 ] & 1 ) + 2 ;
1018
+ /* Set nonce_is_negated bit to input[1]'s 3rd bit */
1019
+ input [0 ] |= (input [1 ] & (1 << 2 ));
1020
+ i ++ ;
1021
+ } while (i < COUNT );
1022
+ }
1023
+
1024
+ void run_schnorrsig_tests (void ) {
969
1025
int i ;
970
1026
run_nonce_function_bip340_tests ();
971
1027
@@ -977,6 +1033,7 @@ static void run_schnorrsig_tests(void) {
977
1033
test_schnorrsig_sign_verify ();
978
1034
}
979
1035
test_schnorrsig_taproot ();
1036
+ test_s2c_opening ();
980
1037
}
981
1038
982
1039
#endif
0 commit comments