cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! Construction of a [`U384`] from a 48-byte big-endian array.
use super::U384;

impl U384 {
    /// Creates a new [`U384`] from a 48-byte array in big-endian order.
    ///
    /// Bytes `[0..8]` become limb `w0` (most significant), bytes `[8..16]`
    /// become `w1`, and so on through bytes `[40..48]` which become `w5`
    /// (least significant).
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u384::U384;
    ///
    /// let mut bytes = [0u8; 48];
    /// bytes[47] = 1;
    /// assert_eq!(U384::from_be_bytes(bytes), U384::from_be_limbs([0, 0, 0, 0, 0, 1]));
    /// ```
    #[inline]
    pub const fn from_be_bytes(bytes: [u8; 48]) -> Self {
        let w0 = u64::from_be_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
        ]);
        let w1 = u64::from_be_bytes([
            bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
        ]);
        let w2 = u64::from_be_bytes([
            bytes[16], bytes[17], bytes[18], bytes[19], bytes[20], bytes[21], bytes[22], bytes[23],
        ]);
        let w3 = u64::from_be_bytes([
            bytes[24], bytes[25], bytes[26], bytes[27], bytes[28], bytes[29], bytes[30], bytes[31],
        ]);
        let w4 = u64::from_be_bytes([
            bytes[32], bytes[33], bytes[34], bytes[35], bytes[36], bytes[37], bytes[38], bytes[39],
        ]);
        let w5 = u64::from_be_bytes([
            bytes[40], bytes[41], bytes[42], bytes[43], bytes[44], bytes[45], bytes[46], bytes[47],
        ]);
        Self([w5, w4, w3, w2, w1, w0])
    }
}

#[cfg(test)]
mod ai_tests {
    use super::*;

    /// All zeros produces the zero value.
    #[test]
    fn zero() {
        assert_eq!(
            U384::from_be_bytes([0; 48]),
            U384::from_be_limbs([0, 0, 0, 0, 0, 0]),
        );
    }

    /// All 0xFF produces the maximum value.
    #[test]
    fn max() {
        assert_eq!(
            U384::from_be_bytes([0xFF; 48]),
            U384::MAX,
        );
    }

    /// Byte 47 (least significant) maps to the low bit of limb w5.
    #[test]
    fn single_lsb() {
        let mut bytes = [0u8; 48];
        bytes[47] = 1;
        assert_eq!(
            U384::from_be_bytes(bytes),
            U384::from_be_limbs([0, 0, 0, 0, 0, 1]),
        );
    }

    /// Byte 0 (most significant) maps to the high bit of limb w0.
    #[test]
    fn single_msb() {
        let mut bytes = [0u8; 48];
        bytes[0] = 0x80;
        assert_eq!(
            U384::from_be_bytes(bytes),
            U384::from_be_limbs([0x8000000000000000, 0, 0, 0, 0, 0]),
        );
    }

    /// Boundary between w2 and w3 at bytes 23/24.
    #[test]
    fn limb_boundary() {
        let mut bytes = [0u8; 48];
        bytes[23] = 1;
        bytes[24] = 1;
        assert_eq!(
            U384::from_be_bytes(bytes),
            U384::from_be_limbs([0, 0, 1, 0x0100000000000000, 0, 0]),
        );
    }
}