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
//! Conversion from [`U256`] into a `[u64; 4]` big-endian limb array.
use super::U256;
/// Converts a [`U256`] into its four `u64` limbs in big-endian order:
/// `[w0, w1, w2, w3]` where `w0` is the most significant.
///
/// This is the inverse of `From<[u64; 4]> for U256` and equivalent to
/// calling [`U256::to_be_limbs`].
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
///
/// let v = U256::from_be_limbs([1, 2, 3, 4]);
/// let arr: [u64; 4] = v.into();
/// assert_eq!(arr, [1, 2, 3, 4]);
/// ```
impl From<U256> for [u64; 4] {
#[inline]
fn from(value: U256) -> Self {
[value.0[3], value.0[2], value.0[1], value.0[0]]
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Round-trip from limbs through U256 back to limbs.
#[test]
fn round_trip() {
let arr = [0xA, 0xB, 0xC, 0xD];
let v = U256::from_be_limbs(arr);
let back: [u64; 4] = v.into();
assert_eq!(back, arr);
}
/// Zero converts to all-zero limbs.
#[test]
fn zero() {
let arr: [u64; 4] = U256::ZERO.into();
assert_eq!(arr, [0, 0, 0, 0]);
}
/// MAX converts to all-max limbs.
#[test]
fn max() {
let arr: [u64; 4] = U256::MAX.into();
assert_eq!(arr, [u64::MAX; 4]);
}
}