@@ -4293,6 +4293,59 @@ void run_eckey_edge_case_test(void) {
42934293 secp256k1_context_set_illegal_callback (ctx , NULL , NULL );
42944294}
42954295
4296+
4297+ void run_s2c_opening_test (void ) {
4298+ int i = 0 ;
4299+ unsigned char output [34 ];
4300+ unsigned char input [34 ] = {
4301+ 0x01 ,
4302+ 0x02 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
4303+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
4304+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
4305+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
4306+ 0x02
4307+ };
4308+ secp256k1_s2c_opening opening ;
4309+ size_t ecount = 0 ;
4310+
4311+ secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount );
4312+
4313+ /* Uninitialized opening can't be serialized. Actually testing that would be
4314+ * undefined behavior. Therefore we simulate it by setting the opening to 0. */
4315+ memset (& opening , 0 , sizeof (opening ));
4316+ CHECK (ecount == 0 );
4317+ CHECK (secp256k1_s2c_opening_serialize (ctx , output , & opening ) == 0 );
4318+ CHECK (ecount == 1 );
4319+
4320+ /* First parsing, then serializing works */
4321+ CHECK (secp256k1_s2c_opening_parse (ctx , & opening , input ) == 1 );
4322+ CHECK (secp256k1_s2c_opening_serialize (ctx , output , & opening ) == 1 );
4323+
4324+ {
4325+ /* Invalid pubkey makes parsing fail */
4326+ unsigned char input_tmp [34 ];
4327+ memcpy (input_tmp , input , sizeof (input_tmp ));
4328+ input_tmp [33 ] = 0 ;
4329+ CHECK (secp256k1_s2c_opening_parse (ctx , & opening , input_tmp ) == 0 );
4330+ }
4331+
4332+ /* Try parsing and serializing a bunch of openings */
4333+ do {
4334+ /* This is expected to fail in about 50% of iterations because the
4335+ * points' x-coordinates are uniformly random */
4336+ if (secp256k1_s2c_opening_parse (ctx , & opening , input ) == 1 ) {
4337+ CHECK (secp256k1_s2c_opening_serialize (ctx , output , & opening ) == 1 );
4338+ CHECK (memcmp (output , input , 34 ) == 0 );
4339+ }
4340+ secp256k1_rand256 (input );
4341+ /* nonce_is_negated */
4342+ input [0 ] = input [0 ] & 1 ;
4343+ /* oddness */
4344+ input [1 ] = (input [1 ] % 2 ) + 2 ;
4345+ i ++ ;
4346+ } while (i < count );
4347+ }
4348+
42964349void random_sign (secp256k1_scalar * sigr , secp256k1_scalar * sigs , const secp256k1_scalar * key , const secp256k1_scalar * msg , int * recid ) {
42974350 secp256k1_scalar nonce ;
42984351 do {
@@ -5464,6 +5517,8 @@ int main(int argc, char **argv) {
54645517 /* EC key edge cases */
54655518 run_eckey_edge_case_test ();
54665519
5520+ run_s2c_opening_test ();
5521+
54675522#ifdef ENABLE_MODULE_ECDH
54685523 /* ecdh tests */
54695524 run_ecdh_tests ();
0 commit comments