Skip to content

Commit d25319b

Browse files
Minor changes
1 parent 8c282b7 commit d25319b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/u256.rs

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ impl U256 {
9191
Self(value)
9292
}
9393

94+
/// Create raw value of unsigned integer
95+
pub(super) fn get_raw(self) -> [u64; 4] {
96+
self.0
97+
}
98+
9499
#[inline(always)]
95100
pub const fn raw_eq(self, other: [u64; 4]) -> bool {
96101
self.0[0] == other[0]

src/u512.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,14 @@ impl core::str::FromStr for U512 {
11421142
}
11431143
}
11441144

1145+
impl From<super::u256::U256> for U512 {
1146+
#[inline]
1147+
fn from(value: super::u256::U256) -> Self {
1148+
let val = value.get_raw();
1149+
Self([0, 0, 0, 0, val[0], val[1], val[2], val[3]])
1150+
}
1151+
}
1152+
11451153
impl From<u128> for U512 {
11461154
#[inline]
11471155
fn from(value: u128) -> Self {
@@ -1184,6 +1192,13 @@ impl From<U512> for u128 {
11841192
}
11851193
}
11861194

1195+
impl From<U512> for super::u256::U256 {
1196+
#[inline]
1197+
fn from(value: U512) -> Self {
1198+
Self::raw([value.0[4], value.0[5], value.0[6], value.0[7]])
1199+
}
1200+
}
1201+
11871202
impl Into<u64> for U512 {
11881203
#[inline]
11891204
fn into(self) -> u64 {
@@ -1591,8 +1606,8 @@ mod test {
15911606
// Equal to 115792089237316195423570985008687907852837564279074904382605163141518161494337
15921607
let value = U512::from_string("115792089237316195423570985008687907852837564279074904382605163141518161494337")?;
15931608
assert_eq!(
1594-
value.0,
1595-
[
1609+
value,
1610+
U512([
15961611
0,
15971612
0,
15981613
0,
@@ -1601,13 +1616,13 @@ mod test {
16011616
18446744073709551614,
16021617
13451932020343611451,
16031618
13822214165235122497
1604-
]
1619+
])
16051620
);
16061621

16071622
let value = U512::from_string("16983810465656793445178183341822322175883642221536626637512293983324")?;
16081623
assert_eq!(
1609-
value.0,
1610-
[
1624+
value,
1625+
U512([
16111626
0,
16121627
0,
16131628
0,
@@ -1616,14 +1631,14 @@ mod test {
16161631
0x4df099df30fc28a1,
16171632
0x69a467e9e47075a9,
16181633
0x0f7e650eb6b7a45c
1619-
]
1634+
])
16201635
);
16211636

16221637
// Max value of 256-bit number
16231638
let value = U512::from_string("115792089237316195423570985008687907853269984665640564039457584007913129639935")?;
16241639
assert_eq!(
1625-
value.0,
1626-
[
1640+
value,
1641+
U512([
16271642
0,
16281643
0,
16291644
0,
@@ -1632,7 +1647,7 @@ mod test {
16321647
0xFFFF_FFFF_FFFF_FFFF,
16331648
0xFFFF_FFFF_FFFF_FFFF,
16341649
0xFFFF_FFFF_FFFF_FFFF,
1635-
]
1650+
])
16361651
);
16371652

16381653
// Max value of 512-bit number
@@ -1760,9 +1775,7 @@ mod test {
17601775

17611776
assert_eq!(
17621777
c,
1763-
U512::from_string(
1764-
"6270385922947262222347954536162455298520515727022267860678267425509717888"
1765-
)?
1778+
"6270385922947262222347954536162455298520515727022267860678267425509717888".parse::<U512>()?
17661779
);
17671780

17681781
Ok(())

0 commit comments

Comments
 (0)