@@ -862,7 +862,68 @@ static void test_schnorrsig_taproot(void) {
862
862
CHECK (secp256k1_xonly_pubkey_tweak_add_check (CTX , output_pk_bytes , pk_parity , & internal_pk , tweak ) == 1 );
863
863
}
864
864
865
- static void run_schnorrsig_tests (void ) {
865
+ void test_s2c_opening (void ) {
866
+ int i = 0 ;
867
+ unsigned char output [33 ];
868
+ /* First byte 0x06 means that nonce_is_negated and EVEN tag for the
869
+ * following compressed pubkey (which is valid). */
870
+ unsigned char input [33 ] = {
871
+ 0x06 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
872
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
873
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
874
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
875
+ 0x02
876
+ };
877
+ secp256k1_schnorrsig_s2c_opening opening ;
878
+ int32_t ecount = 0 ;
879
+
880
+ secp256k1_context_set_illegal_callback (CTX , counting_illegal_callback_fn , & ecount );
881
+
882
+ /* Uninitialized opening can't be serialized. Actually testing that would be
883
+ * undefined behavior. Therefore we simulate it by setting the opening to 0. */
884
+ memset (& opening , 0 , sizeof (opening ));
885
+ CHECK (ecount == 0 );
886
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (CTX , output , & opening ) == 0 );
887
+ CHECK (ecount == 1 );
888
+
889
+ /* First parsing, then serializing works */
890
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input ) == 1 );
891
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (CTX , output , & opening ) == 1 );
892
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input ) == 1 );
893
+
894
+ {
895
+ /* Invalid pubkey makes parsing fail */
896
+ unsigned char input_tmp [33 ];
897
+ memcpy (input_tmp , input , sizeof (input_tmp ));
898
+ /* Pubkey oddness tag is invalid */
899
+ input_tmp [0 ] = 0 ;
900
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input_tmp ) == 0 );
901
+ /* nonce_is_negated bit is set but pubkey oddness tag is invalid */
902
+ input_tmp [0 ] = 5 ;
903
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input_tmp ) == 0 );
904
+ /* Unknown bit is set */
905
+ input_tmp [0 ] = 8 ;
906
+ CHECK (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input_tmp ) == 0 );
907
+ }
908
+
909
+ /* Try parsing and serializing a bunch of openings */
910
+ do {
911
+ /* This is expected to fail in about 50% of iterations because the
912
+ * points' x-coordinates are uniformly random */
913
+ if (secp256k1_schnorrsig_s2c_opening_parse (CTX , & opening , input ) == 1 ) {
914
+ CHECK (secp256k1_schnorrsig_s2c_opening_serialize (CTX , output , & opening ) == 1 );
915
+ CHECK (memcmp (output , input , sizeof (output )) == 0 );
916
+ }
917
+ secp256k1_testrand256 (& input [1 ]);
918
+ /* Set pubkey oddness tag to first bit of input[1] */
919
+ input [0 ] = (input [1 ] & 1 ) + 2 ;
920
+ /* Set nonce_is_negated bit to input[1]'s 3rd bit */
921
+ input [0 ] |= (input [1 ] & (1 << 2 ));
922
+ i ++ ;
923
+ } while (i < COUNT );
924
+ }
925
+
926
+ void run_schnorrsig_tests (void ) {
866
927
int i ;
867
928
run_nonce_function_bip340_tests ();
868
929
@@ -874,6 +935,7 @@ static void run_schnorrsig_tests(void) {
874
935
test_schnorrsig_sign_verify ();
875
936
}
876
937
test_schnorrsig_taproot ();
938
+ test_s2c_opening ();
877
939
}
878
940
879
941
#endif
0 commit comments