Merge pull request #212 from fanatid/verify-doctest

Add doctest to Secp256k1::verify
This commit is contained in:
Andrew Poelstra 2020-05-13 19:00:50 +00:00 committed by GitHub
commit a1842125a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

View File

@ -667,6 +667,29 @@ impl<C: Verification> Secp256k1<C> {
/// be used for Bitcoin consensus checking since there may exist signatures /// be used for Bitcoin consensus checking since there may exist signatures
/// which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a /// which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a
/// verify-capable context. /// verify-capable context.
///
/// ```rust
/// # extern crate secp256k1;
/// # #[cfg(feature="rand")]
/// # extern crate rand;
/// #
/// # fn main() {
/// # #[cfg(feature="rand")] {
/// # use rand::OsRng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// #
/// # let secp = Secp256k1::new();
/// # let mut rng = OsRng::new().expect("OsRng");
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rng);
/// #
/// let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
/// let sig = secp.sign(&message, &secret_key);
/// assert_eq!(secp.verify(&message, &sig, &public_key), Ok(()));
///
/// let message = Message::from_slice(&[0xcd; 32]).expect("32 bytes");
/// assert_eq!(secp.verify(&message, &sig, &public_key), Err(Error::IncorrectSignature));
/// # } }
/// ```
#[inline] #[inline]
pub fn verify(&self, msg: &Message, sig: &Signature, pk: &key::PublicKey) -> Result<(), Error> { pub fn verify(&self, msg: &Message, sig: &Signature, pk: &key::PublicKey) -> Result<(), Error> {
unsafe { unsafe {