Skip to content

Commit 1550ae7

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

File tree

15 files changed

+246
-140
lines changed

15 files changed

+246
-140
lines changed

Cargo.lock

Lines changed: 54 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: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@ 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" }
1818
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"
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+
snafu = { version = "0.7.4", default-features = false }
29+
hex = { version = "0.4.3", default-features = false, optional = true }
30+
31+
[features]
32+
alloc = ["hex?/alloc"]
33+
signature-display = ["dep:hex", "alloc"]
34+
bigdecimal = ["starknet-curve/bigdecimal", "starknet-ff/bigdecimal"]
2935

3036
[dev-dependencies]
3137
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: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
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+
use snafu::prelude::*;
3+
4+
/// Errors when performing ECDSA [`sign`](fn.sign) operations
5+
#[derive(Debug, Snafu)]
6+
pub enum SignError {
7+
#[snafu(display("Invalid message hash"))]
8+
InvalidMessageHash,
9+
#[snafu(display("Invalid k"))]
10+
InvalidK,
11+
}
812
}
13+
pub use sign_error::SignError;
14+
15+
mod verify_error {
16+
use snafu::prelude::*;
917

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,
18+
/// Errors when performing ECDSA [`verify`](fn.verify) operations
19+
#[derive(Debug, Snafu)]
20+
pub enum VerifyError {
21+
#[snafu(display("Invalid message hash"))]
22+
InvalidMessageHash,
23+
#[snafu(display("Invalid r"))]
24+
InvalidR,
25+
#[snafu(display("Invalid s"))]
26+
InvalidS,
27+
}
1928
}
29+
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+
#![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: 6 additions & 5 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]

0 commit comments

Comments
 (0)