Update for language changes (rustc beta is out !!)

This commit is contained in:
Andrew Poelstra 2015-04-04 12:20:38 -05:00
parent abc5b865e7
commit e2daaf875d
3 changed files with 50 additions and 40 deletions

View File

@ -46,15 +46,14 @@ pub static ONE: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1]); 0, 0, 0, 0, 0, 0, 0, 1]);
/// Public key /// Public key
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PublicKey(PublicKeyData); pub struct PublicKey(PublicKeyData);
impl Copy for PublicKey {}
#[derive(Copy, Eq)]
enum PublicKeyData { enum PublicKeyData {
Compressed([u8; constants::COMPRESSED_PUBLIC_KEY_SIZE]), Compressed([u8; constants::COMPRESSED_PUBLIC_KEY_SIZE]),
Uncompressed([u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]) Uncompressed([u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE])
} }
impl Copy for PublicKeyData {}
fn random_32_bytes<R:Rng>(rng: &mut R) -> [u8; 32] { fn random_32_bytes<R:Rng>(rng: &mut R) -> [u8; 32] {
let mut ret = [0u8; 32]; let mut ret = [0u8; 32];
@ -66,8 +65,8 @@ fn random_32_bytes<R:Rng>(rng: &mut R) -> [u8; 32] {
fn bits2octets(data: &[u8]) -> [u8; 32] { fn bits2octets(data: &[u8]) -> [u8; 32] {
let mut ret = [0; 32]; let mut ret = [0; 32];
unsafe { unsafe {
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(data.as_ptr(),
data.as_ptr(), ret.as_mut_ptr(),
cmp::min(data.len(), 32)); cmp::min(data.len(), 32));
} }
ret ret
@ -87,8 +86,8 @@ impl Nonce {
constants::NONCE_SIZE => { constants::NONCE_SIZE => {
let mut ret = [0; constants::NONCE_SIZE]; let mut ret = [0; constants::NONCE_SIZE];
unsafe { unsafe {
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(data.as_ptr(),
data.as_ptr(), ret.as_mut_ptr(),
data.len()); data.len());
} }
Ok(Nonce(ret)) Ok(Nonce(ret))
@ -107,7 +106,7 @@ impl Nonce {
($res:expr; key $key:expr, data $($data:expr),+) => ({ ($res:expr; key $key:expr, data $($data:expr),+) => ({
let mut hmacker = Hmac::new(Sha512::new(), &$key[..]); let mut hmacker = Hmac::new(Sha512::new(), &$key[..]);
$(hmacker.input(&$data[..]);)+ $(hmacker.input(&$data[..]);)+
hmacker.raw_result($res.as_mut_slice()); hmacker.raw_result(&mut $res);
}) })
} }
@ -116,7 +115,7 @@ impl Nonce {
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();
hasher.input(msg); hasher.input(msg);
let mut x = [0; HMAC_SIZE]; let mut x = [0; HMAC_SIZE];
hasher.result(x.as_mut_slice()); hasher.result(&mut x);
let msg_hash = bits2octets(&x); let msg_hash = bits2octets(&x);
// Section 3.2b // Section 3.2b
@ -181,8 +180,8 @@ impl SecretKey {
if ffi::secp256k1_ec_seckey_verify(data.as_ptr()) == 0 { if ffi::secp256k1_ec_seckey_verify(data.as_ptr()) == 0 {
return Err(InvalidSecretKey); return Err(InvalidSecretKey);
} }
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(data.as_ptr(),
data.as_ptr(), ret.as_mut_ptr(),
data.len()); data.len());
} }
Ok(SecretKey(ret)) Ok(SecretKey(ret))
@ -216,11 +215,11 @@ impl SecretKey {
} }
/// An iterator of keypairs `(sk + 1, pk*G)`, `(sk + 2, pk*2G)`, ... /// An iterator of keypairs `(sk + 1, pk*G)`, `(sk + 2, pk*2G)`, ...
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Sequence { pub struct Sequence {
compressed: bool, compressed: bool,
last_sk: SecretKey, last_sk: SecretKey,
} }
impl Copy for Sequence {}
impl Iterator for Sequence { impl Iterator for Sequence {
type Item = (SecretKey, PublicKey); type Item = (SecretKey, PublicKey);
@ -275,8 +274,8 @@ impl PublicKey {
data.len() as ::libc::c_int) == 0 { data.len() as ::libc::c_int) == 0 {
return Err(InvalidPublicKey); return Err(InvalidPublicKey);
} }
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(data.as_ptr(),
data.as_ptr(), ret.as_mut_ptr(),
data.len()); data.len());
} }
Ok(PublicKey(PublicKeyData::Compressed(ret))) Ok(PublicKey(PublicKeyData::Compressed(ret)))
@ -284,8 +283,8 @@ impl PublicKey {
constants::UNCOMPRESSED_PUBLIC_KEY_SIZE => { constants::UNCOMPRESSED_PUBLIC_KEY_SIZE => {
let mut ret = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]; let mut ret = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
unsafe { unsafe {
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(data.as_ptr(),
data.as_ptr(), ret.as_mut_ptr(),
data.len()); data.len());
} }
Ok(PublicKey(PublicKeyData::Uncompressed(ret))) Ok(PublicKey(PublicKeyData::Uncompressed(ret)))
@ -370,8 +369,6 @@ impl PartialEq for PublicKeyData {
} }
} }
impl Eq for PublicKeyData {}
impl fmt::Debug for PublicKeyData { impl fmt::Debug for PublicKeyData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(&self[..]).fmt(f) (&self[..]).fmt(f)

View File

@ -54,8 +54,8 @@ macro_rules! impl_array_newtype {
use std::intrinsics::copy_nonoverlapping; use std::intrinsics::copy_nonoverlapping;
use std::mem; use std::mem;
let mut ret: $thing = mem::uninitialized(); let mut ret: $thing = mem::uninitialized();
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(self.as_ptr(),
self.as_ptr(), ret.as_mut_ptr(),
mem::size_of::<$thing>()); mem::size_of::<$thing>());
ret ret
} }

