Move shuffling to its own crate, update hash fn

Previously blake2s-256 was being used, now blake2b-512[:32] is being
used.
This commit is contained in:
Paul Hauner
2018-10-03 13:13:51 +10:00
parent 6a75aa3246
commit 2763f7bc00
6 changed files with 185 additions and 29 deletions

View File

@@ -0,0 +1,48 @@
extern crate hashing;
mod rng;
use self::rng::ShuffleRng;
#[derive(Debug)]
pub enum ShuffleErr {
ExceedsListLength,
}
/// Performs a deterministic, in-place shuffle of a vector of bytes.
/// The final order of the shuffle is determined by successive hashes
/// of the supplied `seed`.
pub fn shuffle(
seed: &[u8],
mut list: Vec<usize>)
-> Result<Vec<usize>, ShuffleErr>
{
let mut rng = ShuffleRng::new(seed);
if list.len() > rng.rand_max as usize {
return Err(ShuffleErr::ExceedsListLength);
}
for i in 0..(list.len() - 1) {
let n = list.len() - i;
let j = rng.rand_range(n as u32) as usize + i;
list.swap(i, j);
}
Ok(list)
}
#[cfg(test)]
mod tests {
use super::*;
use super::hashing::canonical_hash;
#[test]
fn test_shuffling() {
let seed = canonical_hash(b"4kn4driuctg8");
let list: Vec<usize> = (0..12).collect();
let s = shuffle(&seed, list).unwrap();
assert_eq!(
s,
vec![7, 3, 2, 5, 11, 9, 1, 0, 4, 6, 10, 8],
)
}
}