Skip to content

Commit 6e5b6be

Browse files
authored
Code reorg (#133)
1 parent be520fe commit 6e5b6be

File tree

12 files changed

+701
-583
lines changed

12 files changed

+701
-583
lines changed

crates/starknet-types-core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ zeroize = { version = "1.8.1", default-features = false, optional = true }
3232
[features]
3333
default = ["std", "serde", "curve", "num-traits"]
3434
std = [
35+
"alloc",
3536
"lambdaworks-math/std",
3637
"num-traits/std",
3738
"num-bigint/std",
@@ -52,7 +53,7 @@ papyrus-serialization = ["std"]
5253
zeroize = ["dep:zeroize"]
5354

5455
[dev-dependencies]
55-
proptest = "1.5"
56+
proptest = { version = "1.5", default-features = false, features = ["alloc", "proptest-macro"] }
5657
regex = "1.11"
5758
serde_test = "1"
5859
criterion = "0.5"
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use core::fmt;
2+
3+
use super::alloc;
4+
use super::Felt;
5+
6+
impl Felt {
7+
/// Helper to represent the felt value as a zero-padded hexadecimal string.
8+
///
9+
/// Equivalent to calling `format!("{self:#x}")`.
10+
pub fn to_hex_string(&self) -> alloc::string::String {
11+
alloc::format!("{self:#x}")
12+
}
13+
14+
/// Helper to represent the felt value as a zero-padded hexadecimal string.
15+
///
16+
/// The resulting string will consist of:
17+
/// 1. A`0x` prefix,
18+
/// 2. an amount of padding zeros so that the resulting string length is fixed (This amount may be 0),
19+
/// 3. the felt value represented in hexadecimal
20+
///
21+
/// The resulting string is guaranted to be 66 chars long, which is enough to represent `Felt::MAX`:
22+
/// 2 chars for the `0x` prefix and 64 chars for the padded hexadecimal felt value.
23+
pub fn to_fixed_hex_string(&self) -> alloc::string::String {
24+
let hex_str = alloc::format!("{self:#x}");
25+
if hex_str.len() < 66 {
26+
alloc::format!("0x{:0>64}", hex_str.strip_prefix("0x").unwrap())
27+
} else {
28+
hex_str
29+
}
30+
}
31+
}
32+
33+
/// Represents [Felt] in lowercase hexadecimal format.
34+
impl fmt::LowerHex for Felt {
35+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36+
let hex = alloc::string::ToString::to_string(&self.0);
37+
let hex = hex.strip_prefix("0x").unwrap();
38+
39+
let width = if f.sign_aware_zero_pad() {
40+
f.width().unwrap().min(64)
41+
} else {
42+
1
43+
};
44+
if f.alternate() {
45+
write!(f, "0x")?;
46+
}
47+
48+
if hex.len() < width {
49+
for _ in 0..(width - hex.len()) {
50+
write!(f, "0")?;
51+
}
52+
}
53+
write!(f, "{}", hex)
54+
}
55+
}
56+
57+
/// Represents [Felt] in uppercase hexadecimal format.
58+
impl fmt::UpperHex for Felt {
59+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60+
let hex = alloc::string::ToString::to_string(&self.0);
61+
let hex = hex.strip_prefix("0x").unwrap().to_uppercase();
62+
63+
let width = if f.sign_aware_zero_pad() {
64+
f.width().unwrap().min(64)
65+
} else {
66+
1
67+
};
68+
if f.alternate() {
69+
write!(f, "0x")?;
70+
}
71+
72+
if hex.len() < width {
73+
for _ in 0..(width - hex.len()) {
74+
write!(f, "0")?;
75+
}
76+
}
77+
write!(f, "{}", hex)
78+
}
79+
}
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
use alloc::format;
85+
use proptest::prelude::*;
86+
87+
proptest! {
88+
#[test]
89+
fn to_hex_string_is_same_as_format(ref x in any::<Felt>()) {
90+
let y = alloc::format!("{x:#x}");
91+
prop_assert_eq!(y, x.to_hex_string());
92+
}
93+
}
94+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::felt::Felt;
2+
use arbitrary::{self, Arbitrary, Unstructured};
3+
use lambdaworks_math::field::element::FieldElement;
4+
use lambdaworks_math::unsigned_integer::element::UnsignedInteger;
5+
6+
impl Arbitrary<'_> for Felt {
7+
// Creates an arbitrary `Felt` from unstructured input for fuzzing.
8+
// It uses the default implementation to create the internal limbs and then
9+
// uses the usual constructors from `lambdaworks-math`.
10+
fn arbitrary(u: &mut Unstructured) -> arbitrary::Result<Self> {
11+
let limbs = <[u64; 4]>::arbitrary(u)?;
12+
let uint = UnsignedInteger::from_limbs(limbs);
13+
let felt = FieldElement::new(uint);
14+
Ok(Felt(felt))
15+
}
16+
17+
#[inline]
18+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
19+
<[u64; 4]>::size_hint(depth)
20+
}
21+
}

0 commit comments

Comments
 (0)