Merge rust-bitcoin/rust-bitcoin#1911: Add a verify function to PublicKey

e04ac1e743 Add a verify function to PublicKey (Tobin C. Harding)

Pull request description:

  Expose signature verification functionality for ECDSA signatures on the `PublicKey` type.

  We should have an identical function on `XOnlyPublicKey` but this will have to be done in `secp2561`: https://github.com/rust-bitcoin/rust-secp256k1/pull/618

  Idea from Kixunil: https://github.com/rust-bitcoin/rust-bitcoin/pull/1744#issuecomment-1534200841

ACKs for top commit:
  apoelstra:
    ACK e04ac1e743
  Kixunil:
    ACK e04ac1e743

Tree-SHA512: f26c223a1e5cc89e5c5fc12b22e621b9e8c395b8f91d7a58c6c938d45bc531e6682b178990b5a049718dbea66fff6d19d6fbcf926f142c781ad5213708ee7afa
This commit is contained in:
Andrew Poelstra 2023-06-23 15:31:40 +00:00
commit f7673d9ddb
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 11 additions and 0 deletions

View File

@ -16,6 +16,7 @@ use internals::write_err;
pub use secp256k1::rand; pub use secp256k1::rand;
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey};
use crate::crypto::ecdsa;
use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::hash_types::{PubkeyHash, WPubkeyHash};
use crate::network::constants::Network; use crate::network::constants::Network;
use crate::prelude::*; use crate::prelude::*;
@ -251,6 +252,16 @@ impl PublicKey {
) -> PublicKey { ) -> PublicKey {
sk.public_key(secp) sk.public_key(secp)
} }
/// Checks that `sig` is a valid ECDSA signature for `msg` using this public key.
pub fn verify<C: secp256k1::Verification>(
&self,
secp: &Secp256k1<C>,
msg: &secp256k1::Message,
sig: &ecdsa::Signature,
) -> Result<(), Error> {
Ok(secp.verify_ecdsa(msg, &sig.sig, &self.inner)?)
}
} }
impl From<PublicKey> for XOnlyPublicKey { impl From<PublicKey> for XOnlyPublicKey {