Skip to content

Commit

Permalink
Enable use of global secp256k1 context (#1123)
Browse files Browse the repository at this point in the history
* Enable use of global secp256k1 context

Context creation is expensive.

Use the preallocated context that ships with the library.

* Cargo fmt

* Set `secp256k1` crate as optional

* `global-context` feature depends on `std`
* Its usage scope was already limited to `std` environment

* ink-engine requires `secp256k1` feature
  • Loading branch information
davxy committed Feb 10, 2022
1 parent 1188ee7 commit 88b12d6
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
3 changes: 2 additions & 1 deletion crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ 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"], optional = true }

[features]
default = ["std"]
std = [
"scale/std",
"secp256k1"
]
5 changes: 2 additions & 3 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl Engine {
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,8 +20,8 @@ use secp256k1::{
ecdsa::RecoverableSignature,
Message,
PublicKey,
Secp256k1,
SecretKey,
SECP256K1,
};

/// The public methods of the `contracts` pallet write their result into an
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
5 changes: 3 additions & 2 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ static_assertions = "1.1"
rlibc = "1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ink_engine = { version = "3.0.0-rc8", path = "../engine/", default-features = false, optional = true }
ink_engine = { version = "3.0.0-rc8", path = "../engine/", optional = true }

# Hashes for the off-chain environment.
sha2 = { version = "0.10", optional = true }
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"], optional = true }

# Only used in the off-chain environment.
#
Expand All @@ -60,6 +60,7 @@ std = [
"scale/std",
"scale-info",
"scale-info/std",
"secp256k1",
"rand",
"rand/std",
"rand/std_rng",
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

0 comments on commit 88b12d6

Please sign in to comment.