Skip to content

Commit

Permalink
Merge bitcoin-core/secp256k1#1054: tests: Fix test whose result is im…
Browse files Browse the repository at this point in the history
…plementation-defined

3d7cbaf tests: Fix test whose result is implementation-defined (Tim Ruffing)

Pull request description:

  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".

ACKs for top commit:
  robot-dreams:
    ACK 3d7cbaf
  sipa:
    utACK 3d7cbaf

Tree-SHA512: f8bb643d4915e9ce9c4fe45b48a2878f6cf1f29e654be1c150cdf65c6959cf65f8491928cf098da5a01f1d488ba475914905ca96b232abed499eb6ed65e53fb8
  • Loading branch information
real-or-random committed Dec 25, 2021
2 parents a310e79 + 3d7cbaf commit 39a36db
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -2451,13 +2451,16 @@ 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;
/* Returns true if two field elements have the same representation. */
int fe_identical(const secp256k1_fe *a, const secp256k1_fe *b) {
int ret = 1;
#ifdef VERIFY
t.magnitude = a->magnitude;
t.normalized = a->normalized;
ret &= (a->magnitude == b->magnitude);
ret &= (a->normalized == b->normalized);
#endif
return secp256k1_memcmp_var(a, &t, sizeof(secp256k1_fe));
/* Compare the struct member that holds the limbs. */
ret &= (secp256k1_memcmp_var(a->n, b->n, sizeof(a->n)) == 0);
return ret;
}

void run_field_misc(void) {
Expand All @@ -2483,13 +2486,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_identical(&x, &z));
CHECK(fe_identical(&x, &q));
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_identical(&q, &z));
secp256k1_fe_normalize_var(&x);
secp256k1_fe_normalize_var(&z);
CHECK(!secp256k1_fe_equal_var(&x, &z));
Expand Down

0 comments on commit 39a36db

Please sign in to comment.