Skip to content

Commit df6e1b3

Browse files
authored
ctutils: initial proptests (#1327)
Adds initial proptests for `CtEq::ct_eq` and `CtSelect::ct_select` for all core signed and unsigned integer types
1 parent 1f73405 commit df6e1b3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ctutils/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ cmov = "0.4.3"
2222
# optional dependencies
2323
subtle = { version = "2", optional = true, default-features = false }
2424

25+
[dev-dependencies]
26+
proptest = "1.9"
27+
2528
[package.metadata.docs.rs]
2629
all-features = true

ctutils/tests/proptests.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// Write the proptests for an integer type.
2+
macro_rules! int_proptests {
3+
( $($int:ident),+ ) => {
4+
$(
5+
mod $int {
6+
use ctutils::{CtSelect, CtEq, Choice};
7+
use proptest::prelude::*;
8+
9+
proptest! {
10+
#[test]
11+
fn ct_assign(a in any::<$int>(), b in any::<$int>(), byte in any::<u8>()) {
12+
let choice = Choice::from_u8_lsb(byte);
13+
let mut actual = a;
14+
actual.ct_assign(&b, choice);
15+
16+
let expected = if byte & 1 == 1 {
17+
b
18+
} else {
19+
a
20+
};
21+
22+
prop_assert_eq!(expected, actual);
23+
}
24+
25+
#[test]
26+
fn ct_eq(a in any::<$int>(), b in any::<$int>()) {
27+
let actual = a.ct_eq(&b);
28+
prop_assert_eq!(a == b, actual.to_bool());
29+
}
30+
31+
#[test]
32+
fn ct_ne(a in any::<$int>(), b in any::<$int>()) {
33+
let actual = a.ct_ne(&b);
34+
prop_assert_eq!(a != b, actual.to_bool());
35+
}
36+
37+
#[test]
38+
fn ct_select(a in any::<$int>(), b in any::<$int>(), byte in any::<u8>()) {
39+
let choice = Choice::from_u8_lsb(byte);
40+
let actual = a.ct_select(&b, choice);
41+
42+
let expected = if byte & 1 == 1 {
43+
b
44+
} else {
45+
a
46+
};
47+
48+
prop_assert_eq!(expected, actual);
49+
}
50+
}
51+
}
52+
)+
53+
};
54+
}
55+
56+
int_proptests!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);

0 commit comments

Comments
 (0)