Skip to content

Commit

Permalink
Merge rust-bitcoin#618: Add a verify function to PublicKey
Browse files Browse the repository at this point in the history
d60b891 Add a verify function to PublicKey (Tobin C. Harding)

Pull request description:

  Expose signature verification functionality for schnorr signatures on the `XOnlyPublicKey` type.

  Idea from Kixunil: rust-bitcoin/rust-bitcoin#1744 (comment)

ACKs for top commit:
  apoelstra:
    ACK d60b891

Tree-SHA512: 2ffa3de528b857c5b0a402815b71b35da913c668bea53b63801705fa6a86eb6d44766aa2395c02f67a4712b451c77caf627af9450183ae70957abf246a63c279
  • Loading branch information
apoelstra committed Jul 9, 2023
2 parents 7c8270a + d60b891 commit 408c124
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ use serde::ser::SerializeTuple;

use crate::ffi::types::c_uint;
use crate::ffi::{self, CPtr};
#[cfg(all(feature = "global-context", feature = "rand-std"))]
use crate::schnorr;
use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
use crate::{constants, from_hex, Scalar, Secp256k1, Signing, Verification};
use crate::{constants, from_hex, schnorr, Message, Scalar, Secp256k1, Signing, Verification};
#[cfg(feature = "global-context")]
use crate::{ecdsa, Message, SECP256K1};
use crate::{ecdsa, SECP256K1};
#[cfg(feature = "bitcoin_hashes")]
use crate::{hashes, ThirtyTwoByteHash};

Expand Down Expand Up @@ -1316,6 +1314,16 @@ impl XOnlyPublicKey {
pub fn public_key(&self, parity: Parity) -> PublicKey {
PublicKey::from_x_only_public_key(*self, parity)
}

/// Checks that `sig` is a valid schnorr signature for `msg` using this public key.
pub fn verify<C: Verification>(
&self,
secp: &Secp256k1<C>,
msg: &Message,
sig: &schnorr::Signature,
) -> Result<(), Error> {
secp.verify_schnorr(sig, msg, self)
}
}

/// Represents the parity passed between FFI function calls.
Expand Down

0 comments on commit 408c124

Please sign in to comment.