Skip to content

Commit

Permalink
tests: introduce helper for non-zero random_fe_test results
Browse files Browse the repository at this point in the history
There are several instances in the tests where random non-zero field
elements are generated by calling `random_fe_test` in a do/while-loop.
This commit deduplicates all these by introducing a
`random_fe_non_zero_test` helper. Note that some instances checked the
is-zero condition via `secp256k1_fe_normalizes_to_zero_var`, which is
unnecessary, as the result of `random_fe_test` is already normalized (so
strictly speaking, this is not a pure refactor).
  • Loading branch information
theStack committed Jun 27, 2023
1 parent 304421d commit 5a95a26
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ static void random_fe_test(secp256k1_fe *x) {
} while(1);
}

static void random_fe_non_zero_test(secp256k1_fe *fe) {
do {
random_fe_test(fe);
} while(secp256k1_fe_is_zero(fe));
}

static void random_group_element_test(secp256k1_ge *ge) {
secp256k1_fe fe;
do {
Expand All @@ -129,12 +135,7 @@ static void random_group_element_test(secp256k1_ge *ge) {

static void random_group_element_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *ge) {
secp256k1_fe z2, z3;
do {
random_fe_test(&gej->z);
if (!secp256k1_fe_is_zero(&gej->z)) {
break;
}
} while(1);
random_fe_non_zero_test(&gej->z);
secp256k1_fe_sqr(&z2, &gej->z);
secp256k1_fe_mul(&z3, &z2, &gej->z);
secp256k1_fe_mul(&gej->x, &ge->x, &z2);
Expand Down Expand Up @@ -3810,18 +3811,14 @@ static void test_ge(void) {
}

/* Generate random zf, and zfi2 = 1/zf^2, zfi3 = 1/zf^3 */
do {
random_fe_test(&zf);
} while(secp256k1_fe_is_zero(&zf));
random_fe_non_zero_test(&zf);
random_field_element_magnitude(&zf);
secp256k1_fe_inv_var(&zfi3, &zf);
secp256k1_fe_sqr(&zfi2, &zfi3);
secp256k1_fe_mul(&zfi3, &zfi3, &zfi2);

/* Generate random r */
do {
random_fe_test(&r);
} while(secp256k1_fe_is_zero(&r));
random_fe_non_zero_test(&r);

for (i1 = 0; i1 < 1 + 4 * runs; i1++) {
int i2;
Expand Down Expand Up @@ -4138,10 +4135,7 @@ static void run_gej(void) {
CHECK(!secp256k1_gej_eq_var(&a, &b));

b = a;
random_fe_test(&fe);
if (secp256k1_fe_is_zero(&fe)) {
continue;
}
random_fe_non_zero_test(&fe);
secp256k1_gej_rescale(&a, &fe);
CHECK(secp256k1_gej_eq_var(&a, &b));
}
Expand Down Expand Up @@ -4580,9 +4574,7 @@ static void ecmult_const_mult_xonly(void) {
random_scalar_order_test(&q);
/* If i is odd, n=d*base.x for random non-zero d */
if (i & 1) {
do {
random_fe_test(&d);
} while (secp256k1_fe_normalizes_to_zero_var(&d));
random_fe_non_zero_test(&d);
secp256k1_fe_mul(&n, &base.x, &d);
} else {
n = base.x;
Expand Down Expand Up @@ -4611,9 +4603,7 @@ static void ecmult_const_mult_xonly(void) {
} while (secp256k1_ge_x_on_curve_var(&x));
/* If i is odd, n=d*x for random non-zero d. */
if (i & 1) {
do {
random_fe_test(&d);
} while (secp256k1_fe_normalizes_to_zero_var(&d));
random_fe_non_zero_test(&d);
secp256k1_fe_mul(&n, &x, &d);
} else {
n = x;
Expand Down

0 comments on commit 5a95a26

Please sign in to comment.