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
91 changes: 0 additions & 91 deletions ctutils/src/array.rs

This file was deleted.

13 changes: 8 additions & 5 deletions ctutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,19 @@
#[cfg(feature = "alloc")]
extern crate alloc;

pub mod array;
pub mod slice;

mod choice;
mod ct_option;
mod traits;

pub use choice::Choice;
pub use ct_option::CtOption;
pub use traits::{
ct_assign::CtAssign, ct_eq::CtEq, ct_find::CtFind, ct_gt::CtGt, ct_lookup::CtLookup,
ct_lt::CtLt, ct_neg::CtNeg, ct_select::CtSelect,
ct_assign::{CtAssign, CtAssignSlice},
ct_eq::{CtEq, CtEqSlice},
ct_find::CtFind,
ct_gt::CtGt,
ct_lookup::CtLookup,
ct_lt::CtLt,
ct_neg::CtNeg,
ct_select::{CtSelect, CtSelectArray},
};
83 changes: 0 additions & 83 deletions ctutils/src/slice.rs

This file was deleted.

129 changes: 83 additions & 46 deletions ctutils/src/traits/ct_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,89 @@ use core::{
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec::Vec};

#[cfg(doc)]
use core::num::NonZero;

/// Constant-time conditional assignment: assign a given value to another based on a [`Choice`].
///
/// 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`]
/// - [`cmp::Ordering`]
/// - [`Choice`]
/// - `[T]` and `[T; N]` where `T` impls [`CtAssignSlice`], which the previously mentioned
/// types all do.
pub trait CtAssign<Rhs: ?Sized = Self> {
/// Conditionally assign `rhs` to `self` if `choice` is [`Choice::TRUE`].
fn ct_assign(&mut self, rhs: &Rhs, choice: Choice);
/// Conditionally assign `src` to `self` if `choice` is [`Choice::TRUE`].
fn ct_assign(&mut self, src: &Rhs, choice: Choice);
}

/// Implementing this trait enables use of the [`CtAssign`] trait for `[T]` where `T` is the
/// `Self` type implementing the trait, via a blanket impl.
///
/// It needs to be a separate trait from [`CtAssign`] because we need to be able to impl
/// [`CtAssign`] for `[T]`.
pub trait CtAssignSlice: CtAssign + Sized {
/// Conditionally assign `src` to `dst` if `choice` is [`Choice::TRUE`], or leave it unchanged
/// for [`Choice::FALSE`].
fn ct_assign_slice(dst: &mut [Self], src: &[Self], choice: Choice) {
assert_eq!(
dst.len(),
src.len(),
"source slice length ({}) does not match destination slice length ({})",
src.len(),
dst.len()
);

for (a, b) in dst.iter_mut().zip(src) {
a.ct_assign(b, choice);
}
}
}

impl<T: CtAssignSlice> CtAssign for [T] {
fn ct_assign(&mut self, src: &[T], choice: Choice) {
T::ct_assign_slice(self, src, choice);
}
}

/// Impl `CtAssign` using the `cmov::Cmov` trait
macro_rules! impl_ct_assign_with_cmov {
( $($ty:ty),+ ) => {
$(
impl CtAssign for $ty {
#[inline]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
self.cmovnz(rhs, choice.into());
}
}
)+
};
}

/// Impl `CtAssign` and `CtAssignSlice` using the `cmov::Cmov` trait
macro_rules! impl_ct_assign_slice_with_cmov {
( $($ty:ty),+ ) => {
$(
impl_ct_assign_with_cmov!($ty);

impl CtAssignSlice for $ty {
#[inline]
fn ct_assign_slice(dst: &mut [Self], src: &[Self], choice: Choice) {
dst.cmovnz(src, choice.into());
}
}
)+
};
}

impl_ct_assign_slice_with_cmov!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
impl_ct_assign_with_cmov!(isize, usize);
impl CtAssignSlice for isize {}
impl CtAssignSlice for usize {}

/// Impl `CtAssign` using the `CtSelect` trait.
///
/// In cases where `CtSelect` is more straightforward to implement, but you want to use a provided
Expand All @@ -31,6 +108,8 @@ macro_rules! impl_ct_assign_with_ct_select {
*self = Self::ct_select(self, rhs, choice);
}
}

impl CtAssignSlice for $ty {}
)+
};
}
Expand All @@ -49,50 +128,6 @@ impl_ct_assign_with_ct_select!(
NonZeroU128
);

/// Impl `CtAssign` using the `cmov::Cmov` trait
macro_rules! impl_ct_assign_with_cmov {
( $($ty:ty),+ ) => {
$(
impl CtAssign for $ty {
#[inline]
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
self.cmovnz(rhs, choice.into());
}
}
)+
};
}

impl_ct_assign_with_cmov!(
i8,
i16,
i32,
i64,
i128,
u8,
u16,
u32,
u64,
u128,
[i8],
[i16],
[i32],
[i64],
[i128],
[u8],
[u16],
[u32],
[u64],
[u128]
);

#[cfg(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64"
))]
impl_ct_assign_with_cmov!(isize, usize);

impl<T, const N: usize> CtAssign for [T; N]
where
[T]: CtAssign,
Expand All @@ -103,6 +138,8 @@ where
}
}

impl<T, const N: usize> CtAssignSlice for [T; N] where [T]: CtAssign {}

#[cfg(feature = "alloc")]
impl<T> CtAssign for Box<T>
where
Expand Down
Loading