Skip to content

Commit ac0cf3c

Browse files
authored
Merge pull request #47 from dhardy/work
rand_jitter v0.4, platform_ns_time feature, MSRV=1.51, rustfmt
2 parents 0f25f7e + 8b94537 commit ac0cf3c

File tree

9 files changed

+108
-55
lines changed

9 files changed

+108
-55
lines changed

.github/workflows/test.yml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ jobs:
4848
target: x86_64-pc-windows-msvc
4949
toolchain: beta
5050
# Test both windows-gnu and windows-msvc; use beta rust on one
51-
- os: ubuntu-latest
52-
target: x86_64-unknown-linux-gnu
53-
toolchain: 1.36.0 # MSRV
5451
- os: ubuntu-latest
5552
deps: sudo apt-get update ; sudo apt install gcc-multilib
5653
target: i686-unknown-linux-gnu
@@ -85,12 +82,38 @@ jobs:
8582
cargo test --target ${{ matrix.target }} --manifest-path rand_xoshiro/Cargo.toml --all-features
8683
cargo test --target ${{ matrix.target }} --manifest-path rand_jitter/Cargo.toml --all-features
8784
85+
msrv:
86+
name: MSRV for rand_isaac / rand_xorshift / rand_xoshiro
87+
runs-on: ubuntu-latest
88+
steps:
89+
- uses: actions/checkout@v4
90+
- uses: dtolnay/[email protected] # older versions may work (untested)
91+
- run: cd rand_isaac && cargo test --all-features
92+
- run: cd rand_xorshift && cargo test --all-features
93+
- run: cd rand_xoshiro && cargo test --all-features
94+
95+
msrv_hc:
96+
name: MSRV for rand_hc
97+
runs-on: ubuntu-latest
98+
steps:
99+
- uses: actions/checkout@v4
100+
- uses: dtolnay/[email protected]
101+
- run: cd rand_hc && cargo test --all-features
102+
103+
msrv_jitter:
104+
name: MSRV for rand_jitter
105+
runs-on: ubuntu-latest
106+
steps:
107+
- uses: actions/checkout@v4
108+
- uses: dtolnay/[email protected]
109+
- run: cd rand_jitter && cargo test --features std
110+
88111
test-cross:
89112
runs-on: ubuntu-latest
90113
strategy:
91114
fail-fast: false
92115
matrix:
93-
target: [mips-unknown-linux-gnu]
116+
target: [powerpc-unknown-linux-gnu]
94117
toolchain: [stable]
95118

96119
steps:

rand_jitter/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.0] - 2023-11-20
8+
### Changed
9+
- Update to `rand_core` v0.6
10+
- Bump MSRV to 1.51
11+
- Use return-position impl trait to fix `JitterRng::new` (#16)
12+
713
## [0.3.0] - 2020-09-07
814
### Changed
9-
- `JitterRng::new_with_timer` accepts closures
15+
- `JitterRng::new_with_timer` accepts closures (this breaks `JitterRng::new`)
1016

1117
## [0.2.1] - 2019-08-16
1218
### Changed

rand_jitter/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand_jitter"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["The Rand Project Developers"]
55
license = "MIT OR Apache-2.0"
66
readme = "README.md"
@@ -9,6 +9,7 @@ documentation = "https://docs.rs/rand_jitter"
99
description = "Random number generator based on timing jitter"
1010
keywords = ["random", "rng", "os"]
1111
edition = "2018"
12+
rust-version = "1.51"
1213

1314
[dependencies]
1415
rand_core = { version = "0.6" }

rand_jitter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/)
66
[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_jitter)
77
[![API](https://docs.rs/rand_jitter/badge.svg)](https://docs.rs/rand_jitter)
8-
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.36+-lightgray.svg)](https://github.com/rust-random/rngs#rust-version-requirements)
8+
[![Minimum rustc version](https://img.shields.io/badge/rustc-1.51-lightgray.svg)](https://github.com/rust-random/rngs#rust-version-requirements)
99

1010
Non-physical true random number generator based on timing jitter.
1111

rand_jitter/benches/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![feature(test)]
22
#![cfg(std)]
33

4-
use test::Bencher;
54
use rand_jitter::rand_core::RngCore;
5+
use test::Bencher;
66

77
#[bench]
88
fn bench_add_two(b: &mut Bencher) {
@@ -14,4 +14,3 @@ fn bench_add_two(b: &mut Bencher) {
1414
});
1515
b.bytes = buf.len() as u64;
1616
}
17-

rand_jitter/src/error.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
use rand_core::Error;
1110
use core::fmt;
11+
use rand_core::Error;
1212

1313
/// Base code for all `JitterRng` errors
1414
const ERROR_BASE: u32 = 0xAE53_0400;
1515

1616
/// An error that can occur when [`JitterRng::test_timer`] fails.
17-
///
17+
///
1818
/// All variants have a value of 0xAE530400 = 2924676096 plus a small
1919
/// increment (1 through 5).
2020
///
@@ -68,12 +68,13 @@ impl From<TimerError> for Error {
6868
fn from(err: TimerError) -> Error {
6969
// Timer check is already quite permissive of failures so we don't
7070
// expect false-positive failures, i.e. any error is irrecoverable.
71-
#[cfg(feature = "std")] {
71+
#[cfg(feature = "std")]
72+
{
7273
Error::new(err)
7374
}
74-
#[cfg(not(feature = "std"))] {
75+
#[cfg(not(feature = "std"))]
76+
{
7577
Error::from(core::num::NonZeroU32::new(err as u32).unwrap())
7678
}
7779
}
7880
}
79-

rand_jitter/src/lib.rs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
//! [Jitterentropy]: http://www.chronox.de/jent.html
4242
//! [discussion]: https://github.com/rust-random/rand/issues/699
4343
44-
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
45-
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
46-
html_root_url = "https://rust-random.github.io/rand/")]
47-
44+
#![doc(
45+
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
46+
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
47+
html_root_url = "https://rust-random.github.io/rand/"
48+
)]
4849
#![deny(missing_docs)]
4950
#![deny(missing_debug_implementations)]
5051
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
51-
5252
// Note: the C implementation of `Jitterentropy` relies on being compiled
5353
// without optimizations. This implementation goes through lengths to make the
5454
// compiler not optimize out code which does influence timing jitter, but is
@@ -64,7 +64,7 @@ pub use rand_core;
6464
macro_rules! doc_comment {
6565
($x:expr) => {
6666
#[doc = $x]
67-
extern {}
67+
fn _doc_comment() {}
6868
};
6969
}
7070

