Added a new struct for the DER serialized signature

This commit is contained in:
Elichai Turkel 2019-04-11 21:47:20 +03:00
parent 312b9a55fc
commit dfcf74f9d0
No known key found for this signature in database
GPG Key ID: 5607C93B5F86650C
1 changed files with 77 additions and 5 deletions

View File

@ -156,6 +156,7 @@ pub mod key;
pub use key::SecretKey; pub use key::SecretKey;
pub use key::PublicKey; pub use key::PublicKey;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::ops::Deref;
/// A tag used for recovering the public key from a compact signature /// A tag used for recovering the public key from a compact signature
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
@ -165,6 +166,13 @@ pub struct RecoveryId(i32);
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub struct Signature(ffi::Signature); pub struct Signature(ffi::Signature);
/// A DER serialized Signature
#[derive(Copy, Clone)]
pub struct SerializedSignature {
data: [u8; 72],
len: usize,
}
impl fmt::Debug for Signature { impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f) fmt::Display::fmt(self, f)
@ -231,6 +239,40 @@ pub fn to_i32(self) -> i32 {
} }
} }
impl SerializedSignature {
/// Get a pointer to the underlying data with the specified capacity.
pub(crate) fn get_data_mut_ptr(&mut self) -> *mut u8 {
self.data.as_mut_ptr()
}
/// Get the capacity of the underlying data buffer.
pub fn capacity(&self) -> usize {
self.data.len()
}
/// Get the len of the used data.
pub fn len(&self) -> usize {
self.len
}
/// Set the length of the object.
pub(crate) fn set_len(&mut self, len: usize) {
self.len = len;
}
/// Convert the serialized signature into the Signature struct.
/// (This DER deserializes it)
pub fn to_signature(&self) -> Result<Signature, Error> {
Signature::from_der(&self)
}
/// Create a SerializedSignature from a Signature.
/// (this DER serializes it)
pub fn from_signature(sig: &Signature) -> SerializedSignature {
sig.serialize_der()
}
}
impl Signature { impl Signature {
#[inline] #[inline]
/// Converts a DER-encoded byte slice to a signature /// Converts a DER-encoded byte slice to a signature
@ -337,18 +379,18 @@ impl Signature {
#[inline] #[inline]
/// Serializes the signature in DER format /// Serializes the signature in DER format
pub fn serialize_der(&self) -> Vec<u8> { pub fn serialize_der(&self) -> SerializedSignature {
let mut ret = Vec::with_capacity(72); let mut ret = SerializedSignature::default();
let mut len: usize = ret.capacity() as usize; let mut len: usize = ret.capacity();
unsafe { unsafe {
let err = ffi::secp256k1_ecdsa_signature_serialize_der( let err = ffi::secp256k1_ecdsa_signature_serialize_der(
ffi::secp256k1_context_no_precomp, ffi::secp256k1_context_no_precomp,
ret.as_mut_ptr(), ret.get_data_mut_ptr(),
&mut len, &mut len,
self.as_ptr(), self.as_ptr(),
); );
debug_assert!(err == 1); debug_assert!(err == 1);
ret.set_len(len as usize); ret.set_len(len);
} }
ret ret
} }
@ -590,6 +632,36 @@ impl<C> PartialEq for Secp256k1<C> {
fn eq(&self, _other: &Secp256k1<C>) -> bool { true } fn eq(&self, _other: &Secp256k1<C>) -> bool { true }
} }
impl Default for SerializedSignature {
fn default() -> SerializedSignature {
SerializedSignature {
data: [0u8; 72],
len: 0,
}
}
}
impl PartialEq for SerializedSignature {
fn eq(&self, other: &SerializedSignature) -> bool {
&self.data[..self.len] == &other.data[..other.len]
}
}
impl AsRef<[u8]> for SerializedSignature {
fn as_ref(&self) -> &[u8] {
&self.data[..self.len]
}
}
impl Deref for SerializedSignature {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.data[..self.len]
}
}
impl Eq for SerializedSignature {}
impl<C> Eq for Secp256k1<C> { } impl<C> Eq for Secp256k1<C> { }
impl<C> Drop for Secp256k1<C> { impl<C> Drop for Secp256k1<C> {