Skip to content

Commit 0ce5892

Browse files
add missing group element invariant checks
The group element checks `secp256k1_{ge,gej}_verify` have first been implemented and added in commit f202667 (PR bitcoin-core#1299). This commit adds additional verification calls in group functions, to match the ones that were originally proposed in commit 09dbba5 of WIP-PR bitcoin-core#1032 (which is obviously not rebased on bitcoin-core#1299 yet). Co-authored-by: Peter Dettman <peter.dettman@gmail.com>
1 parent 907a672 commit 0ce5892

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/group_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {
154154
secp256k1_fe_mul(&a->x, &a->x, &z2);
155155
secp256k1_fe_mul(&a->y, &a->y, &z3);
156156
secp256k1_fe_set_int(&a->z, 1);
157+
secp256k1_gej_verify(a);
158+
157159
r->x = a->x;
158160
r->y = a->y;
159161
secp256k1_ge_verify(r);
@@ -173,6 +175,8 @@ static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
173175
secp256k1_fe_mul(&a->x, &a->x, &z2);
174176
secp256k1_fe_mul(&a->y, &a->y, &z3);
175177
secp256k1_fe_set_int(&a->z, 1);
178+
secp256k1_gej_verify(a);
179+
176180
secp256k1_ge_set_xy(r, &a->x, &a->y);
177181
secp256k1_ge_verify(r);
178182
}
@@ -231,6 +235,7 @@ static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const se
231235
secp256k1_fe_verify(&zr[i]);
232236
/* Ensure all y values are in weak normal form for fast negation of points */
233237
secp256k1_fe_normalize_weak(&a[i].y);
238+
secp256k1_ge_verify(&a[i]);
234239
zs = zr[i];
235240

236241
/* Work our way backwards, using the z-ratios to scale the x/y values. */
@@ -269,12 +274,14 @@ static void secp256k1_gej_clear(secp256k1_gej *r) {
269274
secp256k1_fe_clear(&r->x);
270275
secp256k1_fe_clear(&r->y);
271276
secp256k1_fe_clear(&r->z);
277+
secp256k1_gej_verify(r);
272278
}
273279

274280
static void secp256k1_ge_clear(secp256k1_ge *r) {
275281
r->infinity = 0;
276282
secp256k1_fe_clear(&r->x);
277283
secp256k1_fe_clear(&r->y);
284+
secp256k1_ge_verify(r);
278285
}
279286

280287
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
@@ -547,6 +554,7 @@ static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a,
547554
/* 9 mul, 3 sqr, 13 add/negate/normalize_weak/normalizes_to_zero (ignoring special cases) */
548555
secp256k1_fe az, z12, u1, u2, s1, s2, h, i, h2, h3, t;
549556

557+
secp256k1_gej_verify(a);
550558
secp256k1_ge_verify(b);
551559
secp256k1_fe_verify(bzinv);
552560
if (a->infinity) {
@@ -557,6 +565,7 @@ static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a,
557565
secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
558566
secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
559567
secp256k1_fe_set_int(&r->z, 1);
568+
secp256k1_gej_verify(r);
560569
return;
561570
}
562571
if (b->infinity) {
@@ -820,6 +829,7 @@ static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge) {
820829
}
821830
return secp256k1_gej_is_infinity(&out);
822831
#else
832+
secp256k1_ge_verify(ge);
823833
(void)ge;
824834
/* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
825835
return 1;

0 commit comments

Comments
 (0)