Skip to main content

Condition

Struct Condition 

Source
pub struct Condition<T>(/* private fields */)
where
    MaskType<T>: SupportedMaskType;
Expand description

Helper functions for checking some condition on some data using constant-time operations.

Implementations§

Source§

impl Condition<i64>

Source

pub const TRUE: Self

TRUE is the bit vector of all 1’s

Source

pub const FALSE: Self

FALSE is the bit vector of all 0’s

Source

pub const fn from_bool<const VALUE: bool>() -> Self

Source

pub const fn from_bool_var(value: bool) -> Self

Source

pub const fn is_bit_set(value: i64, bit: i64) -> Self

Source

pub const fn is_negative(value: i64) -> Self

Source

pub const fn is_not_zero(value: i64) -> Self

Source

pub const fn is_zero(value: i64) -> Self

Source

pub const fn is_equal(x: i64, y: i64) -> Self

Source

pub const fn is_lt(x: i64, y: i64) -> Self

Source

pub fn is_lte(x: i64, y: i64) -> Self

Source

pub const fn is_gt(x: i64, y: i64) -> Self

Source

pub fn is_gte(x: i64, y: i64) -> Self

Source

pub fn is_within_range(value: i64, min: i64, max: i64) -> Self

Source

pub fn is_in_list(value: i64, list: &[i64]) -> Self

Source

pub fn mov(self, src: i64, dst: &mut i64)

Conditionally move the source value to the destination if the condition is true, otherwise nothing is moved.

Source

pub const fn negate(self, value: i64) -> i64

Conditionally negate the value.

negate(-1) gives -3

value is -1 (i.e., all bits are 1, ...1111)

Condition self.0 is 1 (...0001) (assuming TRUE)

XOR operation was executed as value ^ self.0

Then ...1111 XOR ...0001 = ...1110 (i.e., -2)

Subtraction operation is wrapping_sub(self.0)

Then -2 - 1 = -3

As a result, 1, which is the negation of -1, should be returned, but -3 is output.

Therefore, if the Self::TRUE constant value of the i64 Condition implementation is changed to -1, the test also runs normally.

Source

pub const fn or_halves(value: i64) -> i64

Source

pub const fn select(self, true_value: i64, false_value: i64) -> i64

Conditional selection: return true_value if the condition is true, otherwise return false_value.

Source

pub const fn swap(self, lhs: i64, rhs: i64) -> (i64, i64)

Conditional swap: returns (lhs, rhs) if the condition is true, otherwise returns (rhs, lhs).

Source

pub const fn to_bool_var(self) -> bool

Source§

impl Condition<u64>

Source

pub const TRUE: Self

TRUE is the bit vector of all 1’s

Source

pub const FALSE: Self

FALSE is the bit vector of all 0’s

Source

pub const fn from_bool<const VALUE: bool>() -> Self

this is the core logic for constant-time mask generation for unsigned integers Unlike signed integers where we can rely on Two’s Complement via negation -(v as i64), for u64 we must use wrapping subtraction to achieve the all-ones bit pattern (u64::MAX) for true

Source

pub fn select(self, a: u64, b: u64) -> u64

impl the select function manually for u64 although a fully generic impl<T> would be the ultimate long-term goal

Source

pub fn is_true(&self) -> bool

Trait Implementations§

Source§

impl<T> BitAnd for Condition<T>
where MaskType<T>: SupportedMaskType, T: BitAnd<T, Output = T>,

Source§

type Output = Condition<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self

Performs the & operation. Read more
Source§

impl<T> BitAndAssign for Condition<T>
where MaskType<T>: SupportedMaskType, T: BitAndAssign<T>,

Source§

fn bitand_assign(&mut self, rhs: Self)

Performs the &= operation. Read more
Source§

impl<T> BitOr for Condition<T>
where MaskType<T>: SupportedMaskType, T: BitOr<T, Output = T>,

Source§

type Output = Condition<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self

Performs the | operation. Read more
Source§

impl<T> BitOrAssign for Condition<T>
where MaskType<T>: SupportedMaskType, T: BitOrAssign<T>,

Source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
Source§

impl<T> BitXor for Condition<T>
where MaskType<T>: SupportedMaskType, T: BitXor<T, Output = T>,

Source§

type Output = Condition<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self

Performs the ^ operation. Read more
Source§

impl<T> BitXorAssign for Condition<T>
where MaskType<T>: SupportedMaskType, T: BitXorAssign<T>,

Source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the ^= operation. Read more
Source§

impl<T: Clone> Clone for Condition<T>
where MaskType<T>: SupportedMaskType,

Source§

fn clone(&self) -> Condition<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Not for Condition<T>
where MaskType<T>: SupportedMaskType, T: Not<Output = T>,

Source§

type Output = Condition<T>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self

Performs the unary ! operation. Read more
Source§

impl<T: Copy> Copy for Condition<T>
where MaskType<T>: SupportedMaskType,

Auto Trait Implementations§

§

impl<T> Freeze for Condition<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Condition<T>
where T: RefUnwindSafe,

§

impl<T> Send for Condition<T>
where T: Send,

§

impl<T> Sync for Condition<T>
where T: Sync,

§

impl<T> Unpin for Condition<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Condition<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Condition<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.