Skip to content

Commit 8150ef6

Browse files
committed
Adapt merlin on rand_core 0.4.2 to rand_core 0.5
1 parent e700862 commit 8150ef6

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

Cargo.toml

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ categories = ["cryptography", "no-std"]
1111
description = "Schnorr VRF, signatures, etc. using the Ristretto group"
1212
exclude = [ ".gitignore", "TESTVECTORS", "res/*" ]
1313
edition = "2018"
14+
cargo-features = ["rename-dependencies"]
1415

1516
# [badges]
1617
# travis-ci = { repository = "dalek-cryptography/ed25519-dalek", branch = "master"}
@@ -35,22 +36,26 @@ version = "1.2"
3536
default-features = false
3637

3738
[dependencies.rand]
38-
version = "0.6"
39+
version = "0.7"
3940
default-features = false
4041
optional = true
41-
features = ["i128_support"]
4242

4343
[dependencies.rand_core]
44-
version = "0.4.2" # 0.5
44+
version = "0.5"
45+
default-features = false
46+
47+
[dependencies.old_rand_core]
48+
package = "rand_core"
49+
version = "0.4.2"
4550
default-features = false
4651

4752
[dependencies.rand_os]
48-
version = "0.1.3" # 0.2.1
53+
version = "0.2.1"
4954
default-features = false
5055
optional = true
5156

5257
[dependencies.rand_chacha]
53-
version = "0.1" # 0.2
58+
version = "0.2"
5459
default-features = false
5560
optional = true
5661

@@ -73,10 +78,10 @@ default-features = false
7378
# features = ["zeroize_derive"]
7479

7580
[dev-dependencies]
76-
rand = "0.6"
77-
rand_chacha = "0.1.0"
81+
rand = "0.7"
82+
rand_chacha = "0.2"
7883
# hex = "0.3.2"
79-
hex-literal = "0.2.0"
84+
hex-literal = "0.2"
8085
sha2 = "^0.8"
8186
sha3 = "^0.8"
8287
bincode = "^0.9"

src/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ impl SigningTranscript for Transcript {
150150
Transcript::challenge_bytes(self, label, dest)
151151
}
152152

153-
fn witness_bytes_rng<R>(&self, label: &'static [u8], dest: &mut [u8], nonce_seeds: &[&[u8]], mut rng: R)
153+
fn witness_bytes_rng<R>(&self, label: &'static [u8], dest: &mut [u8], nonce_seeds: &[&[u8]], rng: R)
154154
where R: RngCore+CryptoRng
155155
{
156+
use ::old_rand_core::RngCore;
156157
let mut br = self.build_rng();
157158
for ns in nonce_seeds {
158159
br = br.rekey_with_witness_bytes(label, ns);
159160
}
160-
let mut r = br.finalize(&mut rng);
161+
let mut r = br.finalize(&mut super::RngCore5As4(rng));
161162
r.fill_bytes(dest)
162163
}
163164
}

src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ fn rand_hack() -> impl RngCore+CryptoRng {
238238

239239
#[cfg(all(feature = "rand_os", not(feature = "rand")))]
240240
fn rand_hack() -> impl RngCore+CryptoRng {
241-
::rand_os::OsRng::new().unwrap()
241+
::rand_os::OsRng
242242
}
243243

244244
#[cfg(not(feature = "rand_os"))]
@@ -257,6 +257,27 @@ fn rand_hack() -> impl RngCore+CryptoRng {
257257
PanicRng
258258
}
259259

260+
struct RngCore5As4<R: RngCore>(pub R);
261+
262+
impl<R: RngCore> ::old_rand_core::RngCore for RngCore5As4<R> {
263+
fn next_u32(&mut self) -> u32 { self.0.next_u32() }
264+
fn next_u64(&mut self) -> u64 { self.0.next_u64() }
265+
fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) }
266+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), ::old_rand_core::Error> {
267+
self.0.try_fill_bytes(dest).map_err(|_err| {
268+
let kind = ::old_rand_core::ErrorKind::Unavailable;
269+
let msg = "Unknown error from another rand_core version";
270+
// #[cfg(not(feature="std"))]
271+
::old_rand_core::Error::new(kind,msg)
272+
// #[cfg(feature="std")]
273+
// ::old_rand_core::Error::with_casue(kind,msg,_err.take_inner());
274+
})
275+
}
276+
}
277+
278+
impl<R: RngCore+CryptoRng> ::old_rand_core::CryptoRng for RngCore5As4<R> {}
279+
280+
260281
#[macro_use]
261282
mod serdey;
262283

src/sign.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,14 @@ where
313313
for pk in public_keys {
314314
t.commit_point(b"",pk.as_compressed());
315315
}
316-
t.build_rng().finalize(&mut rand_hack())
316+
t.build_rng().finalize(&mut RngCore5As4(rand_hack()))
317317
};
318318

319319
// Select a random 128-bit scalar for each signature.
320320
// We may represent these as scalars because we use
321321
// variable time 256 bit multiplication below.
322322
let rnd_128bit_scalar = |_| {
323+
use ::old_rand_core::RngCore;
323324
let mut r = [0u8; 16];
324325
csprng.fill_bytes(&mut r);
325326
Scalar::from(u128::from_le_bytes(r))

src/vrf.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,18 @@ impl VRFInOut {
354354
pub fn make_merlin_rng(&self, context: &[u8]) -> merlin::TranscriptRng {
355355
// Very insecure hack except for our commit_witness_bytes below
356356
struct ZeroFakeRng;
357-
impl ::rand_core::RngCore for ZeroFakeRng {
357+
impl ::old_rand_core::RngCore for ZeroFakeRng {
358358
fn next_u32(&mut self) -> u32 { panic!() }
359359
fn next_u64(&mut self) -> u64 { panic!() }
360360
fn fill_bytes(&mut self, dest: &mut [u8]) {
361361
for i in dest.iter_mut() { *i = 0; }
362362
}
363-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), ::rand_core::Error> {
363+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), ::old_rand_core::Error> {
364364
self.fill_bytes(dest);
365365
Ok(())
366366
}
367367
}
368-
impl ::rand_core::CryptoRng for ZeroFakeRng {}
368+
impl ::old_rand_core::CryptoRng for ZeroFakeRng {}
369369

370370
let mut t = Transcript::new(b"VRFResult");
371371
t.append_message(b"",context);
@@ -829,13 +829,14 @@ pub fn dleq_verify_batch(
829829
t.commit_point(b"",pk.as_compressed());
830830
p.commit(&mut t);
831831
}
832-
t.build_rng().finalize(&mut rand_hack())
832+
t.build_rng().finalize(&mut RngCore5As4(rand_hack()))
833833
};
834834

835835
// Select a random 128-bit scalar for each signature.
836836
// We may represent these as scalars because we use
837837
// variable time 256 bit multiplication below.
838838
let rnd_128bit_scalar = |_| {
839+
use ::old_rand_core::RngCore;
839840
let mut r = [0u8; 16];
840841
csprng.fill_bytes(&mut r);
841842
Scalar::from(u128::from_le_bytes(r))

0 commit comments

Comments
 (0)