summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/demo_hash.rs23
-rw-r--r--src/iter.rs10
-rw-r--r--src/load_file.rs12
-rw-r--r--src/main.rs80
4 files changed, 125 insertions, 0 deletions
diff --git a/src/demo_hash.rs b/src/demo_hash.rs
new file mode 100644
index 0000000..115a10f
--- /dev/null
+++ b/src/demo_hash.rs
@@ -0,0 +1,23 @@
+use sha2::Digest;
+
+pub fn hash_bigint(n: usize, i: &num::BigInt) -> Vec<u8>
+{
+ let (_, bytes) = i.to_bytes_be();
+ assert!(bytes.len() <= n);
+
+ let mut hash = sha2::Sha512::new();
+ hash.input(vec![0; n - bytes.len()]);
+ hash.input(bytes);
+
+ hash.result().to_vec()
+}
+
+pub fn show_hash(h: &Vec<u8>)
+{
+ for x in h
+ {
+ print!("{:02x}", x);
+ }
+
+ println!();
+}
diff --git a/src/iter.rs b/src/iter.rs
new file mode 100644
index 0000000..f8aa6cb
--- /dev/null
+++ b/src/iter.rs
@@ -0,0 +1,10 @@
+use num::BigInt;
+
+pub fn next(v: &BigInt) -> BigInt
+{
+ /*let t: BigUint = v | (v - 1);
+ (t + 1) | (((!t & -!t) - 1) >> (v.trailing_zeros() + 1))*/
+
+ let t: BigInt = (v | (v - 1u32)) + 1u32;
+ &t | ((((&t & -&t) / (v & -v)) >> 1) - 1u32)
+}
diff --git a/src/load_file.rs b/src/load_file.rs
new file mode 100644
index 0000000..0fd1541
--- /dev/null
+++ b/src/load_file.rs
@@ -0,0 +1,12 @@
+use std::fs::File;
+use std::io::Read;
+
+pub fn load_file_vec(path: &String) -> std::io::Result<Vec<u8>>
+{
+ let mut file = File::open(path)?;
+ let mut data = Vec::new();
+
+ file.read_to_end(&mut data)?;
+
+ Ok(data)
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..bfed16b
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,80 @@
+mod iter;
+mod load_file;
+mod demo_hash;
+
+use num::BigInt;
+use num::bigint::Sign;
+
+fn vec_pop_cnt(v: &Vec<u8>) -> u32
+{
+ let mut cnt = 0;
+
+ for x in v
+ {
+ cnt += x.count_ones();
+ }
+
+ cnt
+}
+
+fn pop_cnt_vec(c: u32) -> Vec<u8>
+{
+ let mut v = Vec::new();
+ v.push(2u8.pow(c % 8) - 1);
+ for _ in 0..(c / 8)
+ {
+ v.push(0xff);
+ }
+
+ v
+}
+
+fn main()
+{
+ /* "Compression" setup */
+ let raw_file = load_file::load_file_vec(&"data.bin".to_owned()).unwrap();
+ let size = raw_file.len();
+ let popcnt = vec_pop_cnt(&raw_file);
+ let bi_file = BigInt::from_bytes_be(Sign::Plus, &raw_file);
+ let solution_hash = demo_hash::hash_bigint(size, &bi_file);
+
+ println!("Length: {}", size);
+ print!("SHA512: ");
+ demo_hash::show_hash(&solution_hash);
+ println!("popcnt: {}", popcnt);
+
+ /*
+ // , ,
+ for _ in 0..1000000
+ {
+ bi = iter::next(&bi);
+ }
+
+ println!("{:x}", bi);
+ panic!("stopping after benchmark");
+ */
+
+ //panic!("Performing compress only");
+
+ /* "Decompression" loop */
+ let raw = pop_cnt_vec(popcnt);
+ let mut bi = BigInt::from_bytes_be(Sign::Plus, &raw);
+
+ loop
+ {
+ let iteration_hash = demo_hash::hash_bigint(size, &bi);
+ //println!("{:b}", bi);
+ //println!();
+
+ if iteration_hash == solution_hash
+ {
+ demo_hash::show_hash(&iteration_hash);
+ break;
+ }
+
+ bi = iter::next(&bi);
+ }
+
+ println!("Found hash (data follows)!!!");
+ println!("{:x}", bi);
+}