Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 3d9361d

Browse files
bkchrdavxy
andauthored
sp-core: full_crypto doesn't imply std (paritytech#11006)
* sp-core: `full_crypto` doesn't imply `std` This pr changes the feature set of `secp256k1` to not use `global-context` when only the `full_crypto` is enabled. It will be slower when the `std` feature is not enabled as the context always needs to be recreated, but that is fine. * Update client/cli/src/arg_enums.rs Co-authored-by: Davide Galassi <davxy@datawok.net> Co-authored-by: Davide Galassi <davxy@datawok.net>
1 parent 5afd5b8 commit 3d9361d

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

primitives/core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ schnorrkel = { version = "0.9.1", features = [
5656
hex = { version = "0.4", default-features = false, optional = true }
5757
libsecp256k1 = { version = "0.7", default-features = false, features = ["static-context"], optional = true }
5858
merlin = { version = "2.0", default-features = false, optional = true }
59-
secp256k1 = { version = "0.21.2", default-features = false, features = ["recovery", "global-context"], optional = true }
59+
secp256k1 = { version = "0.21.2", default-features = false, features = ["recovery", "alloc"], optional = true }
6060
ss58-registry = { version = "1.11.0", default-features = false }
6161
sp-core-hashing = { version = "4.0.0", path = "./hashing", default-features = false, optional = true }
6262
sp-runtime-interface = { version = "6.0.0", default-features = false, path = "../runtime-interface" }
@@ -108,6 +108,7 @@ std = [
108108
"regex",
109109
"num-traits/std",
110110
"secp256k1/std",
111+
"secp256k1/global-context",
111112
"sp-core-hashing/std",
112113
"sp-debug-derive/std",
113114
"sp-externalities",

primitives/core/src/ecdsa.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ use crate::{
3434
};
3535
#[cfg(feature = "std")]
3636
use bip39::{Language, Mnemonic, MnemonicType};
37-
#[cfg(feature = "full_crypto")]
38-
use core::convert::TryFrom;
37+
#[cfg(all(feature = "full_crypto", not(feature = "std")))]
38+
use secp256k1::Secp256k1;
39+
#[cfg(feature = "std")]
40+
use secp256k1::SECP256K1;
3941
#[cfg(feature = "full_crypto")]
4042
use secp256k1::{
4143
ecdsa::{RecoverableSignature, RecoveryId},
42-
Message, PublicKey, SecretKey, SECP256K1,
44+
Message, PublicKey, SecretKey,
4345
};
4446
#[cfg(feature = "std")]
4547
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
@@ -334,7 +336,13 @@ impl Signature {
334336
let rid = RecoveryId::from_i32(self.0[64] as i32).ok()?;
335337
let sig = RecoverableSignature::from_compact(&self.0[..64], rid).ok()?;
336338
let message = Message::from_slice(message).expect("Message is 32 bytes; qed");
337-
SECP256K1
339+
340+
#[cfg(feature = "std")]
341+
let context = SECP256K1;
342+
#[cfg(not(feature = "std"))]
343+
let context = Secp256k1::verification_only();
344+
345+
context
338346
.recover_ecdsa(&message, &sig)
339347
.ok()
340348
.map(|pubkey| Public(pubkey.serialize()))
@@ -425,7 +433,13 @@ impl TraitPair for Pair {
425433
fn from_seed_slice(seed_slice: &[u8]) -> Result<Pair, SecretStringError> {
426434
let secret =
427435
SecretKey::from_slice(seed_slice).map_err(|_| SecretStringError::InvalidSeedLength)?;
428-
let public = PublicKey::from_secret_key(SECP256K1, &secret);
436+
437+
#[cfg(feature = "std")]
438+
let context = SECP256K1;
439+
#[cfg(not(feature = "std"))]
440+
let context = Secp256k1::signing_only();
441+
442+
let public = PublicKey::from_secret_key(&context, &secret);
429443
let public = Public(public.serialize());
430444
Ok(Pair { public, secret })
431445
}
@@ -503,7 +517,13 @@ impl Pair {
503517
/// Sign a pre-hashed message
504518
pub fn sign_prehashed(&self, message: &[u8; 32]) -> Signature {
505519
let message = Message::from_slice(message).expect("Message is 32 bytes; qed");
506-
SECP256K1.sign_ecdsa_recoverable(&message, &self.secret).into()
520+
521+
#[cfg(feature = "std")]
522+
let context = SECP256K1;
523+
#[cfg(not(feature = "std"))]
524+
let context = Secp256k1::signing_only();
525+
526+
context.sign_ecdsa_recoverable(&message, &self.secret).into()
507527
}
508528

509529
/// Verify a signature on a pre-hashed message. Return `true` if the signature is valid

0 commit comments

Comments
 (0)