@@ -165,11 +165,11 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
165
165
for (i = 3 ; i < 62 ; ++ i ) {
166
166
/* f must always be odd */
167
167
VERIFY_CHECK ((f & 1 ) == 1 );
168
+ /* Minimum trailing zeros count for matrix elements decreases in each iteration */
169
+ VERIFY_CHECK (!((u | v | q | r ) & (0xFFFFFFFFFFFFFFFFULL >> (i - 1 ))));
168
170
/* Applying the matrix so far to the initial f,g gives current f,g. */
169
171
VERIFY_CHECK ((u >> (62 - i )) * f0 + (v >> (62 - i )) * g0 == f << i );
170
172
VERIFY_CHECK ((q >> (62 - i )) * f0 + (r >> (62 - i )) * g0 == g << i );
171
- /* At the beginning of every loop, the matrix variables are even. */
172
- VERIFY_CHECK (!((u | v | q | r ) & 1 ));
173
173
/* Compute conditional masks for (theta < 0) and for (g & 1). */
174
174
c1 = theta >> 63 ;
175
175
c2 = - (g & 1 );
@@ -206,10 +206,9 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
206
206
VERIFY_CHECK (q * f0 + r * g0 == g << 62 );
207
207
/* The determinant of t must be a power of two. This guarantees that multiplication with t
208
208
* does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
209
- * will be divided out again). As each divstep's individual matrix has determinant 2, the
210
- * aggregate of 59 of them will have determinant 2^59. Multiplying with the initial
211
- * 8*identity (which has determinant 2^6) means the overall outputs has determinant
212
- * 2^65. */
209
+ * will be divided out again). As each divstep's individual matrix has determinant 2^-1,
210
+ * the aggregate of 59 of them will have determinant 2^-59. Multiplying with the initial
211
+ * 2^62*identity (which has determinant 2^124) means the result has determinant 2^65. */
213
212
VERIFY_CHECK ((int128_t )t -> u * t -> r - (int128_t )t -> v * t -> q == ((int128_t )1 ) << 65 );
214
213
return theta ;
215
214
}
0 commit comments