Random encoding/decoding tests for `PrimeFieldRepr`. Closes #3.

This commit is contained in:
Sean Bowe 2017-07-18 11:18:42 -06:00
parent a6528a7876
commit 176c77d602
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
5 changed files with 33 additions and 0 deletions

View File

@ -1750,3 +1750,8 @@ fn test_fq_ordering() {
assert!(Fq::from_repr(FqRepr::from(i+1)).unwrap() > Fq::from_repr(FqRepr::from(i)).unwrap());
}
}
#[test]
fn fq_repr_tests() {
::tests::repr::random_repr_tests::<FqRepr>();
}

View File

@ -1453,3 +1453,8 @@ fn fr_field_tests() {
::tests::field::random_sqrt_tests::<Fr>();
::tests::field::random_frobenius_tests::<Fr, _>(Fr::char(), 13);
}
#[test]
fn fr_repr_tests() {
::tests::repr::random_repr_tests::<FrRepr>();
}

View File

@ -332,6 +332,7 @@ pub trait PrimeFieldRepr: Sized +
Ord +
Send +
Sync +
Default +
fmt::Debug +
fmt::Display +
'static +

View File

@ -1,3 +1,4 @@
pub mod curve;
pub mod field;
pub mod engine;
pub mod repr;

21
src/tests/repr.rs Normal file
View File

@ -0,0 +1,21 @@
use rand::{SeedableRng, XorShiftRng};
use ::{PrimeFieldRepr};
pub fn random_repr_tests<R: PrimeFieldRepr>() {
random_encoding_tests::<R>();
}
fn random_encoding_tests<R: PrimeFieldRepr>() {
let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
for _ in 0..1000 {
let r = R::rand(&mut rng);
let mut rdecoded = R::default();
let mut v: Vec<u8> = vec![];
r.write_be(&mut v).unwrap();
rdecoded.read_be(&v[0..]).unwrap();
assert_eq!(r, rdecoded);
}
}