/rust/registry/src/index.crates.io-1949cf8c6b5b557f/crypto-bigint-0.5.5/src/ct_choice.rs
Line | Count | Source |
1 | | use subtle::Choice; |
2 | | |
3 | | use crate::Word; |
4 | | |
5 | | /// A boolean value returned by constant-time `const fn`s. |
6 | | // TODO: should be replaced by `subtle::Choice` or `CtOption` |
7 | | // when `subtle` starts supporting const fns. |
8 | | #[derive(Debug, Copy, Clone)] |
9 | | pub struct CtChoice(Word); |
10 | | |
11 | | impl CtChoice { |
12 | | /// The falsy value. |
13 | | pub const FALSE: Self = Self(0); |
14 | | |
15 | | /// The truthy value. |
16 | | pub const TRUE: Self = Self(Word::MAX); |
17 | | |
18 | | /// Returns the truthy value if `value == Word::MAX`, and the falsy value if `value == 0`. |
19 | | /// Panics for other values. |
20 | 49.2k | pub(crate) const fn from_mask(value: Word) -> Self { |
21 | 49.2k | debug_assert!(value == Self::FALSE.0 || value == Self::TRUE.0); |
22 | 49.2k | Self(value) |
23 | 49.2k | } |
24 | | |
25 | | /// Returns the truthy value if `value == 1`, and the falsy value if `value == 0`. |
26 | | /// Panics for other values. |
27 | 60.6k | pub(crate) const fn from_lsb(value: Word) -> Self { |
28 | 60.6k | debug_assert!(value == 0 || value == 1); |
29 | 60.6k | Self(value.wrapping_neg()) |
30 | 60.6k | } |
31 | | |
32 | | /// Returns the truthy value if `value != 0`, and the falsy value otherwise. |
33 | 0 | pub(crate) const fn from_usize_being_nonzero(value: usize) -> Self { |
34 | | const HI_BIT: u32 = usize::BITS - 1; |
35 | 0 | Self::from_lsb(((value | value.wrapping_neg()) >> HI_BIT) as Word) |
36 | 0 | } |
37 | | |
38 | | /// Returns the truthy value if `x == y`, and the falsy value otherwise. |
39 | 0 | pub(crate) const fn from_usize_equality(x: usize, y: usize) -> Self { |
40 | 0 | Self::from_usize_being_nonzero(x.wrapping_sub(y)).not() |
41 | 0 | } |
42 | | |
43 | | /// Returns the truthy value if `x < y`, and the falsy value otherwise. |
44 | 0 | pub(crate) const fn from_usize_lt(x: usize, y: usize) -> Self { |
45 | 0 | let bit = (((!x) & y) | (((!x) | y) & (x.wrapping_sub(y)))) >> (usize::BITS - 1); |
46 | 0 | Self::from_lsb(bit as Word) |
47 | 0 | } |
48 | | |
49 | 60.6k | pub(crate) const fn not(&self) -> Self { |
50 | 60.6k | Self(!self.0) |
51 | 60.6k | } |
52 | | |
53 | 0 | pub(crate) const fn or(&self, other: Self) -> Self { |
54 | 0 | Self(self.0 | other.0) |
55 | 0 | } |
56 | | |
57 | 0 | pub(crate) const fn and(&self, other: Self) -> Self { |
58 | 0 | Self(self.0 & other.0) |
59 | 0 | } |
60 | | |
61 | | /// Return `b` if `self` is truthy, otherwise return `a`. |
62 | 0 | pub(crate) const fn select(&self, a: Word, b: Word) -> Word { |
63 | 0 | a ^ (self.0 & (a ^ b)) |
64 | 0 | } |
65 | | |
66 | | /// Return `x` if `self` is truthy, otherwise return 0. |
67 | 0 | pub(crate) const fn if_true(&self, x: Word) -> Word { |
68 | 0 | x & self.0 |
69 | 0 | } |
70 | | |
71 | 0 | pub(crate) const fn is_true_vartime(&self) -> bool { |
72 | 0 | self.0 == CtChoice::TRUE.0 |
73 | 0 | } |
74 | | |
75 | 109k | pub(crate) const fn to_u8(self) -> u8 { |
76 | 109k | (self.0 as u8) & 1 |
77 | 109k | } |
78 | | } |
79 | | |
80 | | impl From<CtChoice> for Choice { |
81 | 109k | fn from(choice: CtChoice) -> Self { |
82 | 109k | Choice::from(choice.to_u8()) |
83 | 109k | } |
84 | | } |
85 | | |
86 | | impl From<CtChoice> for bool { |
87 | 0 | fn from(choice: CtChoice) -> Self { |
88 | 0 | choice.is_true_vartime() |
89 | 0 | } |
90 | | } |
91 | | |
92 | | #[cfg(test)] |
93 | | mod tests { |
94 | | use super::CtChoice; |
95 | | use crate::Word; |
96 | | |
97 | | #[test] |
98 | | fn select() { |
99 | | let a: Word = 1; |
100 | | let b: Word = 2; |
101 | | assert_eq!(CtChoice::TRUE.select(a, b), b); |
102 | | assert_eq!(CtChoice::FALSE.select(a, b), a); |
103 | | } |
104 | | } |