Skip to content

Commit

Permalink
tests: Fix test whose result is implementation-defined
Browse files Browse the repository at this point in the history
A compiler may add struct padding and fe_cmov is not guaranteed to
preserve it.

On the way, we restore the name of the function. It was mistakenly
renamed in 6173839 using
"search and replace".
  • Loading branch information
real-or-random committed Dec 23, 2021
1 parent 09971a3 commit 3801e33
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -2451,13 +2451,10 @@ void run_field_convert(void) {
CHECK(secp256k1_memcmp_var(&fes2, &fes, sizeof(fes)) == 0);
}

int fe_secp256k1_memcmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
secp256k1_fe t = *b;
#ifdef VERIFY
t.magnitude = a->magnitude;
t.normalized = a->normalized;
#endif
return secp256k1_memcmp_var(a, &t, sizeof(secp256k1_fe));
int fe_memcmp(const secp256k1_fe *a, const secp256k1_fe *b) {
/* Compare only the struct member that holds the limbs
(there may be others in VERIFY mode). */
return secp256k1_memcmp_var(a->n, b->n, sizeof(a->n));
}

void run_field_misc(void) {
Expand All @@ -2483,13 +2480,13 @@ void run_field_misc(void) {
CHECK(x.normalized && x.magnitude == 1);
#endif
secp256k1_fe_cmov(&x, &x, 1);
CHECK(fe_secp256k1_memcmp_var(&x, &z) != 0);
CHECK(fe_secp256k1_memcmp_var(&x, &q) == 0);
CHECK(fe_memcmp(&x, &z) != 0);
CHECK(fe_memcmp(&x, &q) == 0);
secp256k1_fe_cmov(&q, &z, 1);
#ifdef VERIFY
CHECK(!q.normalized && q.magnitude == z.magnitude);
#endif
CHECK(fe_secp256k1_memcmp_var(&q, &z) == 0);
CHECK(fe_memcmp(&q, &z) == 0);
secp256k1_fe_normalize_var(&x);
secp256k1_fe_normalize_var(&z);
CHECK(!secp256k1_fe_equal_var(&x, &z));
Expand Down

0 comments on commit 3801e33

Please sign in to comment.