Fix VERIFY calculations in _fe_cmov methods
This commit is contained in:
parent
17f7148606
commit
a0601cd79c
|
@ -1083,8 +1083,10 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k
|
||||||
r->n[8] = (r->n[8] & mask0) | (a->n[8] & mask1);
|
r->n[8] = (r->n[8] & mask0) | (a->n[8] & mask1);
|
||||||
r->n[9] = (r->n[9] & mask0) | (a->n[9] & mask1);
|
r->n[9] = (r->n[9] & mask0) | (a->n[9] & mask1);
|
||||||
#ifdef VERIFY
|
#ifdef VERIFY
|
||||||
r->magnitude = (r->magnitude & mask0) | (a->magnitude & mask1);
|
if (a->magnitude > r->magnitude) {
|
||||||
r->normalized = (r->normalized & mask0) | (a->normalized & mask1);
|
r->magnitude = a->magnitude;
|
||||||
|
}
|
||||||
|
r->normalized &= a->normalized;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -414,8 +414,10 @@ static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k
|
||||||
r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
|
r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
|
||||||
r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
|
r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
|
||||||
#ifdef VERIFY
|
#ifdef VERIFY
|
||||||
r->magnitude = (r->magnitude & mask0) | (a->magnitude & mask1);
|
if (a->magnitude > r->magnitude) {
|
||||||
r->normalized = (r->normalized & mask0) | (a->normalized & mask1);
|
r->magnitude = a->magnitude;
|
||||||
|
}
|
||||||
|
r->normalized &= a->normalized;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
18
src/tests.c
18
src/tests.c
|
@ -737,6 +737,15 @@ void run_field_convert(void) {
|
||||||
CHECK(memcmp(&fes2, &fes, sizeof(fes)) == 0);
|
CHECK(memcmp(&fes2, &fes, sizeof(fes)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fe_memcmp(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
|
||||||
|
secp256k1_fe_t t = *b;
|
||||||
|
#ifdef VERIFY
|
||||||
|
t.magnitude = a->magnitude;
|
||||||
|
t.normalized = a->normalized;
|
||||||
|
#endif
|
||||||
|
return memcmp(a, &t, sizeof(secp256k1_fe_t));
|
||||||
|
}
|
||||||
|
|
||||||
void run_field_misc(void) {
|
void run_field_misc(void) {
|
||||||
secp256k1_fe_t x;
|
secp256k1_fe_t x;
|
||||||
secp256k1_fe_t y;
|
secp256k1_fe_t y;
|
||||||
|
@ -757,12 +766,13 @@ void run_field_misc(void) {
|
||||||
q = x;
|
q = x;
|
||||||
secp256k1_fe_cmov(&x, &z, 0);
|
secp256k1_fe_cmov(&x, &z, 0);
|
||||||
secp256k1_fe_cmov(&x, &x, 1);
|
secp256k1_fe_cmov(&x, &x, 1);
|
||||||
CHECK(memcmp(&x, &z, sizeof(x)) != 0);
|
CHECK(fe_memcmp(&x, &z) != 0);
|
||||||
CHECK(memcmp(&x, &q, sizeof(x)) == 0);
|
CHECK(fe_memcmp(&x, &q) == 0);
|
||||||
secp256k1_fe_cmov(&q, &z, 1);
|
secp256k1_fe_cmov(&q, &z, 1);
|
||||||
CHECK(memcmp(&q, &z, sizeof(q)) == 0);
|
CHECK(fe_memcmp(&q, &z) == 0);
|
||||||
/* Test storage conversion and conditional moves. */
|
/* Test storage conversion and conditional moves. */
|
||||||
secp256k1_fe_normalize(&z);
|
secp256k1_fe_normalize_var(&x);
|
||||||
|
secp256k1_fe_normalize_var(&z);
|
||||||
CHECK(!secp256k1_fe_equal_var(&x, &z));
|
CHECK(!secp256k1_fe_equal_var(&x, &z));
|
||||||
secp256k1_fe_to_storage(&xs, &x);
|
secp256k1_fe_to_storage(&xs, &x);
|
||||||
secp256k1_fe_to_storage(&ys, &y);
|
secp256k1_fe_to_storage(&ys, &y);
|
||||||
|
|
Loading…
Reference in New Issue