Challenge 23 eod.
This commit is contained in:
@@ -10,7 +10,7 @@ mod set2;
|
|||||||
mod set3;
|
mod set3;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
const RUN_ALL: bool = true;
|
const RUN_ALL: bool = false;
|
||||||
if RUN_ALL {
|
if RUN_ALL {
|
||||||
set1::challenge1();
|
set1::challenge1();
|
||||||
set1::challenge2();
|
set1::challenge2();
|
||||||
|
|||||||
@@ -18,8 +18,12 @@ impl MT19937 {
|
|||||||
mt
|
mt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn splice(&mut self, state: Vec<u32>) {
|
||||||
|
self.mt = state;
|
||||||
|
self.index = 624;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn seed(&mut self, seed: u32) {
|
pub fn seed(&mut self, seed: u32) {
|
||||||
self.index = N;
|
|
||||||
const F: u32 = 1812433253;
|
const F: u32 = 1812433253;
|
||||||
self.mt[0] = seed;
|
self.mt[0] = seed;
|
||||||
for i in 1..N {
|
for i in 1..N {
|
||||||
@@ -27,14 +31,14 @@ impl MT19937 {
|
|||||||
+ Wrapping(i as u32))
|
+ Wrapping(i as u32))
|
||||||
.0;
|
.0;
|
||||||
}
|
}
|
||||||
|
self.twist();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_number(&mut self) -> u32 {
|
pub fn extract_number(&mut self) -> u32 {
|
||||||
if self.index >= N {
|
if self.index == N {
|
||||||
if self.index > N {
|
|
||||||
panic!("Generator was never seeded");
|
|
||||||
}
|
|
||||||
self.twist();
|
self.twist();
|
||||||
|
} else if self.index > N {
|
||||||
|
panic!("Generator was never seeded");
|
||||||
}
|
}
|
||||||
|
|
||||||
const S: u32 = 7;
|
const S: u32 = 7;
|
||||||
|
|||||||
64
src/set3.rs
64
src/set3.rs
@@ -359,5 +359,67 @@ pub fn challenge22() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn challenge23() {
|
pub fn challenge23() {
|
||||||
println!("[xxxx] Challenge 23: tbd");
|
// Once you have "untemper" working, create a new MT19937 generator, tap it for 624 outputs,
|
||||||
|
/*
|
||||||
|
let mut mt = mt19937::MT19937::new();
|
||||||
|
let seed: u32 = rand::thread_rng().gen::<u32>();
|
||||||
|
mt.seed(seed);
|
||||||
|
let outputs: Vec<u32> = (0..624).map(|_| mt.extract_number()).collect();
|
||||||
|
*/
|
||||||
|
|
||||||
|
fn temper(x: u32) -> u32 {
|
||||||
|
//const S: u32 = 7;
|
||||||
|
//const T: u32 = 15;
|
||||||
|
//const U: u32 = 11;
|
||||||
|
//const B: u32 = 0x9D2C5680;
|
||||||
|
//const C: u32 = 0xEFC60000;
|
||||||
|
//const L: u32 = 18;
|
||||||
|
|
||||||
|
let mut y = x;
|
||||||
|
// y = y ^ (y >> U);
|
||||||
|
// y = y ^ ((y << S) & B);
|
||||||
|
// y = y ^ ((y << T) & C);
|
||||||
|
// y = y ^ (y >> L);
|
||||||
|
y = y ^ (y >> 24);
|
||||||
|
y
|
||||||
|
}
|
||||||
|
|
||||||
|
fn untemper(x: u32) -> u32 {
|
||||||
|
//const S: u32 = 7;
|
||||||
|
//const T: u32 = 15;
|
||||||
|
//const U: u32 = 11;
|
||||||
|
//const B: u32 = 0x9D2C5680;
|
||||||
|
//const C: u32 = 0xEFC60000;
|
||||||
|
//const L: u32 = 18;
|
||||||
|
|
||||||
|
let mut y = x;
|
||||||
|
//y = y ^ (y >> U);
|
||||||
|
//y = y ^ ((y << S) & B);
|
||||||
|
//y = y ^ ((y << T) & C);
|
||||||
|
//y = y ^ (y >> L);
|
||||||
|
|
||||||
|
// xxxx'yyyy
|
||||||
|
// ^0000'xxxx
|
||||||
|
// xxxx'zzzz
|
||||||
|
println!("{:#010x}", u32::MAX << 11 );
|
||||||
|
y = (y & 0xffffff00) | (((y & 0xff000000) >> 24) ^ (y & 0x000000ff));
|
||||||
|
y
|
||||||
|
}
|
||||||
|
|
||||||
|
let a: u32 = 0x12345678;
|
||||||
|
let b = temper(a);
|
||||||
|
let c = untemper(b);
|
||||||
|
println!("{:#010x} -> {:#010x} -> {:#010x}", a, b, c);
|
||||||
|
|
||||||
|
/*
|
||||||
|
// untemper each of them to recreate the state of the generator,
|
||||||
|
let outputs = outputs.iter().map(|o| untemper(*o)).collect();
|
||||||
|
|
||||||
|
//and splice that state into a new instance of the MT19937 generator.
|
||||||
|
let mut new_mt = mt19937::MT19937::new();
|
||||||
|
new_mt.splice(outputs);
|
||||||
|
println!("{} {}", mt.extract_number(), new_mt.extract_number());
|
||||||
|
*/
|
||||||
|
|
||||||
|
println!("[xxxx] Challenge 23: wip");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user