|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! The ISAAC random number generator. |
| 12 | +
|
| 13 | +use rand::{seed, Rng}; |
| 14 | +use iter::{Iterator, range, range_step}; |
| 15 | +use option::{None, Some}; |
| 16 | + |
| 17 | +use cast; |
| 18 | +use cmp; |
| 19 | +use sys; |
| 20 | +use vec; |
| 21 | + |
| 22 | +static RAND_SIZE_LEN: u32 = 8; |
| 23 | +static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN; |
| 24 | + |
| 25 | +/// A random number generator that uses the [ISAAC |
| 26 | +/// algorithm](http://en.wikipedia.org/wiki/ISAAC_%28cipher%29). |
| 27 | +/// |
| 28 | +/// The ISAAC algorithm is suitable for cryptographic purposes. |
| 29 | +pub struct IsaacRng { |
| 30 | + priv cnt: u32, |
| 31 | + priv rsl: [u32, .. RAND_SIZE], |
| 32 | + priv mem: [u32, .. RAND_SIZE], |
| 33 | + priv a: u32, |
| 34 | + priv b: u32, |
| 35 | + priv c: u32 |
| 36 | +} |
| 37 | + |
| 38 | +impl IsaacRng { |
| 39 | + /// Create an ISAAC random number generator with a random seed. |
| 40 | + pub fn new() -> IsaacRng { |
| 41 | + IsaacRng::new_seeded(seed(RAND_SIZE as uint * 4)) |
| 42 | + } |
| 43 | + |
| 44 | + /// Create an ISAAC random number generator with a seed. This can be any |
| 45 | + /// length, although the maximum number of bytes used is 1024 and any more |
| 46 | + /// will be silently ignored. A generator constructed with a given seed |
| 47 | + /// will generate the same sequence of values as all other generators |
| 48 | + /// constructed with the same seed. |
| 49 | + pub fn new_seeded(seed: &[u8]) -> IsaacRng { |
| 50 | + let mut rng = IsaacRng { |
| 51 | + cnt: 0, |
| 52 | + rsl: [0, .. RAND_SIZE], |
| 53 | + mem: [0, .. RAND_SIZE], |
| 54 | + a: 0, b: 0, c: 0 |
| 55 | + }; |
| 56 | + |
| 57 | + let array_size = sys::size_of_val(&rng.rsl); |
| 58 | + let copy_length = cmp::min(array_size, seed.len()); |
| 59 | + |
| 60 | + // manually create a &mut [u8] slice of randrsl to copy into. |
| 61 | + let dest = unsafe { cast::transmute((&mut rng.rsl, array_size)) }; |
| 62 | + vec::bytes::copy_memory(dest, seed, copy_length); |
| 63 | + rng.init(true); |
| 64 | + rng |
| 65 | + } |
| 66 | + |
| 67 | + /// Create an ISAAC random number generator using the default |
| 68 | + /// fixed seed. |
| 69 | + pub fn new_unseeded() -> IsaacRng { |
| 70 | + let mut rng = IsaacRng { |
| 71 | + cnt: 0, |
| 72 | + rsl: [0, .. RAND_SIZE], |
| 73 | + mem: [0, .. RAND_SIZE], |
| 74 | + a: 0, b: 0, c: 0 |
| 75 | + }; |
| 76 | + rng.init(false); |
| 77 | + rng |
| 78 | + } |
| 79 | + |
| 80 | + /// Initialises `self`. If `use_rsl` is true, then use the current value |
| 81 | + /// of `rsl` as a seed, otherwise construct one algorithmically (not |
| 82 | + /// randomly). |
| 83 | + fn init(&mut self, use_rsl: bool) { |
| 84 | + let mut a = 0x9e3779b9; |
| 85 | + let mut b = a; |
| 86 | + let mut c = a; |
| 87 | + let mut d = a; |
| 88 | + let mut e = a; |
| 89 | + let mut f = a; |
| 90 | + let mut g = a; |
| 91 | + let mut h = a; |
| 92 | + |
| 93 | + macro_rules! mix( |
| 94 | + () => {{ |
| 95 | + a^=b<<11; d+=a; b+=c; |
| 96 | + b^=c>>2; e+=b; c+=d; |
| 97 | + c^=d<<8; f+=c; d+=e; |
| 98 | + d^=e>>16; g+=d; e+=f; |
| 99 | + e^=f<<10; h+=e; f+=g; |
| 100 | + f^=g>>4; a+=f; g+=h; |
| 101 | + g^=h<<8; b+=g; h+=a; |
| 102 | + h^=a>>9; c+=h; a+=b; |
| 103 | + }} |
| 104 | + ); |
| 105 | + |
| 106 | + do 4.times { mix!(); } |
| 107 | + |
| 108 | + if use_rsl { |
| 109 | + macro_rules! memloop ( |
| 110 | + ($arr:expr) => {{ |
| 111 | + for i in range_step(0u32, RAND_SIZE, 8) { |
| 112 | + a+=$arr[i ]; b+=$arr[i+1]; |
| 113 | + c+=$arr[i+2]; d+=$arr[i+3]; |
| 114 | + e+=$arr[i+4]; f+=$arr[i+5]; |
| 115 | + g+=$arr[i+6]; h+=$arr[i+7]; |
| 116 | + mix!(); |
| 117 | + self.mem[i ]=a; self.mem[i+1]=b; |
| 118 | + self.mem[i+2]=c; self.mem[i+3]=d; |
| 119 | + self.mem[i+4]=e; self.mem[i+5]=f; |
| 120 | + self.mem[i+6]=g; self.mem[i+7]=h; |
| 121 | + } |
| 122 | + }} |
| 123 | + ); |
| 124 | + |
| 125 | + memloop!(self.rsl); |
| 126 | + memloop!(self.mem); |
| 127 | + } else { |
| 128 | + for i in range_step(0u32, RAND_SIZE, 8) { |
| 129 | + mix!(); |
| 130 | + self.mem[i ]=a; self.mem[i+1]=b; |
| 131 | + self.mem[i+2]=c; self.mem[i+3]=d; |
| 132 | + self.mem[i+4]=e; self.mem[i+5]=f; |
| 133 | + self.mem[i+6]=g; self.mem[i+7]=h; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + self.isaac(); |
| 138 | + } |
| 139 | + |
| 140 | + /// Refills the output buffer (`self.rsl`) |
| 141 | + #[inline] |
| 142 | + fn isaac(&mut self) { |
| 143 | + self.c += 1; |
| 144 | + // abbreviations |
| 145 | + let mut a = self.a; |
| 146 | + let mut b = self.b + self.c; |
| 147 | + |
| 148 | + static MIDPOINT: uint = RAND_SIZE as uint / 2; |
| 149 | + |
| 150 | + macro_rules! ind (($x:expr) => { |
| 151 | + self.mem[($x >> 2) & (RAND_SIZE - 1)] |
| 152 | + }); |
| 153 | + macro_rules! rngstep( |
| 154 | + ($j:expr, $shift:expr) => {{ |
| 155 | + let base = $j; |
| 156 | + let mix = if $shift < 0 { |
| 157 | + a >> -$shift as uint |
| 158 | + } else { |
| 159 | + a << $shift as uint |
| 160 | + }; |
| 161 | + |
| 162 | + let x = self.mem[base + mr_offset]; |
| 163 | + a = (a ^ mix) + self.mem[base + m2_offset]; |
| 164 | + let y = ind!(x) + a + b; |
| 165 | + self.mem[base + mr_offset] = y; |
| 166 | + |
| 167 | + b = ind!(y >> RAND_SIZE_LEN) + x; |
| 168 | + self.rsl[base + mr_offset] = b; |
| 169 | + }} |
| 170 | + ); |
| 171 | + |
| 172 | + let r = [(0, MIDPOINT), (MIDPOINT, 0)]; |
| 173 | + for &(mr_offset, m2_offset) in r.iter() { |
| 174 | + for i in range_step(0u, MIDPOINT, 4) { |
| 175 | + rngstep!(i + 0, 13); |
| 176 | + rngstep!(i + 1, -6); |
| 177 | + rngstep!(i + 2, 2); |
| 178 | + rngstep!(i + 3, -16); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + self.a = a; |
| 183 | + self.b = b; |
| 184 | + self.cnt = RAND_SIZE; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +impl Rng for IsaacRng { |
| 189 | + #[inline] |
| 190 | + fn next(&mut self) -> u32 { |
| 191 | + if self.cnt == 0 { |
| 192 | + // make some more numbers |
| 193 | + self.isaac(); |
| 194 | + } |
| 195 | + self.cnt -= 1; |
| 196 | + self.rsl[self.cnt] |
| 197 | + } |
| 198 | +} |
0 commit comments