@@ -136,133 +136,6 @@ static int secp256k1_fe_sqrt(secp256k1_fe *r, const secp256k1_fe *a) {
136136 return secp256k1_fe_equal (& t1 , a );
137137}
138138
139- static void secp256k1_fe_inv (secp256k1_fe * r , const secp256k1_fe * a ) {
140- secp256k1_fe x2 , x3 , x6 , x9 , x11 , x22 , x44 , x88 , x176 , x220 , x223 , t1 ;
141- int j ;
142-
143- /** The binary representation of (p - 2) has 5 blocks of 1s, with lengths in
144- * { 1, 2, 22, 223 }. Use an addition chain to calculate 2^n - 1 for each block:
145- * [1], [2], 3, 6, 9, 11, [22], 44, 88, 176, 220, [223]
146- */
147-
148- secp256k1_fe_sqr (& x2 , a );
149- secp256k1_fe_mul (& x2 , & x2 , a );
150-
151- secp256k1_fe_sqr (& x3 , & x2 );
152- secp256k1_fe_mul (& x3 , & x3 , a );
153-
154- x6 = x3 ;
155- for (j = 0 ; j < 3 ; j ++ ) {
156- secp256k1_fe_sqr (& x6 , & x6 );
157- }
158- secp256k1_fe_mul (& x6 , & x6 , & x3 );
159-
160- x9 = x6 ;
161- for (j = 0 ; j < 3 ; j ++ ) {
162- secp256k1_fe_sqr (& x9 , & x9 );
163- }
164- secp256k1_fe_mul (& x9 , & x9 , & x3 );
165-
166- x11 = x9 ;
167- for (j = 0 ; j < 2 ; j ++ ) {
168- secp256k1_fe_sqr (& x11 , & x11 );
169- }
170- secp256k1_fe_mul (& x11 , & x11 , & x2 );
171-
172- x22 = x11 ;
173- for (j = 0 ; j < 11 ; j ++ ) {
174- secp256k1_fe_sqr (& x22 , & x22 );
175- }
176- secp256k1_fe_mul (& x22 , & x22 , & x11 );
177-
178- x44 = x22 ;
179- for (j = 0 ; j < 22 ; j ++ ) {
180- secp256k1_fe_sqr (& x44 , & x44 );
181- }
182- secp256k1_fe_mul (& x44 , & x44 , & x22 );
183-
184- x88 = x44 ;
185- for (j = 0 ; j < 44 ; j ++ ) {
186- secp256k1_fe_sqr (& x88 , & x88 );
187- }
188- secp256k1_fe_mul (& x88 , & x88 , & x44 );
189-
190- x176 = x88 ;
191- for (j = 0 ; j < 88 ; j ++ ) {
192- secp256k1_fe_sqr (& x176 , & x176 );
193- }
194- secp256k1_fe_mul (& x176 , & x176 , & x88 );
195-
196- x220 = x176 ;
197- for (j = 0 ; j < 44 ; j ++ ) {
198- secp256k1_fe_sqr (& x220 , & x220 );
199- }
200- secp256k1_fe_mul (& x220 , & x220 , & x44 );
201-
202- x223 = x220 ;
203- for (j = 0 ; j < 3 ; j ++ ) {
204- secp256k1_fe_sqr (& x223 , & x223 );
205- }
206- secp256k1_fe_mul (& x223 , & x223 , & x3 );
207-
208- /* The final result is then assembled using a sliding window over the blocks. */
209-
210- t1 = x223 ;
211- for (j = 0 ; j < 23 ; j ++ ) {
212- secp256k1_fe_sqr (& t1 , & t1 );
213- }
214- secp256k1_fe_mul (& t1 , & t1 , & x22 );
215- for (j = 0 ; j < 5 ; j ++ ) {
216- secp256k1_fe_sqr (& t1 , & t1 );
217- }
218- secp256k1_fe_mul (& t1 , & t1 , a );
219- for (j = 0 ; j < 3 ; j ++ ) {
220- secp256k1_fe_sqr (& t1 , & t1 );
221- }
222- secp256k1_fe_mul (& t1 , & t1 , & x2 );
223- for (j = 0 ; j < 2 ; j ++ ) {
224- secp256k1_fe_sqr (& t1 , & t1 );
225- }
226- secp256k1_fe_mul (r , a , & t1 );
227- }
228-
229- static void secp256k1_fe_inv_var (secp256k1_fe * r , const secp256k1_fe * a ) {
230- #if defined(USE_FIELD_INV_BUILTIN )
231- secp256k1_fe_inv (r , a );
232- #elif defined(USE_FIELD_INV_NUM )
233- secp256k1_num n , m ;
234- static const secp256k1_fe negone = SECP256K1_FE_CONST (
235- 0xFFFFFFFFUL , 0xFFFFFFFFUL , 0xFFFFFFFFUL , 0xFFFFFFFFUL ,
236- 0xFFFFFFFFUL , 0xFFFFFFFFUL , 0xFFFFFFFEUL , 0xFFFFFC2EUL
237- );
238- /* secp256k1 field prime, value p defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
239- static const unsigned char prime [32 ] = {
240- 0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,
241- 0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,
242- 0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,0xFF ,
243- 0xFF ,0xFF ,0xFF ,0xFE ,0xFF ,0xFF ,0xFC ,0x2F
244- };
245- unsigned char b [32 ];
246- int res ;
247- secp256k1_fe c = * a ;
248- secp256k1_fe_normalize_var (& c );
249- secp256k1_fe_get_b32 (b , & c );
250- secp256k1_num_set_bin (& n , b , 32 );
251- secp256k1_num_set_bin (& m , prime , 32 );
252- secp256k1_num_mod_inverse (& n , & n , & m );
253- secp256k1_num_get_bin (b , 32 , & n );
254- res = secp256k1_fe_set_b32 (r , b );
255- (void )res ;
256- VERIFY_CHECK (res );
257- /* Verify the result is the (unique) valid inverse using non-GMP code. */
258- secp256k1_fe_mul (& c , & c , r );
259- secp256k1_fe_add (& c , & negone );
260- CHECK (secp256k1_fe_normalizes_to_zero_var (& c ));
261- #else
262- #error "Please select field inverse implementation"
263- #endif
264- }
265-
266139static int secp256k1_fe_is_quad_var (const secp256k1_fe * a ) {
267140#ifndef USE_NUM_NONE
268141 unsigned char b [32 ];
0 commit comments