@@ -212,6 +212,10 @@ static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
212212 secp256k1_fe_inv (r , a );
213213#elif defined(USE_FIELD_INV_NUM )
214214 secp256k1_num_t n , m ;
215+ static const secp256k1_fe_t negone = SECP256K1_FE_CONST (
216+ 0xFFFFFFFF , 0xFFFFFFFF , 0xFFFFFFFF , 0xFFFFFFFF ,
217+ 0xFFFFFFFF , 0xFFFFFFFF , 0xFFFFFFFE , 0xFFFFFC2E
218+ );
215219 /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
216220 static const unsigned char prime [32 ] = {
217221 0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,
@@ -228,6 +232,10 @@ static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
228232 secp256k1_num_mod_inverse (& n , & n , & m );
229233 secp256k1_num_get_bin (b , 32 , & n );
230234 VERIFY_CHECK (secp256k1_fe_set_b32 (r , b ));
235+ /* Verify the result is the (unique) valid inverse using non-GMP code. */
236+ secp256k1_fe_mul (& c , & c , r );
237+ secp256k1_fe_add (& c , & negone );
238+ CHECK (secp256k1_fe_normalizes_to_zero_var (& c ));
231239#else
232240#error "Please select field inverse implementation"
233241#endif
0 commit comments