Add some property testing, and fix a bug it found.

This commit is contained in:
2018-11-06 21:53:47 -08:00
parent ccde640f99
commit de5ff87f9e
6 changed files with 75 additions and 25 deletions

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
authors = ["awick"] authors = ["awick"]
[dependencies] [dependencies]
quickcheck = "0.7.1" quickcheck = "^0.7.2"

View File

@@ -135,7 +135,7 @@ needs = [ Need RSA (\ size -> [Req (size `div` 2) Sub,
Req (size + 64) SignedAdd, Req (size + 64) SignedAdd,
Req (size + 64) SignedSub, Req (size + 64) SignedSub,
Req (size + 64) SignedCmp Req (size + 64) SignedCmp
]) ])
, Need ModInv (\ size -> [Req size BaseOps, , Need ModInv (\ size -> [Req size BaseOps,
Req (size + 64) SignedBase, Req (size + 64) SignedBase,
Req (size + 64) BaseOps, Req (size + 64) BaseOps,
@@ -143,7 +143,7 @@ needs = [ Need RSA (\ size -> [Req (size `div` 2) Sub,
Req size EGCD, Req size EGCD,
Req (size + 64) SignedAdd, Req (size + 64) SignedAdd,
Req size Barretts Req size Barretts
]) ])
] ]
-- needs = [ Need ModExp (\ size -> [Req size ModMul -- needs = [ Need ModExp (\ size -> [Req size ModMul
-- ,Req size ModSq -- ,Req size ModSq
@@ -219,6 +219,9 @@ rsaSizes = [512,1024,2048,3072,4096,8192,15360]
baseRequirements :: [Requirement] baseRequirements :: [Requirement]
baseRequirements = concatMap (\ x -> [Req x RSA]) rsaSizes baseRequirements = concatMap (\ x -> [Req x RSA]) rsaSizes
++ [Req 192 Add, Req 256 Add, Req 384 Add] -- used for testing
++ [Req 192 Mul, Req 384 Mul] -- used for testing
++ [Req 448 (Convert 512)] -- used for testing
requirements :: [Requirement] requirements :: [Requirement]
requirements = go baseRequirements requirements = go baseRequirements

View File

@@ -1,5 +1,3 @@
#![recursion_limit="1024"]
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
extern crate quickcheck; extern crate quickcheck;
@@ -8,3 +6,45 @@ pub mod signed;
pub mod unsigned; pub mod unsigned;
#[cfg(test)] #[cfg(test)]
pub mod testing; pub mod testing;
#[cfg(test)]
mod arithmetic {
use super::signed::*;
use super::unsigned::*;
quickcheck! {
fn commutivity_signed_add(x: I576, y: I576) -> bool {
(&x + &y) == (&y + &x)
}
fn commutivity_unsigned_add(x: U576, y: U576) -> bool {
(&x + &y) == (&y + &x)
}
fn commutivity_unsigned_mul(x: U192, y: U192) -> bool {
(&x * &y) == (&y * &x)
}
fn associativity_unsigned_add(x: U192, y: U192, z: U192) -> bool {
(U256::from(&x) + (&y + &z)) == ((&x + &y) + U256::from(&z))
}
fn associativity_unsigned_mul(x: U192, y: U192, z: U192) -> bool {
(U384::from(&x) * (&y * &z)) == ((&x * &y) * U384::from(&z))
}
fn identity_signed_add(x: I576) -> bool {
(&x + I576::zero()) == I640::from(&x)
}
fn identity_unsigned_add(x: U576) -> bool {
(&x + U576::zero()) == U640::from(&x)
}
fn identity_unsigned_mul(x: U192) -> bool {
(&x * U192::from(1u64)) == U384::from(&x)
}
fn additive_inverse(x: I576) -> bool {
(&x + x.negate()) == I640::zero()
}
fn distribution(x: U192, y: U192, z: U192) -> bool {
(U256::from(&x) * (&y + &z)) == U512::from((&x * &y) + (&x * &z))
}
}
}

View File

@@ -60,16 +60,19 @@ macro_rules! add_impls
value: &self.value + &rhs.value value: &self.value + &rhs.value
} }
} else { } else {
if self.value > rhs.value { match self.value.cmp(&rhs.value) {
$bigger { Ordering::Greater =>
negative: self.negative, $bigger {
value: $ubigger::from(&self.value - &rhs.value) negative: self.negative,
} value: $ubigger::from(&self.value - &rhs.value)
} else { },
$bigger { Ordering::Less =>
negative: rhs.negative, $bigger {
value: $ubigger::from(&rhs.value - &self.value) negative: rhs.negative,
} value: $ubigger::from(&rhs.value - &self.value)
},
Ordering::Equal =>
$bigger::zero()
} }
} }
} }

View File

@@ -85,16 +85,18 @@ macro_rules! signed_impls {
} }
} }
// #[cfg(test)]
// impl Arbitrary for $sname {
// fn arbitrary<G: Gen>(g: &mut G) -> $name {
// let neg = g.gen::<bool>();
// let val = g.gen::<$name>();
// $sname{ negative: neg, value: val }
// }
// }
#[cfg(test)] #[cfg(test)]
impl Arbitrary for $sname {
fn arbitrary<G>(g: &mut G) -> $sname
where G: Gen
{
let neg = bool::arbitrary(g);
let val = $name::arbitrary(g);
let neg2 = if val.is_zero() { false } else { neg };
$sname{ negative: neg2, value: val }
}
}
impl fmt::Debug for $sname { impl fmt::Debug for $sname {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.negative { if self.negative {

View File

@@ -26,6 +26,8 @@ mod shift;
#[macro_use] #[macro_use]
mod subtraction; mod subtraction;
#[cfg(test)]
use quickcheck::{Arbitrary,Gen};
use std::cmp::{Ord,Ordering,PartialOrd}; use std::cmp::{Ord,Ordering,PartialOrd};
use std::fmt; use std::fmt;
use std::ops::{Add,AddAssign}; use std::ops::{Add,AddAssign};