Skip to main content

crypto_bigint/uint/
bits.rs

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