Skip to main content

crypto_bigint/uint/boxed/
bits.rs

1//! Bit manipulation functions.
2
3use crate::{BitOps, BoxedUint, Choice, Limb, bitlen};
4
5impl BoxedUint {
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 fn bit(&self, index: u32) -> Choice {
10        self.as_uint_ref().bit(index)
11    }
12
13    /// Returns `true` if the bit at position `index` is set, `false` otherwise.
14    ///
15    /// # Remarks
16    /// This operation is variable time with respect to `index` only.
17    #[inline(always)]
18    #[must_use]
19    pub const fn bit_vartime(&self, index: u32) -> bool {
20        self.as_uint_ref().bit_vartime(index)
21    }
22
23    /// Deprecated alias for [`Self::bit_width`].
24    #[deprecated(since = "0.7.11", note = "please use `bit_width` instead")]
25    #[inline]
26    #[must_use]
27    pub fn bits(&self) -> u32 {
28        self.bit_width()
29    }
30
31    /// Returns the minimum number of bits required to represent `self`.
32    ///
33    /// Returns zero if `self` is zero.
34    ///
35    /// Use [`BoxedUint::bits_precision`] to get the total capacity of this integer.
36    #[must_use]
37    pub fn bit_width(&self) -> u32 {
38        self.bits_precision() - self.leading_zeros()
39    }
40
41    /// Deprecated alias for [`Self::bit_width_vartime`].
42    #[deprecated(since = "0.7.11", note = "please use `bit_width_vartime` instead")]
43    #[inline]
44    #[must_use]
45    pub fn bits_vartime(&self) -> u32 {
46        self.bit_width_vartime()
47    }
48
49    /// Returns the minimum number of bits required to represent `self`, in variable time
50    /// with respect to `self`.
51    ///
52    /// Returns zero if `self` is zero.
53    #[must_use]
54    pub fn bit_width_vartime(&self) -> u32 {
55        self.as_uint_ref().bit_width_vartime()
56    }
57
58    /// Calculate the number of leading zeros in the binary representation of this number.
59    #[must_use]
60    pub const fn leading_zeros(&self) -> u32 {
61        self.as_uint_ref().leading_zeros()
62    }
63
64    /// Get the precision of this [`BoxedUint`] in bits.
65    #[inline(always)]
66    #[must_use]
67    pub fn bits_precision(&self) -> u32 {
68        bitlen::from_limbs(self.limbs.len())
69    }
70
71    /// Calculate the number of trailing zeros in the binary representation of this number.
72    #[must_use]
73    pub fn trailing_zeros(&self) -> u32 {
74        self.as_uint_ref().trailing_zeros()
75    }
76
77    /// Calculate the number of trailing ones in the binary representation of this number.
78    #[must_use]
79    pub fn trailing_ones(&self) -> u32 {
80        self.as_uint_ref().trailing_ones()
81    }
82
83    /// Calculate the number of trailing zeros in the binary representation of this number in
84    /// variable-time with respect to `self`.
85    #[must_use]
86    pub fn trailing_zeros_vartime(&self) -> u32 {
87        self.as_uint_ref().trailing_zeros_vartime()
88    }
89
90    /// Calculate the number of trailing ones in the binary representation of this number,
91    /// variable time in `self`.
92    #[must_use]
93    pub fn trailing_ones_vartime(&self) -> u32 {
94        self.as_uint_ref().trailing_ones_vartime()
95    }
96
97    /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`.
98    pub(crate) fn set_bit(&mut self, index: u32, bit_value: Choice) {
99        self.as_mut_uint_ref().set_bit(index, bit_value);
100    }
101
102    pub(crate) fn set_bit_vartime(&mut self, index: u32, bit_value: bool) {
103        self.as_mut_uint_ref().set_bit_vartime(index, bit_value);
104    }
105
106    /// Clear any bits at or above a given bit position.
107    pub(crate) const fn restrict_bits(&mut self, len: u32) {
108        self.as_mut_uint_ref().restrict_bits(len);
109    }
110}
111
112impl BitOps for BoxedUint {
113    fn bits_precision(&self) -> u32 {
114        self.bits_precision()
115    }
116
117    fn bytes_precision(&self) -> usize {
118        self.nlimbs() * Limb::BYTES
119    }
120
121    fn leading_zeros(&self) -> u32 {
122        self.leading_zeros()
123    }
124
125    fn bits(&self) -> u32 {
126        self.bit_width()
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.set_bit(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.set_bit_vartime(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 super::BoxedUint;
169    use crate::Choice;
170    use hex_literal::hex;
171
172    fn uint_with_bits_at(positions: &[u32]) -> BoxedUint {
173        let mut result = BoxedUint::zero_with_precision(256);
174        for &pos in positions {
175            result |= BoxedUint::one_with_precision(256).shl_vartime(pos).unwrap();
176        }
177        result
178    }
179
180    #[test]
181    fn bit_vartime() {
182        let u = uint_with_bits_at(&[16, 48, 112, 127, 255]);
183        assert!(!u.bit_vartime(0));
184        assert!(!u.bit_vartime(1));
185        assert!(u.bit_vartime(16));
186        assert!(u.bit_vartime(127));
187        assert!(u.bit_vartime(255));
188        assert!(!u.bit_vartime(256));
189        assert!(!u.bit_vartime(260));
190    }
191
192    #[test]
193    fn bit_width() {
194        assert_eq!(0, BoxedUint::zero().bit_width());
195        assert_eq!(128, BoxedUint::max(128).bit_width());
196
197        let n1 = BoxedUint::from_be_slice(&hex!("000000000029ffffffffffffffffffff"), 128).unwrap();
198        assert_eq!(86, n1.bit_width());
199
200        let n2 = BoxedUint::from_be_slice(&hex!("00000000004000000000000000000000"), 128).unwrap();
201        assert_eq!(87, n2.bit_width());
202    }
203
204    #[test]
205    fn set_bit() {
206        let mut u = uint_with_bits_at(&[16, 79, 150]);
207        u.set_bit(127, Choice::TRUE);
208        assert_eq!(u, uint_with_bits_at(&[16, 79, 127, 150]));
209
210        let mut u = uint_with_bits_at(&[16, 79, 150]);
211        u.set_bit(150, Choice::TRUE);
212        assert_eq!(u, uint_with_bits_at(&[16, 79, 150]));
213
214        let mut u = uint_with_bits_at(&[16, 79, 150]);
215        u.set_bit(127, Choice::FALSE);
216        assert_eq!(u, uint_with_bits_at(&[16, 79, 150]));
217
218        let mut u = uint_with_bits_at(&[16, 79, 150]);
219        u.set_bit(150, Choice::FALSE);
220        assert_eq!(u, uint_with_bits_at(&[16, 79]));
221    }
222
223    #[test]
224    fn trailing_ones() {
225        let u = !uint_with_bits_at(&[16, 79, 150]);
226        assert_eq!(u.trailing_ones(), 16);
227
228        let u = !uint_with_bits_at(&[79, 150]);
229        assert_eq!(u.trailing_ones(), 79);
230
231        let u = !uint_with_bits_at(&[150, 207]);
232        assert_eq!(u.trailing_ones(), 150);
233
234        let u = !uint_with_bits_at(&[0, 150, 207]);
235        assert_eq!(u.trailing_ones(), 0);
236
237        let u = !BoxedUint::zero_with_precision(256);
238        assert_eq!(u.trailing_ones(), 256);
239    }
240
241    #[test]
242    fn trailing_ones_vartime() {
243        let u = !uint_with_bits_at(&[16, 79, 150]);
244        assert_eq!(u.trailing_ones_vartime(), 16);
245
246        let u = !uint_with_bits_at(&[79, 150]);
247        assert_eq!(u.trailing_ones_vartime(), 79);
248
249        let u = !uint_with_bits_at(&[150, 207]);
250        assert_eq!(u.trailing_ones_vartime(), 150);
251
252        let u = !uint_with_bits_at(&[0, 150, 207]);
253        assert_eq!(u.trailing_ones_vartime(), 0);
254
255        let u = !BoxedUint::zero_with_precision(256);
256        assert_eq!(u.trailing_ones_vartime(), 256);
257    }
258}