Skip to content

Commit 7234606

Browse files
authored
Merge pull request #92 from huitseeker/rand-update
Bumps rand to 0.6.X & associated updates
2 parents 75bc49b + a8a3afe commit 7234606

File tree

6 files changed

+98
-45
lines changed

6 files changed

+98
-45
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ rust:
55
- stable
66
- beta
77
- nightly
8-
- 1.14.0
8+
- 1.22.0
99
os:
1010
- linux
1111
- windows
1212

1313
matrix:
14-
# rand 0.4 actually needs Rust 1.22, which leads to build failures on Rust 1.14 on Windows.
15-
# This is not a problem, because
16-
# - we insist on rust 1.14 only for Debian, and
17-
# - "rand" is only an optional dependency.
14+
# rand 0.6 actually needs Rust 1.22, which leads to build failures on Rust 1.14 on Windows.
15+
# This is a problem, because
16+
# - we insist on rust 1.22 since #92
17+
# - but "rand" is only an optional dependency.
1818
exclude:
19-
- rust: 1.14.0
19+
- rust: 1.22.0
2020
os: windows
2121

2222
script:

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ default = []
3131
fuzztarget = []
3232

3333
[dev-dependencies]
34-
rand = "0.4"
34+
rand = "0.6"
35+
rand_core = "0.3"
3536
serde_test = "1.0"
3637

3738
[dependencies.rand]
38-
version = "0.4"
39+
version = "0.6"
3940
optional = true
4041

4142
[dependencies.serde]

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ Contributions to this library are welcome. A few guidelines:
2020
* Any breaking changes must have an accompanied entry in CHANGELOG.md
2121
* No new dependencies, please.
2222
* No crypto should be implemented in Rust, with the possible exception of hash functions. Cryptographic contributions should be directed upstream to libsecp256k1.
23-
* This library should always compile with any combination of features on **Rust 1.14**, which is the currently shipping compiler on Debian.
24-
23+
* This library should always compile with any combination of features on **Rust 1.22**.

src/ffi.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use std::hash;
2121
use std::os::raw::{c_int, c_uchar, c_uint, c_void};
2222

2323
/// Flag for context to enable no precomputation
24-
pub const SECP256K1_START_NONE: c_uint = (1 << 0) | 0;
24+
pub const SECP256K1_START_NONE: c_uint = 1;
2525
/// Flag for context to enable verification precomputation
26-
pub const SECP256K1_START_VERIFY: c_uint = (1 << 0) | (1 << 8);
26+
pub const SECP256K1_START_VERIFY: c_uint = 1 | (1 << 8);
2727
/// Flag for context to enable signing precomputation
28-
pub const SECP256K1_START_SIGN: c_uint = (1 << 0) | (1 << 9);
28+
pub const SECP256K1_START_SIGN: c_uint = 1 | (1 << 9);
2929
/// Flag for keys to indicate uncompressed serialization format
30-
pub const SECP256K1_SER_UNCOMPRESSED: c_uint = (1 << 1) | 0;
30+
pub const SECP256K1_SER_UNCOMPRESSED: c_uint = (1 << 1);
3131
/// Flag for keys to indicate compressed serialization format
3232
pub const SECP256K1_SER_COMPRESSED: c_uint = (1 << 1) | (1 << 8);
3333

@@ -74,6 +74,12 @@ impl PublicKey {
7474
pub unsafe fn blank() -> PublicKey { mem::uninitialized() }
7575
}
7676

77+
impl Default for PublicKey {
78+
fn default() -> Self {
79+
PublicKey::new()
80+
}
81+
}
82+
7783
impl hash::Hash for PublicKey {
7884
fn hash<H: hash::Hasher>(&self, state: &mut H) {
7985
state.write(&self.0)
@@ -99,13 +105,25 @@ impl Signature {
99105
pub unsafe fn blank() -> Signature { mem::uninitialized() }
100106
}
101107

108+
impl Default for Signature {
109+
fn default() -> Self {
110+
Signature::new()
111+
}
112+
}
113+
102114
impl RecoverableSignature {
103115
/// Create a new (zeroed) signature usable for the FFI interface
104116
pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) }
105117
/// Create a new (uninitialized) signature usable for the FFI interface
106118
pub unsafe fn blank() -> RecoverableSignature { mem::uninitialized() }
107119
}
108120

121+
impl Default for RecoverableSignature {
122+
fn default() -> Self {
123+
RecoverableSignature::new()
124+
}
125+
}
126+
109127
/// Library-internal representation of an ECDH shared secret
110128
#[repr(C)]
111129
pub struct SharedSecret([c_uchar; 32]);
@@ -119,6 +137,12 @@ impl SharedSecret {
119137
pub unsafe fn blank() -> SharedSecret { mem::uninitialized() }
120138
}
121139

