Merge pull request #37 from rust-bitcoin/2018-07-public-key

remove `PublicKey::new()` and `PublicKey::is_valid()`
This commit is contained in:
Andrew Poelstra 2018-07-25 13:19:10 +00:00 committed by GitHub
commit 54ddbc74a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 35 deletions

View File

@ -122,20 +122,6 @@ impl SecretKey {
}
impl PublicKey {
/// Creates a new zeroed out public key
#[inline]
pub fn new() -> PublicKey {
PublicKey(ffi::PublicKey::new())
}
/// Determines whether a pubkey is valid
#[inline]
pub fn is_valid(&self) -> bool {
// The only invalid pubkey the API should be able to create is
// the zero one.
self.0[..].iter().any(|&x| x != 0)
}
/// Obtains a raw pointer suitable for use with FFI functions
#[inline]
pub fn as_ptr(&self) -> *const ffi::PublicKey {

View File

@ -554,16 +554,14 @@ impl<C: Verification> Secp256k1<C> {
/// verify-capable context.
#[inline]
pub fn verify(&self, msg: &Message, sig: &Signature, pk: &key::PublicKey) -> Result<(), Error> {
if !pk.is_valid() {
Err(Error::InvalidPublicKey)
} else if unsafe { ffi::secp256k1_ecdsa_verify(self.ctx, sig.as_ptr(), msg.as_ptr(),
pk.as_ptr()) } == 0 {
unsafe {
if ffi::secp256k1_ecdsa_verify(self.ctx, sig.as_ptr(), msg.as_ptr(), pk.as_ptr()) == 0 {
Err(Error::IncorrectSignature)
} else {
Ok(())
}
}
}
}
#[cfg(test)]
@ -573,7 +571,7 @@ mod tests {
use key::{SecretKey, PublicKey};
use super::constants;
use super::{Secp256k1, Signature, RecoverableSignature, Message, RecoveryId};
use super::Error::{InvalidMessage, InvalidPublicKey, IncorrectSignature, InvalidSignature};
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature};
macro_rules! hex {
($hex:expr) => {
@ -644,18 +642,6 @@ mod tests {
assert_eq!(one, one.clone());
}
#[test]
fn invalid_pubkey() {
let s = Secp256k1::new();
let sig = RecoverableSignature::from_compact(&s, &[1; 64], RecoveryId(0)).unwrap();
let pk = PublicKey::new();
let mut msg = [0u8; 32];
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
assert_eq!(s.verify(&msg, &sig.to_standard(&s), &pk), Err(InvalidPublicKey));
}
#[test]
fn sign() {
let mut s = Secp256k1::new();