Skip to content

Commit

Permalink
Merge pull request #815 from dhardy/from-rng
Browse files Browse the repository at this point in the history
Prepare new releases, from_rng Endianness and inlining
  • Loading branch information
dhardy committed Jun 6, 2019
2 parents cdf9a16 + 7c8284f commit fa83071
Show file tree
Hide file tree
Showing 46 changed files with 139 additions and 63 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,8 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.


## Unreleased
## [0.7.0 Pre-release] - 2019-06-06
Many changes (TODO)
- Enable fork protection of ReseedingRng without `std`
- Remove dependency on `winapi`

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand"
version = "0.6.5"
version = "0.7.0-pre.0"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down Expand Up @@ -54,7 +54,7 @@ members = [
]

[dependencies]
rand_core = { path = "rand_core", version = "0.4" }
rand_core = { path = "rand_core", version = "0.5" }
rand_pcg = { path = "rand_pcg", version = "0.1", optional = true }
# Do not depend on 'getrandom_package' directly; use the 'getrandom' feature!
getrandom_package = { version = "0.1.1", package = "getrandom", optional = true }
Expand Down Expand Up @@ -114,4 +114,4 @@ required-features = ["small_rng"]
all-features = true

[patch.crates-io]
rand_core = { path = "rand_core", version = "0.4" }
rand_core = { path = "rand_core", version = "0.5" }
3 changes: 3 additions & 0 deletions rand_chacha/CHANGELOG.md
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2019-06-06
- Rewrite based on the much faster `c2-chacha` crate (#789)

## [0.1.1] - 2019-01-04
- Disable `i128` and `u128` if the `target_os` is `emscripten` (#671: work-around Emscripten limitation)
- Update readme and doc links
Expand Down
4 changes: 2 additions & 2 deletions rand_chacha/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand_chacha"
version = "0.2.1"
version = "0.2.0"
authors = ["The Rand Project Developers", "The Rust Project Developers", "The CryptoCorrosion Contributors"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand All @@ -19,7 +19,7 @@ travis-ci = { repository = "rust-random/rand" }
appveyor = { repository = "rust-random/rand" }

[dependencies]
rand_core = { path = "../rand_core", version = "0.4" }
rand_core = { path = "../rand_core", version = "0.5" }
c2-chacha = { version = "0.2.2", default-features = false }

[build-dependencies]
Expand Down
5 changes: 4 additions & 1 deletion rand_chacha/README.md
Expand Up @@ -16,11 +16,14 @@ as an RNG. It is an improved variant of the Salsa20 cipher family, which was
selected as one of the "stream ciphers suitable for widespread adoption" by
eSTREAM[^2].

The RNGs provided by this crate are implemented via the fast stream ciphers of
the [`c2-chacha`](https://crates.io/crates/c2-chacha) crate.

Links:

- [API documentation (master)](https://rust-random.github.io/rand/rand_chacha)
- [API documentation (docs.rs)](https://docs.rs/rand_chacha)
- [Changelog](CHANGELOG.md)
- [Changelog](https://github.com/rust-random/rand/blob/master/rand_chacha/CHANGELOG.md)

[rand]: https://crates.io/crates/rand
[^1]: D. J. Bernstein, [*ChaCha, a variant of Salsa20*](
Expand Down
5 changes: 5 additions & 0 deletions rand_core/CHANGELOG.md
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0] - 2019-06-06
- Enable testing with Miri and fix incorrect pointer usages (#779, #780, #781, #783, #784)
- Rewrite `Error` type and adjust API (#800)
- Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64`

## [0.4.0] - 2019-01-24
- Disable the `std` feature by default (#702)

Expand Down
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand_core"
version = "0.4.0"
version = "0.5.0"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
7 changes: 6 additions & 1 deletion rand_core/README.md
Expand Up @@ -25,7 +25,7 @@ Links:

- [API documentation (master)](https://rust-random.github.io/rand/rand_core)
- [API documentation (docs.rs)](https://docs.rs/rand_core)
- [Changelog](CHANGELOG.md)
- [Changelog](https://github.com/rust-random/rand/blob/master/rand_core/CHANGELOG.md)

[rand]: https://crates.io/crates/rand

Expand All @@ -42,6 +42,11 @@ The traits and error types are also available via `rand`.

## Versions

The current version is:
```
rand_core = "0.5.0"
```

Rand libs have inter-dependencies and make use of the
[semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits
compatible across crate versions. (This is especially important for `RngCore`
Expand Down
29 changes: 23 additions & 6 deletions rand_core/src/block.rs
Expand Up @@ -131,6 +131,7 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
impl<R: BlockRngCore> BlockRng<R> {
/// Create a new `BlockRng` from an existing RNG implementing
/// `BlockRngCore`. Results will be generated on first use.
#[inline]
pub fn new(core: R) -> BlockRng<R>{
let results_empty = R::Results::default();
BlockRng {
Expand All @@ -145,18 +146,21 @@ impl<R: BlockRngCore> BlockRng<R> {
/// If this is equal to or larger than the size of the result buffer then
/// the buffer is "empty" and `generate()` must be called to produce new
/// results.
#[inline(always)]
pub fn index(&self) -> usize {
self.index
}

/// Reset the number of available results.
/// This will force a new set of results to be generated on next use.
#[inline]
pub fn reset(&mut self) {
self.index = self.results.as_ref().len();
}

/// Generate a new set of results immediately, setting the index to the
/// given value.
#[inline]
pub fn generate_and_set(&mut self, index: usize) {
assert!(index < self.results.as_ref().len());
self.core.generate(&mut self.results);
Expand All @@ -167,7 +171,7 @@ impl<R: BlockRngCore> BlockRng<R> {
impl<R: BlockRngCore<Item=u32>> RngCore for BlockRng<R>
where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
{
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
if self.index >= self.results.as_ref().len() {
self.generate_and_set(0);
Expand All @@ -178,7 +182,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
value
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
let read_u64 = |results: &[u32], index| {
if cfg!(any(target_endian = "little")) {
Expand Down Expand Up @@ -210,6 +214,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
}
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
while read_len < dest.len() {
Expand All @@ -225,23 +230,26 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
}
}

#[inline(always)]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
Ok(self.fill_bytes(dest))
}
}

impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
type Seed = R::Seed;

#[inline(always)]
fn from_seed(seed: Self::Seed) -> Self {
Self::new(R::from_seed(seed))
}

#[inline(always)]
fn seed_from_u64(seed: u64) -> Self {
Self::new(R::seed_from_u64(seed))
}

#[inline(always)]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
Ok(Self::new(R::from_rng(rng)?))
}
Expand Down Expand Up @@ -296,6 +304,7 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
impl<R: BlockRngCore> BlockRng64<R> {
/// Create a new `BlockRng` from an existing RNG implementing
/// `BlockRngCore`. Results will be generated on first use.
#[inline]
pub fn new(core: R) -> BlockRng64<R>{
let results_empty = R::Results::default();
BlockRng64 {
Expand All @@ -311,19 +320,22 @@ impl<R: BlockRngCore> BlockRng64<R> {
/// If this is equal to or larger than the size of the result buffer then
/// the buffer is "empty" and `generate()` must be called to produce new
/// results.
#[inline(always)]
pub fn index(&self) -> usize {
self.index
}

/// Reset the number of available results.
/// This will force a new set of results to be generated on next use.
#[inline]
pub fn reset(&mut self) {
self.index = self.results.as_ref().len();
self.half_used = false;
}

/// Generate a new set of results immediately, setting the index to the
/// given value.
#[inline]
pub fn generate_and_set(&mut self, index: usize) {
assert!(index < self.results.as_ref().len());
self.core.generate(&mut self.results);
Expand All @@ -335,7 +347,7 @@ impl<R: BlockRngCore> BlockRng64<R> {
impl<R: BlockRngCore<Item=u64>> RngCore for BlockRng64<R>
where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
{
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
let mut index = self.index * 2 - self.half_used as usize;
if index >= self.results.as_ref().len() * 2 {
Expand All @@ -361,7 +373,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
}
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
if self.index >= self.results.as_ref().len() {
self.core.generate(&mut self.results);
Expand All @@ -374,6 +386,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
value
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut read_len = 0;
self.half_used = false;
Expand All @@ -392,6 +405,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
}
}

#[inline(always)]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Ok(self.fill_bytes(dest))
}
Expand All @@ -400,14 +414,17 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
type Seed = R::Seed;

#[inline(always)]
fn from_seed(seed: Self::Seed) -> Self {
Self::new(R::from_seed(seed))
}

#[inline(always)]
fn seed_from_u64(seed: u64) -> Self {
Self::new(R::seed_from_u64(seed))
}

#[inline(always)]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
Ok(Self::new(R::from_rng(rng)?))
}
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/Cargo.toml
Expand Up @@ -19,7 +19,7 @@ travis-ci = { repository = "rust-random/rand" }
appveyor = { repository = "rust-random/rand" }

[dependencies]
rand = { path = "..", version = ">=0.5, <=0.7" }
rand = { path = "..", version = ">=0.5, <=0.7.0-pre.9" }

[dev-dependencies]
rand_pcg = { version = "0.1", path = "../rand_pcg" }
Expand Down
4 changes: 4 additions & 0 deletions rand_hc/CHANGELOG.md
Expand Up @@ -4,5 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.1] - 2019-06-06
- Bump `rand_core` version
- Adjust usage of `#[inline]`

## [0.1.0] - 2018-10-17
- Pulled out of the Rand crate
4 changes: 2 additions & 2 deletions rand_hc/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand_hc"
version = "0.1.0"
version = "0.1.1"
authors = ["The Rand Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand All @@ -18,4 +18,4 @@ travis-ci = { repository = "rust-random/rand" }
appveyor = { repository = "rust-random/rand" }

[dependencies]
rand_core = { path = "../rand_core", version = "0.4" }
rand_core = { path = "../rand_core", version = "0.5" }
2 changes: 1 addition & 1 deletion rand_hc/README.md
Expand Up @@ -19,7 +19,7 @@ Links:

- [API documentation (master)](https://rust-random.github.io/rand/rand_hc)
- [API documentation (docs.rs)](https://docs.rs/rand_hc)
- [Changelog](CHANGELOG.md)
- [Changelog](https://github.com/rust-random/rand/blob/master/rand_hc/CHANGELOG.md)

[rand]: https://crates.io/crates/rand
[^1]: Hongjun Wu (2008). ["The Stream Cipher HC-128"](
Expand Down
9 changes: 7 additions & 2 deletions rand_hc/src/hc128.rs
Expand Up @@ -67,20 +67,22 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
pub struct Hc128Rng(BlockRng<Hc128Core>);

impl RngCore for Hc128Rng {
#[inline(always)]
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}

#[inline(always)]
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
Expand All @@ -89,10 +91,12 @@ impl RngCore for Hc128Rng {
impl SeedableRng for Hc128Rng {
type Seed = <Hc128Core as SeedableRng>::Seed;

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Hc128Rng(BlockRng::<Hc128Core>::from_seed(seed))
}

#[inline]
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
BlockRng::<Hc128Core>::from_rng(rng).map(Hc128Rng)
}
Expand Down Expand Up @@ -268,6 +272,7 @@ impl Hc128Core {
// Initialize an HC-128 random number generator. The seed has to be
// 256 bits in length (`[u32; 8]`), matching the 128 bit `key` followed by
// 128 bit `iv` when HC-128 where to be used as a stream cipher.
#[inline(always)] // single use: SeedableRng::from_seed
fn init(seed: [u32; SEED_WORDS]) -> Self {
#[inline]
fn f1(x: u32) -> u32 {
Expand Down
5 changes: 5 additions & 0 deletions rand_isaac/CHANGELOG.md
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.2] - 2019-06-06
- Bump `rand_core` version
- Remove deprecated code
- Adjust usage of `#[inline]`

## [0.1.1] - 2018-11-26
- Fix `rand_core` version requirement
- Fix doc links
Expand Down

0 comments on commit fa83071

Please sign in to comment.