1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Construction of a [`U256`] from four little-endian `u64` limbs.
use super::U256;
impl U256 {
/// Creates a new [`U256`] from a four-element array of `u64` limbs in
/// little-endian order: `[w0, w1, w2, w3]` where `w0` is the least
/// significant and `w3` is the most significant.
///
/// Since the internal layout is already little-endian, the array is
/// stored directly without reversal. This is the inverse of
/// [`U256::to_le_limbs`].
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let value = U256::from_le_limbs([1, 0, 0, 0]);
/// assert_eq!(value, U256::from_be_limbs([0, 0, 0, 1]));
/// ```
#[inline]
pub const fn from_le_limbs(value: [u64; 4]) -> Self {
Self(value)
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Constructing from four zeros yields a zero value.
#[test]
fn zero() {
assert_eq!(U256::from_le_limbs([0; 4]), U256::ZERO);
}
/// LE limb order is the reverse of BE limb order.
#[test]
fn reverse_of_be() {
let le = U256::from_le_limbs([4, 3, 2, 1]);
let be = U256::from_be_limbs([1, 2, 3, 4]);
assert_eq!(le, be);
}
/// Maximum value fills all four limbs.
#[test]
fn max_value() {
assert_eq!(U256::from_le_limbs([u64::MAX; 4]), U256::MAX);
}
/// Round-trip with to_le_limbs.
#[test]
fn round_trip() {
let limbs = [0xDEAD, 0xBEEF, 0xCAFE, 0xBABE];
assert_eq!(U256::from_le_limbs(limbs).to_le_limbs(), limbs);
}
}