Skip to content

Commit ee86eaf

Browse files
committed
Make the -sys crate optional
Downstream projects, such as `bitcoin` are using types from this crate and need to compile the C code even if they only use the types for checking validity of the data and don't perform cryptographic operations. Compiling C code is slow and unreliable so there was desire to avoid this. This commit introduces pure Rust implementation of basic non-cryptographic operations (is point on the curve, decompression of point, secret key constant time comparisons) and feature-gates cryptographic operations on `secp256k1-sys` which is now optional. To make use of this, downstream crates will have to deactivate the feature and possibly feature gate themselves. The implementation requires a U256 type which was copied from the `bitcoin` crate. We don't depend on a common crate to avoid needless compilation when the feature is turned on. This is a breaking change because users who did cryptographic operations with `default-features = false` will get compilation errors until they enable the feature.
1 parent 33a1893 commit ee86eaf

File tree

12 files changed

+1409
-375
lines changed

12 files changed

+1409
-375
lines changed

Cargo.toml

+9-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ all-features = true
1818
rustdoc-args = ["--cfg", "docsrs"]
1919

2020
[features]
21-
default = ["std"]
22-
std = ["alloc", "secp256k1-sys/std"]
21+
default = ["sys-std"]
22+
std = ["alloc"]
23+
alloc = []
24+
sys-std = ["std", "sys-alloc", "secp256k1-sys/std"]
2325
# allow use of Secp256k1::new and related API that requires an allocator
24-
alloc = ["secp256k1-sys/alloc"]
26+
sys-alloc = ["secp256k1-sys/alloc"]
2527
hashes-std = ["std", "hashes/std"]
2628
rand-std = ["std", "rand", "rand/std", "rand/std_rng"]
2729
recovery = ["secp256k1-sys/recovery"]
@@ -36,7 +38,7 @@ global-context = ["std"]
3638
global-context-less-secure = ["global-context"]
3739

3840
[dependencies]
39-
secp256k1-sys = { version = "0.10.0", default-features = false, path = "./secp256k1-sys" }
41+
secp256k1-sys = { version = "0.10.0", default-features = false, path = "./secp256k1-sys", optional = true }
4042
serde = { version = "1.0.103", default-features = false, optional = true }
4143

4244
# You likely only want to enable these if you explicitly do not want to use "std", otherwise enable
@@ -59,15 +61,15 @@ unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)', 'cfg(secp256k1_fu
5961

6062
[[example]]
6163
name = "sign_verify_recovery"
62-
required-features = ["recovery", "hashes-std"]
64+
required-features = ["recovery", "hashes-std", "secp256k1-sys"]
6365

6466
[[example]]
6567
name = "sign_verify"
66-
required-features = ["hashes-std"]
68+
required-features = ["hashes-std", "secp256k1-sys"]
6769

6870
[[example]]
6971
name = "generate_keys"
70-
required-features = ["rand-std"]
72+
required-features = ["rand-std", "secp256k1-sys"]
7173

7274
[workspace]
7375
members = ["secp256k1-sys"]

contrib/_test.sh

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -ex
44

55
REPO_DIR=$(git rev-parse --show-toplevel)
6-
FEATURES="hashes global-context lowmemory rand recovery serde std alloc hashes-std rand-std"
6+
FEATURES="hashes global-context lowmemory rand recovery serde std alloc hashes-std rand-std secp256k1-sys"
77

88
cargo --version
99
rustc --version
@@ -62,17 +62,17 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then
6262
fi
6363

6464
# Examples
65-
cargo run --locked --example sign_verify --features=hashes-std
66-
cargo run --locked --example sign_verify_recovery --features=recovery,hashes-std
67-
cargo run --locked --example generate_keys --features=rand-std
65+
cargo run --locked --example sign_verify --features=hashes-std,secp256k1-sys
66+
cargo run --locked --example sign_verify_recovery --features=recovery,hashes-std,secp256k1-sys
67+
cargo run --locked --example generate_keys --features=rand-std,secp256k1-sys
6868
fi
6969

7070
if [ "$DO_LINT" = true ]
7171
then
7272
cargo clippy --locked --all-features --all-targets -- -D warnings
73-
cargo clippy --locked --example sign_verify --features=hashes-std -- -D warnings
74-
cargo clippy --locked --example sign_verify_recovery --features=recovery,hashes-std -- -D warnings
75-
cargo clippy --locked --example generate_keys --features=rand-std -- -D warnings
73+
cargo clippy --locked --example sign_verify --features=hashes-std,secp256k1-sys -- -D warnings
74+
cargo clippy --locked --example sign_verify_recovery --features=recovery,hashes-std,secp256k1-sys -- -D warnings
75+
cargo clippy --locked --example generate_keys --features=rand-std,secp256k1-sys -- -D warnings
7676
fi
7777

7878
# Build the docs if told to (this only works with the nightly toolchain)

0 commit comments

Comments
 (0)