Skip to content

Commit 13f2aeb

Browse files
elichaideadalnix
authored andcommitted
Initialize field elements when resulting in infinity
Summary: * Added test with additions resulting in infinity * Clear field elements when writing infinity This is a backport of secp256k1 [[bitcoin-core/secp256k1#699 | PR699]] Test Plan: ninja check-secp256k1 Reviewers: #bitcoin_abc, jasonbcox Reviewed By: #bitcoin_abc, jasonbcox Subscribers: jasonbcox Differential Revision: https://reviews.bitcoinabc.org/D7611
1 parent 521ffb0 commit 13f2aeb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/secp256k1/src/group_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, cons
399399
if (rzr != NULL) {
400400
secp256k1_fe_set_int(rzr, 0);
401401
}
402-
r->infinity = 1;
402+
secp256k1_gej_set_infinity(r);
403403
}
404404
return;
405405
}
@@ -449,7 +449,7 @@ static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, c
449449
if (rzr != NULL) {
450450
secp256k1_fe_set_int(rzr, 0);
451451
}
452-
r->infinity = 1;
452+
secp256k1_gej_set_infinity(r);
453453
}
454454
return;
455455
}
@@ -508,7 +508,7 @@ static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a,
508508
if (secp256k1_fe_normalizes_to_zero_var(&i)) {
509509
secp256k1_gej_double_var(r, a, NULL);
510510
} else {
511-
r->infinity = 1;
511+
secp256k1_gej_set_infinity(r);
512512
}
513513
return;
514514
}

src/secp256k1/src/tests.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,39 @@ void test_ge(void) {
23072307
free(zinv);
23082308
}
23092309

2310+
2311+
void test_intialized_inf(void) {
2312+
secp256k1_ge p;
2313+
secp256k1_gej pj, npj, infj1, infj2, infj3;
2314+
secp256k1_fe zinv;
2315+
2316+
/* Test that adding P+(-P) results in a fully initalized infinity*/
2317+
random_group_element_test(&p);
2318+
secp256k1_gej_set_ge(&pj, &p);
2319+
secp256k1_gej_neg(&npj, &pj);
2320+
2321+
secp256k1_gej_add_var(&infj1, &pj, &npj, NULL);
2322+
CHECK(secp256k1_gej_is_infinity(&infj1));
2323+
CHECK(secp256k1_fe_is_zero(&infj1.x));
2324+
CHECK(secp256k1_fe_is_zero(&infj1.y));
2325+
CHECK(secp256k1_fe_is_zero(&infj1.z));
2326+
2327+
secp256k1_gej_add_ge_var(&infj2, &npj, &p, NULL);
2328+
CHECK(secp256k1_gej_is_infinity(&infj2));
2329+
CHECK(secp256k1_fe_is_zero(&infj2.x));
2330+
CHECK(secp256k1_fe_is_zero(&infj2.y));
2331+
CHECK(secp256k1_fe_is_zero(&infj2.z));
2332+
2333+
secp256k1_fe_set_int(&zinv, 1);
2334+
secp256k1_gej_add_zinv_var(&infj3, &npj, &p, &zinv);
2335+
CHECK(secp256k1_gej_is_infinity(&infj3));
2336+
CHECK(secp256k1_fe_is_zero(&infj3.x));
2337+
CHECK(secp256k1_fe_is_zero(&infj3.y));
2338+
CHECK(secp256k1_fe_is_zero(&infj3.z));
2339+
2340+
2341+
}
2342+
23102343
void test_add_neg_y_diff_x(void) {
23112344
/* The point of this test is to check that we can add two points
23122345
* whose y-coordinates are negatives of each other but whose x
@@ -2380,6 +2413,7 @@ void run_ge(void) {
23802413
test_ge();
23812414
}
23822415
test_add_neg_y_diff_x();
2416+
test_intialized_inf();
23832417
}
23842418

23852419
void test_ec_combine(void) {

0 commit comments

Comments
 (0)