Skip to content

Commit dc55141

Browse files
committed
tests: simplify random_fe_non_zero (remove loop limit and unneeded normalize)
`random_fe_non_zero` contains a loop iteration limit that ensures that we abort if `random_fe` ever yielded zero more than ten times in a row. This construct was first introduced in PR #19 (commit 09ca4f3) for random non-square field elements and was later refactored into the non-zero helper in PR #25 (commit 6d6102f). The copy-over to the exhaustive tests happened recently in PR #1118 (commit 0f86420). This case seems to be practically irrelevant and I'd argue for keeping things simple and removing it; if there's really a worry that the test's random generator is heavily biased towards certain values or value ranges then there should consequently be checks at other places too (e.g. directly in `random_fe` for 256-bit values that repeatedly overflow, i.e. >= p). Also, the _fe_normalize call is not needed and can be removed, as the result of `random_fe` is already normalized.
1 parent 060e32c commit dc55141

File tree

2 files changed

+4
-18
lines changed

2 files changed

+4
-18
lines changed

src/tests.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -2967,16 +2967,9 @@ static void random_fe(secp256k1_fe *x) {
29672967
}
29682968

29692969
static void random_fe_non_zero(secp256k1_fe *nz) {
2970-
int tries = 10;
2971-
while (--tries >= 0) {
2970+
do {
29722971
random_fe(nz);
2973-
secp256k1_fe_normalize(nz);
2974-
if (!secp256k1_fe_is_zero(nz)) {
2975-
break;
2976-
}
2977-
}
2978-
/* Infinitesimal probability of spurious failure here */
2979-
CHECK(tries >= 0);
2972+
} while (secp256k1_fe_is_zero(nz));
29802973
}
29812974

29822975
static void random_fe_non_square(secp256k1_fe *ns) {

src/tests_exhaustive.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,9 @@ static void random_fe(secp256k1_fe *x) {
7070
}
7171

7272
static void random_fe_non_zero(secp256k1_fe *nz) {
73-
int tries = 10;
74-
while (--tries >= 0) {
73+
do {
7574
random_fe(nz);
76-
secp256k1_fe_normalize(nz);
77-
if (!secp256k1_fe_is_zero(nz)) {
78-
break;
79-
}
80-
}
81-
/* Infinitesimal probability of spurious failure here */
82-
CHECK(tries >= 0);
75+
} while (secp256k1_fe_is_zero(nz));
8376
}
8477
/** END stolen from tests.c */
8578

0 commit comments

Comments
 (0)