LCOV - code coverage report
Current view: top level - ballet/secp256k1 - fd_secp256k1.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 61 63 96.8 %
Date: 2026-11-12 04:25:03 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "fd_secp256k1_private.h"
       2             : 
       3             : /* Given the coordinate X and the odd-ness of the Y coordinate, recovers Y and
       4             :    returns the affine group element. Returns NULL if there is no valid pair. */
       5             : static inline fd_secp256k1_point_t *
       6       60084 : fd_secp256k1_recovery_y( fd_secp256k1_point_t *r, fd_secp256k1_fp_t const *x, int odd ) {
       7       60084 :   fd_secp256k1_fp_t x2[1], x3[1];
       8             : 
       9             :   /* x^3 + b */
      10       60084 :   fd_secp256k1_fp_sqr( x2, x );
      11       60084 :   fd_secp256k1_fp_mul( x3, x2, x );
      12       60084 :   fd_secp256k1_fp_add( x3, x3, fd_secp256k1_const_b_mont );
      13             : 
      14             :   /* y^2 = x^3 + b <=> y = sqrt(x^3 + b) */
      15       60084 :   if( FD_UNLIKELY( !fd_secp256k1_fp_sqrt( r->y, x3 ) ) ) {
      16       30024 :     return NULL;
      17       30024 :   }
      18             : 
      19       30060 :   if( fd_secp256k1_fp_is_odd( r->y ) != odd ) {
      20          30 :     fd_secp256k1_fp_negate( r->y, r->y );
      21          30 :   }
      22             : 
      23       30060 :   fd_secp256k1_fp_set( r->x, x );
      24       30060 :   fd_secp256k1_fp_set( r->z, fd_secp256k1_const_one_mont );
      25       30060 :   return r;
      26       60084 : }
      27             : 
      28             : uchar *
      29             : fd_secp256k1_recover( uchar        public_key[64],
      30             :                       uchar const  msg_hash[32],
      31             :                       uchar const  sig[64],
      32       60153 :                       int          recovery_id ) {
      33       60153 :   if( FD_UNLIKELY( !( recovery_id>=0 && recovery_id<=3 ) ) ) {
      34          27 :     return NULL;
      35          27 :   }
      36             : 
      37       60126 :   fd_secp256k1_scalar_t s[1];
      38       60126 :   fd_secp256k1_scalar_t rs[1];
      39       60126 :   if( FD_UNLIKELY( !fd_secp256k1_scalar_frombytes( rs, &sig[  0 ] ) ) ) {
      40          21 :     return NULL;
      41          21 :   }
      42       60105 :   if( FD_UNLIKELY( !fd_secp256k1_scalar_frombytes(  s, &sig[ 32 ] ) ) ) {
      43          12 :     return NULL;
      44          12 :   }
      45             : 
      46       60093 :   fd_secp256k1_fp_t r[1];
      47       60093 :   fd_secp256k1_fp_tomont( r, rs );
      48             : 
      49       60093 :   if( recovery_id & 2 ) {
      50             :     /* If rs >= p - n, return NULL. Otherwise, add the n to r.
      51             :        https://github.com/bitcoin-core/secp256k1/blob/v0.7.1/src/modules/recovery/main_impl.h#L104-L109 */
      52          21 :     if( FD_UNLIKELY( fd_uint256_cmp( rs, fd_secp256k1_const_p_minus_n ) >= 0 ) ) {
      53           9 :       return NULL;
      54           9 :     }
      55             :     /* Note that *only* r is incremented, rs is left unchanged. */
      56          12 :     fd_secp256k1_fp_add( r, r, fd_secp256k1_const_n_mont );
      57          12 :   }
      58             : 
      59             :   /* Recover the full public key group element. */
      60       60084 :   fd_secp256k1_point_t a[1];
      61       60084 :   if( FD_UNLIKELY( !fd_secp256k1_recovery_y( a, r, recovery_id & 1 ) ) ) {
      62       30024 :     return NULL;
      63       30024 :   }
      64             : 
      65       30060 :   fd_uint256_t msg[1];
      66       30060 :   memcpy( msg, msg_hash, 32 );
      67       30060 :   fd_uint256_bswap( msg, msg );
      68             :   /* The message scalar is unconditionally reduced to the scalar field.
      69             :      https://github.com/bitcoin-core/secp256k1/blob/v0.7.1/src/scalar_4x64_impl.h#L151 */
      70       30060 :   fd_secp256k1_scalar_reduce( msg, msg );
      71       30060 :   fd_secp256k1_scalar_tomont( msg, msg );
      72             : 
      73       30060 :   fd_secp256k1_scalar_t rn[1], u1[1], u2[1];
      74       30060 :   fd_secp256k1_point_t pubkey[1];
      75             : 
      76             :   /* We delay converting rs into montgomery domain since
      77             :      we may need to perform the comparison against p-n first. */
      78       30060 :   fd_secp256k1_scalar_tomont( s, s );
      79             : 
      80             :   /* Unfortunately s2n-bignum has no API for performing
      81             :      in-montgomery inversion, so we invert and then convert. */
      82       30060 :   fd_secp256k1_scalar_invert( rn, rs );
      83       30060 :   fd_secp256k1_scalar_tomont( rn, rn );
      84             : 
      85       30060 :   fd_secp256k1_scalar_mul   ( u1, rn, msg );
      86       30060 :   fd_secp256k1_scalar_negate( u1, u1      );
      87       30060 :   fd_secp256k1_scalar_mul   ( u2, rn, s   );
      88             : 
      89       30060 :   fd_secp256k1_scalar_demont( u2, u2 );
      90       30060 :   fd_secp256k1_scalar_demont( u1, u1 );
      91       30060 :   fd_secp256k1_double_base_mul( pubkey, u1, a, u2 );
      92             : 
      93             :   /* If the computed pubkey is the identity point, we return NULL
      94             :      https://github.com/bitcoin-core/secp256k1/blob/v0.7.1/src/modules/recovery/main_impl.h#L120 */
      95       30060 :   if( FD_UNLIKELY( fd_secp256k1_point_is_identity( pubkey ) ) ) {
      96           0 :     return NULL;
      97           0 :   }
      98             : 
      99             :   /* Serialize the public key into an uncompressed form.
     100             :      The output does not have the recovery_id. */
     101       30060 :   fd_secp256k1_point_to_affine( pubkey, pubkey );
     102       30060 :   fd_secp256k1_fp_tobytes( &public_key[  0 ], pubkey->x );
     103       30060 :   fd_secp256k1_fp_tobytes( &public_key[ 32 ], pubkey->y );
     104       30060 :   return public_key;
     105       30060 : }

Generated by: LCOV version 1.14