diff --git a/crates/stark-felt/src/lib.rs b/crates/stark-felt/src/lib.rs index 5b2c28e..cf199de 100644 --- a/crates/stark-felt/src/lib.rs +++ b/crates/stark-felt/src/lib.rs @@ -303,27 +303,60 @@ impl TryFrom<&Felt> for NonZeroFelt { } } -impl FromPrimitive for Felt { - fn from_i64(value: i64) -> Option { - Self::from_i128(value as i128) - } - - fn from_u64(value: u64) -> Option { - Self::from_u128(value as u128) +impl From for Felt { + fn from(value: u128) -> Felt { + Self(FieldElement::from(&UnsignedInteger::from(value))) } +} - fn from_i128(value: i128) -> Option { +impl From for Felt { + fn from(value: i128) -> Felt { let mut res = Self(FieldElement::from(&UnsignedInteger::from( value.unsigned_abs(), ))); if value.is_negative() { res = -res; } - Some(res) + res + } +} + +macro_rules! impl_from { + ($from:ty, $with:ty) => { + impl From<$from> for Felt { + fn from(value: $from) -> Self { + (value as $with).into() + } + } + }; +} + +impl_from!(u8, u128); +impl_from!(u16, u128); +impl_from!(u32, u128); +impl_from!(u64, u128); +impl_from!(usize, u128); +impl_from!(i8, i128); +impl_from!(i16, i128); +impl_from!(i32, i128); +impl_from!(i64, i128); +impl_from!(isize, i128); + +impl FromPrimitive for Felt { + fn from_i64(value: i64) -> Option { + Some(value.into()) + } + + fn from_u64(value: u64) -> Option { + Some(value.into()) + } + + fn from_i128(value: i128) -> Option { + Some(value.into()) } fn from_u128(value: u128) -> Option { - Some(Self(FieldElement::from(&UnsignedInteger::from(value)))) + Some(value.into()) } } @@ -350,40 +383,6 @@ impl ToPrimitive for Felt { } } -macro_rules! impl_from_u128 { - ($type:ty) => { - impl From<$type> for Felt { - fn from(value: $type) -> Self { - Self::from_u128(value as u128).expect("conversion from primitive is infallible") - } - } - }; -} - -impl_from_u128!(u8); -impl_from_u128!(u16); -impl_from_u128!(u32); -impl_from_u128!(u64); -impl_from_u128!(u128); -impl_from_u128!(usize); - -macro_rules! impl_from_i128 { - ($type:ty) => { - impl From<$type> for Felt { - fn from(value: $type) -> Self { - Self::from_i128(value as i128).expect("conversion from primitive is infallible") - } - } - }; -} - -impl_from_i128!(i8); -impl_from_i128!(i16); -impl_from_i128!(i32); -impl_from_i128!(i64); -impl_from_i128!(i128); -impl_from_i128!(isize); - impl Add<&Felt> for u64 { type Output = Option;