@@ -467,19 +467,18 @@ static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *m
467467const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979 ;
468468const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979 ;
469469
470- int secp256k1_ecdsa_sign (const secp256k1_context * ctx , secp256k1_ecdsa_signature * signature , const unsigned char * msg32 , const unsigned char * seckey , secp256k1_nonce_function noncefp , const void * noncedata ) {
471- /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
472- secp256k1_scalar r = secp256k1_scalar_zero , s = secp256k1_scalar_zero ;
470+ static int secp256k1_ecdsa_sign_inner (const secp256k1_context * ctx , secp256k1_scalar * r , secp256k1_scalar * s , int * recid , const unsigned char * msg32 , const unsigned char * seckey , secp256k1_nonce_function noncefp , const void * noncedata ) {
473471 secp256k1_scalar sec , non , msg ;
474472 int ret = 0 ;
475473 int is_sec_valid ;
476474 unsigned char nonce32 [32 ];
477475 unsigned int count = 0 ;
478- VERIFY_CHECK (ctx != NULL );
479- ARG_CHECK (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx ));
480- ARG_CHECK (msg32 != NULL );
481- ARG_CHECK (signature != NULL );
482- ARG_CHECK (seckey != NULL );
476+ /* Default initialization here is important so we won't pass uninit values to the cmov in the end */
477+ * r = secp256k1_scalar_zero ;
478+ * s = secp256k1_scalar_zero ;
479+ if (recid ) {
480+ * recid = 0 ;
481+ }
483482 if (noncefp == NULL ) {
484483 noncefp = secp256k1_nonce_function_default ;
485484 }
@@ -498,7 +497,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
498497 /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
499498 secp256k1_declassify (ctx , & is_nonce_valid , sizeof (is_nonce_valid ));
500499 if (is_nonce_valid ) {
501- ret = secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , & r , & s , & sec , & msg , & non , NULL );
500+ ret = secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , r , s , & sec , & msg , & non , recid );
502501 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
503502 secp256k1_declassify (ctx , & ret , sizeof (ret ));
504503 if (ret ) {
@@ -515,8 +514,25 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
515514 secp256k1_scalar_clear (& msg );
516515 secp256k1_scalar_clear (& non );
517516 secp256k1_scalar_clear (& sec );
518- secp256k1_scalar_cmov (& r , & secp256k1_scalar_zero , !ret );
519- secp256k1_scalar_cmov (& s , & secp256k1_scalar_zero , !ret );
517+ secp256k1_scalar_cmov (r , & secp256k1_scalar_zero , !ret );
518+ secp256k1_scalar_cmov (s , & secp256k1_scalar_zero , !ret );
519+ if (recid ) {
520+ const int zero = 0 ;
521+ secp256k1_int_cmov (recid , & zero , !ret );
522+ }
523+ return ret ;
524+ }
525+
526+ int secp256k1_ecdsa_sign (const secp256k1_context * ctx , secp256k1_ecdsa_signature * signature , const unsigned char * msg32 , const unsigned char * seckey , secp256k1_nonce_function noncefp , const void * noncedata ) {
527+ secp256k1_scalar r , s ;
528+ int ret ;
529+ VERIFY_CHECK (ctx != NULL );
530+ ARG_CHECK (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx ));
531+ ARG_CHECK (msg32 != NULL );
532+ ARG_CHECK (signature != NULL );
533+ ARG_CHECK (seckey != NULL );
534+
535+ ret = secp256k1_ecdsa_sign_inner (ctx , & r , & s , NULL , msg32 , seckey , noncefp , noncedata );
520536 secp256k1_ecdsa_signature_save (signature , & r , & s );
521537 return ret ;
522538}
0 commit comments