View File

@ -60,12 +60,12 @@ pub mod key;
fn assert_type_is_copy<T: Copy>() { } fn assert_type_is_copy<T: Copy>() { }
/// 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)]
pub struct RecoveryId(i32); pub struct RecoveryId(i32);
impl Copy for RecoveryId {}
/// An ECDSA signature /// An ECDSA signature
#[derive(Copy)]
pub struct Signature(usize, [u8; constants::MAX_SIGNATURE_SIZE]); pub struct Signature(usize, [u8; constants::MAX_SIGNATURE_SIZE]);
impl Copy for Signature {}
impl Signature { impl Signature {
/// Converts the signature to a raw pointer suitable for use /// Converts the signature to a raw pointer suitable for use
@ -97,8 +97,8 @@ impl Signature {
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 {
copy_nonoverlapping(ret.as_mut_ptr(), copy_nonoverlapping(data.as_ptr(),
data.as_ptr(), ret.as_mut_ptr(),
data.len()); data.len());
} }
Ok(Signature(data.len(), ret)) Ok(Signature(data.len(), ret))
@ -148,8 +148,22 @@ impl ops::Index<ops::RangeFull> for Signature {
} }
} }
impl Clone for Signature {
#[inline]
fn clone(&self) -> Signature {
unsafe {
use std::mem;
let mut ret: Signature = mem::uninitialized();
copy_nonoverlapping(self.as_ptr(),
ret.as_mut_ptr(),
mem::size_of::<Signature>());
ret
}
}
}
/// An ECDSA error /// An ECDSA error
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub enum Error { pub enum Error {
/// Signature failed verification /// Signature failed verification
IncorrectSignature, IncorrectSignature,
@ -164,7 +178,6 @@ pub enum Error {
/// Boolean-returning function returned the wrong boolean /// Boolean-returning function returned the wrong boolean
Unknown Unknown
} }
impl Copy for Error {}
/// Result type /// Result type
pub type Result<T> = ::std::result::Result<T, Error>; pub type Result<T> = ::std::result::Result<T, Error>;
@ -196,7 +209,7 @@ impl Secp256k1 {
init(); init();
let mut osrng = try!(OsRng::new()); let mut osrng = try!(OsRng::new());
let mut seed = [0; 2048]; let mut seed = [0; 2048];
osrng.fill_bytes(seed.as_mut_slice()); osrng.fill_bytes(&mut seed);
Ok(Secp256k1 { rng: SeedableRng::from_seed(&seed[..]) }) Ok(Secp256k1 { rng: SeedableRng::from_seed(&seed[..]) })
} }
@ -225,7 +238,7 @@ impl Secp256k1 {
let mut len = constants::MAX_SIGNATURE_SIZE as c_int; let mut len = constants::MAX_SIGNATURE_SIZE as c_int;
unsafe { unsafe {
if ffi::secp256k1_ecdsa_sign(msg.as_ptr(), msg.len() as c_int, if ffi::secp256k1_ecdsa_sign(msg.as_ptr(), msg.len() as c_int,
sig.as_mut_slice().as_mut_ptr(), &mut len, (&mut sig).as_mut_ptr(), &mut len,
sk.as_ptr(), nonce.as_ptr()) != 1 { sk.as_ptr(), nonce.as_ptr()) != 1 {
return Err(Error::InvalidNonce); return Err(Error::InvalidNonce);
} }
@ -242,7 +255,7 @@ impl Secp256k1 {
let mut recid = 0; let mut recid = 0;
unsafe { unsafe {
if ffi::secp256k1_ecdsa_sign_compact(msg.as_ptr(), msg.len() as c_int, if ffi::secp256k1_ecdsa_sign_compact(msg.as_ptr(), msg.len() as c_int,
sig.as_mut_slice().as_mut_ptr(), sk.as_ptr(), (&mut sig).as_mut_ptr(), sk.as_ptr(),
nonce.as_ptr(), &mut recid) != 1 { nonce.as_ptr(), &mut recid) != 1 {
return Err(Error::InvalidNonce); return Err(Error::InvalidNonce);
} }
@ -320,9 +333,9 @@ mod tests {
let sig = Signature::from_slice(&[0; 72]).unwrap(); let sig = Signature::from_slice(&[0; 72]).unwrap();
let pk = PublicKey::new(true); let pk = PublicKey::new(true);
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
assert_eq!(Secp256k1::verify(msg.as_mut_slice(), &sig, &pk), Err(InvalidPublicKey)); assert_eq!(Secp256k1::verify(&mut msg, &sig, &pk), Err(InvalidPublicKey));
} }
#[test] #[test]
@ -334,9 +347,9 @@ mod tests {
let mut msg: Vec<u8> = repeat(0).take(32).collect(); let mut msg: Vec<u8> = repeat(0).take(32).collect();
let sig = Signature::from_slice(&[0; 72]).unwrap(); let sig = Signature::from_slice(&[0; 72]).unwrap();
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
assert_eq!(Secp256k1::verify(msg.as_mut_slice(), &sig, &pk), Err(InvalidSignature)); assert_eq!(Secp256k1::verify(&mut msg, &sig, &pk), Err(InvalidSignature));
} }
#[test] #[test]
@ -347,9 +360,9 @@ mod tests {
let mut msg: Vec<u8> = repeat(0).take(32).collect(); let mut msg: Vec<u8> = repeat(0).take(32).collect();
let sig = Signature::from_slice(&[0; 72]).unwrap(); let sig = Signature::from_slice(&[0; 72]).unwrap();
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
assert_eq!(Secp256k1::verify(msg.as_mut_slice(), &sig, &pk), Err(InvalidSignature)); assert_eq!(Secp256k1::verify(&mut msg, &sig, &pk), Err(InvalidSignature));
} }
#[test] #[test]
@ -370,7 +383,7 @@ mod tests {
let mut s = Secp256k1::new().unwrap(); let mut s = Secp256k1::new().unwrap();
let mut msg: Vec<u8> = repeat(0).take(32).collect(); let mut msg: Vec<u8> = repeat(0).take(32).collect();
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
let (sk, pk) = s.generate_keypair(false); let (sk, pk) = s.generate_keypair(false);
let nonce = s.generate_nonce(); let nonce = s.generate_nonce();
@ -385,14 +398,14 @@ mod tests {
let mut s = Secp256k1::new().unwrap(); let mut s = Secp256k1::new().unwrap();
let mut msg: Vec<u8> = repeat(0).take(32).collect(); let mut msg: Vec<u8> = repeat(0).take(32).collect();
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
let (sk, pk) = s.generate_keypair(false); let (sk, pk) = s.generate_keypair(false);
let nonce = s.generate_nonce(); let nonce = s.generate_nonce();
let sig = s.sign(&msg, &sk, &nonce).unwrap(); let sig = s.sign(&msg, &sk, &nonce).unwrap();
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
assert_eq!(Secp256k1::verify(&msg, &sig, &pk), Err(IncorrectSignature)); assert_eq!(Secp256k1::verify(&msg, &sig, &pk), Err(IncorrectSignature));
} }
@ -401,7 +414,7 @@ mod tests {
let mut s = Secp256k1::new().unwrap(); let mut s = Secp256k1::new().unwrap();
let mut msg = [0u8; 32]; let mut msg = [0u8; 32];
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
let (sk, pk) = s.generate_keypair(false); let (sk, pk) = s.generate_keypair(false);
let nonce = s.generate_nonce(); let nonce = s.generate_nonce();
@ -414,7 +427,7 @@ mod tests {
#[test] #[test]
fn deterministic_sign() { fn deterministic_sign() {
let mut msg = [0u8; 32]; let mut msg = [0u8; 32];
thread_rng().fill_bytes(msg.as_mut_slice()); thread_rng().fill_bytes(&mut msg);
let mut s = Secp256k1::new().unwrap(); let mut s = Secp256k1::new().unwrap();
let (sk, pk) = s.generate_keypair(true); let (sk, pk) = s.generate_keypair(true);