Skip to content

Instantly share code, notes, and snippets.

@XMPPwocky
Last active November 25, 2015 03:04
Show Gist options
  • Save XMPPwocky/c35d8eefb690b75ba907 to your computer and use it in GitHub Desktop.
Save XMPPwocky/c35d8eefb690b75ba907 to your computer and use it in GitHub Desktop.
extern crate threadpool;
struct Rng {
state: (u32, u32)
}
impl Rng {
fn random(&mut self) -> f64 {
let r0 = (18273 * (self.state.0 & 0xFFFF)) + (self.state.0 >> 16);
self.state.0 = r0;
let r1 = (36969 * (self.state.1 & 0xFFFF)) + (self.state.1 >> 16);
self.state.1 = r1;
let x = (r0 << 16) + (r1 & 0xFFFF);
let x = x as f64;
let imm = if x < 0.0 {
x + 4294967296.0f64
} else {
x
};
imm * 2.3283064365386962890625e-10
}
}
fn main() {
let count = 4u32;
let tp = threadpool::ThreadPool::new(count as usize);
let (tx, rx) = std::sync::mpsc::channel();
let outputs = vec![
0.8624396452214569,
0.13606369611807168,
0.51276672002859414,
];
for i in 0..count {
let tx = tx.clone();
let outputs = outputs.clone();
tp.execute(move || {
bf_range(std::u32::MAX/count*i, std::u32::MAX/count*(i+1), outputs);
tx.send(());
});
}
rx.iter().take(count as usize).collect::<Vec<_>>();
}
fn dump(state: (u32, u32)) {
let mut rng = Rng { state: state };
for i in 0..200 {
let r = rng.random();
let s = r.to_string();
let lols = String::new() + &s[2..4] + "." + &s[4..];
println!("{}", lols);
}
}
fn bf_range(start: u32, end: u32, outputs: Vec<f64>) {
println!("Bruteforcing from {} to {}...", start, end);
let mut rng = Rng { state: (0, 0) };
// need inclusive range syntax :|
let statesample = (outputs[0] / 2.3283064365386962890625e-10) as u32;
let first_lower = statesample >> 16;
let last_lower = statesample & 0xFFFF;
for skip in 0..50 {
println!("{}", skip);
'o: for guess in start..end {
let first = first_lower + ((guess & 0xFFFF) << 16);
let last = last_lower + (guess & 0xFFFF0000);
// spread it out among the two parts
rng.state = (first, last);
for &output in &outputs[1..] {
for _ in 0..skip {
rng.random();
}
let r = rng.random();
if r != output {
continue 'o;
}
}
println!("----- Got it! {:?}", rng.state);
dump(rng.state);
}
}
println!("Done.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment