@@ -77,6 +77,10 @@ static void secp256k1_fe_normalize(secp256k1_fe *r) {
7777 uint64_t m ;
7878 uint64_t x = t4 >> 48 ; t4 &= 0x0FFFFFFFFFFFFULL ;
7979
80+ #ifdef VERIFY
81+ secp256k1_fe_verify (r );
82+ #endif
83+
8084 /* The first pass ensures the magnitude is 1, ... */
8185 t0 += x * 0x1000003D1ULL ;
8286 t1 += (t0 >> 52 ); t0 &= 0xFFFFFFFFFFFFFULL ;
@@ -119,6 +123,10 @@ static void secp256k1_fe_normalize_weak(secp256k1_fe *r) {
119123 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
120124 uint64_t x = t4 >> 48 ; t4 &= 0x0FFFFFFFFFFFFULL ;
121125
126+ #ifdef VERIFY
127+ secp256k1_fe_verify (r );
128+ #endif
129+
122130 /* The first pass ensures the magnitude is 1, ... */
123131 t0 += x * 0x1000003D1ULL ;
124132 t1 += (t0 >> 52 ); t0 &= 0xFFFFFFFFFFFFFULL ;
@@ -144,6 +152,10 @@ static void secp256k1_fe_normalize_var(secp256k1_fe *r) {
144152 uint64_t m ;
145153 uint64_t x = t4 >> 48 ; t4 &= 0x0FFFFFFFFFFFFULL ;
146154
155+ #ifdef VERIFY
156+ secp256k1_fe_verify (r );
157+ #endif
158+
147159 /* The first pass ensures the magnitude is 1, ... */
148160 t0 += x * 0x1000003D1ULL ;
149161 t1 += (t0 >> 52 ); t0 &= 0xFFFFFFFFFFFFFULL ;
@@ -190,6 +202,10 @@ static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r) {
190202 /* Reduce t4 at the start so there will be at most a single carry from the first pass */
191203 uint64_t x = t4 >> 48 ; t4 &= 0x0FFFFFFFFFFFFULL ;
192204
205+ #ifdef VERIFY
206+ secp256k1_fe_verify (r );
207+ #endif
208+
193209 /* The first pass ensures the magnitude is 1, ... */
194210 t0 += x * 0x1000003D1ULL ;
195211 t1 += (t0 >> 52 ); t0 &= 0xFFFFFFFFFFFFFULL ; z0 = t0 ; z1 = t0 ^ 0x1000003D0ULL ;
@@ -209,6 +225,10 @@ static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r) {
209225 uint64_t z0 , z1 ;
210226 uint64_t x ;
211227
228+ #ifdef VERIFY
229+ secp256k1_fe_verify (r );
230+ #endif
231+
212232 t0 = r -> n [0 ];
213233 t4 = r -> n [4 ];
214234
@@ -429,6 +449,9 @@ SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k
429449}
430450
431451SECP256K1_INLINE static void secp256k1_fe_mul_int (secp256k1_fe * r , int a ) {
452+ #ifdef VERIFY
453+ secp256k1_fe_verify (r );
454+ #endif
432455 r -> n [0 ] *= a ;
433456 r -> n [1 ] *= a ;
434457 r -> n [2 ] *= a ;
@@ -490,6 +513,10 @@ static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a) {
490513static SECP256K1_INLINE void secp256k1_fe_cmov (secp256k1_fe * r , const secp256k1_fe * a , int flag ) {
491514 uint64_t mask0 , mask1 ;
492515 SECP256K1_CHECKMEM_CHECK_VERIFY (r -> n , sizeof (r -> n ));
516+ #ifdef VERIFY
517+ secp256k1_fe_verify (a );
518+ secp256k1_fe_verify (r );
519+ #endif
493520 mask0 = flag + ~((uint64_t )0 );
494521 mask1 = ~mask0 ;
495522 r -> n [0 ] = (r -> n [0 ] & mask0 ) | (a -> n [0 ] & mask1 );
@@ -584,6 +611,7 @@ static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r,
584611static void secp256k1_fe_to_storage (secp256k1_fe_storage * r , const secp256k1_fe * a ) {
585612#ifdef VERIFY
586613 VERIFY_CHECK (a -> normalized );
614+ secp256k1_fe_verify (a );
587615#endif
588616 r -> n [0 ] = a -> n [0 ] | a -> n [1 ] << 52 ;
589617 r -> n [1 ] = a -> n [1 ] >> 12 | a -> n [2 ] << 40 ;
@@ -635,6 +663,7 @@ static void secp256k1_fe_to_signed62(secp256k1_modinv64_signed62 *r, const secp2
635663 const uint64_t a0 = a -> n [0 ], a1 = a -> n [1 ], a2 = a -> n [2 ], a3 = a -> n [3 ], a4 = a -> n [4 ];
636664
637665#ifdef VERIFY
666+ secp256k1_fe_verify (a );
638667 VERIFY_CHECK (a -> normalized );
639668#endif
640669
@@ -654,13 +683,18 @@ static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x) {
654683 secp256k1_fe tmp ;
655684 secp256k1_modinv64_signed62 s ;
656685
686+ #ifdef VERIFY
687+ secp256k1_fe_verify (x );
688+ #endif
689+
657690 tmp = * x ;
658691 secp256k1_fe_normalize (& tmp );
659692 secp256k1_fe_to_signed62 (& s , & tmp );
660693 secp256k1_modinv64 (& s , & secp256k1_const_modinfo_fe );
661694 secp256k1_fe_from_signed62 (r , & s );
662695
663696#ifdef VERIFY
697+ secp256k1_fe_verify (r );
664698 VERIFY_CHECK (secp256k1_fe_normalizes_to_zero (r ) == secp256k1_fe_normalizes_to_zero (& tmp ));
665699#endif
666700}
@@ -669,13 +703,18 @@ static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *x) {
669703 secp256k1_fe tmp ;
670704 secp256k1_modinv64_signed62 s ;
671705
706+ #ifdef VERIFY
707+ secp256k1_fe_verify (x );
708+ #endif
709+
672710 tmp = * x ;
673711 secp256k1_fe_normalize_var (& tmp );
674712 secp256k1_fe_to_signed62 (& s , & tmp );
675713 secp256k1_modinv64_var (& s , & secp256k1_const_modinfo_fe );
676714 secp256k1_fe_from_signed62 (r , & s );
677715
678716#ifdef VERIFY
717+ secp256k1_fe_verify (r );
679718 VERIFY_CHECK (secp256k1_fe_normalizes_to_zero (r ) == secp256k1_fe_normalizes_to_zero (& tmp ));
680719#endif
681720}
0 commit comments