Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Enable use of global secp256k1 context
Context creation is expensive.

Use the preallocated context that ships with the library.
  • Loading branch information
davxy committed Feb 7, 2022
commit d9c49d64718827909b684d22bea72e550d5e6e07
2 changes: 1 addition & 1 deletion crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sha3 = { version = "0.10" }
blake2 = { version = "0.10" }

# ECDSA for the off-chain environment.
secp256k1 = { version = "0.21.2", features = ["recovery"] }
secp256k1 = { version = "0.21.2", features = ["recovery", "global-context"] }

[features]
default = ["std"]
Expand Down
7 changes: 3 additions & 4 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ impl Engine {
use secp256k1::{
ecdsa::{
RecoverableSignature,
RecoveryId,
RecoveryId
},
Message,
Secp256k1,
SECP256K1,
};

// In most implementations, the v is just 0 or 1 internally, but 27 was added
Expand All @@ -414,8 +414,7 @@ impl Engine {
panic!("Unable to parse the signature: {}", error)
});

let secp = Secp256k1::new();
let pub_key = secp.recover_ecdsa(&message, &signature);
let pub_key = SECP256K1.recover_ecdsa(&message, &signature);
match pub_key {
Ok(pub_key) => {
*output = pub_key.serialize();
Expand Down
5 changes: 2 additions & 3 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use secp256k1::{
ecdsa::RecoverableSignature,
Message,
PublicKey,
Secp256k1,
SECP256K1,
SecretKey,
};

Expand Down Expand Up @@ -238,7 +238,6 @@ fn ecdsa_recovery_test_from_contracts_pallet() {
fn ecdsa_recovery_with_secp256k1_crate() {
// given
let mut engine = Engine::new();
let secp = Secp256k1::new();
let seckey = [
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203,
174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
Expand All @@ -255,7 +254,7 @@ fn ecdsa_recovery_with_secp256k1_crate() {
let msg = Message::from_slice(&msg_hash).expect("message creation failed");
let seckey = SecretKey::from_slice(&seckey).expect("secret key creation failed");
let recoverable_signature: RecoverableSignature =
secp.sign_ecdsa_recoverable(&msg, &seckey);
SECP256K1.sign_ecdsa_recoverable(&msg, &seckey);

let recovery_id = recoverable_signature.serialize_compact().0.to_i32() as u8;
let mut signature = recoverable_signature.serialize_compact().1.to_vec();
Expand Down
2 changes: 1 addition & 1 deletion crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sha3 = { version = "0.10", optional = true }
blake2 = { version = "0.10", optional = true }

# ECDSA for the off-chain environment.
secp256k1 = { version = "0.21.2", features = ["recovery"] }
secp256k1 = { version = "0.21.2", features = ["recovery", "global-context"] }

# Only used in the off-chain environment.
#
Expand Down
5 changes: 2 additions & 3 deletions crates/env/src/engine/experimental_off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl EnvBackend for EnvInstance {
RecoveryId,
},
Message,
Secp256k1,
SECP256K1,
};

// In most implementations, the v is just 0 or 1 internally, but 27 was added
Expand All @@ -278,8 +278,7 @@ impl EnvBackend for EnvInstance {
panic!("Unable to parse the signature: {}", error)
});

let secp = Secp256k1::new();
let pub_key = secp.recover_ecdsa(&message, &signature);
let pub_key = SECP256K1.recover_ecdsa(&message, &signature);
match pub_key {
Ok(pub_key) => {
*output = pub_key.serialize();
Expand Down
5 changes: 2 additions & 3 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl EnvBackend for EnvInstance {
RecoveryId,
},
Message,
Secp256k1,
SECP256K1,
};

// In most implementations, the v is just 0 or 1 internally, but 27 was added
Expand All @@ -224,8 +224,7 @@ impl EnvBackend for EnvInstance {
panic!("Unable to parse the signature: {}", error)
});

let secp = Secp256k1::new();
let pub_key = secp.recover_ecdsa(&message, &signature);
let pub_key = SECP256K1.recover_ecdsa(&message, &signature);
match pub_key {
Ok(pub_key) => {
*output = pub_key.serialize();
Expand Down