use subtle::Choice;
use crate::Word;
#[derive(Debug, Copy, Clone)]
pub struct CtChoice(Word);
impl CtChoice {
pub const FALSE: Self = Self(0);
pub const TRUE: Self = Self(Word::MAX);
pub(crate) const fn from_mask(value: Word) -> Self {
debug_assert!(value == Self::FALSE.0 || value == Self::TRUE.0);
Self(value)
}
pub(crate) const fn from_lsb(value: Word) -> Self {
debug_assert!(value == 0 || value == 1);
Self(value.wrapping_neg())
}
pub(crate) const fn from_usize_being_nonzero(value: usize) -> Self {
const HI_BIT: u32 = usize::BITS - 1;
Self::from_lsb(((value | value.wrapping_neg()) >> HI_BIT) as Word)
}
pub(crate) const fn from_usize_equality(x: usize, y: usize) -> Self {
Self::from_usize_being_nonzero(x.wrapping_sub(y)).not()
}
pub(crate) const fn from_usize_lt(x: usize, y: usize) -> Self {
let bit = (((!x) & y) | (((!x) | y) & (x.wrapping_sub(y)))) >> (usize::BITS - 1);
Self::from_lsb(bit as Word)
}
pub(crate) const fn not(&self) -> Self {
Self(!self.0)
}
pub(crate) const fn or(&self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub(crate) const fn and(&self, other: Self) -> Self {
Self(self.0 & other.0)
}
pub(crate) const fn select(&self, a: Word, b: Word) -> Word {
a ^ (self.0 & (a ^ b))
}
pub(crate) const fn if_true(&self, x: Word) -> Word {
x & self.0
}
pub(crate) const fn is_true_vartime(&self) -> bool {
self.0 == CtChoice::TRUE.0
}
pub(crate) const fn to_u8(self) -> u8 {
(self.0 as u8) & 1
}
}
impl From<CtChoice> for Choice {
fn from(choice: CtChoice) -> Self {
Choice::from(choice.to_u8())
}
}
impl From<CtChoice> for bool {
fn from(choice: CtChoice) -> Self {
choice.is_true_vartime()
}
}
#[cfg(test)]
mod tests {
use super::CtChoice;
use crate::Word;
#[test]
fn select() {
let a: Word = 1;
let b: Word = 2;
assert_eq!(CtChoice::TRUE.select(a, b), b);
assert_eq!(CtChoice::FALSE.select(a, b), a);
}
}