Skip to content

Commit 39a36db

Browse files
Merge bitcoin-core/secp256k1#1054: tests: Fix test whose result is implementation-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
2 parents a310e79 + 3d7cbaf commit 39a36db

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/tests.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -2451,13 +2451,16 @@ void run_field_convert(void) {
24512451
CHECK(secp256k1_memcmp_var(&fes2, &fes, sizeof(fes)) == 0);
24522452
}
24532453

2454-
int fe_secp256k1_memcmp_var(const secp256k1_fe *a, const secp256k1_fe *b) {
2455-
secp256k1_fe t = *b;
2454+
/* Returns true if two field elements have the same representation. */
2455+
int fe_identical(const secp256k1_fe *a, const secp256k1_fe *b) {
2456+
int ret = 1;
24562457
#ifdef VERIFY
2457-
t.magnitude = a->magnitude;
2458-
t.normalized = a->normalized;
2458+
ret &= (a->magnitude == b->magnitude);
2459+
ret &= (a->normalized == b->normalized);
24592460
#endif
2460-
return secp256k1_memcmp_var(a, &t, sizeof(secp256k1_fe));
2461+
/* Compare the struct member that holds the limbs. */
2462+
ret &= (secp256k1_memcmp_var(a->n, b->n, sizeof(a->n)) == 0);
2463+
return ret;
24612464
}
24622465

24632466
void run_field_misc(void) {
@@ -2483,13 +2486,13 @@ void run_field_misc(void) {
24832486
CHECK(x.normalized && x.magnitude == 1);
24842487
#endif
24852488
secp256k1_fe_cmov(&x, &x, 1);
2486-
CHECK(fe_secp256k1_memcmp_var(&x, &z) != 0);
2487-
CHECK(fe_secp256k1_memcmp_var(&x, &q) == 0);
2489+
CHECK(!fe_identical(&x, &z));
2490+
CHECK(fe_identical(&x, &q));
24882491
secp256k1_fe_cmov(&q, &z, 1);
24892492
#ifdef VERIFY
24902493
CHECK(!q.normalized && q.magnitude == z.magnitude);
24912494
#endif
2492-
CHECK(fe_secp256k1_memcmp_var(&q, &z) == 0);
2495+
CHECK(fe_identical(&q, &z));
24932496
secp256k1_fe_normalize_var(&x);
24942497
secp256k1_fe_normalize_var(&z);
24952498
CHECK(!secp256k1_fe_equal_var(&x, &z));

0 commit comments

Comments
 (0)