@@ -185,9 +185,13 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t theta, uint32_t f0, uint32
185
185
int i ;
186
186
187
187
for (i = 0 ; i < 30 ; ++ i ) {
188
- VERIFY_CHECK ((f & 1 ) == 1 ); /* f must always be odd */
189
- VERIFY_CHECK (((u >> (30 - i )) * f0 + (v >> (30 - i )) * g0 ) == f << i );
190
- VERIFY_CHECK (((q >> (30 - i )) * f0 + (r >> (30 - i )) * g0 ) == g << i );
188
+ /* f must always be odd */
189
+ VERIFY_CHECK ((f & 1 ) == 1 );
190
+ /* Applying the matrix so far to the initial f,g gives current f,g. */
191
+ VERIFY_CHECK ((u >> (30 - i )) * f0 + (v >> (30 - i )) * g0 == f << i );
192
+ VERIFY_CHECK ((q >> (30 - i )) * f0 + (r >> (30 - i )) * g0 == g << i );
193
+ /* At the beginning of every loop, the matrix variables are even. */
194
+ VERIFY_CHECK (!((u | v | q | r ) & 1 ));
191
195
/* Compute conditional masks for (theta < 0) and for (g & 1). */
192
196
c1 = theta >> 31 ;
193
197
c2 = - (g & 1 );
@@ -219,6 +223,9 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t theta, uint32_t f0, uint32
219
223
t -> v = v ;
220
224
t -> q = q ;
221
225
t -> r = r ;
226
+ /* Applying the final matrix to the initial f,g gives final f,g. */
227
+ VERIFY_CHECK (u * f0 + v * g0 == f << 30 );
228
+ VERIFY_CHECK (q * f0 + r * g0 == g << 30 );
222
229
/* The determinant of t must be a power of two. This guarantees that multiplication with t
223
230
* does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
224
231
* will be divided out again). As each divstep's individual matrix has determinant 2, the
0 commit comments