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
1 change: 1 addition & 0 deletions .github/workflows/ctutils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
- run: cargo test
- run: cargo test --features subtle
- run: cargo test --all-features
- run: cargo test --all-features --release

Expand Down
24 changes: 20 additions & 4 deletions ctutils/src/choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ impl From<Choice> for bool {
}
}

impl Not for Choice {
type Output = Choice;

#[inline]
fn not(self) -> Choice {
self.not()
}
}

#[cfg(feature = "subtle")]
impl From<subtle::Choice> for Choice {
#[inline]
Expand All @@ -426,12 +435,19 @@ impl From<Choice> for subtle::Choice {
}
}

impl Not for Choice {
type Output = Choice;
#[cfg(feature = "subtle")]
impl subtle::ConditionallySelectable for Choice {
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
CtSelect::ct_select(a, b, choice.into())
}
}

#[cfg(feature = "subtle")]
impl subtle::ConstantTimeEq for Choice {
#[inline]
fn not(self) -> Choice {
self.not()
fn ct_eq(&self, other: &Self) -> subtle::Choice {
CtEq::ct_eq(self, other).into()
}
}

Expand Down
23 changes: 23 additions & 0 deletions ctutils/src/ct_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,29 @@ impl<T> From<CtOption<T>> for subtle::CtOption<T> {
}
}

#[cfg(feature = "subtle")]
impl<T> subtle::ConditionallySelectable for CtOption<T>
where
T: Copy, // `ConditionallySelectable` supertrait bound
Self: CtSelect,
{
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
CtSelect::ct_select(a, b, choice.into())
}
}

#[cfg(feature = "subtle")]
impl<T> subtle::ConstantTimeEq for CtOption<T>
where
Self: CtEq,
{
#[inline]
fn ct_eq(&self, other: &Self) -> subtle::Choice {
CtEq::ct_eq(self, other).into()
}
}

#[cfg(test)]
mod tests {
use crate::{Choice, CtEq, CtOption, CtSelect};
Expand Down
22 changes: 22 additions & 0 deletions ctutils/src/traits/ct_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::Choice;
use cmov::CmovEq;
use core::cmp;

#[cfg(feature = "subtle")]
use crate::CtOption;

/// Constant-time equality: like `(Partial)Eq` with [`Choice`] instead of [`bool`].
///
/// Impl'd for: [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`], [`cmp::Ordering`],
Expand Down Expand Up @@ -89,6 +92,25 @@ impl<T: CtEq, const N: usize> CtEq for [T; N] {
}
}

#[cfg(feature = "subtle")]
impl CtEq for subtle::Choice {
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
self.unwrap_u8().ct_eq(&other.unwrap_u8())
}
}

#[cfg(feature = "subtle")]
impl<T> CtEq for subtle::CtOption<T>
where
T: CtEq + Default + subtle::ConditionallySelectable,
{
#[inline]
fn ct_eq(&self, other: &Self) -> Choice {
CtOption::from(*self).ct_eq(&CtOption::from(*other))
}
}

#[cfg(test)]
mod tests {
use super::CtEq;
Expand Down
26 changes: 26 additions & 0 deletions ctutils/src/traits/ct_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::Choice;
use cmov::Cmov;
use core::cmp;

#[cfg(feature = "subtle")]
use crate::CtOption;

/// Constant-time selection: pick between two values based on a given [`Choice`].
pub trait CtSelect: Sized {
/// Select between `self` and `other` based on `choice`, returning a copy of the value.
Expand Down Expand Up @@ -114,6 +117,29 @@ where
}
}

#[cfg(feature = "subtle")]
impl CtSelect for subtle::Choice {
#[inline]
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
Choice::from(*self)
.ct_select(&Choice::from(*other), choice)
.into()
}
}

#[cfg(feature = "subtle")]
impl<T> CtSelect for subtle::CtOption<T>
where
T: CtSelect + Default + subtle::ConditionallySelectable,
{
#[inline]
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
CtOption::from(*self)
.ct_select(&CtOption::from(*other), choice)
.into()
}
}

#[cfg(test)]
mod tests {
use super::{Choice, CtSelect, cmp};
Expand Down