Skip to content

Commit 6614cf5

Browse files
AbdelStarktdelabro
authored andcommitted
feat: introduce no_std support for ff and crypto
1 parent 2f2928b commit 6614cf5

File tree

15 files changed

+265
-146
lines changed

15 files changed

+265
-146
lines changed

Cargo.lock

Lines changed: 18 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ url = "2.2.2"
4848
[features]
4949
default = ["bigdecimal"]
5050
bigdecimal = ["starknet-core/bigdecimal"]
51-
no_unknown_fields = ["starknet-core/no_unknown_fields", "starknet-providers/no_unknown_fields"]
51+
no_unknown_fields = [
52+
"starknet-core/no_unknown_fields",
53+
"starknet-providers/no_unknown_fields",
54+
]

starknet-core/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ all-features = true
1717

1818
[dependencies]
1919
starknet-crypto = { version = "0.2.0", path = "../starknet-crypto" }
20-
starknet-ff = { version = "0.2.0", path = "../starknet-ff", default-features = false }
20+
starknet-ff = { version = "0.2.0", path = "../starknet-ff", features = [
21+
"serde",
22+
] }
2123
base64 = "0.13.0"
2224
ethereum-types = "0.12.1"
2325
flate2 = "1.0.24"
@@ -37,7 +39,7 @@ wasm-bindgen-test = "0.3.29"
3739

3840
[features]
3941
default = ["bigdecimal"]
40-
bigdecimal = ["starknet-ff/bigdecimal"]
42+
bigdecimal = ["starknet-ff/bigdecimal", "starknet-crypto/bigdecimal"]
4143
no_unknown_fields = []
4244

4345
[[bench]]

starknet-crypto-codegen/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ homepage = "https://starknet.rs/"
1010
description = """
1111
Codegen macros for `starknet-crypto`
1212
"""
13-
keywords = ["ethereum", "starknet", "web3"]
13+
keywords = ["ethereum", "starknet", "web3", "no_std"]
1414

1515
[lib]
1616
proc-macro = true
1717

1818
[dependencies]
19-
starknet-curve = { version = "0.1.0", path = "../starknet-curve" }
20-
starknet-ff = { version = "0.2.0", path = "../starknet-ff" }
21-
syn = "1.0.96"
19+
starknet-curve = { version = "0.1.0", path = "../starknet-curve", default-features = false }
20+
starknet-ff = { version = "0.2.0", path = "../starknet-ff", default-features = false }
21+
syn = { version = "1.0.96", default-features = false }

starknet-crypto/Cargo.toml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@ homepage = "https://starknet.rs/"
1010
description = """
1111
Low-level cryptography utilities for Starknet
1212
"""
13-
keywords = ["ethereum", "starknet", "web3"]
13+
keywords = ["ethereum", "starknet", "web3", "no_std"]
1414

1515
[dependencies]
1616
starknet-crypto-codegen = { version = "0.1.0", path = "../starknet-crypto-codegen" }
1717
starknet-curve = { version = "0.1.0", path = "../starknet-curve" }
18-
starknet-ff = { version = "0.2.0", path = "../starknet-ff" }
19-
crypto-bigint = "0.4.9"
20-
hmac = "0.12.1"
21-
num-bigint = "0.4.3"
22-
num-integer = "0.1.44"
23-
num-traits = "0.2.14"
24-
rfc6979 = "0.3.1"
25-
sha2 = "0.10.6"
26-
thiserror = "1.0.30"
27-
zeroize = "1.5.0"
28-
hex = "0.4.3"
18+
starknet-ff = { version = "0.2.0", path = "../starknet-ff", default-features = false }
19+
crypto-bigint = { version = "0.4.9", default-features = false }
20+
hmac = { version = "0.12.1", default-features = false }
21+
num-bigint = { version = "0.4.3", default-features = false }
22+
num-integer = { version = "0.1.44", default-features = false }
23+
num-traits = { version = "0.2.14", default-features = false }
24+
rfc6979 = { version = "0.3.1", default-features = false }
25+
sha2 = { version = "0.10.6", default-features = false }
26+
zeroize = { version = "1.5.0", default-features = false }
27+
hashbrown = { version = "0.13.2", default-features = false }
28+
hex = { version = "0.4.3", default-features = false, optional = true }
29+
30+
[features]
31+
default = ["std"]
32+
std = []
33+
alloc = ["hex?/alloc"]
34+
signature-display = ["dep:hex", "alloc"]
35+
bigdecimal = ["starknet-curve/bigdecimal", "starknet-ff/bigdecimal"]
2936

3037
[dev-dependencies]
3138
criterion = { version = "0.4.0", default-features = false }

starknet-crypto/src/ecdsa.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
fe_utils::{add_unbounded, bigint_mul_mod_floor, mod_inverse, mul_mod_floor},
88
FieldElement, SignError, VerifyError,
99
};
10-
use std::fmt;
1110

