Skip to main content

crypto_bigint/limb/
bits.rs

1use crate::{BitOps, Choice, Word, primitives};
2
3use super::Limb;
4
5impl Limb {
6    /// Get the value of the bit at position `index`, as a truthy or falsy [`Choice`].
7    /// Returns the falsy value for indices out of range.
8    #[must_use]
9    pub const fn bit(self, index: u32) -> Choice {
10        let index_in_limb = index & (Limb::BITS - 1);
11        self.shr(index_in_limb)
12            .lsb_to_choice()
13            .and(Choice::from_u32_eq(index, index_in_limb))
14    }
15
16    /// Returns `true` if the bit at position `index` is set, `false` for an unset bit
17    /// or for indices out of range.
18    ///
19    /// # Remarks
20    /// This operation is variable time with respect to `index` only.
21    #[inline(always)]
22    #[must_use]
23    pub const fn bit_vartime(self, index: u32) -> bool {
24        if index >= Limb::BITS {
25            false
26        } else {
27            (self.0 >> index) & 1 == 1
28        }
29    }
30
31    /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`.
32    #[inline(always)]
33    fn set_bit(self, index: u32, bit_value: Choice) -> Self {
34        let mask = Limb::ONE.shl(index);
35        Limb::select(self.bitand(mask.not()), self.bitor(mask), bit_value)
36    }
37
38    /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`, in variable-time
39    /// with respect to `index`.
40    #[inline(always)]
41    fn set_bit_vartime(self, index: u32, bit_value: bool) -> Self {
42        if bit_value {
43            self.bitor(Limb::ONE.shl(index))
44        } else {
45            self.bitand(Limb::ONE.shl(index).not())
46        }
47    }
48
49    /// Returns the minimum number of bits required to represent `self`.
50    ///
51    /// Returns zero if `self` is zero.
52    #[inline(always)]
53    #[must_use]
54    pub const fn bit_width(self) -> u32 {
55        Limb::BITS - self.leading_zeros()
56    }
57
58    /// Deprecated alias for [`Self::bit_width`].
59    #[deprecated(since = "0.7.11", note = "please use `bit_width` instead")]
60    #[inline]
61    #[must_use]
62    pub const fn bits(self) -> u32 {
63        self.bit_width()
64    }
65
66    /// Calculate the number of leading zeros in the binary representation of this number.
67    #[inline(always)]
68    #[must_use]
69    pub const fn leading_zeros(self) -> u32 {
70        primitives::leading_zeros(self.0)
71    }
72
73    /// Calculate the number of trailing zeros in the binary representation of this number.
74    #[inline(always)]
75    #[must_use]
76    pub const fn trailing_zeros(self) -> u32 {
77        primitives::trailing_zeros(self.0)
78    }
79
80    /// Calculate the number of trailing ones the binary representation of this number.
81    #[inline(always)]
82    #[must_use]
83    pub const fn trailing_ones(self) -> u32 {
84        primitives::trailing_zeros(!self.0)
85    }
86
87    /// Clear bits at or above the given bit position.
88    #[must_use]
89    pub const fn restrict_bits(self, len: u32) -> Self {
90        #[allow(trivial_numeric_casts)]
91        self.bitand(Limb((1 as Word).overflowing_shl(len).0 - 1))
92    }
93}
94
95impl BitOps for Limb {
96    #[inline(always)]
97    fn bit(&self, index: u32) -> Choice {
98        Limb::bit(*self, index)
99    }
100
101    #[inline(always)]
102    fn bit_vartime(&self, index: u32) -> bool {
103        Limb::bit_vartime(*self, index)
104    }
105
106    #[inline(always)]
107    fn bits_precision(&self) -> u32 {
108        Limb::BITS
109    }
110
111    #[inline(always)]
112    fn bits(&self) -> u32 {
113        Limb::bit_width(*self)
114    }
115
116    #[inline(always)]
117    fn bytes_precision(&self) -> usize {
118        Limb::BYTES
119    }
120
121    #[inline(always)]
122    fn leading_zeros(&self) -> u32 {
123        Limb::leading_zeros(*self)
124    }
125
126    #[inline(always)]
127    fn log2_bits(&self) -> u32 {
128        Limb::LOG2_BITS
129    }
130
131    #[inline(always)]
132    fn set_bit(&mut self, index: u32, bit_value: Choice) {
133        *self = Limb::set_bit(*self, index, bit_value);
134    }
135
136    #[inline(always)]
137    fn set_bit_vartime(&mut self, index: u32, bit_value: bool) {
138        *self = Limb::set_bit_vartime(*self, index, bit_value);
139    }
140
141    #[inline(always)]
142    fn trailing_ones(&self) -> u32 {
143        Limb::trailing_ones(*self)
144    }
145
146    #[inline(always)]
147    fn trailing_zeros(&self) -> u32 {
148        Limb::trailing_zeros(*self)
149    }
150}
151
152#[cfg(test)]
153mod tests {
154    use crate::{BitOps, Choice, Limb};
155
156    fn limb_with_bits_at(positions: &[u32]) -> Limb {
157        let mut result = Limb::ZERO;
158        for pos in positions {
159            result |= Limb::ONE << *pos;
160        }
161        result
162    }
163
164    #[test]
165    fn bit() {
166        let u = limb_with_bits_at(&[2, 4, 8, 15]);
167        assert!(!BitOps::bit(&u, 0).to_bool());
168        assert!(!BitOps::bit(&u, 1).to_bool());
169        assert!(BitOps::bit(&u, 2).to_bool());
170        assert!(BitOps::bit(&u, 4).to_bool());
171        assert!(BitOps::bit(&u, 8).to_bool());
172        assert!(!BitOps::bit(&u, Limb::BITS).to_bool());
173        assert!(!BitOps::bit(&u, 300).to_bool());
174    }
175
176    #[test]
177    fn bit_vartime() {
178        let u = limb_with_bits_at(&[2, 4, 8, 15]);
179        assert!(!BitOps::bit_vartime(&u, 0));
180        assert!(!BitOps::bit_vartime(&u, 1));
181        assert!(BitOps::bit_vartime(&u, 2));
182        assert!(BitOps::bit_vartime(&u, 4));
183        assert!(BitOps::bit_vartime(&u, 8));
184        assert!(!BitOps::bit_vartime(&u, Limb::BITS));
185        assert!(!BitOps::bit_vartime(&u, 300));
186    }
187
188    #[test]
189    fn leading_zeros() {
190        let u = limb_with_bits_at(&[Limb::BITS - 16]);
191        assert_eq!(BitOps::leading_zeros(&u), 15);
192
193        let u = limb_with_bits_at(&[Limb::BITS - 12, Limb::BITS - 13]);
194        assert_eq!(BitOps::leading_zeros(&u), 11);
195
196        assert_eq!(BitOps::leading_zeros(&Limb::MAX), 0);
197        assert_eq!(BitOps::leading_zeros(&Limb::ZERO), Limb::BITS);
198    }
199
200    #[test]
201    fn leading_zeros_vartime() {
202        let u = limb_with_bits_at(&[Limb::BITS - 16]);
203        assert_eq!(BitOps::leading_zeros_vartime(&u), 15);
204
205        let u = limb_with_bits_at(&[Limb::BITS - 12, Limb::BITS - 13]);
206        assert_eq!(BitOps::leading_zeros_vartime(&u), 11);
207
208        assert_eq!(BitOps::leading_zeros_vartime(&Limb::MAX), 0);
209        assert_eq!(BitOps::leading_zeros_vartime(&Limb::ZERO), Limb::BITS);
210    }
211
212    #[test]
213    fn trailing_zeros() {
214        let u = limb_with_bits_at(&[16]);
215        assert_eq!(BitOps::trailing_zeros(&u), 16);
216
217        let u = limb_with_bits_at(&[12, 13]);
218        assert_eq!(BitOps::trailing_zeros(&u), 12);
219
220        assert_eq!(BitOps::trailing_zeros(&Limb::MAX), 0);
221        assert_eq!(BitOps::trailing_zeros(&Limb::ZERO), Limb::BITS);
222    }
223
224    #[test]
225    fn trailing_zeros_vartime() {
226        let u = limb_with_bits_at(&[16]);
227        assert_eq!(BitOps::trailing_zeros_vartime(&u), 16);
228
229        let u = limb_with_bits_at(&[12, 13]);
230        assert_eq!(BitOps::trailing_zeros_vartime(&u), 12);
231
232        assert_eq!(BitOps::trailing_zeros_vartime(&Limb::MAX), 0);
233        assert_eq!(BitOps::trailing_zeros_vartime(&Limb::ZERO), Limb::BITS);
234    }
235
236    #[test]
237    fn trailing_ones() {
238        let u = !limb_with_bits_at(&[16]);
239        assert_eq!(BitOps::trailing_ones(&u), 16);
240
241        let u = !limb_with_bits_at(&[12, 13]);
242        assert_eq!(BitOps::trailing_ones(&u), 12);
243
244        assert_eq!(BitOps::trailing_ones(&Limb::MAX), Limb::BITS);
245        assert_eq!(BitOps::trailing_ones(&Limb::ZERO), 0);
246    }
247
248    #[test]
249    fn trailing_ones_vartime() {
250        let u = !limb_with_bits_at(&[16]);
251        assert_eq!(BitOps::trailing_ones_vartime(&u), 16);
252
253        let u = !limb_with_bits_at(&[12, 13]);
254        assert_eq!(BitOps::trailing_ones_vartime(&u), 12);
255
256        assert_eq!(BitOps::trailing_ones_vartime(&Limb::MAX), Limb::BITS);
257        assert_eq!(BitOps::trailing_ones_vartime(&Limb::ZERO), 0);
258    }
259
260    #[test]
261    fn set_bit() {
262        let mut u = limb_with_bits_at(&[2, 8]);
263        BitOps::set_bit(&mut u, 5, Choice::TRUE);
264        assert_eq!(u, limb_with_bits_at(&[2, 5, 8]));
265
266        let mut u = limb_with_bits_at(&[2, 8]);
267        BitOps::set_bit(&mut u, 8, Choice::TRUE);
268        assert_eq!(u, limb_with_bits_at(&[2, 8]));
269
270        let mut u = limb_with_bits_at(&[2, 8]);
271        BitOps::set_bit(&mut u, 2, Choice::FALSE);
272        assert_eq!(u, limb_with_bits_at(&[8]));
273
274        let mut u = limb_with_bits_at(&[2, 8, 9]);
275        BitOps::set_bit(&mut u, 9, Choice::FALSE);
276        assert_eq!(u, limb_with_bits_at(&[2, 8]));
277    }
278
279    #[test]
280    fn set_bit_vartime() {
281        let mut u = limb_with_bits_at(&[2, 8]);
282        BitOps::set_bit_vartime(&mut u, 5, true);
283        assert_eq!(u, limb_with_bits_at(&[2, 5, 8]));
284
285        let mut u = limb_with_bits_at(&[2, 8]);
286        BitOps::set_bit_vartime(&mut u, 8, true);
287        assert_eq!(u, limb_with_bits_at(&[2, 8]));
288
289        let mut u = limb_with_bits_at(&[2, 8]);
290        BitOps::set_bit_vartime(&mut u, 2, false);
291        assert_eq!(u, limb_with_bits_at(&[8]));
292
293        let mut u = limb_with_bits_at(&[2, 8, 9]);
294        BitOps::set_bit_vartime(&mut u, 9, false);
295        assert_eq!(u, limb_with_bits_at(&[2, 8]));
296    }
297}