Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f54e1a2

Browse files
authored
Replace libsecp256k1 with k256 in FRAME related code (#10883)
* Replace libsecp256k1 with k256 in beefy-mmr * Port of FRAME `contracts` benchmarking from `libsecp256k1` to `k256` * Newtype to allow `Pcg32` rng usage with `k256` in contracts benchmarks * Use `sp-io::crypto` to generate dummy keys in `contracts` bechmarks * More compact code * Cargo fmt * Build `sp-keystore` only for dev profile * Move public key generation back to the `map`
1 parent 42b2d62 commit f54e1a2

File tree

7 files changed

+166
-34
lines changed

7 files changed

+166
-34
lines changed

Cargo.lock

Lines changed: 142 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ hash-db = { opt-level = 3 }
250250
hmac = { opt-level = 3 }
251251
httparse = { opt-level = 3 }
252252
integer-sqrt = { opt-level = 3 }
253+
k256 = { opt-level = 3 }
253254
keccak = { opt-level = 3 }
254255
libm = { opt-level = 3 }
255256
librocksdb-sys = { opt-level = 3 }

frame/beefy-mmr/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repository = "https://github.com/paritytech/substrate"
1010
[dependencies]
1111
hex = { version = "0.4", optional = true }
1212
codec = { version = "2.2.0", package = "parity-scale-codec", default-features = false, features = ["derive"] }
13-
libsecp256k1 = { version = "0.7.0", default-features = false }
13+
k256 = { version = "0.10.2", default-features = false, features = ["arithmetic"] }
1414
log = { version = "0.4.13", default-features = false }
1515
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
1616
serde = { version = "1.0.136", optional = true }
@@ -43,7 +43,7 @@ std = [
4343
"frame-support/std",
4444
"frame-system/std",
4545
"hex",
46-
"libsecp256k1/std",
46+
"k256/std",
4747
"log/std",
4848
"pallet-beefy/std",
4949
"pallet-mmr-primitives/std",

frame/beefy-mmr/src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,20 @@ where
7272
pub struct BeefyEcdsaToEthereum;
7373
impl Convert<beefy_primitives::crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToEthereum {
7474
fn convert(a: beefy_primitives::crypto::AuthorityId) -> Vec<u8> {
75+
use k256::{elliptic_curve::sec1::ToEncodedPoint, PublicKey};
7576
use sp_core::crypto::ByteArray;
76-
let compressed_key = a.as_slice();
77-
78-
libsecp256k1::PublicKey::parse_slice(
79-
compressed_key,
80-
Some(libsecp256k1::PublicKeyFormat::Compressed),
81-
)
82-
// uncompress the key
83-
.map(|pub_key| pub_key.serialize().to_vec())
84-
// now convert to ETH address
85-
.map(|uncompressed| sp_io::hashing::keccak_256(&uncompressed[1..])[12..].to_vec())
86-
.map_err(|_| {
87-
log::error!(target: "runtime::beefy", "Invalid BEEFY PublicKey format!");
88-
})
89-
.unwrap_or_default()
77+
78+
PublicKey::from_sec1_bytes(a.as_slice())
79+
.map(|pub_key| {
80+
// uncompress the key
81+
let uncompressed = pub_key.to_encoded_point(false);
82+
// convert to ETH address
83+
sp_io::hashing::keccak_256(&uncompressed.as_bytes()[1..])[12..].to_vec()
84+
})
85+
.map_err(|_| {
86+
log::error!(target: "runtime::beefy", "Invalid BEEFY PublicKey format!");
87+
})
88+
.unwrap_or_default()
9089
}
9190
}
9291

frame/contracts/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ smallvec = { version = "1", default-features = false, features = [
2828
wasmi-validation = { version = "0.4", default-features = false }
2929

3030
# Only used in benchmarking to generate random contract code
31-
libsecp256k1 = { version = "0.7", optional = true, default-features = false, features = ["hmac", "static-context"] }
3231
rand = { version = "0.8", optional = true, default-features = false }
3332
rand_pcg = { version = "0.3", optional = true }
3433

@@ -56,6 +55,7 @@ pallet-balances = { version = "4.0.0-dev", path = "../balances" }
5655
pallet-timestamp = { version = "4.0.0-dev", path = "../timestamp" }
5756
pallet-randomness-collective-flip = { version = "4.0.0-dev", path = "../randomness-collective-flip" }
5857
pallet-utility = { version = "4.0.0-dev", path = "../utility" }
58+
sp-keystore = { version = "0.11.0", path = "../../primitives/keystore" }
5959

6060
[features]
6161
default = ["std"]
@@ -77,11 +77,9 @@ std = [
7777
"pallet-contracts-proc-macro/full",
7878
"log/std",
7979
"rand/std",
80-
"libsecp256k1/std",
8180
]
8281
runtime-benchmarks = [
8382
"frame-benchmarking/runtime-benchmarks",
84-
"libsecp256k1",
8583
"rand",
8684
"rand_pcg",
8785
"unstable-interface",

frame/contracts/src/benchmarking/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,20 +1866,14 @@ benchmarks! {
18661866
// It generates different private keys and signatures for the message "Hello world".
18671867
seal_ecdsa_recover {
18681868
let r in 0 .. API_BENCHMARK_BATCHES;
1869-
use rand::SeedableRng;
1870-
let mut rng = rand_pcg::Pcg32::seed_from_u64(123456);
18711869

18721870
let message_hash = sp_io::hashing::blake2_256("Hello world".as_bytes());
1871+
let key_type = sp_core::crypto::KeyTypeId(*b"code");
18731872
let signatures = (0..r * API_BENCHMARK_BATCH_SIZE)
18741873
.map(|i| {
1875-
use libsecp256k1::{SecretKey, Message, sign};
1876-
1877-
let private_key = SecretKey::random(&mut rng);
1878-
let (signature, recovery_id) = sign(&Message::parse(&message_hash), &private_key);
1879-
let mut full_signature = [0; 65];
1880-
full_signature[..64].copy_from_slice(&signature.serialize());
1881-
full_signature[64] = recovery_id.serialize();
1882-
full_signature
1874+
let pub_key = sp_io::crypto::ecdsa_generate(key_type, None);
1875+
let sig = sp_io::crypto::ecdsa_sign_prehashed(key_type, &pub_key, &message_hash).expect("Generates signature");
1876+
AsRef::<[u8; 65]>::as_ref(&sig).to_vec()
18831877
})
18841878
.collect::<Vec<_>>();
18851879
let signatures = signatures.iter().flatten().cloned().collect::<Vec<_>>();

0 commit comments

Comments
 (0)