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

feat: impl TryInto<primitive> for Felt #74

Merged
merged 4 commits into from
Sep 16, 2024
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
54 changes: 4 additions & 50 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(test)]
mod felt_arbitrary;
mod primitive_conversions;

use core::ops::{Add, Mul, Neg};
use core::str::FromStr;
Expand Down Expand Up @@ -38,13 +39,14 @@ use lambdaworks_math::{
#[cfg(feature = "arbitrary")]
use arbitrary::{self, Arbitrary, Unstructured};

#[repr(transparent)]
/// Definition of the Field Element type.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Felt(pub(crate) FieldElement<Stark252PrimeField>);

/// A non-zero [Felt].
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NonZeroFelt(FieldElement<Stark252PrimeField>);

impl NonZeroFelt {
Expand Down Expand Up @@ -533,33 +535,6 @@ impl TryFrom<&Felt> for NonZeroFelt {
}
}

impl From<u128> for Felt {
fn from(value: u128) -> Felt {
Self(FieldElement::from(&UnsignedInteger::from(value)))
}
}

impl From<i128> for Felt {
fn from(value: i128) -> Felt {
let mut res = Self(FieldElement::from(&UnsignedInteger::from(
value.unsigned_abs(),
)));
if value.is_negative() {
res = -res;
}
res
}
}

impl From<bool> for Felt {
fn from(value: bool) -> Felt {
match value {
true => Felt::ONE,
false => Felt::ZERO,
}
}
}

impl From<&BigInt> for Felt {
fn from(bigint: &BigInt) -> Felt {
let (sign, bytes) = bigint.to_bytes_le();
Expand Down Expand Up @@ -595,27 +570,6 @@ impl From<BigUint> for Felt {
}
}

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 FromStr for Felt {
type Err = FromStrError;

Expand Down
Loading
Loading