Skip to content

Commit

Permalink
Make the -sys crate optional
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kixunil committed Jul 2, 2024
1 parent 6648126 commit f465167
Show file tree
Hide file tree
Showing 12 changed files with 1,397 additions and 370 deletions.
16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["std"]
std = ["alloc", "secp256k1-sys/std"]
default = ["sys-std"]
std = ["alloc"]
alloc = []
sys-std = ["std", "sys-alloc", "secp256k1-sys/std"]
# allow use of Secp256k1::new and related API that requires an allocator
alloc = ["secp256k1-sys/alloc"]
sys-alloc = ["secp256k1-sys/alloc"]
hashes-std = ["std", "hashes/std"]
rand-std = ["std", "rand", "rand/std", "rand/std_rng"]
recovery = ["secp256k1-sys/recovery"]
Expand All @@ -36,7 +38,7 @@ global-context = ["std"]
global-context-less-secure = ["global-context"]

[dependencies]
secp256k1-sys = { version = "0.10.0", default-features = false, path = "./secp256k1-sys" }
secp256k1-sys = { version = "0.10.0", default-features = false, path = "./secp256k1-sys", optional = true }
serde = { version = "1.0.103", default-features = false, optional = true }

# You likely only want to enable these if you explicitly do not want to use "std", otherwise enable
Expand All @@ -57,15 +59,15 @@ getrandom = { version = "0.2", features = ["js"] }

[[example]]
name = "sign_verify_recovery"
required-features = ["recovery", "hashes-std"]
required-features = ["recovery", "hashes-std", "secp256k1-sys"]

[[example]]
name = "sign_verify"
required-features = ["hashes-std"]
required-features = ["hashes-std", "secp256k1-sys"]

[[example]]
name = "generate_keys"
required-features = ["rand-std"]
required-features = ["rand-std", "secp256k1-sys"]

[workspace]
members = ["secp256k1-sys"]
Expand Down
14 changes: 7 additions & 7 deletions contrib/_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -ex

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

cargo --version
rustc --version
Expand Down Expand Up @@ -62,17 +62,17 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then
fi

# Examples
cargo run --locked --example sign_verify --features=hashes-std
cargo run --locked --example sign_verify_recovery --features=recovery,hashes-std
cargo run --locked --example generate_keys --features=rand-std
cargo run --locked --example sign_verify --features=hashes-std,secp256k1-sys
cargo run --locked --example sign_verify_recovery --features=recovery,hashes-std,secp256k1-sys
cargo run --locked --example generate_keys --features=rand-std,secp256k1-sys
fi

if [ "$DO_LINT" = true ]
then
cargo clippy --locked --all-features --all-targets -- -D warnings
cargo clippy --locked --example sign_verify --features=hashes-std -- -D warnings
cargo clippy --locked --example sign_verify_recovery --features=recovery,hashes-std -- -D warnings
cargo clippy --locked --example generate_keys --features=rand-std -- -D warnings
cargo clippy --locked --example sign_verify --features=hashes-std,secp256k1-sys -- -D warnings
cargo clippy --locked --example sign_verify_recovery --features=recovery,hashes-std,secp256k1-sys -- -D warnings
cargo clippy --locked --example generate_keys --features=rand-std,secp256k1-sys -- -D warnings
fi

# Build the docs if told to (this only works with the nightly toolchain)
Expand Down
Loading

0 comments on commit f465167

Please sign in to comment.