From bafef68c56b89c3e55856320ac28fccb7fb98016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Fri, 11 Jan 2019 12:57:34 -0800 Subject: [PATCH 1/3] Bumps rand to 0.6.3 & associated updates --- Cargo.toml | 5 +++-- src/key.rs | 41 ++++++++++++++++++++++++++++++++--------- src/lib.rs | 4 ++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b9cc39c..20bf1b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,11 +31,12 @@ default = [] fuzztarget = [] [dev-dependencies] -rand = "0.4" +rand = "0.6" +rand_core = "0.4" serde_test = "1.0" [dependencies.rand] -version = "0.4" +version = "0.6" optional = true [dependencies.serde] diff --git a/src/key.rs b/src/key.rs index 896eaf9..2d90c2e 100644 --- a/src/key.rs +++ b/src/key.rs @@ -393,7 +393,8 @@ mod test { use super::{PublicKey, SecretKey}; use super::super::constants; - use rand::{Rng, thread_rng}; + use rand::{Error, ErrorKind, RngCore, thread_rng}; + use rand_core::impls; use std::iter; use std::str::FromStr; @@ -462,8 +463,9 @@ mod test { fn test_out_of_range() { struct BadRng(u8); - impl Rng for BadRng { + impl RngCore for BadRng { fn next_u32(&mut self) -> u32 { unimplemented!() } + fn next_u64(&mut self) -> u64 { unimplemented!() } // This will set a secret key to a little over the // group order, then decrement with repeated calls // until it returns a valid key @@ -478,6 +480,9 @@ mod test { data[31] = self.0; self.0 -= 1; } + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + Ok(self.fill_bytes(dest)) + } } let s = Secp256k1::new(); @@ -518,18 +523,28 @@ mod test { #[test] fn test_debug_output() { struct DumbRng(u32); - impl Rng for DumbRng { + impl RngCore for DumbRng { fn next_u32(&mut self) -> u32 { self.0 = self.0.wrapping_add(1); self.0 } + fn next_u64(&mut self) -> u64 { + self.next_u32() as u64 + } + fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> { + Err(Error::new(ErrorKind::Unavailable, "not implemented")) + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + impls::fill_bytes_via_next(self, dest); + } } let s = Secp256k1::new(); let (sk, _) = s.generate_keypair(&mut DumbRng(0)); assert_eq!(&format!("{:?}", sk), - "SecretKey(0200000001000000040000000300000006000000050000000800000007000000)"); + "SecretKey(0100000000000000020000000000000003000000000000000400000000000000)"); } #[test] @@ -588,19 +603,29 @@ mod test { #[test] fn test_pubkey_serialize() { struct DumbRng(u32); - impl Rng for DumbRng { + impl RngCore for DumbRng { fn next_u32(&mut self) -> u32 { self.0 = self.0.wrapping_add(1); self.0 } + fn next_u64(&mut self) -> u64 { + self.next_u32() as u64 + } + fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> { + Err(Error::new(ErrorKind::Unavailable, "not implemented")) + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + impls::fill_bytes_via_next(self, dest); + } } let s = Secp256k1::new(); let (_, pk1) = s.generate_keypair(&mut DumbRng(0)); assert_eq!(&pk1.serialize_uncompressed()[..], - &[4, 149, 16, 196, 140, 38, 92, 239, 179, 65, 59, 224, 230, 183, 91, 238, 240, 46, 186, 252, 175, 102, 52, 249, 98, 178, 123, 72, 50, 171, 196, 254, 236, 1, 189, 143, 242, 227, 16, 87, 247, 183, 162, 68, 237, 140, 92, 205, 151, 129, 166, 58, 111, 96, 123, 64, 180, 147, 51, 12, 209, 89, 236, 213, 206][..]); + &[4, 124, 121, 49, 14, 253, 63, 197, 50, 39, 194, 107, 17, 193, 219, 108, 154, 126, 9, 181, 248, 2, 12, 149, 233, 198, 71, 149, 134, 250, 184, 154, 229, 185, 28, 165, 110, 27, 3, 162, 126, 238, 167, 157, 242, 221, 76, 251, 237, 34, 231, 72, 39, 245, 3, 191, 64, 111, 170, 117, 103, 82, 28, 102, 163][..]); assert_eq!(&pk1.serialize()[..], - &[2, 149, 16, 196, 140, 38, 92, 239, 179, 65, 59, 224, 230, 183, 91, 238, 240, 46, 186, 252, 175, 102, 52, 249, 98, 178, 123, 72, 50, 171, 196, 254, 236][..]); + &[3, 124, 121, 49, 14, 253, 63, 197, 50, 39, 194, 107, 17, 193, 219, 108, 154, 126, 9, 181, 248, 2, 12, 149, 233, 198, 71, 149, 134, 250, 184, 154, 229][..]); } #[test] @@ -732,5 +757,3 @@ mod test { assert_tokens(&pk, &[Token::BorrowedBytes(&PK_BYTES[..])]); } } - - diff --git a/src/lib.rs b/src/lib.rs index 866b3cf..3bf2b49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,6 +136,7 @@ #![cfg_attr(all(test, feature = "unstable"), feature(test))] #[cfg(all(test, feature = "unstable"))] extern crate test; #[cfg(any(test, feature = "rand"))] pub extern crate rand; +#[cfg(any(test))] extern crate rand_core; #[cfg(feature = "serde")] pub extern crate serde; #[cfg(all(test, feature = "serde"))] extern crate serde_test; @@ -778,7 +779,7 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result { #[cfg(test)] mod tests { - use rand::{Rng, thread_rng}; + use rand::{RngCore, thread_rng}; use std::str::FromStr; use key::{SecretKey, PublicKey}; @@ -1227,4 +1228,3 @@ mod benches { }); } } - From e69eabb36f254c7cf123f9d162b9d5262333d209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Wed, 30 Jan 2019 15:44:26 -0800 Subject: [PATCH 2/3] Bumping minimum Rust version 1.14.0 -> 1.22.0, as per https://git.io/fhDLO --- .travis.yml | 4 ++-- Cargo.toml | 2 +- README.md | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index d306781..23acf4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ rust: - stable - beta - nightly - - 1.14.0 + - 1.22.0 os: - linux - windows @@ -16,7 +16,7 @@ matrix: # - we insist on rust 1.14 only for Debian, and # - "rand" is only an optional dependency. exclude: - - rust: 1.14.0 + - rust: 1.22.0 os: windows script: diff --git a/Cargo.toml b/Cargo.toml index 20bf1b4..f114fef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ fuzztarget = [] [dev-dependencies] rand = "0.6" -rand_core = "0.4" +rand_core = "0.3" serde_test = "1.0" [dependencies.rand] diff --git a/README.md b/README.md index 502d4df..5ae0e09 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,4 @@ Contributions to this library are welcome. A few guidelines: * Any breaking changes must have an accompanied entry in CHANGELOG.md * No new dependencies, please. * No crypto should be implemented in Rust, with the possible exception of hash functions. Cryptographic contributions should be directed upstream to libsecp256k1. -* This library should always compile with any combination of features on **Rust 1.14**, which is the currently shipping compiler on Debian. - +* This library should always compile with any combination of features on **Rust 1.22**. From a8a3afe8db4ce01535210af791429824fed6ed2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Mon, 11 Feb 2019 10:33:17 -0800 Subject: [PATCH 3/3] Fix clippy errors Changes include cargo-fix generated, Default impls --- .travis.yml | 8 +++---- src/ffi.rs | 68 ++++++++++++++++++++++++++++++++++++----------------- src/lib.rs | 10 ++++++-- 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index 23acf4f..c742039 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,10 +11,10 @@ os: - windows matrix: - # rand 0.4 actually needs Rust 1.22, which leads to build failures on Rust 1.14 on Windows. - # This is not a problem, because - # - we insist on rust 1.14 only for Debian, and - # - "rand" is only an optional dependency. + # rand 0.6 actually needs Rust 1.22, which leads to build failures on Rust 1.14 on Windows. + # This is a problem, because + # - we insist on rust 1.22 since #92 + # - but "rand" is only an optional dependency. exclude: - rust: 1.22.0 os: windows diff --git a/src/ffi.rs b/src/ffi.rs index 1b7d17e..a7633b5 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -21,13 +21,13 @@ use std::hash; use std::os::raw::{c_int, c_uchar, c_uint, c_void}; /// Flag for context to enable no precomputation -pub const SECP256K1_START_NONE: c_uint = (1 << 0) | 0; +pub const SECP256K1_START_NONE: c_uint = 1; /// Flag for context to enable verification precomputation -pub const SECP256K1_START_VERIFY: c_uint = (1 << 0) | (1 << 8); +pub const SECP256K1_START_VERIFY: c_uint = 1 | (1 << 8); /// Flag for context to enable signing precomputation -pub const SECP256K1_START_SIGN: c_uint = (1 << 0) | (1 << 9); +pub const SECP256K1_START_SIGN: c_uint = 1 | (1 << 9); /// Flag for keys to indicate uncompressed serialization format -pub const SECP256K1_SER_UNCOMPRESSED: c_uint = (1 << 1) | 0; +pub const SECP256K1_SER_UNCOMPRESSED: c_uint = (1 << 1); /// Flag for keys to indicate compressed serialization format pub const SECP256K1_SER_COMPRESSED: c_uint = (1 << 1) | (1 << 8); @@ -74,6 +74,12 @@ impl PublicKey { pub unsafe fn blank() -> PublicKey { mem::uninitialized() } } +impl Default for PublicKey { + fn default() -> Self { + PublicKey::new() + } +} + impl hash::Hash for PublicKey { fn hash(&self, state: &mut H) { state.write(&self.0) @@ -99,6 +105,12 @@ impl Signature { pub unsafe fn blank() -> Signature { mem::uninitialized() } } +impl Default for Signature { + fn default() -> Self { + Signature::new() + } +} + impl RecoverableSignature { /// Create a new (zeroed) signature usable for the FFI interface pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) } @@ -106,6 +118,12 @@ impl RecoverableSignature { pub unsafe fn blank() -> RecoverableSignature { mem::uninitialized() } } +impl Default for RecoverableSignature { + fn default() -> Self { + RecoverableSignature::new() + } +} + /// Library-internal representation of an ECDH shared secret #[repr(C)] pub struct SharedSecret([c_uchar; 32]); @@ -119,6 +137,12 @@ impl SharedSecret { pub unsafe fn blank() -> SharedSecret { mem::uninitialized() } } +impl Default for SharedSecret { + fn default() -> Self { + SharedSecret::new() + } +} + #[cfg(not(feature = "fuzztarget"))] extern "C" { /// Default ECDH hash function @@ -369,8 +393,8 @@ mod fuzz_dummy { } // Signatures - pub unsafe fn secp256k1_ecdsa_signature_parse_der(cx: *const Context, sig: *mut Signature, - input: *const c_uchar, in_len: usize) + pub unsafe fn secp256k1_ecdsa_signature_parse_der(_cx: *const Context, _sig: *mut Signature, + _input: *const c_uchar, _in_len: usize) -> c_int { unimplemented!(); } @@ -385,8 +409,8 @@ mod fuzz_dummy { 1 } - pub unsafe fn ecdsa_signature_parse_der_lax(cx: *const Context, sig: *mut Signature, - input: *const c_uchar, in_len: usize) + pub unsafe fn ecdsa_signature_parse_der_lax(_cx: *const Context, _sig: *mut Signature, + _input: *const c_uchar, _in_len: usize) -> c_int { unimplemented!(); } @@ -438,26 +462,26 @@ mod fuzz_dummy { 1 } - pub unsafe fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature, - input64: *const c_uchar, recid: c_int) + pub unsafe fn secp256k1_ecdsa_recoverable_signature_parse_compact(_cx: *const Context, _sig: *mut RecoverableSignature, + _input64: *const c_uchar, _recid: c_int) -> c_int { unimplemented!(); } - pub unsafe fn secp256k1_ecdsa_recoverable_signature_serialize_compact(cx: *const Context, output64: *const c_uchar, - recid: *mut c_int, sig: *const RecoverableSignature) + pub unsafe fn secp256k1_ecdsa_recoverable_signature_serialize_compact(_cx: *const Context, _output64: *const c_uchar, + _recid: *mut c_int, _sig: *const RecoverableSignature) -> c_int { unimplemented!(); } - pub unsafe fn secp256k1_ecdsa_recoverable_signature_convert(cx: *const Context, sig: *mut Signature, - input: *const RecoverableSignature) + pub unsafe fn secp256k1_ecdsa_recoverable_signature_convert(_cx: *const Context, _sig: *mut Signature, + _input: *const RecoverableSignature) -> c_int { unimplemented!(); } - pub unsafe fn secp256k1_ecdsa_signature_normalize(cx: *const Context, out_sig: *mut Signature, - in_sig: *const Signature) + pub unsafe fn secp256k1_ecdsa_signature_normalize(_cx: *const Context, _out_sig: *mut Signature, + _in_sig: *const Signature) -> c_int { unimplemented!(); } @@ -521,10 +545,10 @@ mod fuzz_dummy { 1 } - pub unsafe fn secp256k1_ecdsa_recover(cx: *const Context, - pk: *mut PublicKey, - sig: *const RecoverableSignature, - msg32: *const c_uchar) + pub unsafe fn secp256k1_ecdsa_recover(_cx: *const Context, + _pk: *mut PublicKey, + _sig: *const RecoverableSignature, + _msg32: *const c_uchar) -> c_int { unimplemented!(); } @@ -640,8 +664,8 @@ mod fuzz_dummy { out: *mut SharedSecret, point: *const PublicKey, scalar: *const c_uchar, - hashfp: EcdhHashFn, - data: *mut c_void, + _hashfp: EcdhHashFn, + _data: *mut c_void, ) -> c_int { assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0); if secp256k1_ec_seckey_verify(cx, scalar) != 1 { return 0; } diff --git a/src/lib.rs b/src/lib.rs index 3bf2b49..6f7632b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -223,7 +223,7 @@ pub fn from_i32(id: i32) -> Result { #[inline] /// Allows library users to convert recovery IDs to i32. -pub fn to_i32(&self) -> i32 { +pub fn to_i32(self) -> i32 { self.0 } } @@ -474,7 +474,7 @@ impl Message { /// Converts a `MESSAGE_SIZE`-byte slice to a message object #[inline] pub fn from_slice(data: &[u8]) -> Result { - if data == &[0; constants::MESSAGE_SIZE] { + if data == [0; constants::MESSAGE_SIZE] { return Err(Error::InvalidMessage); } @@ -617,6 +617,12 @@ impl Secp256k1 { } } +impl Default for Secp256k1 { + fn default() -> Self { + Self::new() + } +} + impl Secp256k1 { /// Creates a new Secp256k1 context that can only be used for signing pub fn signing_only() -> Secp256k1 {