Skip to content

Commit 78ef599

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 <[email protected]>
1 parent 67214f5 commit 78ef599

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) {
@@ -544,6 +551,7 @@ static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a,
544551
/* 9 mul, 3 sqr, 13 add/negate/normalize_weak/normalizes_to_zero (ignoring special cases) */
545552
secp256k1_fe az, z12, u1, u2, s1, s2, h, i, h2, h3, t;
546553

554+
secp256k1_gej_verify(a);
547555
secp256k1_ge_verify(b);
548556
secp256k1_fe_verify(bzinv);
549557
if (a->infinity) {
@@ -554,6 +562,7 @@ static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a,
554562
secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
555563
secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
556564
secp256k1_fe_set_int(&r->z, 1);
565+
secp256k1_gej_verify(r);
557566
return;
558567
}
559568
if (b->infinity) {
@@ -817,6 +826,7 @@ static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge* ge) {
817826
}
818827
return secp256k1_gej_is_infinity(&out);
819828
#else
829+
secp256k1_ge_verify(ge);
820830
(void)ge;
821831
/* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
822832
return 1;

0 commit comments

Comments
 (0)