Skip to content

Commit

Permalink
Merge bitcoin#937: Have ge_set_gej_var, gej_double_var and ge_set_all…
Browse files Browse the repository at this point in the history
…_gej_var initialize all fields of their outputs.

14c9739 tests: Improve secp256k1_ge_set_all_gej_var for some infinity inputs (Tim Ruffing)
4a19668 tests: Test secp256k1_ge_set_all_gej_var for all infinity inputs (Tim Ruffing)
45b6468 Have secp256k1_ge_set_all_gej_var initialize all fields. Previous behaviour would not initialize r->y values in the case where infinity is passed in. Furthermore, the previous behaviour wouldn't initialize anything in the case where all inputs were infinity. (Russell O'Connor)
31c0f6d Have secp256k1_gej_double_var initialize all fields. Previous behaviour would not initialize r->x and r->y values in the case where infinity is passed in. (Russell O'Connor)
dd6c3de Have secp256k1_ge_set_gej_var initialize all fields. Previous behaviour would not initialize r->x and r->y values in the case where infinity is passed in. (Russell O'Connor)

Pull request description:

  Previous behaviour would not initialize `r->x` and `r->y` values in the case where infinity is passed in.

ACKs for top commit:
  gmaxwell:
    ACK 14c9739
  sipa:
    utACK 14c9739
  real-or-random:
    ACK 14c9739

Tree-SHA512: 2e779b767f02e348af4bbc62aa9871c3d1d29e61a6c643c879c49f2de27556a3588850acd2f7c7483790677597d01064025e14befdbf29e783f57996fe4430f9
  • Loading branch information
real-or-random committed May 6, 2021
2 parents 185a6af + 14c9739 commit 6c52ae8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/group_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {

static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
secp256k1_fe z2, z3;
r->infinity = a->infinity;
if (a->infinity) {
secp256k1_ge_set_infinity(r);
return;
}
secp256k1_fe_inv_var(&a->z, &a->z);
Expand All @@ -110,8 +110,7 @@ static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
secp256k1_fe_mul(&a->x, &a->x, &z2);
secp256k1_fe_mul(&a->y, &a->y, &z3);
secp256k1_fe_set_int(&a->z, 1);
r->x = a->x;
r->y = a->y;
secp256k1_ge_set_xy(r, &a->x, &a->y);
}

static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
Expand All @@ -120,7 +119,9 @@ static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a
size_t last_i = SIZE_MAX;

for (i = 0; i < len; i++) {
if (!a[i].infinity) {
if (a[i].infinity) {
secp256k1_ge_set_infinity(&r[i]);
} else {
/* Use destination's x coordinates as scratch space */
if (last_i == SIZE_MAX) {
r[i].x = a[i].z;
Expand Down Expand Up @@ -148,7 +149,6 @@ static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a
r[last_i].x = u;

for (i = 0; i < len; i++) {
r[i].infinity = a[i].infinity;
if (!a[i].infinity) {
secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
}
Expand Down Expand Up @@ -311,7 +311,7 @@ static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, s
* point will be gibberish (z = 0 but infinity = 0).
*/
if (a->infinity) {
r->infinity = 1;
secp256k1_gej_set_infinity(r);
if (rzr != NULL) {
secp256k1_fe_set_int(rzr, 1);
}
Expand Down
18 changes: 16 additions & 2 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3101,20 +3101,34 @@ void test_ge(void) {

/* Test batch gej -> ge conversion with many infinities. */
for (i = 0; i < 4 * runs + 1; i++) {
int odd;
random_group_element_test(&ge[i]);
odd = secp256k1_fe_is_odd(&ge[i].x);
CHECK(odd == 0 || odd == 1);
/* randomly set half the points to infinity */
if(secp256k1_fe_is_odd(&ge[i].x)) {
if (odd == i % 2) {
secp256k1_ge_set_infinity(&ge[i]);
}
secp256k1_gej_set_ge(&gej[i], &ge[i]);
}
/* batch invert */
/* batch convert */
secp256k1_ge_set_all_gej_var(ge, gej, 4 * runs + 1);
/* check result */
for (i = 0; i < 4 * runs + 1; i++) {
ge_equals_gej(&ge[i], &gej[i]);
}

/* Test batch gej -> ge conversion with all infinities. */
for (i = 0; i < 4 * runs + 1; i++) {
secp256k1_gej_set_infinity(&gej[i]);
}
/* batch convert */
secp256k1_ge_set_all_gej_var(ge, gej, 4 * runs + 1);
/* check result */
for (i = 0; i < 4 * runs + 1; i++) {
CHECK(secp256k1_ge_is_infinity(&ge[i]));
}

free(ge);
free(gej);
}
Expand Down

0 comments on commit 6c52ae8

Please sign in to comment.