diff --git a/src/key.rs b/src/key.rs index 1e2aa09..b8c9740 100644 --- a/src/key.rs +++ b/src/key.rs @@ -18,6 +18,7 @@ use std::intrinsics::copy_nonoverlapping; use std::cmp; use std::fmt; +use std::ops; use rand::Rng; use serialize::{Decoder, Decodable, Encoder, Encodable}; @@ -106,8 +107,8 @@ impl Nonce { macro_rules! hmac { ($res:expr; key $key:expr, data $($data:expr),+) => ({ - let mut hmacker = Hmac::new(Sha512::new(), $key.as_slice()); - $(hmacker.input($data.as_slice());)+ + let mut hmacker = Hmac::new(Sha512::new(), &$key[..]); + $(hmacker.input(&$data[..]);)+ hmacker.raw_result($res.as_mut_slice()); }) } @@ -118,7 +119,7 @@ impl Nonce { hasher.input(msg); let mut x = [0; HMAC_SIZE]; hasher.result(x.as_mut_slice()); - let msg_hash = bits2octets(x.as_slice()); + let msg_hash = bits2octets(&x); // Section 3.2b let mut V = [0x01u8; HMAC_SIZE]; @@ -144,7 +145,7 @@ impl Nonce { let mut T = [0x00u8; HMAC_SIZE]; hmac!(T; key K, data V); - k = Nonce::from_slice(T.slice_to(constants::NONCE_SIZE)); + k = Nonce::from_slice(&T[..constants::NONCE_SIZE]); // Replace K, V if k.is_err() { @@ -315,13 +316,6 @@ impl PublicKey { } } - /// Converts the public key into a byte slice - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [u8] { - let &PublicKey(ref data) = self; - data.as_slice() - } - /// Converts the public key to a raw pointer suitable for use /// with the FFI functions #[inline] @@ -360,21 +354,11 @@ impl PublicKey { } } -impl PublicKeyData { - #[inline] - fn as_slice<'a>(&'a self) -> &'a [u8] { - match *self { - PublicKeyData::Compressed(ref x) => x.as_slice(), - PublicKeyData::Uncompressed(ref x) => x.as_slice() - } - } -} - // We have to do all these impls ourselves as Rust can't derive // them for arrays impl fmt::Debug for Nonce { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_slice().fmt(f) + (&self[..]).fmt(f) } } @@ -384,7 +368,7 @@ impl Clone for PublicKeyData { impl PartialEq for PublicKeyData { fn eq(&self, other: &PublicKeyData) -> bool { - self.as_slice() == other.as_slice() + &self[..] == &other[..] } } @@ -392,7 +376,118 @@ impl Eq for PublicKeyData {} impl fmt::Debug for PublicKeyData { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_slice().fmt(f) + (&self[..]).fmt(f) + } +} + + +impl ops::Index for PublicKeyData { + type Output = u8; + + #[inline] + fn index(&self, index: usize) -> &u8 { + match *self { + PublicKeyData::Compressed(ref x) => &x[index], + PublicKeyData::Uncompressed(ref x) => &x[index] + } + } +} + +impl ops::Index for PublicKey { + type Output = u8; + + #[inline] + fn index(&self, index: usize) -> &u8 { + let &PublicKey(ref dat) = self; + &dat[index] + } +} + +impl ops::Index> for PublicKeyData { + type Output = [u8]; + + #[inline] + fn index(&self, index: ops::Range) -> &[u8] { + match *self { + PublicKeyData::Compressed(ref x) => &x[index.start..index.end], + PublicKeyData::Uncompressed(ref x) => &x[index.start..index.end] + } + } +} + +impl ops::Index> for PublicKey { + type Output = [u8]; + + #[inline] + fn index(&self, index: ops::Range) -> &[u8] { + let &PublicKey(ref dat) = self; + &dat[index.start..index.end] + } +} + +impl ops::Index> for PublicKeyData { + type Output = [u8]; + + #[inline] + fn index(&self, index: ops::RangeTo) -> &[u8] { + match *self { + PublicKeyData::Compressed(ref x) => &x[..index.end], + PublicKeyData::Uncompressed(ref x) => &x[..index.end] + } + } +} + +impl ops::Index> for PublicKey { + type Output = [u8]; + + #[inline] + fn index(&self, index: ops::RangeTo) -> &[u8] { + let &PublicKey(ref dat) = self; + &dat[..index.end] + } +} + +impl ops::Index> for PublicKeyData { + type Output = [u8]; + + #[inline] + fn index(&self, index: ops::RangeFrom) -> &[u8] { + match *self { + PublicKeyData::Compressed(ref x) => &x[index.start..], + PublicKeyData::Uncompressed(ref x) => &x[index.start..] + } + } +} + +impl ops::Index> for PublicKey { + type Output = [u8]; + + #[inline] + fn index(&self, index: ops::RangeFrom) -> &[u8] { + let &PublicKey(ref dat) = self; + &dat[index.start..] + } +} + +impl ops::Index for PublicKeyData { + type Output = [u8]; + + #[inline] + fn index(&self, _: ops::RangeFull) -> &[u8] { + match *self { + PublicKeyData::Compressed(ref x) => &x[..], + PublicKeyData::Uncompressed(ref x) => &x[..] + } + } +} + +impl ops::Index for PublicKey { + type Output = [u8]; + + #[inline] + fn index(&self, _: ops::RangeFull) -> &[u8] { + let &PublicKey(ref dat) = self; + &dat[..] } } @@ -426,13 +521,13 @@ impl Decodable for PublicKey { impl Encodable for PublicKey { fn encode(&self, s: &mut S) -> ::std::result::Result<(), S::Error> { - self.as_slice().encode(s) + (&self[..]).encode(s) } } impl fmt::Debug for SecretKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_slice().fmt(f) + (&self[..]).fmt(f) } } diff --git a/src/macros.rs b/src/macros.rs index dbe13a9..47226a9 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -19,34 +19,6 @@ macro_rules! impl_array_newtype { impl Copy for $thing {} impl $thing { - #[inline] - /// Provides an immutable view into the object - pub fn as_slice<'a>(&'a self) -> &'a [$ty] { - let &$thing(ref dat) = self; - dat.as_slice() - } - - #[inline] - /// Provides an immutable view into the object from index `s` inclusive to `e` exclusive - pub fn slice<'a>(&'a self, s: usize, e: usize) -> &'a [$ty] { - let &$thing(ref dat) = self; - dat.slice(s, e) - } - - #[inline] - /// Provides an immutable view into the object, up to index `n` exclusive - pub fn slice_to<'a>(&'a self, n: usize) -> &'a [$ty] { - let &$thing(ref dat) = self; - dat.slice_to(n) - } - - #[inline] - /// Provides an immutable view into the object, starting from index `n` - pub fn slice_from<'a>(&'a self, n: usize) -> &'a [$ty] { - let &$thing(ref dat) = self; - dat.slice_from(n) - } - #[inline] /// Converts the object to a raw pointer for FFI interfacing pub fn as_ptr(&self) -> *const $ty { @@ -69,7 +41,7 @@ macro_rules! impl_array_newtype { impl PartialEq for $thing { #[inline] fn eq(&self, other: &$thing) -> bool { - self.as_slice() == other.as_slice() + &self[..] == &other[..] } } @@ -90,6 +62,56 @@ macro_rules! impl_array_newtype { } } + impl ::std::ops::Index for $thing { + type Output = $ty; + + #[inline] + fn index(&self, index: usize) -> &$ty { + let &$thing(ref dat) = self; + &dat[index] + } + } + + impl ::std::ops::Index<::std::ops::Range> for $thing { + type Output = [$ty]; + + #[inline] + fn index(&self, index: ::std::ops::Range) -> &[$ty] { + let &$thing(ref dat) = self; + &dat[index.start..index.end] + } + } + + impl ::std::ops::Index<::std::ops::RangeTo> for $thing { + type Output = [$ty]; + + #[inline] + fn index(&self, index: ::std::ops::RangeTo) -> &[$ty] { + let &$thing(ref dat) = self; + &dat[..index.end] + } + } + + impl ::std::ops::Index<::std::ops::RangeFrom> for $thing { + type Output = [$ty]; + + #[inline] + fn index(&self, index: ::std::ops::RangeFrom) -> &[$ty] { + let &$thing(ref dat) = self; + &dat[index.start..] + } + } + + impl ::std::ops::Index<::std::ops::RangeFull> for $thing { + type Output = [$ty]; + + #[inline] + fn index(&self, _: ::std::ops::RangeFull) -> &[$ty] { + let &$thing(ref dat) = self; + &dat[..] + } + } + impl ::serialize::Decodable for $thing { fn decode(d: &mut D) -> ::std::result::Result<$thing, D::Error> { use serialize::Decodable; @@ -116,7 +138,7 @@ macro_rules! impl_array_newtype { impl ::serialize::Encodable for $thing { fn encode(&self, s: &mut S) -> ::std::result::Result<(), S::Error> { - self.as_slice().encode(s) + self[..].encode(s) } } } @@ -126,7 +148,7 @@ macro_rules! impl_array_newtype { // for testing macro_rules! hex_slice { ($s:expr) => ( - $s.from_hex().unwrap().as_slice() + &$s.from_hex().unwrap()[..] ) } diff --git a/src/secp256k1.rs b/src/secp256k1.rs index 69063dc..e198443 100644 --- a/src/secp256k1.rs +++ b/src/secp256k1.rs @@ -73,7 +73,7 @@ impl Signature { #[inline] pub fn as_ptr(&self) -> *const u8 { let &Signature(_, ref data) = self; - data.as_slice().as_ptr() + data.as_ptr() } /// Converts the signature to a mutable raw pointer suitable for use @@ -81,14 +81,14 @@ impl Signature { #[inline] pub fn as_mut_ptr(&mut self) -> *mut u8 { let &mut Signature(_, ref mut data) = self; - data.as_mut_slice().as_mut_ptr() + data.as_mut_ptr() } /// Converts the signature to a byte slice suitable for verification #[inline] pub fn as_slice<'a>(&'a self) -> &'a [u8] { let &Signature(len, ref data) = self; - data.slice_to(len) + &data[..len] } /// Returns the length of the signature @@ -164,7 +164,7 @@ impl Secp256k1 { let mut osrng = try!(OsRng::new()); let mut seed = [0; 2048]; osrng.fill_bytes(seed.as_mut_slice()); - Ok(Secp256k1 { rng: SeedableRng::from_seed(seed.as_slice()) }) + Ok(Secp256k1 { rng: SeedableRng::from_seed(&seed[..]) }) } /// Generates a random keypair. Convenience function for `key::SecretKey::new`