140+
impl Default for SharedSecret {
141+
fn default() -> Self {
142+
SharedSecret::new()
143+
}
144+
}
145+
122146
#[cfg(not(feature = "fuzztarget"))]
123147
extern "C" {
124148
/// Default ECDH hash function
@@ -369,8 +393,8 @@ mod fuzz_dummy {
369393
}
370394

371395
// Signatures
372-
pub unsafe fn secp256k1_ecdsa_signature_parse_der(cx: *const Context, sig: *mut Signature,
373-
input: *const c_uchar, in_len: usize)
396+
pub unsafe fn secp256k1_ecdsa_signature_parse_der(_cx: *const Context, _sig: *mut Signature,
397+
_input: *const c_uchar, _in_len: usize)
374398
-> c_int {
375399
unimplemented!();
376400
}
@@ -385,8 +409,8 @@ mod fuzz_dummy {
385409
1
386410
}
387411

388-
pub unsafe fn ecdsa_signature_parse_der_lax(cx: *const Context, sig: *mut Signature,
389-
input: *const c_uchar, in_len: usize)
412+
pub unsafe fn ecdsa_signature_parse_der_lax(_cx: *const Context, _sig: *mut Signature,
413+
_input: *const c_uchar, _in_len: usize)
390414
-> c_int {
391415
unimplemented!();
392416
}
@@ -438,26 +462,26 @@ mod fuzz_dummy {
438462
1
439463
}
440464

441-
pub unsafe fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature,
442-
input64: *const c_uchar, recid: c_int)
465+
pub unsafe fn secp256k1_ecdsa_recoverable_signature_parse_compact(_cx: *const Context, _sig: *mut RecoverableSignature,
466+
_input64: *const c_uchar, _recid: c_int)
443467
-> c_int {
444468
unimplemented!();
445469
}
446470

447-
pub unsafe fn secp256k1_ecdsa_recoverable_signature_serialize_compact(cx: *const Context, output64: *const c_uchar,
448-
recid: *mut c_int, sig: *const RecoverableSignature)
471+
pub unsafe fn secp256k1_ecdsa_recoverable_signature_serialize_compact(_cx: *const Context, _output64: *const c_uchar,
472+
_recid: *mut c_int, _sig: *const RecoverableSignature)
449473
-> c_int {
450474
unimplemented!();
451475
}
452476

453-
pub unsafe fn secp256k1_ecdsa_recoverable_signature_convert(cx: *const Context, sig: *mut Signature,
454-
input: *const RecoverableSignature)
477+
pub unsafe fn secp256k1_ecdsa_recoverable_signature_convert(_cx: *const Context, _sig: *mut Signature,
478+
_input: *const RecoverableSignature)
455479
-> c_int {
456480
unimplemented!();
457481
}
458482

459-
pub unsafe fn secp256k1_ecdsa_signature_normalize(cx: *const Context, out_sig: *mut Signature,
460-
in_sig: *const Signature)
483+
pub unsafe fn secp256k1_ecdsa_signature_normalize(_cx: *const Context, _out_sig: *mut Signature,
484+
_in_sig: *const Signature)
461485
-> c_int {
462486
unimplemented!();
463487
}
@@ -521,10 +545,10 @@ mod fuzz_dummy {
521545
1
522546
}
523547

