@@ -471,7 +471,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
471471 secp256k1_scalar r , s ;
472472 secp256k1_scalar sec , non , msg ;
473473 int ret = 0 ;
474- int overflow = 0 ;
474+ int is_sec_valid ;
475475 unsigned char nonce32 [32 ];
476476 unsigned int count = 0 ;
477477 VERIFY_CHECK (ctx != NULL );
@@ -483,22 +483,20 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
483483 noncefp = secp256k1_nonce_function_default ;
484484 }
485485
486- secp256k1_scalar_set_b32 (& sec , seckey , & overflow );
487486 /* Fail if the secret key is invalid. */
488- overflow |= secp256k1_scalar_is_zero (& sec );
489- secp256k1_scalar_cmov (& sec , & secp256k1_scalar_one , overflow );
487+ is_sec_valid = secp256k1_scalar_set_b32_seckey (& sec , seckey );
488+ secp256k1_scalar_cmov (& sec , & secp256k1_scalar_one , ! is_sec_valid );
490489 secp256k1_scalar_set_b32 (& msg , msg32 , NULL );
491490 while (1 ) {
492- int koverflow ;
493- ret = noncefp (nonce32 , msg32 , seckey , NULL , (void * )noncedata , count );
491+ int is_nonce_valid ;
492+ ret = !! noncefp (nonce32 , msg32 , seckey , NULL , (void * )noncedata , count );
494493 if (!ret ) {
495494 break ;
496495 }
497- secp256k1_scalar_set_b32 (& non , nonce32 , & koverflow );
498- koverflow |= secp256k1_scalar_is_zero (& non );
499- /* The nonce is still secret here, but it overflowing or being zero is is less likely than 1:2^255. */
500- secp256k1_declassify (ctx , & koverflow , sizeof (koverflow ));
501- if (!koverflow ) {
496+ is_nonce_valid = secp256k1_scalar_set_b32_seckey (& non , nonce32 );
497+ /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */
498+ secp256k1_declassify (ctx , & is_nonce_valid , sizeof (is_nonce_valid ));
499+ if (is_nonce_valid ) {
502500 ret = secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , & r , & s , & sec , & msg , & non , NULL );
503501 /* The final signature is no longer a secret, nor is the fact that we were successful or not. */
504502 secp256k1_declassify (ctx , & ret , sizeof (ret ));
@@ -508,25 +506,27 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
508506 }
509507 count ++ ;
510508 }
509+ /* We don't want to declassify is_sec_valid and therefore the range of
510+ * seckey. As a result is_sec_valid is included in ret only after ret was
511+ * used as a branching variable. */
512+ ret &= is_sec_valid ;
511513 memset (nonce32 , 0 , 32 );
512514 secp256k1_scalar_clear (& msg );
513515 secp256k1_scalar_clear (& non );
514516 secp256k1_scalar_clear (& sec );
515- secp256k1_scalar_cmov (& r , & secp256k1_scalar_zero , ( !ret ) | overflow );
516- secp256k1_scalar_cmov (& s , & secp256k1_scalar_zero , ( !ret ) | overflow );
517+ secp256k1_scalar_cmov (& r , & secp256k1_scalar_zero , !ret );
518+ secp256k1_scalar_cmov (& s , & secp256k1_scalar_zero , !ret );
517519 secp256k1_ecdsa_signature_save (signature , & r , & s );
518- return !! ret & ! overflow ;
520+ return ret ;
519521}
520522
521523int secp256k1_ec_seckey_verify (const secp256k1_context * ctx , const unsigned char * seckey ) {
522524 secp256k1_scalar sec ;
523525 int ret ;
524- int overflow ;
525526 VERIFY_CHECK (ctx != NULL );
526527 ARG_CHECK (seckey != NULL );
527528
528- secp256k1_scalar_set_b32 (& sec , seckey , & overflow );
529- ret = !overflow & !secp256k1_scalar_is_zero (& sec );
529+ ret = secp256k1_scalar_set_b32_seckey (& sec , seckey );
530530 secp256k1_scalar_clear (& sec );
531531 return ret ;
532532}
@@ -535,16 +535,14 @@ int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *p
535535 secp256k1_gej pj ;
536536 secp256k1_ge p ;
537537 secp256k1_scalar sec ;
538- int overflow ;
539538 int ret = 0 ;
540539 VERIFY_CHECK (ctx != NULL );
541540 ARG_CHECK (pubkey != NULL );
542541 memset (pubkey , 0 , sizeof (* pubkey ));
543542 ARG_CHECK (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx ));
544543 ARG_CHECK (seckey != NULL );
545544
546- secp256k1_scalar_set_b32 (& sec , seckey , & overflow );
547- ret = !overflow & !secp256k1_scalar_is_zero (& sec );
545+ ret = secp256k1_scalar_set_b32_seckey (& sec , seckey );
548546 secp256k1_scalar_cmov (& sec , & secp256k1_scalar_one , !ret );
549547
550548 secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pj , & sec );
0 commit comments