Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update libsecp256k1 requirement from 0.3.5 to 0.6.0 #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Prev Previous commit
Bump libsecp256k1 to 0.6.0
  • Loading branch information
hacpy committed Aug 25, 2021
commit ea40549a85407b2904971bffb3159f78bef2ae4e
2 changes: 1 addition & 1 deletion keys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ std = [
bs58 = { version = "0.4", default-features = false, features = ["alloc"] }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
hex = { version = "0.4", default-features = false }
libsecp256k1 = { version = "0.6.0", default-features = false, features = ["hmac"] }
libsecp256k1 = { version = "0.6.0", default-features = false, features = ["hmac", "static-context"] }
serde = { version = "1.0", features = ["derive"], optional = true }

light-bitcoin-crypto = { path = "../crypto", default-features = false }
12 changes: 6 additions & 6 deletions keys/src/error.rs
Original file line number Diff line number Diff line change
@@ -32,13 +32,13 @@ impl core::fmt::Display for Error {
}
}

impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Self {
impl From<libsecp256k1::Error> for Error {
fn from(e: libsecp256k1::Error) -> Self {
match e {
secp256k1::Error::InvalidSignature => Error::InvalidSignature,
secp256k1::Error::InvalidPublicKey => Error::InvalidPublic,
secp256k1::Error::InvalidSecretKey => Error::InvalidSecret,
secp256k1::Error::InvalidMessage => Error::InvalidMessage,
libsecp256k1::Error::InvalidSignature => Error::InvalidSignature,
libsecp256k1::Error::InvalidPublicKey => Error::InvalidPublic,
libsecp256k1::Error::InvalidSecretKey => Error::InvalidSecret,
libsecp256k1::Error::InvalidMessage => Error::InvalidMessage,
_ => Error::InvalidSignature,
}
}
10 changes: 5 additions & 5 deletions keys/src/keypair.rs
Original file line number Diff line number Diff line change
@@ -32,8 +32,8 @@ impl KeyPair {
}

pub fn from_private(private: Private) -> Result<KeyPair, Error> {
let secret_key = secp256k1::SecretKey::parse(private.secret.as_fixed_bytes())?;
let pub_key = secp256k1::PublicKey::from_secret_key(&secret_key);
let secret_key = libsecp256k1::SecretKey::parse(private.secret.as_fixed_bytes())?;
let pub_key = libsecp256k1::PublicKey::from_secret_key(&secret_key);
let public = if private.compressed {
let public = H264::from_slice(&pub_key.serialize_compressed());
Public::Compressed(public)
@@ -46,11 +46,11 @@ impl KeyPair {
}

pub fn from_keypair(
sec: secp256k1::SecretKey,
public: secp256k1::PublicKey,
sec: libsecp256k1::SecretKey,
public: libsecp256k1::PublicKey,
network: Network,
) -> Self {
let sec: secp256k1::curve::Scalar = sec.into();
let sec: libsecp256k1::curve::Scalar = sec.into();
let sec = sec.b32();
let secret = Secret::from_slice(&sec[..]);
let serialized = public.serialize();
12 changes: 6 additions & 6 deletions keys/src/private.rs
Original file line number Diff line number Diff line change
@@ -43,16 +43,16 @@ impl str::FromStr for Private {

impl Private {
pub fn sign(&self, message: &Message) -> Result<Signature, Error> {
let secret = secp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = secp256k1::Message::parse(message.as_fixed_bytes());
let (signature, _recovery_id) = secp256k1::sign(&message, &secret);
let secret = libsecp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
let (signature, _recovery_id) = libsecp256k1::sign(&message, &secret);
Ok(signature.serialize_der().as_ref().to_vec().into())
}

pub fn sign_compact(&self, message: &Message) -> Result<CompactSignature, Error> {
let secret = secp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = secp256k1::Message::parse(message.as_fixed_bytes());
let (signature, recovery_id) = secp256k1::sign(&message, &secret);
let secret = libsecp256k1::SecretKey::parse(self.secret.as_fixed_bytes())?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
let (signature, recovery_id) = libsecp256k1::sign(&message, &secret);
let recovery_id = recovery_id.serialize();
let data = signature.serialize();

28 changes: 14 additions & 14 deletions keys/src/public.rs
Original file line number Diff line number Diff line change
@@ -73,37 +73,37 @@ impl Public {

pub fn verify(&self, message: &Message, signature: &Signature) -> Result<bool, Error> {
let public = match self {
Public::Normal(pubkey) => secp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Normal(pubkey) => libsecp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Compressed(pubkey) => {
secp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
libsecp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
}
};
let mut signature = secp256k1::Signature::parse_der_lax(&**signature)?;
let mut signature = libsecp256k1::Signature::parse_der_lax(&**signature)?;
signature.normalize_s();
let message = secp256k1::Message::parse(message.as_fixed_bytes());
Ok(secp256k1::verify(&message, &signature, &public))
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
Ok(libsecp256k1::verify(&message, &signature, &public))
}

pub fn verify_compact(&self, message: &Message, signature: &[u8; 64]) -> Result<bool, Error> {
let public = match self {
Public::Normal(pubkey) => secp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Normal(pubkey) => libsecp256k1::PublicKey::parse(pubkey.as_fixed_bytes())?,
Public::Compressed(pubkey) => {
secp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
libsecp256k1::PublicKey::parse_compressed(pubkey.as_fixed_bytes())?
}
};
let signature = secp256k1::Signature::parse(signature);
let message = secp256k1::Message::parse(message.as_fixed_bytes());
Ok(secp256k1::verify(&message, &signature, &public))
let signature = libsecp256k1::Signature::parse_standard(signature)?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
Ok(libsecp256k1::verify(&message, &signature, &public))
}

pub fn recover_compact(message: &Message, signature: &CompactSignature) -> Result<Self, Error> {
let recovery_id = (signature[0] - 27) & 3;
let compressed = (signature[0] - 27) & 4 != 0;
let recovery_id = secp256k1::RecoveryId::parse(recovery_id)?;
let recovery_id = libsecp256k1::RecoveryId::parse(recovery_id)?;
let sign = H512::from_slice(&signature[1..65]);
let signature = secp256k1::Signature::parse(sign.as_fixed_bytes());
let message = secp256k1::Message::parse(message.as_fixed_bytes());
let pub_key = secp256k1::recover(&message, &signature, &recovery_id)?;
let signature = libsecp256k1::Signature::parse_standard(sign.as_fixed_bytes())?;
let message = libsecp256k1::Message::parse(message.as_fixed_bytes());
let pub_key = libsecp256k1::recover(&message, &signature, &recovery_id)?;

let public = if compressed {
let public = H264::from_slice(&pub_key.serialize_compressed());