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
//! Construction of a [`U256`] from four big-endian `u64` limbs.
use super::U256;
impl U256 {
/// Creates a new [`U256`] from a four-element array of `u64` limbs in
/// big-endian order: `[w0, w1, w2, w3]` where `w0` is the most
/// significant and `w3` is the least significant.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let value = U256::from_be_limbs([0, 0, 0, 1]);
/// ```
#[inline]
pub const fn from_be_limbs(value: [u64; 4]) -> Self {
Self([value[3], value[2], value[1], value[0]])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Constructing from four zeros yields a zero value.
#[test]
fn zero() {
let z = U256::from_be_limbs([0, 0, 0, 0]);
assert_eq!(z.high_u128(), 0);
assert_eq!(z.low_u128(), 0);
}
/// High and low u128 values are reconstructed correctly from limbs.
#[test]
fn high_and_low() {
let v = U256::from_be_limbs([0, 1, 0, 2]);
assert_eq!(v.high_u128(), 1);
assert_eq!(v.low_u128(), 2);
}
/// Maximum value fills all four limbs.
#[test]
fn max_value() {
let v = U256::from_be_limbs([u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
assert_eq!(v.high_u128(), u128::MAX);
assert_eq!(v.low_u128(), u128::MAX);
}
}