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
60
//! Extraction of the high 256 bits from a [`U512`].
use super::U512;
use crate::u256::U256;
impl U512 {
/// Returns the high (most significant) 256-bit value from LE
/// limbs `[4..7]`.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u256::U256;
/// use cnfy_uint::u512::U512;
///
/// let v = U512::from_be_limbs([1, 2, 3, 4, 0xAA, 0xBB, 0xCC, 0xDD]);
/// assert_eq!(v.high_u256(), U256::from_be_limbs([1, 2, 3, 4]));
/// ```
#[inline]
pub const fn high_u256(&self) -> U256 {
U256([self.0[4], self.0[5], self.0[6], self.0[7]])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// High of zero is zero.
#[test]
fn zero() {
assert_eq!(U512::ZERO.high_u256(), U256::ZERO);
}
/// High of ONE is zero (1 is in the low half).
#[test]
fn one() {
assert_eq!(U512::ONE.high_u256(), U256::ZERO);
}
/// High ignores the lower four limbs.
#[test]
fn ignores_lower() {
let v = U512::from_be_limbs([1, 2, 3, 4, 0xFF, 0xEE, 0xDD, 0xCC]);
assert_eq!(v.high_u256(), U256::from_be_limbs([1, 2, 3, 4]));
}
/// High of MAX has all bits set.
#[test]
fn max() {
assert_eq!(U512::MAX.high_u256(), U256::MAX);
}
/// High and low together reconstruct the full value.
#[test]
fn high_low_complement() {
let v = U512::from_be_limbs([0xA, 0xB, 0xC, 0xD, 0x1, 0x2, 0x3, 0x4]);
assert_eq!(v.high_u256(), U256::from_be_limbs([0xA, 0xB, 0xC, 0xD]));
assert_eq!(v.low_u256(), U256::from_be_limbs([0x1, 0x2, 0x3, 0x4]));
}
}