Skip to content

Commit

Permalink
build(deps): Update libsecp256k1 requirement from 0.3.1 to 0.5.0 (#2074)
Browse files Browse the repository at this point in the history
* build(deps): Update libsecp256k1 requirement from 0.3.1 to 0.5.0

Updates the requirements on [libsecp256k1](https://github.com/paritytech/libsecp256k1) to permit the latest version.
- [Release notes](https://github.com/paritytech/libsecp256k1/releases)
- [Changelog](https://github.com/paritytech/libsecp256k1/blob/master/CHANGELOG.md)
- [Commits](https://github.com/paritytech/libsecp256k1/commits)

Signed-off-by: dependabot[bot] <[email protected]>

* core/identity/scp256k1: Use libsecp256k1::SecretKey::random directly

* core/: Update changelog and cargo toml

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Max Inden <[email protected]>
  • Loading branch information
dependabot[bot] and mxinden authored May 21, 2021
1 parent d9f1c71 commit bf0cdbb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.28.4 [unreleased]

- Update dependencies.

# 0.28.3 [2021-04-26]

- Fix build with secp256k1 disabled [PR 2057](https://github.com/libp2p/rust-libp2p/pull/2057].
Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "libp2p-core"
edition = "2018"
description = "Core traits and structs of libp2p"
version = "0.28.3"
version = "0.28.4"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand All @@ -18,7 +18,7 @@ fnv = "1.0"
futures = { version = "0.3.1", features = ["executor", "thread-pool"] }
futures-timer = "3"
lazy_static = "1.2"
libsecp256k1 = { version = "0.3.1", optional = true }
libsecp256k1 = { version = "0.5.0", optional = true }
log = "0.4"
multiaddr = { package = "parity-multiaddr", version = "0.11.2", path = "../misc/multiaddr" }
multihash = { version = "0.13", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] }
Expand Down
28 changes: 9 additions & 19 deletions core/src/identity/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
//! Secp256k1 keys.

use asn1_der::typed::{DerDecodable, Sequence};
use rand::RngCore;
use sha2::{Digest as ShaDigestTrait, Sha256};
use secp256k1::{Message, Signature};
use libsecp256k1::{Message, Signature};
use super::error::{DecodingError, SigningError};
use zeroize::Zeroize;
use core::fmt;
Expand Down Expand Up @@ -61,7 +60,7 @@ impl fmt::Debug for Keypair {
/// Promote a Secp256k1 secret key into a keypair.
impl From<SecretKey> for Keypair {
fn from(secret: SecretKey) -> Keypair {
let public = PublicKey(secp256k1::PublicKey::from_secret_key(&secret.0));
let public = PublicKey(libsecp256k1::PublicKey::from_secret_key(&secret.0));
Keypair { secret, public }
}
}
Expand All @@ -75,7 +74,7 @@ impl From<Keypair> for SecretKey {

/// A Secp256k1 secret key.
#[derive(Clone)]
pub struct SecretKey(secp256k1::SecretKey);
pub struct SecretKey(libsecp256k1::SecretKey);

impl fmt::Debug for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -86,24 +85,15 @@ impl fmt::Debug for SecretKey {
impl SecretKey {
/// Generate a new Secp256k1 secret key.
pub fn generate() -> SecretKey {
let mut r = rand::thread_rng();
let mut b = [0; secp256k1::util::SECRET_KEY_SIZE];
// This is how it is done in `secp256k1::SecretKey::random` which
// we do not use here because it uses `rand::Rng` from rand-0.4.
loop {
r.fill_bytes(&mut b);
if let Ok(k) = secp256k1::SecretKey::parse(&b) {
return SecretKey(k)
}
}
SecretKey(libsecp256k1::SecretKey::random(&mut rand::thread_rng()))
}

/// Create a secret key from a byte slice, zeroing the slice on success.
/// If the bytes do not constitute a valid Secp256k1 secret key, an
/// error is returned.
pub fn from_bytes(mut sk: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> {
let sk_bytes = sk.as_mut();
let secret = secp256k1::SecretKey::parse_slice(&*sk_bytes)
let secret = libsecp256k1::SecretKey::parse_slice(&*sk_bytes)
.map_err(|_| DecodingError::new("failed to parse secp256k1 secret key"))?;
sk_bytes.zeroize();
Ok(SecretKey(secret))
Expand Down Expand Up @@ -146,13 +136,13 @@ impl SecretKey {
pub fn sign_hash(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
let m = Message::parse_slice(msg)
.map_err(|_| SigningError::new("failed to parse secp256k1 digest"))?;
Ok(secp256k1::sign(&m, &self.0).0.serialize_der().as_ref().into())
Ok(libsecp256k1::sign(&m, &self.0).0.serialize_der().as_ref().into())
}
}

/// A Secp256k1 public key.
#[derive(PartialEq, Eq, Clone)]
pub struct PublicKey(secp256k1::PublicKey);
pub struct PublicKey(libsecp256k1::PublicKey);

impl fmt::Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -173,7 +163,7 @@ impl PublicKey {
/// Verify the Secp256k1 DER-encoded signature on a raw 256-bit message using the public key.
pub fn verify_hash(&self, msg: &[u8], sig: &[u8]) -> bool {
Message::parse_slice(msg)
.and_then(|m| Signature::parse_der(sig).map(|s| secp256k1::verify(&m, &s, &self.0)))
.and_then(|m| Signature::parse_der(sig).map(|s| libsecp256k1::verify(&m, &s, &self.0)))
.unwrap_or(false)
}

Expand All @@ -191,7 +181,7 @@ impl PublicKey {
/// Decode a public key from a byte slice in the the format produced
/// by `encode`.
pub fn decode(k: &[u8]) -> Result<PublicKey, DecodingError> {
secp256k1::PublicKey::parse_slice(k, Some(secp256k1::PublicKeyFormat::Compressed))
libsecp256k1::PublicKey::parse_slice(k, Some(libsecp256k1::PublicKeyFormat::Compressed))
.map_err(|_| DecodingError::new("failed to parse secp256k1 public key"))
.map(PublicKey)
}
Expand Down

0 comments on commit bf0cdbb

Please sign in to comment.