Merge pull request #37 from rust-bitcoin/2018-07-public-key
remove `PublicKey::new()` and `PublicKey::is_valid()`
This commit is contained in:
commit
54ddbc74a4
14
src/key.rs
14
src/key.rs
|
@ -122,20 +122,6 @@ impl SecretKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PublicKey {
|
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
|
/// Obtains a raw pointer suitable for use with FFI functions
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_ptr(&self) -> *const ffi::PublicKey {
|
pub fn as_ptr(&self) -> *const ffi::PublicKey {
|
||||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -554,17 +554,15 @@ impl<C: Verification> Secp256k1<C> {
|
||||||
/// verify-capable context.
|
/// verify-capable context.
|
||||||
#[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 {
|
||||||
if !pk.is_valid() {
|
if ffi::secp256k1_ecdsa_verify(self.ctx, sig.as_ptr(), msg.as_ptr(), pk.as_ptr()) == 0 {
|
||||||
Err(Error::InvalidPublicKey)
|
|
||||||
} else if unsafe { ffi::secp256k1_ecdsa_verify(self.ctx, sig.as_ptr(), msg.as_ptr(),
|
|
||||||
pk.as_ptr()) } == 0 {
|
|
||||||
Err(Error::IncorrectSignature)
|
Err(Error::IncorrectSignature)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -573,7 +571,7 @@ mod tests {
|
||||||
use key::{SecretKey, PublicKey};
|
use key::{SecretKey, PublicKey};
|
||||||
use super::constants;
|
use super::constants;
|
||||||
use super::{Secp256k1, Signature, RecoverableSignature, Message, RecoveryId};
|
use super::{Secp256k1, Signature, RecoverableSignature, Message, RecoveryId};
|
||||||
use super::Error::{InvalidMessage, InvalidPublicKey, IncorrectSignature, InvalidSignature};
|
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature};
|
||||||
|
|
||||||
macro_rules! hex {
|
macro_rules! hex {
|
||||||
($hex:expr) => {
|
($hex:expr) => {
|
||||||
|
@ -644,18 +642,6 @@ mod tests {
|
||||||
assert_eq!(one, one.clone());
|
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]
|
#[test]
|
||||||
fn sign() {
|
fn sign() {
|
||||||
let mut s = Secp256k1::new();
|
let mut s = Secp256k1::new();
|
||||||
|
|
Loading…
Reference in New Issue