//! Construction of a [`U320`] from a 40-byte big-endian array.
use super::U320;

impl U320 {
    /// Creates a new [`U320`] from a 40-byte array in big-endian order.
    ///
    /// Bytes `[0..8]` become the most significant limb, bytes `[8..16]`
    /// become the next, and so on down to bytes `[32..40]` which become
    /// the least significant limb. The result is stored in little-endian
    /// internal layout.
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u320::U320;
    ///
    /// let mut bytes = [0u8; 40];
    /// bytes[39] = 1;
    /// assert_eq!(U320::from_be_bytes(bytes), U320::from_be_limbs([0, 0, 0, 0, 1]));
    /// ```
    #[inline]
    pub const fn from_be_bytes(bytes: [u8; 40]) -> 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],
        ]);
        Self([w4, w3, w2, w1, w0])
    }
}

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

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

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

    /// Byte 39 (least significant) maps to the low bit of limb w4.
    #[test]
    fn single_lsb() {
        let mut bytes = [0u8; 40];
        bytes[39] = 1;
        assert_eq!(
            U320::from_be_bytes(bytes),
            U320::from_be_limbs([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; 40];
        bytes[0] = 0x80;
        assert_eq!(
            U320::from_be_bytes(bytes),
            U320::from_be_limbs([0x8000000000000000, 0, 0, 0, 0]),
        );
    }

    /// Boundary between w3 and w4 at bytes 31/32.
    #[test]
    fn limb_boundary_w3_w4() {
        let mut bytes = [0u8; 40];
        bytes[31] = 1;
        bytes[32] = 1;
        assert_eq!(
            U320::from_be_bytes(bytes),
            U320::from_be_limbs([0, 0, 0, 1, 0x0100000000000000]),
        );
    }

    /// Boundary between w0 and w1 at bytes 7/8.
    #[test]
    fn limb_boundary_w0_w1() {
        let mut bytes = [0u8; 40];
        bytes[7] = 0xFF;
        bytes[8] = 0x01;
        assert_eq!(
            U320::from_be_bytes(bytes),
            U320::from_be_limbs([0xFF, 0x0100000000000000, 0, 0, 0]),
        );
    }
}
