Skip to content

Commit

Permalink
Fix VERIFY calculations in _fe_cmov methods
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdettman committed Jul 4, 2015
1 parent 17f7148 commit a0601cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/field_10x26_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[9] = (r->n[9] & mask0) | (a->n[9] & mask1);
#ifdef VERIFY
r->magnitude = (r->magnitude & mask0) | (a->magnitude & mask1);
r->normalized = (r->normalized & mask0) | (a->normalized & mask1);
if (a->magnitude > r->magnitude) {
r->magnitude = a->magnitude;
}
r->normalized &= a->normalized;
#endif
}

Expand Down
6 changes: 4 additions & 2 deletions src/field_5x52_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
#ifdef VERIFY
r->magnitude = (r->magnitude & mask0) | (a->magnitude & mask1);
r->normalized = (r->normalized & mask0) | (a->normalized & mask1);
if (a->magnitude > r->magnitude) {
r->magnitude = a->magnitude;
}
r->normalized &= a->normalized;
#endif
}

Expand Down
18 changes: 14 additions & 4 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,15 @@ void run_field_convert(void) {
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) {
secp256k1_fe_t x;
secp256k1_fe_t y;
Expand All @@ -757,12 +766,13 @@ void run_field_misc(void) {
q = x;
secp256k1_fe_cmov(&x, &z, 0);
secp256k1_fe_cmov(&x, &x, 1);
CHECK(memcmp(&x, &z, sizeof(x)) != 0);
CHECK(memcmp(&x, &q, sizeof(x)) == 0);
CHECK(fe_memcmp(&x, &z) != 0);
CHECK(fe_memcmp(&x, &q) == 0);
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. */
secp256k1_fe_normalize(&z);
secp256k1_fe_normalize_var(&x);
secp256k1_fe_normalize_var(&z);
CHECK(!secp256k1_fe_equal_var(&x, &z));
secp256k1_fe_to_storage(&xs, &x);
secp256k1_fe_to_storage(&ys, &y);
Expand Down

0 comments on commit a0601cd

Please sign in to comment.