Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions crates/primitives/src/signed/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{BigIntConversionError, ParseSignedError, Sign, Signed, utils::twos_complement};
use alloc::string::String;
use core::str::FromStr;
use ruint::{ToUintError, Uint, UintTryFrom};
use ruint::{FromUintError, ToUintError, Uint, UintTryFrom, UintTryTo};

impl<const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>> for Signed<BITS, LIMBS> {
type Error = BigIntConversionError;
Expand All @@ -28,7 +28,7 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for Uin
}
}

/// Conversion between `Signed` of different `BITS` or `LIMBS` length.
/// Conversion from `Signed` of different `BITS` or `LIMBS` length.
impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_SRC: usize>
UintTryFrom<Signed<BITS_SRC, LIMBS_SRC>> for Signed<BITS, LIMBS>
{
Expand All @@ -48,6 +48,37 @@ impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_S
}
}

/// Conversion to `Signed` of different `BITS` or `LIMBS` length.
impl<const BITS: usize, const LIMBS: usize, const BITS_TARGET: usize, const LIMBS_TARGET: usize>
UintTryTo<Signed<BITS_TARGET, LIMBS_TARGET>> for Signed<BITS, LIMBS>
{
#[inline]
fn uint_try_to(
&self,
) -> Result<Signed<BITS_TARGET, LIMBS_TARGET>, FromUintError<Signed<BITS_TARGET, LIMBS_TARGET>>>
{
let (sign, abs) = self.into_sign_and_abs();
let resized = Signed::<BITS_TARGET, LIMBS_TARGET>::from_raw(
Uint::uint_try_to(&abs).map_err(|e| match e {
FromUintError::Overflow(b, t, v) => {
FromUintError::Overflow(b, Signed(t), Signed(v))
}
})?,
);
if resized.is_negative() {
return Err(FromUintError::Overflow(BITS_TARGET, resized, Signed::MAX));
}
Ok(match sign {
Sign::Negative => resized.checked_neg().ok_or(FromUintError::Overflow(
BITS_TARGET,
resized,
Signed::MAX,
))?,
Sign::Positive => resized,
})
}
}

/// Conversion from positive `Signed` to `Uint` of different `BITS` or `LIMBS` length.
impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_SRC: usize>
UintTryFrom<Signed<BITS_SRC, LIMBS_SRC>> for Uint<BITS, LIMBS>
Expand All @@ -61,6 +92,22 @@ impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_S
}
}

/// Conversion to `Uint` from positive `Signed` of different `BITS` or `LIMBS` length.
impl<const BITS: usize, const LIMBS: usize, const BITS_TARGET: usize, const LIMBS_TARGET: usize>
UintTryTo<Uint<BITS_TARGET, LIMBS_TARGET>> for Signed<BITS, LIMBS>
{
#[inline]
fn uint_try_to(
&self,
) -> Result<Uint<BITS_TARGET, LIMBS_TARGET>, FromUintError<Uint<BITS_TARGET, LIMBS_TARGET>>>
{
if self.is_negative() {
return Err(FromUintError::Overflow(BITS_TARGET, Uint::ZERO, Uint::MAX));
}
Uint::uint_try_to(&self.into_raw())
}
}

/// Conversion from `Uint` to positive `Signed` of different `BITS` or `LIMBS` length.
impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_SRC: usize>
UintTryFrom<Uint<BITS_SRC, LIMBS_SRC>> for Signed<BITS, LIMBS>
Expand All @@ -76,6 +123,29 @@ impl<const BITS: usize, const LIMBS: usize, const BITS_SRC: usize, const LIMBS_S
}
}

/// Conversion to positive `Signed` from `Uint` of different `BITS` or `LIMBS` length.
impl<const BITS: usize, const LIMBS: usize, const BITS_TARGET: usize, const LIMBS_TARGET: usize>
UintTryTo<Signed<BITS_TARGET, LIMBS_TARGET>> for Uint<BITS, LIMBS>
{
#[inline]
fn uint_try_to(
&self,
) -> Result<Signed<BITS_TARGET, LIMBS_TARGET>, FromUintError<Signed<BITS_TARGET, LIMBS_TARGET>>>
{
let resized = Signed::<BITS_TARGET, LIMBS_TARGET>::from_raw(
Self::uint_try_to(self).map_err(|e| match e {
FromUintError::Overflow(b, t, v) => {
FromUintError::Overflow(b, Signed(t), Signed(v))
}
})?,
);
if resized.is_negative() {
return Err(FromUintError::Overflow(BITS_TARGET, resized, Signed::MAX));
}
Ok(resized)
}
}

fn signed_err<const BITS: usize, const LIMBS: usize>(
err: ToUintError<Uint<BITS, LIMBS>>,
) -> ToUintError<Signed<BITS, LIMBS>> {
Expand Down
11 changes: 11 additions & 0 deletions crates/primitives/src/signed/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1692,28 +1692,39 @@ mod tests {
let m_i256 = I256::unchecked_from(-4);
let m_i24 = I24::from(m_i256);
assert_eq!(m_i24, I24::from_dec_str("-4").unwrap());
assert_eq!(m_i24.to::<I256>(), m_i256);
let m_i56 = I56::from(m_i24);
assert_eq!(m_i56, I56::from_dec_str("-4").unwrap());
assert_eq!(m_i56.to::<I24>(), m_i24);
let m_i128 = I128::from(m_i56);
assert_eq!(m_i128, I128::from_dec_str("-4").unwrap());
assert_eq!(m_i128.to::<I56>(), m_i56);
let m_i96 = I96::from(m_i128);
assert_eq!(m_i96, I96::from_dec_str("-4").unwrap());
assert_eq!(m_i96.to::<I128>(), m_i128);

// convert positive signed to unsigned
assert_eq!(U24::from(I24::from_hex_str("0x7FFFFF").unwrap()), U24::from(0x7FFFFF));
assert_eq!(I24::from_hex_str("0x7FFFFF").unwrap().to::<U24>(), U24::from(0x7FFFFF));

// convert unsigned to positive signed
assert_eq!(I24::from(U24::from(0x7FFFFF)), I24::from_hex_str("0x7FFFFF").unwrap());
assert_eq!(U24::from(0x7FFFFF).to::<I24>(), I24::from_hex_str("0x7FFFFF").unwrap());
assert_eq!(I24::from(U96::from(0x7FFFFF)), I24::from_hex_str("0x7FFFFF").unwrap());
assert_eq!(U96::from(0x7FFFFF).to::<I24>(), I24::from_hex_str("0x7FFFFF").unwrap());

// can't convert negative signed to unsigned
assert!(U24::uint_try_from(m_i24).is_err());
assert!(<I24 as UintTryTo<U24>>::uint_try_to(&m_i24).is_err());

// can't convert unsigned to positive signed if too large
assert!(I24::uint_try_from(U24::from(0x800000)).is_err());
assert!(<U24 as UintTryTo<I24>>::uint_try_to(&U24::from(0x800000)).is_err());

// out-of-bounds conversions
assert!(I24::uint_try_from(I128::MIN).is_err());
assert!(<I128 as UintTryTo<I24>>::uint_try_to(&I128::MIN).is_err());
assert!(I24::uint_try_from(I128::MAX).is_err());
assert!(<I128 as UintTryTo<I24>>::uint_try_to(&I128::MAX).is_err());
}
}
Loading