Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmov/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ mod slice;
use core::{
cmp,
num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
},
};

Expand Down Expand Up @@ -316,11 +316,13 @@ impl_cmov_traits_for_nonzero_integers!(
NonZeroI32,
NonZeroI64,
NonZeroI128,
NonZeroIsize,
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128
NonZeroU128,
NonZeroUsize
);

impl Cmov for cmp::Ordering {
Expand Down
14 changes: 10 additions & 4 deletions cmov/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{Cmov, CmovEq, Condition};
use core::{
cmp,
num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
},
ops::{BitOrAssign, Shl},
ptr, slice,
Expand Down Expand Up @@ -134,7 +134,7 @@ macro_rules! impl_cmov_with_loop {
}

// These types are large enough we don't need to use anything more complex than a simple loop
impl_cmov_with_loop!(u32, u64, u128);
impl_cmov_with_loop!(u32, u64, u128, usize);

/// Ensure the two provided types have the same size and alignment.
macro_rules! assert_size_and_alignment_eq {
Expand Down Expand Up @@ -183,16 +183,19 @@ impl_cmov_with_cast!(
i32 => u32,
i64 => u64,
i128 => u128,
isize => usize,
NonZeroI8 => i8,
NonZeroI16 => i16,
NonZeroI32 => i32,
NonZeroI64 => i64,
NonZeroI128 => i128,
NonZeroIsize => isize,
NonZeroU8 => u8,
NonZeroU16 => u16,
NonZeroU32 => u32,
NonZeroU64 => u64,
NonZeroU128 => u128,
NonZeroUsize => usize,
cmp::Ordering => i8 // #[repr(i8)]
);

Expand Down Expand Up @@ -249,7 +252,7 @@ macro_rules! impl_cmoveq_with_loop {
}

// TODO(tarcieri): investigate word-coalescing impls
impl_cmoveq_with_loop!(u16, u32, u64, u128);
impl_cmoveq_with_loop!(u16, u32, u64, u128, usize);

/// Implement [`CmovEq`] traits by casting to a different type that impls the traits.
macro_rules! impl_cmoveq_with_cast {
Expand Down Expand Up @@ -280,16 +283,19 @@ impl_cmoveq_with_cast!(
i32 => u32,
i64 => u64,
i128 => u128,
isize => usize,
NonZeroI8 => i8,
NonZeroI16 => i16,
NonZeroI32 => i32,
NonZeroI64 => i64,
NonZeroI128 => i128,
NonZeroIsize => isize,
NonZeroU8 => u8,
NonZeroU16 => u16,
NonZeroU32 => u32,
NonZeroU64 => u64,
NonZeroU128 => u128,
NonZeroUsize => usize,
cmp::Ordering => i8 // #[repr(i8)]
);

Expand Down
10 changes: 6 additions & 4 deletions ctutils/src/traits/ct_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use cmov::Cmov;
use core::{
cmp,
num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
},
};

Expand All @@ -19,8 +19,8 @@ use core::num::NonZero;
/// This crate provides built-in implementations for the following types:
/// - [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`isize`]
/// - [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`]
/// - [`NonZeroI8`], [`NonZeroI16`], [`NonZeroI32`], [`NonZeroI64`], [`NonZeroI128`]
/// - [`NonZeroU8`], [`NonZeroU16`], [`NonZeroU32`], [`NonZeroU64`], [`NonZeroU128`]
/// - [`NonZeroI8`], [`NonZeroI16`], [`NonZeroI32`], [`NonZeroI64`], [`NonZeroI128`], [`NonZeroI128`]
/// - [`NonZeroU8`], [`NonZeroU16`], [`NonZeroU32`], [`NonZeroU64`], [`NonZeroU128`],, [`NonZeroUsize`]
/// - [`cmp::Ordering`]
/// - [`Choice`]
/// - `[T]` and `[T; N]` where `T` impls [`CtAssignSlice`], which the previously mentioned
Expand Down Expand Up @@ -106,11 +106,13 @@ impl_ct_assign_slice_with_cmov!(
NonZeroI32,
NonZeroI64,
NonZeroI128,
NonZeroIsize,
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
NonZeroUsize,
cmp::Ordering
);

Expand Down
39 changes: 29 additions & 10 deletions ctutils/src/traits/ct_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,24 +259,43 @@ mod tests {
};
}

macro_rules! ct_eq_test {
macro_rules! ct_eq_test_unsigned {
($ty:ty, $name:ident) => {
#[test]
fn $name() {
let a: $ty = 42;
let b: $ty = 42;
let c: $ty = 1;
let a = <$ty>::MAX;
let b = <$ty>::MAX;
let c = <$ty>::MIN;
truth_table!(a, b, c);
}
};
}

ct_eq_test!(u8, u8_ct_eq);
ct_eq_test!(u16, u16_ct_eq);
ct_eq_test!(u32, u32_ct_eq);
ct_eq_test!(u64, u64_ct_eq);
ct_eq_test!(u128, u128_ct_eq);
ct_eq_test!(usize, usize_ct_eq);
macro_rules! ct_eq_test_signed {
($ty:ty, $name:ident) => {
#[test]
fn $name() {
let a = <$ty>::MAX;
let b = <$ty>::MAX;
let c = <$ty>::MIN;
truth_table!(a, b, c);
}
};
}

ct_eq_test_unsigned!(u8, u8_ct_eq);
ct_eq_test_unsigned!(u16, u16_ct_eq);
ct_eq_test_unsigned!(u32, u32_ct_eq);
ct_eq_test_unsigned!(u64, u64_ct_eq);
ct_eq_test_unsigned!(u128, u128_ct_eq);
ct_eq_test_unsigned!(usize, usize_ct_eq);

ct_eq_test_signed!(i8, i8_ct_eq);
ct_eq_test_signed!(i16, i16_ct_eq);
ct_eq_test_signed!(i32, i32_ct_eq);
ct_eq_test_signed!(i64, i64_ct_eq);
ct_eq_test_signed!(i128, i128_ct_eq);
ct_eq_test_signed!(isize, isize_ct_eq);

#[test]
fn array_ct_eq() {
Expand Down
41 changes: 31 additions & 10 deletions ctutils/src/traits/ct_gt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Choice;
use core::{
cmp,
num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128},
num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize},
};

/// Constant time greater than.
Expand All @@ -26,7 +26,7 @@ macro_rules! impl_unsigned_ct_gt {
};
}

