Add `Display` impl to `Error`; cleanup `Result` mess

This commit is contained in:
Andrew Poelstra 2015-04-05 20:27:43 -05:00
parent ff29303da1
commit f6585616b1
3 changed files with 24 additions and 21 deletions

View File

@ -26,8 +26,7 @@ use crypto::hmac::Hmac;
use crypto::mac::Mac; use crypto::mac::Mac;
use super::init; use super::init;
use super::Result; use super::Error::{self, InvalidNonce, InvalidPublicKey, InvalidSecretKey, Unknown};
use super::Error::{InvalidNonce, InvalidPublicKey, InvalidSecretKey, Unknown};
use constants; use constants;
use ffi; use ffi;
@ -81,7 +80,7 @@ impl Nonce {
/// Converts a `NONCE_SIZE`-byte slice to a nonce /// Converts a `NONCE_SIZE`-byte slice to a nonce
#[inline] #[inline]
pub fn from_slice(data: &[u8]) -> Result<Nonce> { pub fn from_slice(data: &[u8]) -> Result<Nonce, Error> {
match data.len() { match data.len() {
constants::NONCE_SIZE => { constants::NONCE_SIZE => {
let mut ret = [0; constants::NONCE_SIZE]; let mut ret = [0; constants::NONCE_SIZE];
@ -171,7 +170,7 @@ impl SecretKey {
/// Converts a `SECRET_KEY_SIZE`-byte slice to a secret key /// Converts a `SECRET_KEY_SIZE`-byte slice to a secret key
#[inline] #[inline]
pub fn from_slice(data: &[u8]) -> Result<SecretKey> { pub fn from_slice(data: &[u8]) -> Result<SecretKey, Error> {
init(); init();
match data.len() { match data.len() {
constants::SECRET_KEY_SIZE => { constants::SECRET_KEY_SIZE => {
@ -195,7 +194,7 @@ impl SecretKey {
/// Marked `unsafe` since you must /// Marked `unsafe` since you must
/// call `init()` (or construct a `Secp256k1`, which does this for you) before /// call `init()` (or construct a `Secp256k1`, which does this for you) before
/// using this function /// using this function
pub fn add_assign(&mut self, other: &SecretKey) -> Result<()> { pub fn add_assign(&mut self, other: &SecretKey) -> Result<(), Error> {
init(); init();
unsafe { unsafe {
if ffi::secp256k1_ec_privkey_tweak_add(self.as_mut_ptr(), other.as_ptr()) != 1 { if ffi::secp256k1_ec_privkey_tweak_add(self.as_mut_ptr(), other.as_ptr()) != 1 {
@ -265,7 +264,7 @@ impl PublicKey {
/// Creates a public key directly from a slice /// Creates a public key directly from a slice
#[inline] #[inline]
pub fn from_slice(data: &[u8]) -> Result<PublicKey> { pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
match data.len() { match data.len() {
constants::COMPRESSED_PUBLIC_KEY_SIZE => { constants::COMPRESSED_PUBLIC_KEY_SIZE => {
let mut ret = [0; constants::COMPRESSED_PUBLIC_KEY_SIZE]; let mut ret = [0; constants::COMPRESSED_PUBLIC_KEY_SIZE];
@ -337,7 +336,7 @@ impl PublicKey {
#[inline] #[inline]
/// Adds the pk corresponding to `other` to the pk `self` in place /// Adds the pk corresponding to `other` to the pk `self` in place
pub fn add_exp_assign(&mut self, other: &SecretKey) -> Result<()> { pub fn add_exp_assign(&mut self, other: &SecretKey) -> Result<(), Error> {
init(); init();
unsafe { unsafe {
if ffi::secp256k1_ec_pubkey_tweak_add(self.as_mut_ptr(), if ffi::secp256k1_ec_pubkey_tweak_add(self.as_mut_ptr(),
@ -487,7 +486,7 @@ impl ops::Index<ops::RangeFull> for PublicKey {
} }
impl Decodable for PublicKey { impl Decodable for PublicKey {
fn decode<D: Decoder>(d: &mut D) -> ::std::result::Result<PublicKey, D::Error> { fn decode<D: Decoder>(d: &mut D) -> Result<PublicKey, D::Error> {
d.read_seq(|d, len| { d.read_seq(|d, len| {
if len == constants::UNCOMPRESSED_PUBLIC_KEY_SIZE { if len == constants::UNCOMPRESSED_PUBLIC_KEY_SIZE {
unsafe { unsafe {
@ -515,13 +514,13 @@ impl Decodable for PublicKey {
} }
impl Encodable for PublicKey { impl Encodable for PublicKey {
fn encode<S: Encoder>(&self, s: &mut S) -> ::std::result::Result<(), S::Error> { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
(&self[..]).encode(s) (&self[..]).encode(s)
} }
} }
impl fmt::Debug for SecretKey { impl fmt::Debug for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
(&self[..]).fmt(f) (&self[..]).fmt(f)
} }
} }

View File

@ -113,7 +113,7 @@ macro_rules! impl_array_newtype {
} }
impl ::serialize::Decodable for $thing { impl ::serialize::Decodable for $thing {
fn decode<D: ::serialize::Decoder>(d: &mut D) -> ::std::result::Result<$thing, D::Error> { fn decode<D: ::serialize::Decoder>(d: &mut D) -> Result<$thing, D::Error> {
use serialize::Decodable; use serialize::Decodable;
::assert_type_is_copy::<$ty>(); ::assert_type_is_copy::<$ty>();
@ -137,7 +137,7 @@ macro_rules! impl_array_newtype {
impl ::serialize::Encodable for $thing { impl ::serialize::Encodable for $thing {
fn encode<S: ::serialize::Encoder>(&self, s: &mut S) fn encode<S: ::serialize::Encoder>(&self, s: &mut S)
-> ::std::result::Result<(), S::Error> { -> Result<(), S::Error> {
self[..].encode(s) self[..].encode(s)
} }
} }

View File

@ -43,7 +43,7 @@ extern crate libc;
extern crate rand; extern crate rand;
use std::intrinsics::copy_nonoverlapping; use std::intrinsics::copy_nonoverlapping;
use std::{io, ops}; use std::{fmt, io, ops};
use std::sync::{Once, ONCE_INIT}; use std::sync::{Once, ONCE_INIT};
use libc::c_int; use libc::c_int;
use rand::{OsRng, Rng, SeedableRng}; use rand::{OsRng, Rng, SeedableRng};
@ -93,7 +93,7 @@ impl Signature {
/// Converts a byte slice to a signature /// Converts a byte slice to a signature
#[inline] #[inline]
pub fn from_slice(data: &[u8]) -> Result<Signature> { pub fn from_slice(data: &[u8]) -> Result<Signature, Error> {
if data.len() <= constants::MAX_SIGNATURE_SIZE { if data.len() <= constants::MAX_SIGNATURE_SIZE {
let mut ret = [0; constants::MAX_SIGNATURE_SIZE]; let mut ret = [0; constants::MAX_SIGNATURE_SIZE];
unsafe { unsafe {
@ -179,8 +179,12 @@ pub enum Error {
Unknown Unknown
} }
/// Result type // Passthrough Debug to Display, since errors should be user-visible
pub type Result<T> = ::std::result::Result<T, Error>; impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
fmt::Debug::fmt(self, f)
}
}
static mut Secp256k1_init: Once = ONCE_INIT; static mut Secp256k1_init: Once = ONCE_INIT;
@ -233,7 +237,7 @@ impl Secp256k1 {
/// Constructs a signature for `msg` using the secret key `sk` and nonce `nonce` /// Constructs a signature for `msg` using the secret key `sk` and nonce `nonce`
pub fn sign(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce) pub fn sign(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce)
-> Result<Signature> { -> Result<Signature, Error> {
let mut sig = [0; constants::MAX_SIGNATURE_SIZE]; let mut sig = [0; constants::MAX_SIGNATURE_SIZE];
let mut len = constants::MAX_SIGNATURE_SIZE as c_int; let mut len = constants::MAX_SIGNATURE_SIZE as c_int;
unsafe { unsafe {
@ -250,7 +254,7 @@ impl Secp256k1 {
/// Constructs a compact signature for `msg` using the secret key `sk` /// Constructs a compact signature for `msg` using the secret key `sk`
pub fn sign_compact(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce) pub fn sign_compact(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce)
-> Result<(Signature, RecoveryId)> { -> Result<(Signature, RecoveryId), Error> {
let mut sig = [0; constants::MAX_SIGNATURE_SIZE]; let mut sig = [0; constants::MAX_SIGNATURE_SIZE];
let mut recid = 0; let mut recid = 0;
unsafe { unsafe {
@ -267,7 +271,7 @@ impl Secp256k1 {
/// `msg`. Returns through the out-pointer `pubkey`. /// `msg`. Returns through the out-pointer `pubkey`.
pub fn recover_compact(&self, msg: &[u8], sig: &[u8], pub fn recover_compact(&self, msg: &[u8], sig: &[u8],
compressed: bool, recid: RecoveryId) compressed: bool, recid: RecoveryId)
-> Result<key::PublicKey> { -> Result<key::PublicKey, Error> {
let mut pk = key::PublicKey::new(compressed); let mut pk = key::PublicKey::new(compressed);
let RecoveryId(recid) = recid; let RecoveryId(recid) = recid;
@ -290,14 +294,14 @@ impl Secp256k1 {
/// there with zero-padded signatures that don't fit in the `Signature` type. /// there with zero-padded signatures that don't fit in the `Signature` type.
/// Use `verify_raw` instead. /// Use `verify_raw` instead.
#[inline] #[inline]
pub fn verify(msg: &[u8], sig: &Signature, pk: &key::PublicKey) -> Result<()> { pub fn verify(msg: &[u8], sig: &Signature, pk: &key::PublicKey) -> Result<(), Error> {
Secp256k1::verify_raw(msg, &sig[..], pk) Secp256k1::verify_raw(msg, &sig[..], pk)
} }
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public /// Checks that `sig` is a valid ECDSA signature for `msg` using the public
/// key `pubkey`. Returns `Ok(true)` on success. /// key `pubkey`. Returns `Ok(true)` on success.
#[inline] #[inline]
pub fn verify_raw(msg: &[u8], sig: &[u8], pk: &key::PublicKey) -> Result<()> { pub fn verify_raw(msg: &[u8], sig: &[u8], pk: &key::PublicKey) -> Result<(), Error> {
init(); // This is a static function, so we have to init init(); // This is a static function, so we have to init
let res = unsafe { let res = unsafe {
ffi::secp256k1_ecdsa_verify(msg.as_ptr(), msg.len() as c_int, ffi::secp256k1_ecdsa_verify(msg.as_ptr(), msg.len() as c_int,