From 15e9fffaa675670d66c89ff46d02b9e2ad657871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Thu, 21 Dec 2023 15:47:16 +0100 Subject: [PATCH] fix: improve the alloc feature in types-core --- crates/starknet-types-core/Cargo.toml | 6 ++-- crates/starknet-types-core/src/felt/mod.rs | 36 ++++++++++++++++------ crates/starknet-types-rpc/Cargo.toml | 2 +- ensure_no_std/src/main.rs | 2 ++ 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/crates/starknet-types-core/Cargo.toml b/crates/starknet-types-core/Cargo.toml index 3bbfbbf..01a451e 100644 --- a/crates/starknet-types-core/Cargo.toml +++ b/crates/starknet-types-core/Cargo.toml @@ -23,7 +23,7 @@ lazy_static = { version = "1.4.0", default-features = false, features = [ # Optional arbitrary = { version = "1.3.0", optional = true } -serde = { version = "1.0.163", optional = true, default-features = false } +serde = { version = "1.0.163", optional = true, default-features = false, features = ["alloc"] } lambdaworks-crypto = { version = "0.3.0", default-features = false, optional = true } parity-scale-codec = { version = "3.2.2", default-features = false, optional = true } @@ -37,13 +37,13 @@ std = [ "num-integer/std", "serde?/std", ] +alloc = [] curve = [] hash = ["dep:lambdaworks-crypto"] arbitrary = ["std", "dep:arbitrary"] parity-scale-codec = ["dep:parity-scale-codec"] -serde = ["dep:serde"] +serde = ["alloc", "dep:serde"] num-traits = [] -alloc = ["serde?/alloc"] [dev-dependencies] proptest = "1.1.0" diff --git a/crates/starknet-types-core/src/felt/mod.rs b/crates/starknet-types-core/src/felt/mod.rs index 0cfbcf2..5b76705 100644 --- a/crates/starknet-types-core/src/felt/mod.rs +++ b/crates/starknet-types-core/src/felt/mod.rs @@ -27,12 +27,9 @@ pub type BitArrayStore = [u64; 4]; #[cfg(not(target_pointer_width = "64"))] pub type BitArrayStore = [u32; 8]; +#[cfg(any(test, feature = "alloc"))] pub extern crate alloc; -use alloc::string::ToString; -#[cfg(not(target_pointer_width = "64"))] -use alloc::vec::Vec; - use lambdaworks_math::{ field::{ element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField, @@ -200,16 +197,26 @@ impl Felt { /// Converts to big-endian bit representation. /// This is as performant as [to_bits_le](Felt::to_bits_le) + #[cfg(target_pointer_width = "64")] + pub fn to_bits_be(&self) -> BitArray { + let mut limbs = self.0.representative().limbs; + limbs.reverse(); + + BitArray::new(limbs) + } + + /// Converts to big-endian bit representation. + /// This is as performant as [to_bits_le](Felt::to_bits_le) + #[cfg(all(feature = "alloc", not(target_pointer_width = "64")))] pub fn to_bits_be(&self) -> BitArray { let mut limbs = self.0.representative().limbs; limbs.reverse(); - #[cfg(not(target_pointer_width = "64"))] // Split limbs to adjust to BitArrayStore = [u32; 8] let limbs: [u32; 8] = limbs .into_iter() .flat_map(|n| [(n >> 32) as u32, n as u32]) - .collect::>() + .collect::>() .try_into() .unwrap(); @@ -225,15 +232,24 @@ impl Felt { /// Converts to little-endian bit representation. /// This is as performant as [to_bits_be](Felt::to_bits_be) + #[cfg(target_pointer_width = "64")] + pub fn to_bits_le(&self) -> BitArray { + let limbs = self.0.representative().limbs; + + BitArray::new(limbs) + } + + /// Converts to little-endian bit representation. + /// This is as performant as [to_bits_be](Felt::to_bits_be) + #[cfg(all(feature = "alloc", not(target_pointer_width = "64")))] pub fn to_bits_le(&self) -> BitArray { let limbs = self.0.representative().limbs; - #[cfg(not(target_pointer_width = "64"))] // Split limbs to adjust to BitArrayStore = [u32; 8] let limbs: [u32; 8] = limbs .into_iter() .flat_map(|n| [n as u32, (n >> 32) as u32]) - .collect::>() + .collect::>() .try_into() .unwrap(); @@ -929,13 +945,13 @@ mod formatting { } /// Represents [Felt] in uppercase hexadecimal format. + #[cfg(feature = "alloc")] impl fmt::UpperHex for Felt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "0x{}", - self.0 - .to_string() + alloc::string::ToString::to_string(&self.0) .strip_prefix("0x") .unwrap() .to_uppercase() diff --git a/crates/starknet-types-rpc/Cargo.toml b/crates/starknet-types-rpc/Cargo.toml index 4760201..38ae114 100644 --- a/crates/starknet-types-rpc/Cargo.toml +++ b/crates/starknet-types-rpc/Cargo.toml @@ -16,7 +16,7 @@ default = ["std"] std = ["serde/std", "starknet-types-core/std"] [dependencies] -starknet-types-core = { path = "../starknet-types-core", default-features = false, features = ["serde", "alloc"] } +starknet-types-core = { path = "../starknet-types-core", default-features = false, features = ["serde"] } serde = { version = "1", default-features = false, features = ["derive"] } [dev-dependencies] diff --git a/ensure_no_std/src/main.rs b/ensure_no_std/src/main.rs index ae94696..3f94bd6 100644 --- a/ensure_no_std/src/main.rs +++ b/ensure_no_std/src/main.rs @@ -19,3 +19,5 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; #[allow(unused_imports)] use starknet_types_core; +#[allow(unused_imports)] +use starknet_types_rpc;