Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(primitives): Signed cleanup #395

Merged
merged 3 commits into from
Nov 1, 2023
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ once_cell = "1"
proptest = "1"
proptest-derive = "0.4"
rand = { version = "0.8", default-features = false }
ruint = { version = "1.10.1", default-features = false, features = ["alloc"] }
ruint = { version = "1.11.0", default-features = false, features = ["alloc"] }
ruint-macro = { version = "1", default-features = false }
tiny-keccak = "2.0"
winnow = { version = "0.5", default-features = false, features = ["alloc"] }
42 changes: 24 additions & 18 deletions crates/primitives/src/signed/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruint::Uint;
impl<const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>> for Signed<BITS, LIMBS> {
type Error = BigIntConversionError;

#[inline(always)]
#[inline]
fn try_from(from: Uint<BITS, LIMBS>) -> Result<Self, Self::Error> {
let value = Self(from);
match value.sign() {
Expand All @@ -19,7 +19,7 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<Uint<BITS, LIMBS>> for Signe
impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for Uint<BITS, LIMBS> {
type Error = BigIntConversionError;

#[inline(always)]
#[inline]
fn try_from(value: Signed<BITS, LIMBS>) -> Result<Self, Self::Error> {
match value.sign() {
Sign::Positive => Ok(value.0),
Expand All @@ -29,27 +29,27 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for Uin
}

impl<const BITS: usize, const LIMBS: usize> TryFrom<&str> for Signed<BITS, LIMBS> {
type Error = <Self as FromStr>::Err;
type Error = ParseSignedError;

#[inline(always)]
#[inline]
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::from_str(value)
}
}

impl<const BITS: usize, const LIMBS: usize> TryFrom<&String> for Signed<BITS, LIMBS> {
type Error = <Self as FromStr>::Err;
type Error = ParseSignedError;

#[inline(always)]
#[inline]
fn try_from(value: &String) -> Result<Self, Self::Error> {
value.parse()
}
}

impl<const BITS: usize, const LIMBS: usize> TryFrom<String> for Signed<BITS, LIMBS> {
type Error = <Self as FromStr>::Err;
type Error = ParseSignedError;

#[inline(always)]
#[inline]
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
Expand All @@ -58,9 +58,15 @@ impl<const BITS: usize, const LIMBS: usize> TryFrom<String> for Signed<BITS, LIM
impl<const BITS: usize, const LIMBS: usize> FromStr for Signed<BITS, LIMBS> {
type Err = ParseSignedError;

#[inline(always)]
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::from_dec_str(value).or_else(|_| Self::from_hex_str(value))
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (sign, s) = match s.as_bytes().first() {
Some(b'+') => (Sign::Positive, &s[1..]),
Some(b'-') => (Sign::Negative, &s[1..]),
_ => (Sign::Positive, s),
};
let abs = Uint::<BITS, LIMBS>::from_str(s)?;
Self::checked_from_sign_and_abs(sign, abs).ok_or(ParseSignedError::IntegerOverflow)
}
}

Expand Down Expand Up @@ -157,7 +163,7 @@ macro_rules! impl_conversions {
impl<const BITS: usize, const LIMBS: usize> TryFrom<$u> for Signed<BITS, LIMBS> {
type Error = BigIntConversionError;

#[inline(always)]
#[inline]
fn try_from(value: $u) -> Result<Self, Self::Error> {
let u = Uint::<BITS, LIMBS>::try_from(value).map_err(|_| BigIntConversionError)?;
Signed::checked_from_sign_and_abs(Sign::Positive, u).ok_or(BigIntConversionError)
Expand All @@ -167,7 +173,7 @@ macro_rules! impl_conversions {
impl<const BITS: usize, const LIMBS: usize> TryFrom<$i> for Signed<BITS, LIMBS> {
type Error = BigIntConversionError;

#[inline(always)]
#[inline]
fn try_from(value: $i) -> Result<Self, Self::Error> {
let uint: $u = value as $u;

Expand All @@ -184,7 +190,7 @@ macro_rules! impl_conversions {
impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for $u {
type Error = BigIntConversionError;

#[inline(always)]
#[inline]
fn try_from(value: Signed<BITS, LIMBS>) -> Result<$u, Self::Error> {
u128::try_from(value)?.try_into().map_err(|_| BigIntConversionError)
}
Expand All @@ -193,7 +199,7 @@ macro_rules! impl_conversions {
impl<const BITS: usize, const LIMBS: usize> TryFrom<Signed<BITS, LIMBS>> for $i {
type Error = BigIntConversionError;

#[inline(always)]
#[inline]
fn try_from(value: Signed<BITS, LIMBS>) -> Result<$i, Self::Error> {
i128::try_from(value)?.try_into().map_err(|_| BigIntConversionError)
}
Expand All @@ -203,7 +209,7 @@ macro_rules! impl_conversions {

(@impl_fns $t:ty, $actual_low:ident $low:ident $as:ident) => {
/// Low word.
#[inline(always)]
#[inline]
pub const fn $low(&self) -> $t {
if BITS == 0 {
return 0
Expand All @@ -216,8 +222,8 @@ macro_rules! impl_conversions {
///
/// # Panics
///
#[doc = concat!("If the number is outside the ", stringify!($t), " valid range.")]
#[inline(always)]
#[doc = concat!("Panics if the number is outside the ", stringify!($t), " valid range.")]
#[inline]
#[track_caller]
pub fn $as(&self) -> $t {
<$t as TryFrom<Self>>::try_from(*self).unwrap()
Expand Down
11 changes: 9 additions & 2 deletions crates/primitives/src/signed/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ impl From<ruint::ParseError> for ParseSignedError {
}

#[cfg(feature = "std")]
impl std::error::Error for ParseSignedError {}
impl std::error::Error for ParseSignedError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Ruint(err) => Some(err),
Self::IntegerOverflow => None,
}
}
}

impl fmt::Display for ParseSignedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Ruint(err) => write!(f, "Parsing Error: {err}"),
Self::Ruint(e) => e.fmt(f),
Self::IntegerOverflow => f.write_str("number does not fit in the integer size"),
}
}
Expand Down
Loading