Add missing implementations; update FFI for libsecp256k1's new cloning fn

This commit is contained in:
Andrew Poelstra 2015-04-12 09:36:49 -05:00
parent edab2568d2
commit 9a01401746
2 changed files with 60 additions and 9 deletions

View File

@ -55,6 +55,8 @@ extern "C" {
pub fn secp256k1_context_create(flags: c_uint) -> Context; pub fn secp256k1_context_create(flags: c_uint) -> Context;
pub fn secp256k1_context_clone(cx: Context) -> Context;
pub fn secp256k1_context_destroy(cx: Context); pub fn secp256k1_context_destroy(cx: Context);
pub fn secp256k1_ecdsa_verify(cx: Context, msg32: *const c_uchar, pub fn secp256k1_ecdsa_verify(cx: Context, msg32: *const c_uchar,

View File

@ -44,7 +44,7 @@ extern crate libc;
extern crate rand; extern crate rand;
use std::intrinsics::copy_nonoverlapping; use std::intrinsics::copy_nonoverlapping;
use std::{fmt, io, ops, ptr}; use std::{cmp, fmt, io, ops, ptr};
use libc::c_int; use libc::c_int;
use rand::{OsRng, Rng, SeedableRng}; use rand::{OsRng, Rng, SeedableRng};
@ -105,13 +105,23 @@ impl Signature {
} }
} }
impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Signature("));
for i in self[..].iter().cloned() {
try!(write!(f, "{:02x}", i));
}
write!(f, ")")
}
}
impl ops::Index<usize> for Signature { impl ops::Index<usize> for Signature {
type Output = u8; type Output = u8;
#[inline] #[inline]
fn index(&self, index: usize) -> &u8 { fn index(&self, index: usize) -> &u8 {
let &Signature(_, ref dat) = self; assert!(index < self.0);
&dat[index] &self.1[index]
} }
} }
@ -120,8 +130,8 @@ impl ops::Index<ops::Range<usize>> for Signature {
#[inline] #[inline]
fn index(&self, index: ops::Range<usize>) -> &[u8] { fn index(&self, index: ops::Range<usize>) -> &[u8] {
let &Signature(_, ref dat) = self; assert!(index.end < self.0);
&dat[index.start..index.end] &self.1[index]
} }
} }
@ -130,8 +140,7 @@ impl ops::Index<ops::RangeFrom<usize>> for Signature {
#[inline] #[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] { fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
let &Signature(_, ref dat) = self; &self.1[index.start..self.0]
&dat[index.start..]
} }
} }
@ -140,11 +149,18 @@ impl ops::Index<ops::RangeFull> for Signature {
#[inline] #[inline]
fn index(&self, _: ops::RangeFull) -> &[u8] { fn index(&self, _: ops::RangeFull) -> &[u8] {
let &Signature(_, ref dat) = self; &self.1[0..self.0]
&dat[..]
} }
} }
impl cmp::PartialEq for Signature {
#[inline]
fn eq(&self, other: &Signature) -> bool {
&self[..] == &other[..]
}
}
impl cmp::Eq for Signature { }
impl Clone for Signature { impl Clone for Signature {
#[inline] #[inline]
fn clone(&self) -> Signature { fn clone(&self) -> Signature {
@ -182,6 +198,16 @@ impl Message {
} }
} }
impl fmt::Debug for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Message("));
for i in self[..].iter().cloned() {
try!(write!(f, "{:02x}", i));
}
write!(f, ")")
}
}
/// An ECDSA error /// An ECDSA error
#[derive(Copy, PartialEq, Eq, Clone, Debug)] #[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub enum Error { pub enum Error {
@ -214,6 +240,29 @@ pub struct Secp256k1<R = Fortuna> {
rng: R rng: R
} }
impl<R: Clone> Clone for Secp256k1<R> {
fn clone(&self) -> Secp256k1<R> {
Secp256k1 {
ctx: unsafe { ffi::secp256k1_context_clone(self.ctx) },
rng: self.rng.clone()
}
}
}
impl<R: PartialEq> PartialEq for Secp256k1<R> {
fn eq(&self, other: &Secp256k1<R>) -> bool {
// The contexts will always be "equal" in a functional sense
self.rng == other.rng
}
}
impl<R: Eq> Eq for Secp256k1<R> { }
impl<R: fmt::Debug> fmt::Debug for Secp256k1<R> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Secp256k1 {{ ctx: (secp256k1 context), rng: {:?} }}", self.rng)
}
}
impl<R> Drop for Secp256k1<R> { impl<R> Drop for Secp256k1<R> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::secp256k1_context_destroy(self.ctx); } unsafe { ffi::secp256k1_context_destroy(self.ctx); }