524-
pub unsafe fn secp256k1_ecdsa_recover(cx: *const Context,
525-
pk: *mut PublicKey,
526-
sig: *const RecoverableSignature,
527-
msg32: *const c_uchar)
548+
pub unsafe fn secp256k1_ecdsa_recover(_cx: *const Context,
549+
_pk: *mut PublicKey,
550+
_sig: *const RecoverableSignature,
551+
_msg32: *const c_uchar)
528552
-> c_int {
529553
unimplemented!();
530554
}
@@ -640,8 +664,8 @@ mod fuzz_dummy {
640664
out: *mut SharedSecret,
641665
point: *const PublicKey,
642666
scalar: *const c_uchar,
643-
hashfp: EcdhHashFn,
644-
data: *mut c_void,
667+
_hashfp: EcdhHashFn,
668+
_data: *mut c_void,
645669
) -> c_int {
646670
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
647671
if secp256k1_ec_seckey_verify(cx, scalar) != 1 { return 0; }

src/key.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ mod test {
393393
use super::{PublicKey, SecretKey};
394394
use super::super::constants;
395395

396-
use rand::{Rng, thread_rng};
396+
use rand::{Error, ErrorKind, RngCore, thread_rng};
397+
use rand_core::impls;
397398
use std::iter;
398399
use std::str::FromStr;
399400

@@ -462,8 +463,9 @@ mod test {
462463
fn test_out_of_range() {
463464

464465
struct BadRng(u8);
465-
impl Rng for BadRng {
466+
impl RngCore for BadRng {
466467
fn next_u32(&mut self) -> u32 { unimplemented!() }
468+
fn next_u64(&mut self) -> u64 { unimplemented!() }
467469
// This will set a secret key to a little over the
468470
// group order, then decrement with repeated calls
469471
// until it returns a valid key
@@ -478,6 +480,9 @@ mod test {
478480
data[31] = self.0;
479481
self.0 -= 1;
480482
}
483+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
484+
Ok(self.fill_bytes(dest))
485+
}
481486
}
482487

483488
let s = Secp256k1::new();
@@ -518,18 +523,28 @@ mod test {
518523
#[test]
519524
fn test_debug_output() {
520525
struct DumbRng(u32);
521-
impl Rng for DumbRng {
526+
impl RngCore for DumbRng {
522527
fn next_u32(&mut self) -> u32 {
523528
self.0 = self.0.wrapping_add(1);
524529
self.0
525530
}
531+
fn next_u64(&mut self) -> u64 {
532+
self.next_u32() as u64
533+
}
534+
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
535+
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
536+
}
537+
538+
fn fill_bytes(&mut self, dest: &mut [u8]) {
539+
impls::fill_bytes_via_next(self, dest);
540+
}
526541
}
527542

528543
let s = Secp256k1::new();
529544
let (sk, _) = s.generate_keypair(&mut DumbRng(0));
530545

531546
assert_eq!(&format!("{:?}", sk),
532-
"SecretKey(0200000001000000040000000300000006000000050000000800000007000000)");
547+
"SecretKey(0100000000000000020000000000000003000000000000000400000000000000)");
533548
}
534549

535550
#[test]
@@ -588,19 +603,29 @@ mod test {
588603
#[test]
589604
fn test_pubkey_serialize() {
590605
struct DumbRng(u32);
591-
impl Rng for DumbRng {
606+
impl RngCore for DumbRng {
592607
fn next_u32(&mut self) -> u32 {
593608
self.0 = self.0.wrapping_add(1);
594609
self.0
595610
}
611+
fn next_u64(&mut self) -> u64 {
612+
self.next_u32() as u64
613+
}
614+
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
615+
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
616+
}
617+
618+
fn fill_bytes(&mut self, dest: &mut [u8]) {
619+
impls::fill_bytes_via_next(self, dest);
620+
}
596621
}
597622

598623
let s = Secp256k1::new();
599624
let (_, pk1) = s.generate_keypair(&mut DumbRng(0));
600625
assert_eq!(&pk1.serialize_uncompressed()[..],
601-
&[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][..]);
626+
&[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][..]);
602627
assert_eq!(&pk1.serialize()[..],
603-
&[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][..]);
628+
&[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][..]);
604629
}
605630

606631
#[test]
@@ -732,5 +757,3 @@ mod test {
732757
assert_tokens(&pk, &[Token::BorrowedBytes(&PK_BYTES[..])]);
733758
}
734759
}
735-
736-

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
137137
#[cfg(all(test, feature = "unstable"))] extern crate test;
138138
#[cfg(any(test, feature = "rand"))] pub extern crate rand;
139+
#[cfg(any(test))] extern crate rand_core;
139140
#[cfg(feature = "serde")] pub extern crate serde;
140141
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
141142

@@ -222,7 +223,7 @@ pub fn from_i32(id: i32) -> Result<RecoveryId, Error> {
222223

223224
#[inline]
224225
/// Allows library users to convert recovery IDs to i32.
225-
pub fn to_i32(&self) -> i32 {
226+
pub fn to_i32(self) -> i32 {
226227
self.0
227228
}
228229
}
@@ -473,7 +474,7 @@ impl Message {
473474
/// Converts a `MESSAGE_SIZE`-byte slice to a message object
474475
#[inline]
475476
pub fn from_slice(data: &[u8]) -> Result<Message, Error> {
476-
if data == &[0; constants::MESSAGE_SIZE] {
477+
if data == [0; constants::MESSAGE_SIZE] {
477478
return Err(Error::InvalidMessage);
478479
}
479480

@@ -616,6 +617,12 @@ impl Secp256k1<All> {
616617
}
617618
}
618619

620+
impl Default for Secp256k1<All> {
621+
fn default() -> Self {
622+
Self::new()
623+
}
624+
}
625+
619626
impl Secp256k1<SignOnly> {
620627
/// Creates a new Secp256k1 context that can only be used for signing
621628
pub fn signing_only() -> Secp256k1<SignOnly> {
@@ -778,7 +785,7 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
778785

779786
#[cfg(test)]
780787
mod tests {
781-
use rand::{Rng, thread_rng};
788+
use rand::{RngCore, thread_rng};
782789
use std::str::FromStr;
783790

784791
use key::{SecretKey, PublicKey};
@@ -1227,4 +1234,3 @@ mod benches {
12271234
});
12281235
}
12291236
}
1230-

0 commit comments

Comments
 (0)