3.5.2 Add more tests to ff-group-tests

The audit recommends checking failure cases for from_bytes,
from_bytes_unechecked, and from_repr. This isn't feasible.

from_bytes is allowed to have non-canonical values. [0xff; 32] may accordingly
be a valid point for non-SEC1-encoded curves.

from_bytes_unchecked doesn't have a defined failure mode, and by name,
unchecked, shouldn't necessarily fail. The audit acknowledges the tests should
test for whatever result is 'appropriate', yet any result which isn't a failure
on a valid element is appropriate.

from_repr must be canonical, yet for a binary field of 2^n where n % 8 == 0, a
[0xff; n / 8] repr would be valid.
This commit is contained in:
Luke Parker
2023-02-24 06:03:56 -05:00
parent 32c18cac84
commit 93f7afec8b
13 changed files with 95 additions and 25 deletions

View File

@@ -1,3 +1,4 @@
use rand_core::RngCore;
use group::ff::Field;
/// Perform basic tests on equality.
@@ -106,8 +107,27 @@ pub fn test_cube<F: Field>() {
assert_eq!(two.cube(), two * two * two, "2^3 != 8");
}
/// Test random.
pub fn test_random<R: RngCore, F: Field>(rng: &mut R) {
let a = F::random(&mut *rng);
// Run up to 128 times so small fields, which may occasionally return the same element twice,
// are statistically unlikely to fail
// Field of order 1 will always fail this test due to lack of distinct elements to sample
// from
let mut pass = false;
for _ in 0 .. 128 {
let b = F::random(&mut *rng);
// This test passes if a distinct element is returned at least once
if b != a {
pass = true;
}
}
assert!(pass, "random always returned the same value");
}
/// Run all tests on fields implementing Field.
pub fn test_field<F: Field>() {
pub fn test_field<R: RngCore, F: Field>(rng: &mut R) {
test_eq::<F>();
test_conditional_select::<F>();
test_add::<F>();
@@ -119,4 +139,5 @@ pub fn test_field<F: Field>() {
test_sqrt::<F>();
test_is_zero::<F>();
test_cube::<F>();
test_random::<R, F>(rng);
}