Skip to content

Commit 6afd499

Browse files
committed
Improve verify check and comments
1 parent a33f77c commit 6afd499

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/modinv32_impl.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t theta, uint32_t f0, uint32
187187
for (i = 0; i < 30; ++i) {
188188
/* f must always be odd */
189189
VERIFY_CHECK((f & 1) == 1);
190+
/* Minimum trailing zeros count for matrix elements decreases in each iteration */
191+
VERIFY_CHECK(!((u | v | q | r) & (0xFFFFFFFFULL >> (i + 2))));
190192
/* Applying the matrix so far to the initial f,g gives current f,g. */
191193
VERIFY_CHECK((u >> (30 - i)) * f0 + (v >> (30 - i)) * g0 == f << i);
192194
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));
195195
/* Compute conditional masks for (theta < 0) and for (g & 1). */
196196
c1 = theta >> 31;
197197
c2 = -(g & 1);
@@ -228,8 +228,9 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t theta, uint32_t f0, uint32
228228
VERIFY_CHECK(q * f0 + r * g0 == g << 30);
229229
/* The determinant of t must be a power of two. This guarantees that multiplication with t
230230
* does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
231-
* will be divided out again). As each divstep's individual matrix has determinant 2, the
232-
* aggregate of 30 of them will have determinant 2^30. */
231+
* will be divided out again). As each divstep's individual matrix has determinant 2^-1,
232+
* the aggregate of 30 of them will have determinant 2^-30. Multiplying with the initial
233+
* 2^30*identity (which has determinant 2^60) means the result has determinant 2^30. */
233234
VERIFY_CHECK((int64_t)t->u * t->r - (int64_t)t->v * t->q == ((int64_t)1) << 30);
234235
return theta;
235236
}

src/modinv64_impl.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
165165
for (i = 3; i < 62; ++i) {
166166
/* f must always be odd */
167167
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))));
168170
/* Applying the matrix so far to the initial f,g gives current f,g. */
169171
VERIFY_CHECK((u >> (62 - i)) * f0 + (v >> (62 - i)) * g0 == f << i);
170172
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));
173173
/* Compute conditional masks for (theta < 0) and for (g & 1). */
174174
c1 = theta >> 63;
175175
c2 = -(g & 1);
@@ -206,10 +206,9 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
206206
VERIFY_CHECK(q * f0 + r * g0 == g << 62);
207207
/* The determinant of t must be a power of two. This guarantees that multiplication with t
208208
* 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. */
213212
VERIFY_CHECK((int128_t)t->u * t->r - (int128_t)t->v * t->q == ((int128_t)1) << 65);
214213
return theta;
215214
}

0 commit comments

Comments
 (0)