Skip to content

Commit

Permalink
Auto merge of #29117 - mseri:patch-5a, r=nrc
Browse files Browse the repository at this point in the history
r? @nrc

Re-submission of the closed PR #29054 with the additional rustfmt-zation of the full librand.
  • Loading branch information
bors committed Oct 18, 2015
2 parents d3f4978 + 8a0b9c0 commit 9ed32bb
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 145 deletions.
45 changes: 26 additions & 19 deletions src/librand/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ const CHACHA_ROUNDS: usize = 20; // Cryptographically secure from 8 upwards as o
/// Salsa20*](http://cr.yp.to/chacha.html)
#[derive(Copy, Clone)]
pub struct ChaChaRng {
buffer: [u32; STATE_WORDS], // Internal buffer of output
state: [u32; STATE_WORDS], // Initial state
index: usize, // Index into state
buffer: [u32; STATE_WORDS], // Internal buffer of output
state: [u32; STATE_WORDS], // Initial state
index: usize, // Index into state
}

static EMPTY: ChaChaRng = ChaChaRng {
buffer: [0; STATE_WORDS],
state: [0; STATE_WORDS],
index: STATE_WORDS
buffer: [0; STATE_WORDS],
state: [0; STATE_WORDS],
index: STATE_WORDS,
};


