Skip to content

Commit 1bce1d1

Browse files
sipapeterdettman
authored andcommitted
Improve matrix computation assertions
1 parent e6db501 commit 1bce1d1

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/modinv32_impl.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,13 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t theta, uint32_t f0, uint32
185185
int i;
186186

187187
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));
191195
/* Compute conditional masks for (theta < 0) and for (g & 1). */
192196
c1 = theta >> 31;
193197
c2 = -(g & 1);
@@ -219,6 +223,9 @@ static int32_t secp256k1_modinv32_divsteps_30(int32_t theta, uint32_t f0, uint32
219223
t->v = v;
220224
t->q = q;
221225
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);
222229
/* The determinant of t must be a power of two. This guarantees that multiplication with t
223230
* does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
224231
* will be divided out again). As each divstep's individual matrix has determinant 2, the

src/modinv64_impl.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,13 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
163163
int i;
164164

165165
for (i = 3; i < 62; ++i) {
166-
VERIFY_CHECK((f & 1) == 1); /* f must always be odd */
167-
VERIFY_CHECK(((u >> (62 - i)) * f0 + (v >> (62 - i)) * g0) == f << i);
168-
VERIFY_CHECK(((q >> (62 - i)) * f0 + (r >> (62 - i)) * g0) == g << i);
166+
/* f must always be odd */
167+
VERIFY_CHECK((f & 1) == 1);
168+
/* Applying the matrix so far to the initial f,g gives current f,g. */
169+
VERIFY_CHECK((u >> (62 - i)) * f0 + (v >> (62 - i)) * g0 == f << i);
170+
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));
169173
/* Compute conditional masks for (theta < 0) and for (g & 1). */
170174
c1 = theta >> 63;
171175
c2 = -(g & 1);
@@ -197,6 +201,9 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t theta, uint64_t f0, uint64
197201
t->v = v;
198202
t->q = q;
199203
t->r = r;
204+
/* Applying the final matrix to the initial f,g gives final f,g. */
205+
VERIFY_CHECK(u * f0 + v * g0 == f << 62);
206+
VERIFY_CHECK(q * f0 + r * g0 == g << 62);
200207
/* The determinant of t must be a power of two. This guarantees that multiplication with t
201208
* does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
202209
* will be divided out again). As each divstep's individual matrix has determinant 2, the

0 commit comments

Comments
 (0)