From 92c73fb12520b2c208c345bfdf0540d3e10b99c1 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Thu, 4 Aug 2022 17:13:38 -0400 Subject: [PATCH] Challenge 23 eod. --- src/main.rs | 2 +- src/mt19937.rs | 14 +++++++---- src/set3.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3f5816a..d9fc37a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ mod set2; mod set3; fn main() { - const RUN_ALL: bool = true; + const RUN_ALL: bool = false; if RUN_ALL { set1::challenge1(); set1::challenge2(); diff --git a/src/mt19937.rs b/src/mt19937.rs index ffa8170..c5ca01a 100644 --- a/src/mt19937.rs +++ b/src/mt19937.rs @@ -18,8 +18,12 @@ impl MT19937 { mt } + pub fn splice(&mut self, state: Vec) { + self.mt = state; + self.index = 624; + } + pub fn seed(&mut self, seed: u32) { - self.index = N; const F: u32 = 1812433253; self.mt[0] = seed; for i in 1..N { @@ -27,14 +31,14 @@ impl MT19937 { + Wrapping(i as u32)) .0; } + self.twist(); } pub fn extract_number(&mut self) -> u32 { - if self.index >= N { - if self.index > N { - panic!("Generator was never seeded"); - } + if self.index == N { self.twist(); + } else if self.index > N { + panic!("Generator was never seeded"); } const S: u32 = 7; diff --git a/src/set3.rs b/src/set3.rs index 8180867..e6ced6f 100644 --- a/src/set3.rs +++ b/src/set3.rs @@ -359,5 +359,67 @@ pub fn challenge22() { } 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::(); + 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; + + 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"); }