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>
impl Condition<i64>
Sourcepub const fn from_bool_var(value: bool) -> Self
pub const fn from_bool_var(value: bool) -> Self
Sourcepub const fn is_bit_set(value: i64, bit: i64) -> Self
pub const fn is_bit_set(value: i64, bit: i64) -> Self
Sourcepub const fn is_negative(value: i64) -> Self
pub const fn is_negative(value: i64) -> Self
Sourcepub const fn is_not_zero(value: i64) -> Self
pub const fn is_not_zero(value: i64) -> Self
Sourcepub fn is_within_range(value: i64, min: i64, max: i64) -> Self
pub fn is_within_range(value: i64, min: i64, max: i64) -> Self
Sourcepub fn is_in_list(value: i64, list: &[i64]) -> Self
pub fn is_in_list(value: i64, list: &[i64]) -> Self
Sourcepub fn mov(self, src: i64, dst: &mut i64)
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.
Sourcepub const fn negate(self, value: i64) -> i64
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.
Sourcepub const fn select(self, true_value: i64, false_value: i64) -> i64
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.
Sourcepub const fn swap(self, lhs: i64, rhs: i64) -> (i64, i64)
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).
Sourcepub const fn to_bool_var(self) -> bool
pub const fn to_bool_var(self) -> bool
Source§impl Condition<u64>
impl Condition<u64>
Sourcepub const fn from_bool<const VALUE: bool>() -> Self
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
Trait Implementations§
Source§impl<T> BitAnd for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitAnd<T, Output = T>,
impl<T> BitAnd for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitAnd<T, Output = T>,
Source§impl<T> BitAndAssign for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitAndAssign<T>,
impl<T> BitAndAssign for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitAndAssign<T>,
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&= operation. Read moreSource§impl<T> BitOrAssign for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitOrAssign<T>,
impl<T> BitOrAssign for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitOrAssign<T>,
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|= operation. Read moreSource§impl<T> BitXor for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitXor<T, Output = T>,
impl<T> BitXor for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitXor<T, Output = T>,
Source§impl<T> BitXorAssign for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitXorAssign<T>,
impl<T> BitXorAssign for Condition<T>where
MaskType<T>: SupportedMaskType,
T: BitXorAssign<T>,
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^= operation. Read more