diff --git a/src/set3.rs b/src/set3.rs index e6ced6f..a3843f1 100644 --- a/src/set3.rs +++ b/src/set3.rs @@ -359,50 +359,51 @@ pub fn challenge22() { } pub fn challenge23() { - // 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::(); - mt.seed(seed); - let outputs: Vec = (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; + 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 << 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; + const M: u32 = 32; 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)); + // reverse y = y ^ (y >> L); L = 18; + // const UPPER_18_BITS: u32 = u32::MAX << (M - 18); + // const LOWER_14_BITS: u32 = u32::MAX >> 18; + // let mut o = y & UPPER_18_BITS; // upper 18 bits are correct + // o |= ((o & UPPER_18_BITS) >> 18) ^ (y & LOWER_14_BITS); // all 32 bits are correct + // y = o; + + // reverse y = y ^ ((y << T) & C); + + // reverse y = y ^ ((y << S) & B); + + // reverse y = y ^ (y >> U); U = 11; + // const UPPER_11_BITS: u32 = u32::MAX << (M - 11); + // const SECOND_11_BITS: u32 = UPPER_11_BITS >> 11; + // const LOWER_10_BITS: u32 = u32::MAX >> (M - 10); + // let mut o = y & UPPER_11_BITS; // upper 11 bits are correct + // o |= ((o & UPPER_11_BITS) >> 11) ^ (y & SECOND_11_BITS); // upper 22 bits are correct + // o |= ((o & SECOND_11_BITS) >> 11) ^ (y & LOWER_10_BITS); // all 32 bits are correct + // y = o; + y } @@ -412,13 +413,19 @@ pub fn challenge23() { println!("{:#010x} -> {:#010x} -> {:#010x}", a, b, c); /* + // 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::(); + mt.seed(seed); + let outputs: Vec = (0..624).map(|_| mt.extract_number()).collect(); + // 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()); + // and splice that state into a new instance of the MT19937 generator. + let mut spliced_mt = mt19937::MT19937::new(); + spliced_mt.splice(outputs); + println!("{} {}", mt.extract_number(), spliced_mt.extract_number()); */ println!("[xxxx] Challenge 23: wip");