Skip to content

Commit

Permalink
fix: improve the alloc feature in types-core
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Dec 22, 2023
1 parent 68ca0a8 commit 15e9fff
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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"
Expand Down
36 changes: 26 additions & 10 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<BitArrayStore> {
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<BitArrayStore> {
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::<Vec<u32>>()
.collect::<alloc::vec::Vec<u32>>()
.try_into()
.unwrap();

Expand All @@ -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<BitArrayStore> {
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<BitArrayStore> {
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::<Vec<u32>>()
.collect::<alloc::vec::Vec<u32>>()
.try_into()
.unwrap();

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet-types-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions ensure_no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 15e9fff

Please sign in to comment.