@@ -888,6 +888,67 @@ void test_schnorrsig_taproot(void) {
888
888
CHECK (secp256k1_xonly_pubkey_tweak_add_check (ctx , output_pk_bytes , pk_parity , & internal_pk , tweak ) == 1 );
889
889
}
890
890
891
+ void test_s2c_opening (void ) {
892
+ int i = 0 ;
893
+ unsigned char output [33 ];
894
+ /* First byte 0x06 means that nonce_is_negated and EVEN tag for the
895
+ * following compressed pubkey (which is valid). */
896
+ unsigned char input [33 ] = {
897
+ 0x06 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
898
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
899
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
900
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
901
+ 0x02
902
+ };
903
+ secp256k1_schnorrsig_s2c_opening opening ;
904
+ size_t ecount = 0 ;
905
+
906
+ secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount );
907
+
908
+ /* Uninitialized opening can't be serialized. Actually testing that would be
909
+ * undefined behavior. Therefore we simulate it by setting the opening to 0. */
910
+ memset (& opening , 0 , sizeof (opening ));
911
+ CHECK (ecount == 0 );
912
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (ctx , output , & opening ) == 0 );
913
+ CHECK (ecount == 1 );
914
+
915
+ /* First parsing, then serializing works */
916
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (ctx , & opening , input ) == 1 );
917
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (ctx , output , & opening ) == 1 );
918
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (ctx , & opening , input ) == 1 );
919
+
920
+ {
921
+ /* Invalid pubkey makes parsing fail */
922
+ unsigned char input_tmp [33 ];
923
+ memcpy (input_tmp , input , sizeof (input_tmp ));
924
+ /* Pubkey oddness tag is invalid */
925
+ input_tmp [0 ] = 0 ;
926
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (ctx , & opening , input_tmp ) == 0 );
927
+ /* nonce_is_negated bit is set but pubkey oddness tag is invalid */
928
+ input_tmp [0 ] = 5 ;
929
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (ctx , & opening , input_tmp ) == 0 );
930
+ /* Unknown bit is set */
931
+ input_tmp [0 ] = 8 ;
932
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (ctx , & opening , input_tmp ) == 0 );
933
+ }
934
+
935
+ /* Try parsing and serializing a bunch of openings */
936
+ do {
937
+ /* This is expected to fail in about 50% of iterations because the
938
+ * points' x-coordinates are uniformly random */
939
+ if (secp256k1_schnorrsig_s2c_opening_parse (ctx , & opening , input ) == 1 ) {
940
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (ctx , output , & opening ) == 1 );
941
+ CHECK (memcmp (output , input , sizeof (output )) == 0 );
942
+ }
943
+ secp256k1_testrand256 (& input [1 ]);
944
+ /* Set pubkey oddness tag to first bit of input[1] */
945
+ input [0 ] = (input [1 ] & 1 ) + 2 ;
946
+ /* Set nonce_is_negated bit to input[1]'s 3rd bit */
947
+ input [0 ] |= (input [1 ] & (1 << 2 ));
948
+ i ++ ;
949
+ } while (i < count );
950
+ }
951
+
891
952
void run_schnorrsig_tests (void ) {
892
953
int i ;
893
954
run_nonce_function_bip340_tests ();
@@ -900,6 +961,7 @@ void run_schnorrsig_tests(void) {
900
961
test_schnorrsig_sign_verify ();
901
962
}
902
963
test_schnorrsig_taproot ();
964
+ test_s2c_opening ();
903
965
}
904
966
905
967
#endif
0 commit comments