Coverage Report

Created: 2025-05-07 06:59

/rust/registry/src/index.crates.io-6f17d22bba15001f/ring-0.17.14/src/bits.rs
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Brian Smith.
2
//
3
// Permission to use, copy, modify, and/or distribute this software for any
4
// purpose with or without fee is hereby granted, provided that the above
5
// copyright notice and this permission notice appear in all copies.
6
//
7
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15
//! Bit lengths.
16
17
use crate::{error::InputTooLongError, polyfill};
18
19
/// The length of something, in bits.
20
///
21
/// This can represent a bit length that isn't a whole number of bytes.
22
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
23
#[repr(transparent)]
24
pub struct BitLength<T = usize>(T);
25
26
pub(crate) trait FromByteLen<T>: Sized {
27
    /// Constructs a `BitLength` from the given length in bytes.
28
    ///
29
    /// Fails if `bytes * 8` is too large for a `T`.
30
    fn from_byte_len(bytes: T) -> Result<Self, InputTooLongError<T>>;
31
}
32
33
impl FromByteLen<usize> for BitLength<usize> {
34
    #[inline]
35
0
    fn from_byte_len(bytes: usize) -> Result<Self, InputTooLongError> {
36
0
        match bytes.checked_mul(8) {
37
0
            Some(bits) => Ok(Self(bits)),
38
0
            None => Err(InputTooLongError::new(bytes)),
39
        }
40
0
    }
41
}
42
43
impl FromByteLen<u64> for BitLength<u64> {
44
    #[inline]
45
0
    fn from_byte_len(bytes: u64) -> Result<Self, InputTooLongError<u64>> {
46
0
        match bytes.checked_mul(8) {
47
0
            Some(bits) => Ok(Self(bits)),
48
0
            None => Err(InputTooLongError::new(bytes)),
49
        }
50
0
    }
51
}
52
53
impl FromByteLen<usize> for BitLength<u64> {
54
    #[inline]
55
0
    fn from_byte_len(bytes: usize) -> Result<Self, InputTooLongError<usize>> {
56
0
        match polyfill::u64_from_usize(bytes).checked_mul(8) {
57
0
            Some(bits) => Ok(Self(bits)),
58
0
            None => Err(InputTooLongError::new(bytes)),
59
        }
60
0
    }
61
}
62
63
impl<T> BitLength<T> {
64
    /// Constructs a `BitLength` from the given length in bits.
65
    #[inline]
66
0
    pub const fn from_bits(bits: T) -> Self {
67
0
        Self(bits)
68
0
    }
Unexecuted instantiation: <ring::bits::BitLength<i32>>::from_bits
Unexecuted instantiation: <ring::bits::BitLength>::from_bits
69
}
70
71
impl<T: Copy> BitLength<T> {
72
    /// The number of bits this bit length represents, as the underlying type.
73
    #[inline]
74
0
    pub fn as_bits(self) -> T {
75
0
        self.0
76
0
    }
77
}
78
79
// Lengths measured in bits, where all arithmetic is guaranteed not to
80
// overflow.
81
impl BitLength<usize> {
82
    #[cfg(feature = "alloc")]
83
    #[inline]
84
0
    pub(crate) fn half_rounded_up(&self) -> Self {
85
0
        let round_up = self.0 & 1;
86
0
        Self((self.0 / 2) + round_up)
87
0
    }
88
89
    /// The bit length, rounded up to a whole number of bytes.
90
    #[inline]
91
0
    pub const fn as_usize_bytes_rounded_up(&self) -> usize {
92
0
        // Equivalent to (self.0 + 7) / 8, except with no potential for
93
0
        // overflow and without branches.
94
0
95
0
        // Branchless round_up = if self.0 & 0b111 != 0 { 1 } else { 0 };
96
0
        let round_up = ((self.0 >> 2) | (self.0 >> 1) | self.0) & 1;
97
0
98
0
        (self.0 / 8) + round_up
99
0
    }
100
101
    #[cfg(feature = "alloc")]
102
    #[inline]
103
0
    pub(crate) fn try_sub_1(self) -> Result<Self, crate::error::Unspecified> {
104
0
        let sum = self.0.checked_sub(1).ok_or(crate::error::Unspecified)?;
105
0
        Ok(Self(sum))
106
0
    }
107
}
108
109
impl BitLength<u64> {
110
0
    pub fn to_be_bytes(self) -> [u8; 8] {
111
0
        self.0.to_be_bytes()
112
0
    }
113
}
114
115
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
116
impl From<BitLength<usize>> for BitLength<u64> {
117
0
    fn from(BitLength(value): BitLength<usize>) -> Self {
118
0
        BitLength(polyfill::u64_from_usize(value))
119
0
    }
120
}
121
122
impl TryFrom<BitLength<u64>> for BitLength<core::num::NonZeroU64> {
123
    type Error = <core::num::NonZeroU64 as TryFrom<u64>>::Error;
124
125
0
    fn try_from(BitLength(value): BitLength<u64>) -> Result<Self, Self::Error> {
126
0
        value.try_into().map(BitLength)
127
0
    }
128
}
129
130
const _TEST_AS_USIZE_BYTES_ROUNDED_UP_EVEN: () =
131
    assert!(BitLength::from_bits(8192).as_usize_bytes_rounded_up() == 8192 / 8);
132
const _TEST_AS_USIZE_BYTES_ROUNDED_UP_ONE_BIT_HIGH: () =
133
    assert!(BitLength::from_bits(8192 + 1).as_usize_bytes_rounded_up() == (8192 / 8) + 1);
134
const _TEST_AS_USIZE_BYTES_ROUNDED_UP_SEVEN_BITS_HIGH: () =
135
    assert!(BitLength::from_bits(8192 + 7).as_usize_bytes_rounded_up() == (8192 / 8) + 1);