@@ -146,7 +146,9 @@ typedef struct {
146
146
} secp256k1_modinv64_trans2x2 ;
147
147
148
148
/* Compute the transition matrix and theta for 59 divsteps (where theta=delta-1/2)
149
- * Note that the transformation matrix is scaled by 2^62 and not 2^59.
149
+ * Although only 59 divsteps are performed, the resulting transformation matrix
150
+ * is scaled by 2^62 to allow reuse of _update_de_62 and _update_fg_62 between
151
+ * _modinv64 and _modinv64_var.
150
152
*
151
153
* Input: theta: initial theta
152
154
* f0: bottom limb of initial f
@@ -165,11 +167,11 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
165
167
for (i = 3 ; i < 62 ; ++ i ) {
166
168
/* f must always be odd */
167
169
VERIFY_CHECK ((f & 1 ) == 1 );
170
+ /* Minimum trailing zeros count for matrix elements decreases in each iteration */
171
+ VERIFY_CHECK (!((u | v | q | r ) & (0xFFFFFFFFFFFFFFFFULL >> (i - 1 ))));
168
172
/* Applying the matrix so far to the initial f,g gives current f,g. */
169
173
VERIFY_CHECK ((u >> (62 - i )) * f0 + (v >> (62 - i )) * g0 == f << i );
170
174
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
175
/* Compute conditional masks for (theta < 0) and for (g & 1). */
174
176
c1 = theta >> 63 ;
175
177
c2 = - (g & 1 );
@@ -206,10 +208,9 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
206
208
VERIFY_CHECK (q * f0 + r * g0 == g << 62 );
207
209
/* The determinant of t must be a power of two. This guarantees that multiplication with t
208
210
* 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. */
211
+ * will be divided out again). As each divstep's individual matrix has determinant 2^-1,
212
+ * the aggregate of 59 of them will have determinant 2^-59. Multiplying with the initial
213
+ * 2^62*identity (which has determinant 2^124) means the result has determinant 2^65. */
213
214
VERIFY_CHECK ((int128_t )t -> u * t -> r - (int128_t )t -> v * t -> q == ((int128_t )1 ) << 65 );
214
215
return theta ;
215
216
}
0 commit comments