1211
const ELEMENT_UPPER_BOUND: FieldElement = FieldElement::from_mont([
1312
18446743986131435553,
@@ -25,8 +24,9 @@ pub struct Signature {
2524
pub s: FieldElement,
2625
}
2726

28-
impl fmt::Display for Signature {
29-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
#[cfg(feature = "hex")]
28+
impl crate::stdlib::fmt::Display for Signature {
29+
fn fmt(&self, f: &mut crate::stdlib::fmt::Formatter) -> crate::stdlib::fmt::Result {
3030
write!(
3131
f,
3232
"{}{}",

starknet-crypto/src/error.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1-
/// Errors when performing ECDSA [`sign`](fn.sign) operations
2-
#[derive(Debug, thiserror::Error)]
3-
pub enum SignError {
4-
#[error("Invalid message hash")]
5-
InvalidMessageHash,
6-
#[error("Invalid k")]
7-
InvalidK,
1+
mod sign_error {
2+
/// Errors when performing ECDSA [`sign`](fn.sign) operations
3+
#[derive(Debug)]
4+
pub enum SignError {
5+
InvalidMessageHash,
6+
InvalidK,
7+
}
8+
9+
#[cfg(feature = "std")]
10+
impl std::error::Error for SignError {}
11+
12+
impl core::fmt::Display for SignError {
13+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14+
match self {
15+
Self::InvalidMessageHash => write!(f, "Invalid message hash"),
16+
Self::InvalidK => write!(f, "Invalid k"),
17+
}
18+
}
19+
}
820
}
21+
pub use sign_error::SignError;
22+
23+
mod verify_error {
24+
/// Errors when performing ECDSA [`verify`](fn.verify) operations
25+
#[derive(Debug)]
26+
pub enum VerifyError {
27+
InvalidMessageHash,
28+
InvalidR,
29+
InvalidS,
30+
}
31+
32+
#[cfg(feature = "std")]
33+
impl std::error::Error for VerifyError {}
934

10-
/// Errors when performing ECDSA [`verify`](fn.verify) operations
11-
#[derive(Debug, thiserror::Error)]
12-
pub enum VerifyError {
13-
#[error("Invalid message hash")]
14-
InvalidMessageHash,
15-
#[error("Invalid r")]
16-
InvalidR,
17-
#[error("Invalid s")]
18-
InvalidS,
35+
impl core::fmt::Display for VerifyError {
36+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37+
match self {
38+
Self::InvalidMessageHash => write!(f, "Invalid message hash"),
39+
Self::InvalidR => write!(f, "Invalid r"),
40+
Self::InvalidS => write!(f, "Invalid s"),
41+
}
42+
}
43+
}
1944
}
45+
pub use verify_error::VerifyError;

starknet-crypto/src/fe_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use core::ops::{Add, Mul};
2+
13
use num_bigint::BigInt;
24
use num_integer::Integer;
35
use num_traits::{One, Zero};
4-
use std::ops::{Add, Mul};
56

67
use crate::FieldElement;
78

starknet-crypto/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
12
#![doc = include_str!("../README.md")]
23

4+
#[cfg(any(test, feature = "alloc"))]
5+
extern crate alloc;
6+
37
mod ecdsa;
48
mod error;
59
mod fe_utils;

starknet-crypto/src/rfc6979.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ where
8181
mod tests {
8282
use super::*;
8383
use crate::test_utils::field_element_from_be_hex;
84+
use alloc::vec::Vec;
8485

8586
use serde::Deserialize;
8687

8788
#[derive(Deserialize)]
88-
struct Rfc6979TestVecotr {
89-
msg_hash: String,
90-
priv_key: String,
91-
seed: String,
92-
k: String,
89+
struct Rfc6979TestVecotr<'a> {
90+
msg_hash: &'a str,
91+
priv_key: &'a str,
92+
seed: &'a str,
93+
k: &'a str,
9394
}
9495

9596
#[test]
@@ -110,10 +111,10 @@ mod tests {
110111
let test_vectors: Vec<Rfc6979TestVecotr> = serde_json::from_str(json_str).unwrap();
111112

112113
for test_vector in test_vectors.iter() {
113-
let msg_hash = field_element_from_be_hex(&test_vector.msg_hash);
114-
let priv_key = field_element_from_be_hex(&test_vector.priv_key);
115-
let seed = field_element_from_be_hex(&test_vector.seed);
116-
let expected_k = field_element_from_be_hex(&test_vector.k);
114+
let msg_hash = field_element_from_be_hex(test_vector.msg_hash);
115+
let priv_key = field_element_from_be_hex(test_vector.priv_key);
116+
let seed = field_element_from_be_hex(test_vector.seed);
117+
let expected_k = field_element_from_be_hex(test_vector.k);
117118

118119
let k = generate_k(&msg_hash, &priv_key, Some(&seed));
119120

starknet-curve/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ homepage = "https://starknet.rs/"
1010
description = """
1111
Stark curve
1212
"""
13-
keywords = ["ethereum", "starknet", "web3"]
13+
keywords = ["ethereum", "starknet", "web3", "no_std"]
1414

1515
[dependencies]
16-
starknet-ff = { version = "0.2.0", path = "../starknet-ff" }
16+
starknet-ff = { version = "0.2.0", path = "../starknet-ff", default-features = false }
17+
18+
[features]
19+
bigdecimal = ["starknet-ff/bigdecimal"]

0 commit comments

Comments
 (0)