impl_unsigned_ct_gt!(u8, u16, u32, u64, u128);
impl_unsigned_ct_gt!(u8, u16, u32, u64, u128, usize);

/// Impl `CtGt` for `NonZero<T>` by calling `NonZero::get`.
macro_rules! impl_ct_gt_for_nonzero_integer {
Expand All @@ -42,7 +42,14 @@ macro_rules! impl_ct_gt_for_nonzero_integer {
};
}

impl_ct_gt_for_nonzero_integer!(NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128);
impl_ct_gt_for_nonzero_integer!(
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
NonZeroUsize
);

impl CtGt for cmp::Ordering {
#[inline]
Expand All @@ -59,15 +66,29 @@ mod tests {
use super::CtGt;
use core::cmp::Ordering;

#[test]
fn ct_gt() {
let a = 42u64;
let b = 43u64;
assert!(!a.ct_gt(&a).to_bool());
assert!(!a.ct_gt(&b).to_bool());
assert!(b.ct_gt(&a).to_bool());
/// Test `CtGt`
macro_rules! ct_gt_tests {
( $($int:ident),+ ) => {
$(
mod $int {
use super::CtGt;

#[test]
fn ct_gt() {
let a = <$int>::MIN;
let b = <$int>::MAX;
assert!(!a.ct_gt(&a).to_bool());
assert!(!a.ct_gt(&b).to_bool());
assert!(b.ct_gt(&a).to_bool());
}

}
)+
};
}

ct_gt_tests!(u8, u16, u32, u64, u128, usize);

#[test]
fn ordering() {
assert!(!Ordering::Equal.ct_gt(&Ordering::Equal).to_bool());
Expand Down
36 changes: 33 additions & 3 deletions ctutils/src/traits/ct_lt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Choice;
use core::{
cmp,
num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128},
num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize},
};

/// Constant time less than.
Expand All @@ -26,7 +26,7 @@ macro_rules! impl_unsigned_ct_lt {
};
}

impl_unsigned_ct_lt!(u8, u16, u32, u64, u128);
impl_unsigned_ct_lt!(u8, u16, u32, u64, u128, usize);

/// Impl `CtLt` for `NonZero<T>` by calling `NonZero::get`.
macro_rules! impl_ct_lt_for_nonzero_integer {
Expand All @@ -42,7 +42,14 @@ macro_rules! impl_ct_lt_for_nonzero_integer {
};
}

impl_ct_lt_for_nonzero_integer!(NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128);
impl_ct_lt_for_nonzero_integer!(
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
NonZeroUsize
);

impl CtLt for cmp::Ordering {
#[inline]
Expand All @@ -68,6 +75,29 @@ mod tests {
assert!(!b.ct_lt(&a).to_bool());
}

/// Test `CtLt`
macro_rules! ct_lt_tests {
( $($int:ident),+ ) => {
$(
mod $int {
use super::CtLt;

#[test]
fn ct_gt() {
let a = <$int>::MIN;
let b = <$int>::MAX;
assert!(!a.ct_lt(&a).to_bool());
assert!(a.ct_lt(&b).to_bool());
assert!(!b.ct_lt(&a).to_bool());
}

}
)+
};
}

ct_lt_tests!(u8, u16, u32, u64, u128, usize);

#[test]
fn ordering() {
assert!(!Ordering::Equal.ct_lt(&Ordering::Equal).to_bool());
Expand Down
27 changes: 18 additions & 9 deletions ctutils/src/traits/ct_neg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Choice, CtAssign, CtSelect};
use core::num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16, NonZeroU32,
NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
};

/// Constant-time conditional negation: negates a value when `choice` is [`Choice::TRUE`].
Expand Down Expand Up @@ -62,13 +62,15 @@ impl_signed_ct_neg!(
i32,
i64,
i128,
isize,
NonZeroI8,
NonZeroI16,
NonZeroI32,
NonZeroI64,
NonZeroI128
NonZeroI128,
NonZeroIsize
);
impl_unsigned_ct_neg!(u8, u16, u32, u64, u128);
impl_unsigned_ct_neg!(u8, u16, u32, u64, u128, usize);

/// Unfortunately `NonZeroU*` doesn't support `wrapping_neg` for some reason (but `NonZeroI*` does),
/// even though the wrapping negation of any non-zero integer should also be non-zero.
Expand All @@ -89,7 +91,14 @@ macro_rules! impl_ct_neg_for_unsigned_nonzero {
};
}

impl_ct_neg_for_unsigned_nonzero!(NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128);
impl_ct_neg_for_unsigned_nonzero!(
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
NonZeroUsize
);

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -130,14 +139,14 @@ mod tests {
use crate::{Choice, CtNeg};

#[test]
fn u32_ct_neg() {
fn ct_neg() {
let n: $uint = 42;
assert_eq!(n, n.ct_neg(Choice::FALSE));
assert_eq!(<$uint>::MAX - n + 1, n.ct_neg(Choice::TRUE));
}

#[test]
fn u32_ct_neg_assign() {
fn ct_neg_assign() {
let n: $uint = 42;
let mut x = n;
x.ct_neg_assign(Choice::FALSE);
Expand All @@ -151,6 +160,6 @@ mod tests {
};
}

signed_ct_neg_tests!(i8, i16, i32, i64, i128);
unsigned_ct_neg_tests!(u8, u16, u32, u64, u128);
signed_ct_neg_tests!(i8, i16, i32, i64, i128, isize);
unsigned_ct_neg_tests!(u8, u16, u32, u64, u128, usize);
}
Loading