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
59
//! Extraction of the low 256 bits from a [`U512`].
use super::U512;
use crate::u256::U256;
impl U512 {
/// Returns the low (least significant) 256-bit value from LE
/// limbs `[0..3]`.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
/// use cnfy_uint::u512::U512;
///
/// let v = U512::from_be_limbs([0xAA, 0xBB, 0xCC, 0xDD, 1, 2, 3, 4]);
/// assert_eq!(v.low_u256(), U256::from_be_limbs([1, 2, 3, 4]));
/// ```
#[inline]
pub const fn low_u256(&self) -> U256 {
U256([self.0[0], self.0[1], self.0[2], self.0[3]])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Low of zero is zero.
#[test]
fn zero() {
assert_eq!(U512::ZERO.low_u256(), U256::ZERO);
}
/// Low of ONE is ONE.
#[test]
fn one() {
assert_eq!(U512::ONE.low_u256(), U256::ONE);
}
/// Low ignores the upper four limbs.
#[test]
fn ignores_upper() {
let v = U512::from_be_limbs([0xFF, 0xEE, 0xDD, 0xCC, 1, 2, 3, 4]);
assert_eq!(v.low_u256(), U256::from_be_limbs([1, 2, 3, 4]));
}
/// Low of MAX has all bits set in lower 256 bits.
#[test]
fn max() {
assert_eq!(U512::MAX.low_u256(), U256::MAX);
}
/// Round-trip: from_u256 then low_u256 recovers the original.
#[test]
fn round_trip_from_u256() {
let original = U256::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0]);
assert_eq!(U512::from(original).low_u256(), original);
}
}