Clean up field tests

This commit is contained in:
Henry de Valence 2017-03-12 23:17:34 -07:00
parent 97b60d61ab
commit 92bf47f990

View file

@ -961,40 +961,46 @@ impl FieldElement {
} }
#[cfg(test)] #[cfg(test)]
mod test { mod bench {
use field::*; use super::*;
use field;
use test::Bencher; use test::Bencher;
use subtle::CTNegatable;
#[bench] #[bench]
fn bench_mul64(b: &mut Bencher) { fn bench_mul64(b: &mut Bencher) {
let x = [1u64; 5]; let x = [1u64; 5];
let y = [1u64; 5]; let y = [1u64; 5];
b.iter(|| mul64(&x,&y)); b.iter(|| mul64(&x, &y));
} }
#[bench] #[bench]
fn bench_fieldelement_a_mul_a(b: &mut Bencher) { fn mul_operator(b: &mut Bencher) {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&field::test::A_BYTES);
b.iter(|| &a*&a); b.iter(|| &a*&a);
} }
#[bench] #[bench]
fn bench_fieldelement_a_sq(b: &mut Bencher) { fn square(b: &mut Bencher) {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&field::test::A_BYTES);
b.iter(|| a.square()); b.iter(|| a.square());
} }
#[bench] #[bench]
fn bench_fieldelement_a_inv(b: &mut Bencher) { fn invert(b: &mut Bencher) {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&field::test::A_BYTES);
b.iter(|| a.invert()); b.iter(|| a.invert());
} }
}
#[cfg(test)]
mod test {
use field::*;
use subtle::CTNegatable;
/// Random element a of GF(2^255-19), from Sage /// Random element a of GF(2^255-19), from Sage
/// a = 1070314506888354081329385823235218444233221\ /// a = 1070314506888354081329385823235218444233221\
/// 2228051251926706380353716438957572 /// 2228051251926706380353716438957572
static A_BYTES: [u8;32] = pub static A_BYTES: [u8;32] =
[ 0x04, 0xfe, 0xdf, 0x98, 0xa7, 0xfa, 0x0a, 0x68, [ 0x04, 0xfe, 0xdf, 0x98, 0xa7, 0xfa, 0x0a, 0x68,
0x84, 0x92, 0xbd, 0x59, 0x08, 0x07, 0xa7, 0x03, 0x84, 0x92, 0xbd, 0x59, 0x08, 0x07, 0xa7, 0x03,
0x9e, 0xd1, 0xf6, 0xf2, 0xe1, 0xd9, 0xe2, 0xa4, 0x9e, 0xd1, 0xf6, 0xf2, 0xe1, 0xd9, 0xe2, 0xa4,
@ -1022,43 +1028,55 @@ mod test {
0x15, 0x21, 0xf9, 0xe3, 0xe1, 0x61, 0x21, 0x55]; 0x15, 0x21, 0xf9, 0xe3, 0xe1, 0x61, 0x21, 0x55];
#[test] #[test]
fn test_fieldelement_a_mul_a() { fn a_mul_a_vs_a_squared_constant() {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&A_BYTES);
let asq = FieldElement::from_bytes(&ASQ_BYTES); let asq = FieldElement::from_bytes(&ASQ_BYTES);
assert_eq!(asq, &a*&a); assert_eq!(asq, &a*&a);
}
#[test]
fn a_square_vs_a_squared_constant() {
let a = FieldElement::from_bytes(&A_BYTES);
let asq = FieldElement::from_bytes(&ASQ_BYTES);
assert_eq!(asq, a.square()); assert_eq!(asq, a.square());
} }
#[test] #[test]
fn test_fieldelement_a_square2() { fn a_square2_vs_a_squared_constant() {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&A_BYTES);
let asq = FieldElement::from_bytes(&ASQ_BYTES); let asq = FieldElement::from_bytes(&ASQ_BYTES);
assert_eq!(a.square2(), &asq+&asq); assert_eq!(a.square2(), &asq+&asq);
} }
#[test] #[test]
fn test_fieldelement_a_inv() { fn a_invert_vs_inverse_of_a_constant() {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&A_BYTES);
let ainv = FieldElement::from_bytes(&AINV_BYTES); let ainv = FieldElement::from_bytes(&AINV_BYTES);
assert_eq!(ainv, a.invert()); let should_be_inverse = a.invert();
assert_eq!(ainv, should_be_inverse);
assert_eq!(FieldElement::one(), &a * &should_be_inverse);
} }
#[test] #[test]
fn test_fieldelement_a_p58() { fn a_p58_vs_ap58_constant() {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&A_BYTES);
let ap58 = FieldElement::from_bytes(&AP58_BYTES); let ap58 = FieldElement::from_bytes(&AP58_BYTES);
assert_eq!(ap58, a.pow_p58()); assert_eq!(ap58, a.pow_p58());
} }
#[test] #[test]
fn test_fieldelement_a_chi() { fn chi_on_square_and_nonsquare() {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&A_BYTES);
// a is square // a is square
assert_eq!(a.chi(), FieldElement::one()); assert_eq!(a.chi(), FieldElement::one());
let mut two_bytes = [0u8; 32]; two_bytes[0] = 2;
let two = FieldElement::from_bytes(&two_bytes);
// 2 is nonsquare
assert_eq!(two.chi(), FieldElement::minus_one());
} }
#[test] #[test]
fn test_fieldelement_eq() { fn equality() {
let a = FieldElement::from_bytes(&A_BYTES); let a = FieldElement::from_bytes(&A_BYTES);
let ainv = FieldElement::from_bytes(&AINV_BYTES); let ainv = FieldElement::from_bytes(&AINV_BYTES);
assert!(a == a); assert!(a == a);
@ -1068,42 +1086,44 @@ mod test {
/// Notice that the last element has the high bit set, which /// Notice that the last element has the high bit set, which
/// should be ignored /// should be ignored
static B_BYTES: [u8;32] = static B_BYTES: [u8;32] =
[113, 191, 169, 143, 91, 234, 121, 15, 241, 131, 217, 36, 230, 101, 92, 234, 8, 208, 170, 251, 97, 127, 70, 210, 58, 23, 166, 87, 240, 169, 184, 178]; [113, 191, 169, 143, 91, 234, 121, 15,
241, 131, 217, 36, 230, 101, 92, 234,
8, 208, 170, 251, 97, 127, 70, 210,
58, 23, 166, 87, 240, 169, 184, 178];
static B_LIMBS: FieldElement = FieldElement( static B_LIMBS_RADIX_25_5: FieldElement = FieldElement(
[-5652623, 8034020, 8266223, -13556020, -5672552, -5582839, -12603138, 15161929, -16418207, 13296296]); [-5652623, 8034020, 8266223, -13556020, -5672552,
-5582839, -12603138, 15161929, -16418207, 13296296]);
#[test] #[test]
fn test_fieldelement_frombytes_highbit_is_ignored() { fn from_bytes_highbit_is_ignored() {
let mut cleared_bytes = B_BYTES.clone(); let mut cleared_bytes = B_BYTES.clone();
cleared_bytes[31] &= 127u8; cleared_bytes[31] &= 127u8;
let orig_elt = FieldElement::from_bytes(&B_BYTES); let with_highbit_set = FieldElement::from_bytes(&B_BYTES);
let cleared_elt = FieldElement::from_bytes(&cleared_bytes); let without_highbit_set = FieldElement::from_bytes(&cleared_bytes);
for i in 0..10 { assert_eq!(without_highbit_set, with_highbit_set);
assert!(orig_elt[i] == cleared_elt[i]);
}
} }
#[test] #[test]
fn test_fieldelement_to_bytes() { fn from_bytes_vs_radix_25_5_limb_constants() {
let test_elt = FieldElement::from_bytes(&B_BYTES); let test_elt = FieldElement::from_bytes(&B_BYTES);
for i in 0..10 { for i in 0..10 {
assert!(test_elt[i] == B_LIMBS[i]); assert!(test_elt[i] == B_LIMBS_RADIX_25_5[i]);
} }
} }
#[test] #[test]
fn test_fieldelement_from_bytes() { fn radix_25_5_limb_constants_to_bytes_vs_byte_constants() {
let test_bytes = B_LIMBS.to_bytes(); let test_bytes = B_LIMBS_RADIX_25_5.to_bytes();
for i in 0..31 { for i in 0..31 {
assert!(test_bytes[i] == B_BYTES[i]); assert!(test_bytes[i] == B_BYTES[i]);
} }
// high bit is set to zero in to_bytes // Check that high bit is set to zero in to_bytes
assert!(test_bytes[31] == (B_BYTES[31] & 127u8)); assert!(test_bytes[31] == (B_BYTES[31] & 127u8));
} }
#[test] #[test]
fn test_conditional_negate() { fn conditional_negate() {
let one = FieldElement([ 1,0,0,0,0,0,0,0,0,0]); let one = FieldElement([ 1,0,0,0,0,0,0,0,0,0]);
let minus_one = FieldElement([-1,0,0,0,0,0,0,0,0,0]); let minus_one = FieldElement([-1,0,0,0,0,0,0,0,0,0]);
let mut x = one; let mut x = one;