pub fn verify(
    sig: &types::SignatureBytes,
    msg: &[u8],
    pk: &types::PublicKeyBytes,
) -> CryptoResult<()> {
    let (r, s) = r_s_from_sig_bytes(sig)?;
    let secp256k1_sig =
        EcdsaSig::from_private_components(r, s).map_err(|e| CryptoError::MalformedSignature {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            sig_bytes: sig.0.to_vec(),
            internal_error: e.to_string(),
        })?;
    let group = EcGroup::from_curve_name(CURVE_NAME)
        .map_err(|e| wrap_openssl_err(e, "unable to create EC group"))?;
    let mut ctx =
        BigNumContext::new().map_err(|e| wrap_openssl_err(e, "unable to create BigNumContext"))?;
    let point = EcPoint::from_bytes(&group, &pk.0, &mut ctx).map_err(|e| {
        CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            key_bytes: Some(pk.0.to_vec()),
            internal_error: e.to_string(),
        }
    })?;
    let ec_pk =
        EcKey::from_public_key(&group, &point).map_err(|e| CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            key_bytes: Some(pk.0.to_vec()),
            internal_error: e.to_string(),
        })?;
    let verified =
        secp256k1_sig
            .verify(msg, &ec_pk)
            .map_err(|e| CryptoError::SignatureVerification {
                algorithm: AlgorithmId::EcdsaSecp256k1,
                public_key_bytes: pk.0.to_vec(),
                sig_bytes: sig.0.to_vec(),
                internal_error: e.to_string(),
            })?;
    if verified {
        Ok(())
    } else {
        Err(CryptoError::SignatureVerification {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            public_key_bytes: pk.0.to_vec(),
            sig_bytes: sig.0.to_vec(),
            internal_error: "verification failed".to_string(),
        })
    }
}
/// Verify a signature using a secp256k1 public key
///
/// # Arguments
/// * `sig` is the signature to be verified
/// * `msg` is the message
/// * `pk` is the public key
/// # Errors
/// * `MalformedSignature` if the signature could not be parsed
/// * `AlgorithmNotSupported` if an error occurred while invoking OpenSSL
/// * `MalformedPublicKey` if the public key could not be parsed
/// * `SignatureVerification` if the signature could not be verified
/// # Returns
/// `Ok(())` if the signature validated, or an error otherwise
Verify a signature using a secp256k1 public key
Verify a signature using a secp256k1 public key
pub fn verify(
    sig: &types::SignatureBytes,
    msg: &[u8],
    pk: &types::PublicKeyBytes,
) -> CryptoResult<()> {
    let (r, s) = r_s_from_sig_bytes(sig)?;
    let secp256k1_sig =
        EcdsaSig::from_private_components(r, s).map_err(|e| CryptoError::MalformedSignature {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            sig_bytes: sig.0.to_vec(),
            internal_error: e.to_string(),
        })?;
    let group = EcGroup::from_curve_name(CURVE_NAME)
        .map_err(|e| wrap_openssl_err(e, "unable to create EC group"))?;
    let mut ctx =
        BigNumContext::new().map_err(|e| wrap_openssl_err(e, "unable to create BigNumContext"))?;
    let point = EcPoint::from_bytes(&group, &pk.0, &mut ctx).map_err(|e| {
        CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            key_bytes: Some(pk.0.to_vec()),
            internal_error: e.to_string(),
        }
    })?;
    let ec_pk =
        EcKey::from_public_key(&group, &point).map_err(|e| CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            key_bytes: Some(pk.0.to_vec()),
            internal_error: e.to_string(),
        })?;
    let verified =
        secp256k1_sig
            .verify(msg, &ec_pk)
            .map_err(|e| CryptoError::SignatureVerification {
                algorithm: AlgorithmId::EcdsaSecp256k1,
                public_key_bytes: pk.0.to_vec(),
                sig_bytes: sig.0.to_vec(),
                internal_error: e.to_string(),
            })?;
    if verified {
        Ok(())
    } else {
        Err(CryptoError::SignatureVerification {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            public_key_bytes: pk.0.to_vec(),
            sig_bytes: sig.0.to_vec(),
            internal_error: "verification failed".to_string(),
        })
    }
}
pub fn verify ( sig : & types :: SignatureBytes , msg : & [ u8 ] , pk : & types :: PublicKeyBytes , ) -> CryptoResult < ( ) > { let ( r , s ) = r_s_from_sig_bytes ( sig ) ? ; let secp256k1_sig = EcdsaSig :: from_private_components ( r , s ) . map_err ( | e | CryptoError :: MalformedSignature { algorithm : AlgorithmId :: EcdsaSecp256k1 , sig_bytes : sig . 0 . to_vec ( ) , internal_error : e . to_string ( ) , } ) ? ; let group = EcGroup :: from_curve_name ( CURVE_NAME ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let mut ctx = BigNumContext :: new ( ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let point = EcPoint :: from_bytes ( & group , & pk . 0 , & mut ctx ) . map_err ( | e | { CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaSecp256k1 , key_bytes : Some ( pk . 0 . to_vec ( ) ) , internal_error : e . to_string ( ) , } } ) ? ; let ec_pk = EcKey :: from_public_key ( & group , & point ) . map_err ( | e | CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaSecp256k1 , key_bytes : Some ( pk . 0 . to_vec ( ) ) , internal_error : e . to_string ( ) , } ) ? ; let verified = secp256k1_sig . verify ( msg , & ec_pk ) . map_err ( | e | CryptoError :: SignatureVerification { algorithm : AlgorithmId :: EcdsaSecp256k1 , public_key_bytes : pk . 0 . to_vec ( ) , sig_bytes : sig . 0 . to_vec ( ) , internal_error : e . to_string ( ) , } ) ? ; if verified { Ok ( ( ) ) } else { Err ( CryptoError :: SignatureVerification { algorithm : AlgorithmId :: EcdsaSecp256k1 , public_key_bytes : pk . 0 . to_vec ( ) , sig_bytes : sig . 0 . to_vec ( ) , internal_error : " " . to_string ( ) , } ) } }
Verify a signature using a secp256k1 public key
Verify a signature using a secp256k1 public key
sig
&types::SignatureBytes
msg
&[u8]
pk
&types::PublicKeyBytes
`Ok(())` if the signature validated, or an error otherwise
` Ok (( )) ` if the signature validated or an error otherwise
sig
&types::SignatureBytes
msg
&[u8]
pk
&types::PublicKeyBytes
errors
`MalformedSignature` if the signature could not be parsed
`AlgorithmNotSupported` if an error occurred while invoking OpenSSL
`MalformedPublicKey` if the public key could not be parsed
`SignatureVerification` if the signature could not be verified
` MalformedSignature ` if the signature could not be parsed ` AlgorithmNotSupported ` if an error occurred while invoking OpenSSL ` MalformedPublicKey ` if the public key could not be parsed ` SignatureVerification ` if the signature could not be verified