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

Commit

Permalink
Added v2 host function for ecdsa_verify
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-crypto committed Jul 20, 2021
1 parent c653284 commit 82fc9da
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
41 changes: 35 additions & 6 deletions primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,7 @@ impl From<(libsecp256k1::Signature, libsecp256k1::RecoveryId)> for Signature {
impl<'a> TryFrom<&'a Signature> for (libsecp256k1::Signature, libsecp256k1::RecoveryId) {
type Error = ();
fn try_from(x: &'a Signature) -> Result<(libsecp256k1::Signature, libsecp256k1::RecoveryId), Self::Error> {
Ok((
libsecp256k1::Signature::parse_overflowing_slice(&x.0[0..64]).expect("hardcoded to 64 bytes; qed"),
libsecp256k1::RecoveryId::parse(x.0[64]).map_err(|_| ())?,
))
parse_signature(&x.0).map_err(|_| ())
}
}

Expand Down Expand Up @@ -511,8 +508,10 @@ impl TraitPair for Pair {
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig: &[u8], message: M, pubkey: P) -> bool {
let message = libsecp256k1::Message::parse(&blake2_256(message.as_ref()));
if sig.len() != 65 { return false }
let ri = match libsecp256k1::RecoveryId::parse(sig[64]) { Ok(x) => x, _ => return false };
let sig = match libsecp256k1::Signature::parse_overflowing_slice(&sig[0..64]) { Ok(x) => x, _ => return false };
let (sig, ri) = match parse_signature(&sig) {
Ok(sigri) => sigri,
_ => return false,
};
match libsecp256k1::recover(&message, &sig, &ri) {
Ok(actual) => pubkey.as_ref() == &actual.serialize()[1..],
_ => false,
Expand Down Expand Up @@ -565,6 +564,36 @@ impl Pair {
_ => false,
}
}

/// Verify a signature on a message. Returns true if the signature is good.
/// Parses Signature using parse_overflowing_slice
pub fn verify_deprecated<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &Public) -> bool {
let message = libsecp256k1::Message::parse(&blake2_256(message.as_ref()));
let (sig, ri) = match parse_signature_deprecated(&sig.0) {
Ok(sigri) => sigri,
_ => return false
};
match libsecp256k1::recover(&message, &sig, &ri) {
Ok(actual) => pubkey.0[..] == actual.serialize_compressed()[..],
_ => false,
}
}
}

fn parse_signature(
x: &[u8],
) -> Result<(libsecp256k1::Signature, libsecp256k1::RecoveryId), libsecp256k1::Error> {
let sig = libsecp256k1::Signature::parse_standard_slice(&x[0..64])?;
let ri = libsecp256k1::RecoveryId::parse(x[64])?;
Ok((sig, ri))
}

fn parse_signature_deprecated(
x: &[u8],
) -> Result<(libsecp256k1::Signature, libsecp256k1::RecoveryId), libsecp256k1::Error> {
let sig = libsecp256k1::Signature::parse_overflowing_slice(&x[0..64])?;
let ri = libsecp256k1::RecoveryId::parse(x[64])?;
Ok((sig, ri))
}

impl CryptoType for Public {
Expand Down
12 changes: 12 additions & 0 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,18 @@ pub trait Crypto {
/// Verify `ecdsa` signature.
///
/// Returns `true` when the verification was successful.
fn ecdsa_verify(
sig: &ecdsa::Signature,
msg: &[u8],
pub_key: &ecdsa::Public,
) -> bool {
ecdsa::Pair::verify_deprecated(sig, msg, pub_key)
}

/// Verify `ecdsa` signature.
///
/// Returns `true` when the verification was successful.
#[version(2)]
fn ecdsa_verify(
sig: &ecdsa::Signature,
msg: &[u8],
Expand Down

0 comments on commit 82fc9da

Please sign in to comment.