Skip to content

Commit 875d68b

Browse files
Merge bitcoin#699: Initialize field elements when resulting in infinity
47a7b83 Clear field elements when writing infinity (Elichai Turkel) 61d1ecb Added test with additions resulting in infinity (Elichai Turkel) Pull request description: Currently if `secp256k1_gej_add_var` / `secp256k1_gej_add_ge_var` /` secp256k1_gej_add_zinv_var` receive `P + (-P)` it will set `gej->infinity = 1` but doesn't call initialize the field elements. Notice that this is the only branch in the function that results in an uninitialized output. By using `secp256k1_gej_set_infinity()` it will set the field elements to zero while also setting the infinity flag. I also added a test that fails with valgrind on current master but passes with the fix. EDIT: This isn't a bug or something necessary, I just personally found this helpful. ACKs for top commit: real-or-random: ACK 47a7b83 Tree-SHA512: cdc2efc242a1b04b4f081183c07d4b2602cdba705e6b30b548df4e115e54fb97691f4b1a28f142f02d5e523c020721337a297b17d732acde147b910f5c53bd0a
2 parents 54caf2e + 47a7b83 commit 875d68b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/group_impl.h

+3-3
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/tests.c

+34
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,39 @@ void test_ge(void) {
23182318
free(zinv);
23192319
}
23202320

2321+
2322+
void test_intialized_inf(void) {
2323+
secp256k1_ge p;
2324+
secp256k1_gej pj, npj, infj1, infj2, infj3;
2325+
secp256k1_fe zinv;
2326+
2327+
/* Test that adding P+(-P) results in a fully initalized infinity*/
2328+
random_group_element_test(&p);
2329+
secp256k1_gej_set_ge(&pj, &p);
2330+
secp256k1_gej_neg(&npj, &pj);
2331+
2332+
secp256k1_gej_add_var(&infj1, &pj, &npj, NULL);
2333+
CHECK(secp256k1_gej_is_infinity(&infj1));
2334+
CHECK(secp256k1_fe_is_zero(&infj1.x));
2335+
CHECK(secp256k1_fe_is_zero(&infj1.y));
2336+
CHECK(secp256k1_fe_is_zero(&infj1.z));
2337+
2338+
secp256k1_gej_add_ge_var(&infj2, &npj, &p, NULL);
2339+
CHECK(secp256k1_gej_is_infinity(&infj2));
2340+
CHECK(secp256k1_fe_is_zero(&infj2.x));
2341+
CHECK(secp256k1_fe_is_zero(&infj2.y));
2342+
CHECK(secp256k1_fe_is_zero(&infj2.z));
2343+
2344+
secp256k1_fe_set_int(&zinv, 1);
2345+
secp256k1_gej_add_zinv_var(&infj3, &npj, &p, &zinv);
2346+
CHECK(secp256k1_gej_is_infinity(&infj3));
2347+
CHECK(secp256k1_fe_is_zero(&infj3.x));
2348+
CHECK(secp256k1_fe_is_zero(&infj3.y));
2349+
CHECK(secp256k1_fe_is_zero(&infj3.z));
2350+
2351+
2352+
}
2353+
23212354
void test_add_neg_y_diff_x(void) {
23222355
/* The point of this test is to check that we can add two points
23232356
* whose y-coordinates are negatives of each other but whose x
@@ -2391,6 +2424,7 @@ void run_ge(void) {
23912424
test_ge();
23922425
}
23932426
test_add_neg_y_diff_x();
2427+
test_intialized_inf();
23942428
}
23952429

23962430
void test_ec_combine(void) {

0 commit comments

Comments
 (0)