Skip to content

Commit

Permalink
Merge branch 'main' into arni/felt/code_dedup/from_big_int
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Dec 25, 2024
2 parents 8f0de48 + 7ef3023 commit 581d108
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 70 deletions.
35 changes: 17 additions & 18 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starknet-types-core"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
license = "MIT"
homepage = "https://github.com/starknet-io/types-rs"
Expand All @@ -11,19 +11,19 @@ description = "Core types representation for Starknet"
readme = "README.md"

[dependencies]
lambdaworks-math = { version = "0.7.0", default-features = false }
num-traits = { version = "0.2.18", default-features = false }
num-bigint = { version = "0.4.4", default-features = false }
num-integer = { version = "0.1.45", default-features = false }
lambdaworks-math = { version = "0.10.0", default-features = false }
num-traits = { version = "0.2", default-features = false }
num-bigint = { version = "0.4", default-features = false }
num-integer = { version = "0.1", default-features = false }

# Optional
arbitrary = { version = "1.3.2", optional = true }
serde = { version = "1.0.197", optional = true, default-features = false, features = [
arbitrary = { version = "1.3", optional = true }
serde = { version = "1", optional = true, default-features = false, features = [
"alloc",
] }
lambdaworks-crypto = { version = "0.7.0", default-features = false, optional = true }
parity-scale-codec = { version = "3.6.9", default-features = false, optional = true }
lazy_static = { version = "1.4.0", default-features = false, optional = true }
lambdaworks-crypto = { version = "0.10.0", default-features = false, optional = true }
parity-scale-codec = { version = "3.6", default-features = false, optional = true }
lazy_static = { version = "1.5", default-features = false, optional = true }

[features]
default = ["std", "serde", "curve", "num-traits"]
Expand All @@ -46,15 +46,14 @@ num-traits = []
papyrus-serialization = ["std"]

[dev-dependencies]
proptest = "1.4.0"
regex = "1.10.4"
serde_test = "1.0.176"
criterion = "0.5.1"
rand_chacha = "0.3.1"
rand = "0.8.5"
lazy_static = { version = "1.4.0", default-features = false }
proptest = "1.5"
regex = "1.11"
serde_test = "1"
criterion = "0.5"
rand_chacha = "0.3"
rand = "0.8"
lazy_static = { version = "1.5", default-features = false }

[[bench]]
name = "criterion_hashes"
harness = false

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 @@ -590,27 +565,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

0 comments on commit 581d108

Please sign in to comment.