Update for language changes (rustc beta is out !!)
This commit is contained in:
parent
abc5b865e7
commit
e2daaf875d
33
src/key.rs
33
src/key.rs
|
@ -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]);
|
||||
|
||||
/// Public key
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub struct PublicKey(PublicKeyData);
|
||||
impl Copy for PublicKey {}
|
||||
|
||||
#[derive(Copy, Eq)]
|
||||
enum PublicKeyData {
|
||||
Compressed([u8; constants::COMPRESSED_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] {
|
||||
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] {
|
||||
let mut ret = [0; 32];
|
||||
unsafe {
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
data.as_ptr(),
|
||||
copy_nonoverlapping(data.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
cmp::min(data.len(), 32));
|
||||
}
|
||||
ret
|
||||
|
@ -87,8 +86,8 @@ impl Nonce {
|
|||
constants::NONCE_SIZE => {
|
||||
let mut ret = [0; constants::NONCE_SIZE];
|
||||
unsafe {
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
data.as_ptr(),
|
||||
copy_nonoverlapping(data.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
data.len());
|
||||
}
|
||||
Ok(Nonce(ret))
|
||||
|
@ -107,7 +106,7 @@ impl Nonce {
|
|||
($res:expr; key $key:expr, data $($data:expr),+) => ({
|
||||
let mut hmacker = Hmac::new(Sha512::new(), &$key[..]);
|
||||
$(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();
|
||||
hasher.input(msg);
|
||||
let mut x = [0; HMAC_SIZE];
|
||||
hasher.result(x.as_mut_slice());
|
||||
hasher.result(&mut x);
|
||||
let msg_hash = bits2octets(&x);
|
||||
|
||||
// Section 3.2b
|
||||
|
@ -181,8 +180,8 @@ impl SecretKey {
|
|||
if ffi::secp256k1_ec_seckey_verify(data.as_ptr()) == 0 {
|
||||
return Err(InvalidSecretKey);
|
||||
}
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
data.as_ptr(),
|
||||
copy_nonoverlapping(data.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
data.len());
|
||||
}
|
||||
Ok(SecretKey(ret))
|
||||
|
@ -216,11 +215,11 @@ impl SecretKey {
|
|||
}
|
||||
|
||||
/// An iterator of keypairs `(sk + 1, pk*G)`, `(sk + 2, pk*2G)`, ...
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub struct Sequence {
|
||||
compressed: bool,
|
||||
last_sk: SecretKey,
|
||||
}
|
||||
impl Copy for Sequence {}
|
||||
|
||||
impl Iterator for Sequence {
|
||||
type Item = (SecretKey, PublicKey);
|
||||
|
@ -275,8 +274,8 @@ impl PublicKey {
|
|||
data.len() as ::libc::c_int) == 0 {
|
||||
return Err(InvalidPublicKey);
|
||||
}
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
data.as_ptr(),
|
||||
copy_nonoverlapping(data.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
data.len());
|
||||
}
|
||||
Ok(PublicKey(PublicKeyData::Compressed(ret)))
|
||||
|
@ -284,8 +283,8 @@ impl PublicKey {
|
|||
constants::UNCOMPRESSED_PUBLIC_KEY_SIZE => {
|
||||
let mut ret = [0; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
|
||||
unsafe {
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
data.as_ptr(),
|
||||
copy_nonoverlapping(data.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
data.len());
|
||||
}
|
||||
Ok(PublicKey(PublicKeyData::Uncompressed(ret)))
|
||||
|
@ -370,8 +369,6 @@ impl PartialEq for PublicKeyData {
|
|||
}
|
||||
}
|
||||
|
||||
impl Eq for PublicKeyData {}
|
||||
|
||||
impl fmt::Debug for PublicKeyData {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
(&self[..]).fmt(f)
|
||||
|
|
|
@ -54,8 +54,8 @@ macro_rules! impl_array_newtype {
|
|||
use std::intrinsics::copy_nonoverlapping;
|
||||
use std::mem;
|
||||
let mut ret: $thing = mem::uninitialized();
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
self.as_ptr(),
|
||||
copy_nonoverlapping(self.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
mem::size_of::<$thing>());
|
||||
ret
|
||||
}
|
||||
|
|
|
@ -60,12 +60,12 @@ pub mod key;
|
|||
fn assert_type_is_copy<T: Copy>() { }
|
||||
|
||||
/// A tag used for recovering the public key from a compact signature
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub struct RecoveryId(i32);
|
||||
impl Copy for RecoveryId {}
|
||||
|
||||
/// An ECDSA signature
|
||||
#[derive(Copy)]
|
||||
pub struct Signature(usize, [u8; constants::MAX_SIGNATURE_SIZE]);
|
||||
impl Copy for Signature {}
|
||||
|
||||
impl Signature {
|
||||
/// Converts the signature to a raw pointer suitable for use
|
||||
|
@ -97,8 +97,8 @@ impl Signature {
|
|||
if data.len() <= constants::MAX_SIGNATURE_SIZE {
|
||||
let mut ret = [0; constants::MAX_SIGNATURE_SIZE];
|
||||
unsafe {
|
||||
copy_nonoverlapping(ret.as_mut_ptr(),
|
||||
data.as_ptr(),
|
||||
copy_nonoverlapping(data.as_ptr(),
|
||||
ret.as_mut_ptr(),
|
||||
data.len());
|
||||
}
|
||||
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
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
|
||||
pub enum Error {
|
||||
/// Signature failed verification
|
||||
IncorrectSignature,
|
||||
|
@ -164,7 +178,6 @@ pub enum Error {
|
|||
/// Boolean-returning function returned the wrong boolean
|
||||
Unknown
|
||||
}
|
||||
impl Copy for Error {}
|
||||
|
||||
/// Result type
|
||||
pub type Result<T> = ::std::result::Result<T, Error>;
|
||||
|
@ -196,7 +209,7 @@ impl Secp256k1 {
|
|||
init();
|
||||
let mut osrng = try!(OsRng::new());
|
||||
let mut seed = [0; 2048];
|
||||
osrng.fill_bytes(seed.as_mut_slice());
|
||||
osrng.fill_bytes(&mut seed);
|
||||
Ok(Secp256k1 { rng: SeedableRng::from_seed(&seed[..]) })
|
||||
}
|
||||
|
||||
|
@ -225,7 +238,7 @@ impl Secp256k1 {
|
|||
let mut len = constants::MAX_SIGNATURE_SIZE as c_int;
|
||||
unsafe {
|
||||
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 {
|
||||
return Err(Error::InvalidNonce);
|
||||
}
|
||||
|
@ -242,7 +255,7 @@ impl Secp256k1 {
|
|||
let mut recid = 0;
|
||||
unsafe {
|
||||
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 {
|
||||
return Err(Error::InvalidNonce);
|
||||
}
|
||||
|
@ -320,9 +333,9 @@ mod tests {
|
|||
let sig = Signature::from_slice(&[0; 72]).unwrap();
|
||||
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]
|
||||
|
@ -334,9 +347,9 @@ mod tests {
|
|||
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
||||
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]
|
||||
|
@ -347,9 +360,9 @@ mod tests {
|
|||
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
||||
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]
|
||||
|
@ -370,7 +383,7 @@ mod tests {
|
|||
let mut s = Secp256k1::new().unwrap();
|
||||
|
||||
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 nonce = s.generate_nonce();
|
||||
|
@ -385,14 +398,14 @@ mod tests {
|
|||
let mut s = Secp256k1::new().unwrap();
|
||||
|
||||
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 nonce = s.generate_nonce();
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -401,7 +414,7 @@ mod tests {
|
|||
let mut s = Secp256k1::new().unwrap();
|
||||
|
||||
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 nonce = s.generate_nonce();
|
||||
|
@ -414,7 +427,7 @@ mod tests {
|
|||
#[test]
|
||||
fn deterministic_sign() {
|
||||
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 (sk, pk) = s.generate_keypair(true);
|
||||
|
|
Loading…
Reference in New Issue