//! Serialization of a [`U512`] into a 64-byte little-endian array.
use super::U512;

impl U512 {
    /// Returns the 64-byte little-endian representation of this value.
    ///
    /// Byte `[0]` is the least significant and byte `[63]` is the most
    /// significant. Each limb is serialized via `u64::to_le_bytes`,
    /// reading from LSB (internal index 0) up to MSB (internal index 7).
    ///
    /// This is the inverse of [`U512::from_le_bytes`].
    ///
    /// # Examples
    ///
    /// ```
    /// use cnfy_uint::u512::U512;
    ///
    /// let v = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 1]);
    /// let bytes = v.to_le_bytes();
    /// assert_eq!(bytes[0], 1);
    /// assert_eq!(bytes[63], 0);
    /// ```
    #[inline]
    pub const fn to_le_bytes(&self) -> [u8; 64] {
        let w0 = self.0[0].to_le_bytes();
        let w1 = self.0[1].to_le_bytes();
        let w2 = self.0[2].to_le_bytes();
        let w3 = self.0[3].to_le_bytes();
        let w4 = self.0[4].to_le_bytes();
        let w5 = self.0[5].to_le_bytes();
        let w6 = self.0[6].to_le_bytes();
        let w7 = self.0[7].to_le_bytes();
        [
            w0[0], w0[1], w0[2], w0[3], w0[4], w0[5], w0[6], w0[7],
            w1[0], w1[1], w1[2], w1[3], w1[4], w1[5], w1[6], w1[7],
            w2[0], w2[1], w2[2], w2[3], w2[4], w2[5], w2[6], w2[7],
            w3[0], w3[1], w3[2], w3[3], w3[4], w3[5], w3[6], w3[7],
            w4[0], w4[1], w4[2], w4[3], w4[4], w4[5], w4[6], w4[7],
            w5[0], w5[1], w5[2], w5[3], w5[4], w5[5], w5[6], w5[7],
            w6[0], w6[1], w6[2], w6[3], w6[4], w6[5], w6[6], w6[7],
            w7[0], w7[1], w7[2], w7[3], w7[4], w7[5], w7[6], w7[7],
        ]
    }
}

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

    /// Round-trip: from_le_bytes then to_le_bytes returns the original.
    #[test]
    fn round_trip() {
        let mut bytes = [0u8; 64];
        bytes[0] = 0xEF;
        bytes[31] = 0xCD;
        bytes[63] = 0xAB;
        let v = U512::from_le_bytes(bytes);
        assert_eq!(v.to_le_bytes(), bytes);
    }

    /// Zero produces all-zero bytes.
    #[test]
    fn zero() {
        assert_eq!(U512::ZERO.to_le_bytes(), [0u8; 64]);
    }

    /// MAX produces all-0xFF bytes.
    #[test]
    fn max() {
        assert_eq!(U512::MAX.to_le_bytes(), [0xFF; 64]);
    }

    /// Byte 0 holds the LSB of the lowest limb.
    #[test]
    fn lsb_position() {
        let v = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0x42]);
        let bytes = v.to_le_bytes();
        assert_eq!(bytes[0], 0x42);
        assert_eq!(bytes[1], 0);
    }

    /// to_le_bytes is the byte-reversal of to_be_bytes.
    #[test]
    fn reverse_of_be() {
        let v = U512::from_be_limbs([0xDEAD, 0xBEEF, 0x1234, 0x5678, 0xCAFE, 0xBABE, 0x42, 0xFF]);
        let be = v.to_be_bytes();
        let le = v.to_le_bytes();
        let mut reversed = be;
        reversed.reverse();
        assert_eq!(le, reversed);
    }
}
