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)]
mod test {
use field::*;
mod bench {
use super::*;
use field;
use test::Bencher;
use subtle::CTNegatable;
#[bench]
fn bench_mul64(b: &mut Bencher) {
let x = [1u64; 5];
let y = [1u64; 5];
b.iter(|| mul64(&x,&y));
b.iter(|| mul64(&x, &y));
}
#[bench]
fn bench_fieldelement_a_mul_a(b: &mut Bencher) {
let a = FieldElement::from_bytes(&A_BYTES);
fn mul_operator(b: &mut Bencher) {
let a = FieldElement::from_bytes(&field::test::A_BYTES);
b.iter(|| &a*&a);
}
#[bench]
fn bench_fieldelement_a_sq(b: &mut Bencher) {
let a = FieldElement::from_bytes(&A_BYTES);
fn square(b: &mut Bencher) {
let a = FieldElement::from_bytes(&field::test::A_BYTES);
b.iter(|| a.square());
}
#[bench]
fn bench_fieldelement_a_inv(b: &mut Bencher) {
let a = FieldElement::from_bytes(&A_BYTES);
fn invert(b: &mut Bencher) {
let a = FieldElement::from_bytes(&field::test::A_BYTES);
b.iter(|| a.invert());
}
}
#[cfg(test)]
mod test {
use field::*;
use subtle::CTNegatable;
/// Random element a of GF(2^255-19), from Sage
/// a = 1070314506888354081329385823235218444233221\
/// 2228051251926706380353716438957572
static A_BYTES: [u8;32] =
pub static A_BYTES: [u8;32] =
[ 0x04, 0xfe, 0xdf, 0x98, 0xa7, 0xfa, 0x0a, 0x68,
0x84, 0x92, 0xbd, 0x59, 0x08, 0x07, 0xa7, 0x03,
0x9e, 0xd1, 0xf6, 0xf2, 0xe1, 0xd9, 0xe2, 0xa4,
@ -1022,43 +1028,55 @@ mod test {
0x15, 0x21, 0xf9, 0xe3, 0xe1, 0x61, 0x21, 0x55];
#[test]
fn test_fieldelement_a_mul_a() {
fn a_mul_a_vs_a_squared_constant() {
let a = FieldElement::from_bytes(&A_BYTES);
let asq = FieldElement::from_bytes(&ASQ_BYTES);
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());
}
#[test]
fn test_fieldelement_a_square2() {
fn a_square2_vs_a_squared_constant() {
let a = FieldElement::from_bytes(&A_BYTES);
let asq = FieldElement::from_bytes(&ASQ_BYTES);
assert_eq!(a.square2(), &asq+&asq);
}
#[test]
fn test_fieldelement_a_inv() {
fn a_invert_vs_inverse_of_a_constant() {
let a = FieldElement::from_bytes(&A_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]
fn test_fieldelement_a_p58() {
fn a_p58_vs_ap58_constant() {
let a = FieldElement::from_bytes(&A_BYTES);
let ap58 = FieldElement::from_bytes(&AP58_BYTES);
assert_eq!(ap58, a.pow_p58());
}
#[test]
fn test_fieldelement_a_chi() {
fn chi_on_square_and_nonsquare() {
let a = FieldElement::from_bytes(&A_BYTES);
// a is square
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]
fn test_fieldelement_eq() {
fn equality() {
let a = FieldElement::from_bytes(&A_BYTES);
let ainv = FieldElement::from_bytes(&AINV_BYTES);
assert!(a == a);
@ -1068,42 +1086,44 @@ mod test {
/// Notice that the last element has the high bit set, which
/// should be ignored
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(
[-5652623, 8034020, 8266223, -13556020, -5672552, -5582839, -12603138, 15161929, -16418207, 13296296]);
static B_LIMBS_RADIX_25_5: FieldElement = FieldElement(
[-5652623, 8034020, 8266223, -13556020, -5672552,
-5582839, -12603138, 15161929, -16418207, 13296296]);
#[test]
fn test_fieldelement_frombytes_highbit_is_ignored() {
fn from_bytes_highbit_is_ignored() {
let mut cleared_bytes = B_BYTES.clone();
cleared_bytes[31] &= 127u8;
let orig_elt = FieldElement::from_bytes(&B_BYTES);
let cleared_elt = FieldElement::from_bytes(&cleared_bytes);
for i in 0..10 {
assert!(orig_elt[i] == cleared_elt[i]);
}
let with_highbit_set = FieldElement::from_bytes(&B_BYTES);
let without_highbit_set = FieldElement::from_bytes(&cleared_bytes);
assert_eq!(without_highbit_set, with_highbit_set);
}
#[test]
fn test_fieldelement_to_bytes() {
fn from_bytes_vs_radix_25_5_limb_constants() {
let test_elt = FieldElement::from_bytes(&B_BYTES);
for i in 0..10 {
assert!(test_elt[i] == B_LIMBS[i]);
assert!(test_elt[i] == B_LIMBS_RADIX_25_5[i]);
}
}
#[test]
fn test_fieldelement_from_bytes() {
let test_bytes = B_LIMBS.to_bytes();
fn radix_25_5_limb_constants_to_bytes_vs_byte_constants() {
let test_bytes = B_LIMBS_RADIX_25_5.to_bytes();
for i in 0..31 {
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));
}
#[test]
fn test_conditional_negate() {
fn conditional_negate() {
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 mut x = one;