Expand Down Expand Up @@ -95,9 +95,9 @@ impl ChaChaRng {
/// associated with a particular nonce can call this function with
/// arguments `0, desired_nonce`.
pub fn set_counter(&mut self, counter_low: u64, counter_high: u64) {
self.state[12] = (counter_low >> 0) as u32;
self.state[12] = (counter_low >> 0) as u32;
self.state[13] = (counter_low >> 32) as u32;
self.state[14] = (counter_high >> 0) as u32;
self.state[14] = (counter_high >> 0) as u32;
self.state[15] = (counter_high >> 32) as u32;
self.index = STATE_WORDS; // force recomputation
}
Expand Down Expand Up @@ -127,7 +127,7 @@ impl ChaChaRng {
self.state[3] = 0x6B206574;

for i in 0..KEY_WORDS {
self.state[4+i] = key[i];
self.state[4 + i] = key[i];
}

self.state[12] = 0;
Expand All @@ -144,11 +144,17 @@ impl ChaChaRng {
self.index = 0;
// update 128-bit counter
self.state[12] += 1;
if self.state[12] != 0 { return };
if self.state[12] != 0 {
return;
}
self.state[13] += 1;
if self.state[13] != 0 { return };
if self.state[13] != 0 {
return;
}
self.state[14] += 1;
if self.state[14] != 0 { return };
if self.state[14] != 0 {
return;
}
self.state[15] += 1;
}
}
Expand All @@ -172,7 +178,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
// reset state
self.init(&[0; KEY_WORDS]);
// set key in place
let key = &mut self.state[4 .. 4+KEY_WORDS];
let key = &mut self.state[4..4 + KEY_WORDS];
for (k, s) in key.iter_mut().zip(seed) {
*k = *s;
}
Expand All @@ -191,7 +197,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {

impl Rand for ChaChaRng {
fn rand<R: Rng>(other: &mut R) -> ChaChaRng {
let mut key : [u32; KEY_WORDS] = [0; KEY_WORDS];
let mut key: [u32; KEY_WORDS] = [0; KEY_WORDS];
for word in &mut key {
*word = other.gen();
}
Expand Down Expand Up @@ -219,7 +225,7 @@ mod tests {

#[test]
fn test_rng_seeded() {
let seed : &[_] = &[0,1,2,3,4,5,6,7];
let seed: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);
let mut rb: ChaChaRng = SeedableRng::from_seed(seed);
assert!(order::equals(ra.gen_ascii_chars().take(100),
Expand All @@ -239,10 +245,11 @@ mod tests {
}

#[test]
#[rustfmt_skip]
fn test_rng_true_values() {
// Test vectors 1 and 2 from
// http://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
let seed : &[_] = &[0; 8];
let seed: &[_] = &[0; 8];
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);

let v = (0..16).map(|_| ra.next_u32()).collect::<Vec<_>>();
Expand All @@ -260,12 +267,12 @@ mod tests {
0x281fed31, 0x45fb0a51, 0x1f0ae1ac, 0x6f4d794b));


let seed : &[_] = &[0,1,2,3,4,5,6,7];
let seed: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);

// Store the 17*i-th 32-bit word,
// i.e., the i-th word of the i-th 16-word block
let mut v : Vec<u32> = Vec::new();
let mut v: Vec<u32> = Vec::new();
for _ in 0..16 {
v.push(ra.next_u32());
for _ in 0..16 {
Expand All @@ -282,7 +289,7 @@ mod tests {

#[test]
fn test_rng_clone() {
let seed : &[_] = &[0; 8];
let seed: &[_] = &[0; 8];
let mut rng: ChaChaRng = SeedableRng::from_seed(seed);
let mut clone = rng.clone();
for _ in 0..16 {
Expand Down
16 changes: 10 additions & 6 deletions src/librand/distributions/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ pub struct Exp1(pub f64);
// This could be done via `-rng.gen::<f64>().ln()` but that is slower.
impl Rand for Exp1 {
#[inline]
fn rand<R:Rng>(rng: &mut R) -> Exp1 {
fn rand<R: Rng>(rng: &mut R) -> Exp1 {
#[inline]
fn pdf(x: f64) -> f64 {
(-x).exp()
}
#[inline]
fn zero_case<R:Rng>(rng: &mut R, _u: f64) -> f64 {
fn zero_case<R: Rng>(rng: &mut R, _u: f64) -> f64 {
ziggurat_tables::ZIG_EXP_R - rng.gen::<f64>().ln()
}

Exp1(ziggurat(rng, false,
Exp1(ziggurat(rng,
false,
&ziggurat_tables::ZIG_EXP_X,
&ziggurat_tables::ZIG_EXP_F,
pdf, zero_case))
pdf,
zero_case))
}
}

Expand All @@ -59,7 +61,7 @@ impl Rand for Exp1 {
#[derive(Copy, Clone)]
pub struct Exp {
/// `lambda` stored as `1/lambda`, since this is what we scale by.
lambda_inverse: f64
lambda_inverse: f64,
}

impl Exp {
Expand All @@ -72,7 +74,9 @@ impl Exp {
}

impl Sample<f64> for Exp {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}
impl IndependentSample<f64> for Exp {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
Expand Down
52 changes: 32 additions & 20 deletions src/librand/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct Gamma {
enum GammaRepr {
Large(GammaLargeShape),
One(Exp),
Small(GammaSmallShape)
Small(GammaSmallShape),
}

// These two helpers could be made public, but saving the
Expand All @@ -65,7 +65,7 @@ enum GammaRepr {
/// shape parameters.
struct GammaSmallShape {
inv_shape: f64,
large_shape: GammaLargeShape
large_shape: GammaLargeShape,
}

/// Gamma distribution where the shape parameter is larger than 1.
Expand All @@ -75,7 +75,7 @@ struct GammaSmallShape {
struct GammaLargeShape {
scale: f64,
c: f64,
d: f64
d: f64,
}

impl Gamma {
Expand All @@ -88,9 +88,9 @@ impl Gamma {
assert!(scale > 0.0, "Gamma::new called with scale <= 0");

let repr = match shape {
1.0 => One(Exp::new(1.0 / scale)),
1.0 => One(Exp::new(1.0 / scale)),
0.0 ... 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
_ => Large(GammaLargeShape::new_raw(shape, scale))
_ => Large(GammaLargeShape::new_raw(shape, scale)),
};
Gamma { repr: repr }
}
Expand All @@ -100,7 +100,7 @@ impl GammaSmallShape {
fn new_raw(shape: f64, scale: f64) -> GammaSmallShape {
GammaSmallShape {
inv_shape: 1. / shape,
large_shape: GammaLargeShape::new_raw(shape + 1.0, scale)
large_shape: GammaLargeShape::new_raw(shape + 1.0, scale),
}
}
}
Expand All @@ -111,19 +111,25 @@ impl GammaLargeShape {
GammaLargeShape {
scale: scale,
c: 1. / (9. * d).sqrt(),
d: d
d: d,
}
}
}

impl Sample<f64> for Gamma {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}
impl Sample<f64> for GammaSmallShape {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}
impl Sample<f64> for GammaLargeShape {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}

impl IndependentSample<f64> for Gamma {
Expand All @@ -148,16 +154,16 @@ impl IndependentSample<f64> for GammaLargeShape {
let StandardNormal(x) = rng.gen::<StandardNormal>();
let v_cbrt = 1.0 + self.c * x;
if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0
continue
continue;
}

let v = v_cbrt * v_cbrt * v_cbrt;
let Open01(u) = rng.gen::<Open01<f64>>();

let x_sqr = x * x;
if u < 1.0 - 0.0331 * x_sqr * x_sqr ||
u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) {
return self.d * v * self.scale
u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) {
return self.d * v * self.scale;
}
}
}
Expand Down Expand Up @@ -196,7 +202,9 @@ impl ChiSquared {
}
}
impl Sample<f64> for ChiSquared {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}
impl IndependentSample<f64> for ChiSquared {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
Expand All @@ -206,7 +214,7 @@ impl IndependentSample<f64> for ChiSquared {
let StandardNormal(norm) = rng.gen::<StandardNormal>();
norm * norm
}
DoFAnythingElse(ref g) => g.ind_sample(rng)
DoFAnythingElse(ref g) => g.ind_sample(rng),
}
}
}
Expand Down Expand Up @@ -234,12 +242,14 @@ impl FisherF {
FisherF {
numer: ChiSquared::new(m),
denom: ChiSquared::new(n),
dof_ratio: n / m
dof_ratio: n / m,
}
}
}
impl Sample<f64> for FisherF {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}
impl IndependentSample<f64> for FisherF {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
Expand All @@ -251,7 +261,7 @@ impl IndependentSample<f64> for FisherF {
/// freedom.
pub struct StudentT {
chi: ChiSquared,
dof: f64
dof: f64,
}

impl StudentT {
Expand All @@ -261,12 +271,14 @@ impl StudentT {
assert!(n > 0.0, "StudentT::new called with `n <= 0`");
StudentT {
chi: ChiSquared::new(n),
dof: n
dof: n,
}
}
}
impl Sample<f64> for StudentT {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
self.ind_sample(rng)
}
}
impl IndependentSample<f64> for StudentT {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
Expand Down
Loading

0 comments on commit 9ed32bb

Please sign in to comment.