Add fe_half tests for worst-case inputs

- Add field method _fe_get_bounds
This commit is contained in:
Peter Dettman
2021-12-23 16:41:10 +07:00
parent 4eb8b932ff
commit d64bb5d4f3
4 changed files with 89 additions and 0 deletions

View File

@@ -2471,6 +2471,55 @@ int fe_identical(const secp256k1_fe *a, const secp256k1_fe *b) {
return ret;
}
void run_field_half(void) {
secp256k1_fe t, u;
int m;
/* Check magnitude 0 input */
secp256k1_fe_get_bounds(&t, 0);
secp256k1_fe_half(&t);
#ifdef VERIFY
CHECK(t.magnitude == 1);
CHECK(t.normalized == 0);
#endif
CHECK(secp256k1_fe_normalizes_to_zero(&t));
/* Check non-zero magnitudes in the supported range */
for (m = 1; m < 32; m++) {
/* Check max-value input */
secp256k1_fe_get_bounds(&t, m);
u = t;
secp256k1_fe_half(&u);
#ifdef VERIFY
CHECK(u.magnitude == (m >> 1) + 1);
CHECK(u.normalized == 0);
#endif
secp256k1_fe_normalize_weak(&u);
secp256k1_fe_add(&u, &u);
CHECK(check_fe_equal(&t, &u));
/* Check worst-case input: ensure the LSB is 1 so that P will be added,
* which will also cause all carries to be 1, since all limbs that can
* generate a carry are initially even and all limbs of P are odd in
* every existing field implementation. */
secp256k1_fe_get_bounds(&t, m);
CHECK(t.n[0] > 0);
CHECK((t.n[0] & 1) == 0);
--t.n[0];
u = t;
secp256k1_fe_half(&u);
#ifdef VERIFY
CHECK(u.magnitude == (m >> 1) + 1);
CHECK(u.normalized == 0);
#endif
secp256k1_fe_normalize_weak(&u);
secp256k1_fe_add(&u, &u);
CHECK(check_fe_equal(&t, &u));
}
}
void run_field_misc(void) {
secp256k1_fe x;
secp256k1_fe y;
@@ -6924,6 +6973,7 @@ int main(int argc, char **argv) {
run_scalar_tests();
/* field tests */
run_field_half();
run_field_misc();
run_field_convert();
run_fe_mul();