@@ -102,12 +102,12 @@ macro_rules! error { ($($x:tt)*) => (
102102
}
103103
) }
104104

105+
mod error;
105106
#[cfg(feature = "std")]
106107
mod platform;
107-
mod error;
108108

109-
use rand_core::{RngCore, Error, impls};
110109
pub use crate::error::TimerError;
110+
use rand_core::{impls, Error, RngCore};
111111

112112
use core::{fmt, mem, ptr};
113113
#[cfg(feature = "std")]
@@ -191,7 +191,9 @@ impl<F> fmt::Debug for JitterRng<F> {
191191
}
192192

193193
impl<F> Clone for JitterRng<F>
194-
where F: Clone {
194+
where
195+
F: Clone,
196+
{
195197
fn clone(&self) -> JitterRng<F> {
196198
JitterRng {
197199
data: self.data,
@@ -209,9 +211,7 @@ where F: Clone {
209211
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
210212
static JITTER_ROUNDS: AtomicUsize = AtomicUsize::new(0);
211213

212-
impl<F> JitterRng<F>
213-
where F: Fn() -> u64 + Send + Sync {
214-
/* FIXME: this method is broken - see #16
214+
impl JitterRng<()> {
215215
/// Create a new `JitterRng`. Makes use of `std::time` for a timer, or a
216216
/// platform-specific function with higher accuracy if necessary and
217217
/// available.
@@ -220,7 +220,7 @@ where F: Fn() -> u64 + Send + Sync {
220220
/// hundred times. If this does not pass basic quality tests, an error is
221221
/// returned. The test result is cached to make subsequent calls faster.
222222
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
223-
pub fn new() -> Result<JitterRng<fn() -> u64>, TimerError> {
223+
pub fn new() -> Result<JitterRng<impl Fn() -> u64 + Send + Sync>, TimerError> {
224224
if cfg!(target_arch = "wasm32") {
225225
return Err(TimerError::NoTimer);
226226
}
@@ -239,8 +239,12 @@ where F: Fn() -> u64 + Send + Sync {
239239
state.gen_entropy();
240240
Ok(state)
241241
}
242-
*/
242+
}
243243

244+
impl<F> JitterRng<F>
245+
where
246+
F: Fn() -> u64 + Send + Sync,
247+
{
244248
/// Create a new `JitterRng`.
245249
/// A custom timer can be supplied, making it possible to use `JitterRng` in
246250
/// `no_std` environments.
@@ -352,7 +356,7 @@ where F: Fn() -> u64 + Send + Sync {
352356
// the loop in this function implies that careful retesting must be done.
353357
#[inline(never)]
354358
fn lfsr_time(&mut self, time: u64, var_rounds: bool) {
355-
fn lfsr(mut data: u64, time: u64) -> u64{
359+
fn lfsr(mut data: u64, time: u64) -> u64 {
356360
for i in 1..65 {
357361
let mut tmp = time << (64 - i);
358362
tmp >>= 64 - 1;
@@ -382,7 +386,9 @@ where F: Fn() -> u64 + Send + Sync {
382386
// other rounds are not optimised out, we first run all but the last
383387
// round on a throw-away value instead of the real `self.data`.
384388
let mut lfsr_loop_cnt = 0;
385-
if var_rounds { lfsr_loop_cnt = self.random_loop_cnt(4) };
389+
if var_rounds {
390+
lfsr_loop_cnt = self.random_loop_cnt(4)
391+
};
386392

387393
let mut throw_away: u64 = 0;
388394
for _ in 0..lfsr_loop_cnt {
@@ -412,7 +418,9 @@ where F: Fn() -> u64 + Send + Sync {
412418
#[inline(never)]
413419
fn memaccess(&mut self, mem: &mut [u8; MEMORY_SIZE], var_rounds: bool) {
414420
let mut acc_loop_cnt = 128;
415-
if var_rounds { acc_loop_cnt += self.random_loop_cnt(4) };
421+
if var_rounds {
422+
acc_loop_cnt += self.random_loop_cnt(4)
423+
};
416424

417425
let mut index = self.mem_prev_index as usize;
418426
for _ in 0..acc_loop_cnt {
@@ -454,7 +462,9 @@ where F: Fn() -> u64 + Send + Sync {
454462

455463
// Check whether we have a stuck measurement (i.e. does the last
456464
// measurement holds entropy?).
457-
if ec.stuck(current_delta) { return None };
465+
if ec.stuck(current_delta) {
466+
return None;
467+
};
458468

459469
// Rotate the data buffer by a prime number (any odd number would
460470
// do) to ensure that every bit position of the input time stamp
@@ -599,21 +609,29 @@ where F: Fn() -> u64 + Send + Sync {
599609
// already have had an impact on the caches, branch prediction,
600610
// etc. with the goal to clear it to get the worst case
601611
// measurements.
602-
if i < CLEARCACHE { continue; }
612+
if i < CLEARCACHE {
613+
continue;
614+
}
603615

604-
if ec.stuck(delta) { count_stuck += 1; }
616+
if ec.stuck(delta) {
617+
count_stuck += 1;
618+
}
605619

606620
// Test whether we have an increasing timer.
607-
if time2 <= time { time_backwards += 1; }
621+
if time2 <= time {
622+
time_backwards += 1;
623+
}
608624

609625
// Count the number of times the counter increases in steps of 100ns
610626
// or greater.
611-
if (delta % 100) == 0 { count_mod += 1; }
627+
if (delta % 100) == 0 {
628+
count_mod += 1;
629+
}
612630

613631
// Ensure that we have a varying delta timer which is necessary for
614632
// the calculation of entropy -- perform this check only after the
615633
// first loop is executed as we need to prime the old_delta value
616-
delta_sum += (delta - old_delta).abs() as u64;
634+
delta_sum += (delta - old_delta).unsigned_abs() as u64;
617635
old_delta = delta;
618636
}
619637

@@ -674,14 +692,15 @@ where F: Fn() -> u64 + Send + Sync {
674692
if delta_average >= 16 {
675693
let log2 = 64 - delta_average.leading_zeros();
676694
// Do something similar to roundup(64/(log2/2)):
677-
Ok( ((64u32 * 2 + log2 - 1) / log2) as u8)
695+
Ok(((64u32 * 2 + log2 - 1) / log2) as u8)
678696
} else {
679697
// For values < 16 the rounding error becomes too large, use a
680698
// lookup table.
681699
// Values 0 and 1 are invalid, and filtered out by the
682700
// `delta_sum < TESTLOOPCOUNT` test above.
683-
let log2_lookup = [0, 0, 128, 81, 64, 56, 50, 46,
684-
43, 41, 39, 38, 36, 35, 34, 33];
701+
let log2_lookup = [
702+
0, 0, 128, 81, 64, 56, 50, 46, 43, 41, 39, 38, 36, 35, 34, 33,
703+
];
685704
Ok(log2_lookup[delta_average as usize])
686705
}
687706
}
@@ -721,8 +740,10 @@ fn black_box<T>(dummy: T) -> T {
721740
}
722741
}
723742

724-
impl<F> RngCore for JitterRng<F>
725-
where F: Fn() -> u64 + Send + Sync {
743+
impl<F> RngCore for JitterRng<F>
744+
where
745+
F: Fn() -> u64 + Send + Sync,
746+
{
726747
fn next_u32(&mut self) -> u32 {
727748
// We want to use both parts of the generated entropy
728749
if self.data_half_used {
@@ -736,8 +757,8 @@ where F: Fn() -> u64 + Send + Sync {
736757
}
737758

738759
fn next_u64(&mut self) -> u64 {
739-
self.data_half_used = false;
740-
self.gen_entropy()
760+
self.data_half_used = false;
761+
self.gen_entropy()
741762
}
742763

743764
fn fill_bytes(&mut self, dest: &mut [u8]) {

rand_jitter/src/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn get_nstime() -> u64 {
2222
#[cfg(any(target_os = "macos", target_os = "ios"))]
2323
pub fn get_nstime() -> u64 {
2424
use libc;
25-
25+
2626
// On Mac OS and iOS std::time::SystemTime only has 1000ns resolution.
2727
// We use `mach_absolute_time` instead. This provides a CPU dependent
2828
// unit, to get real nanoseconds the result should by multiplied by

0 commit comments

Comments
 (0)