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
58
//! Conversion from [`U256`] into [`U384`].
use super::U384;
use crate::u256::U256;
/// Creates a [`U384`] from a [`U256`] value by embedding it in the lower
/// four limbs (`[2..5]`), with the upper two limbs set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
/// use cnfy_uint::u384::U384;
///
/// let v = U256::from_be_limbs([1, 2, 3, 4]);
/// let wide = U384::from(v);
/// assert_eq!(wide, U384::from_be_limbs([0, 0, 1, 2, 3, 4]));
/// ```
impl From<U256> for U384 {
#[inline]
fn from(v: U256) -> Self {
U384([v.0[0], v.0[1], v.0[2], v.0[3], 0, 0])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero U256 produces zero U384.
#[test]
fn zero() {
assert_eq!(U384::from(U256::from_be_limbs([0, 0, 0, 0])), U384::ZERO);
}
/// One U256 produces one U384.
#[test]
fn one() {
assert_eq!(U384::from(U256::from_be_limbs([0, 0, 0, 1])), U384::ONE);
}
/// All limbs are placed in the correct positions.
#[test]
fn all_limbs() {
let v = U256::from_be_limbs([0xA, 0xB, 0xC, 0xD]);
let wide = U384::from(v);
assert_eq!(wide.to_be_limbs(), [0, 0, 0xA, 0xB, 0xC, 0xD]);
}
/// U256::MAX maps to lower 256 bits of U384.
#[test]
fn max_u256() {
let wide = U384::from(U256::from_be_limbs([u64::MAX; 4]));
assert_eq!(
wide,
U384::from_be_limbs([0, 0, u64::MAX, u64::MAX, u64::MAX, u64::MAX]),
);
}
}