diff options
author | Malfurious <m@lfurio.us> | 2021-08-28 09:45:57 -0400 |
---|---|---|
committer | Malfurious <m@lfurio.us> | 2021-08-28 10:01:42 -0400 |
commit | 7bd18b4b22f3a6bf86b131329345f9a75d8f699c (patch) | |
tree | 07f2ac0cb38a7424b711c2dd9b59116e2ed0848b | |
parent | 1522a21d24add0d78f7e4a5fae7654b709045754 (diff) | |
download | SorensenCompression-7bd18b4b22f3a6bf86b131329345f9a75d8f699c.tar.gz SorensenCompression-7bd18b4b22f3a6bf86b131329345f9a75d8f699c.zip |
Commit Rust POC
This replaces the Python script. Since all MIT-licensed code is
removed, the LICENSE file is as well.
'cargo run' to run the program.
Signed-off-by: Malfurious <m@lfurio.us>
-rw-r--r-- | Cargo.lock | 180 | ||||
-rw-r--r-- | Cargo.toml | 12 | ||||
-rwxr-xr-x | GarbageProofOfConcept.py | 46 | ||||
-rw-r--r-- | LICENSE | 21 | ||||
-rw-r--r-- | data.bin | 1 | ||||
-rw-r--r-- | src/demo_hash.rs | 23 | ||||
-rw-r--r-- | src/iter.rs | 10 | ||||
-rw-r--r-- | src/load_file.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 80 |
9 files changed, 318 insertions, 67 deletions
diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d85fb8d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,180 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +dependencies = [ + "block-padding", + "byte-tools", + "byteorder", + "generic-array", +] + +[[package]] +name = "block-padding" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +dependencies = [ + "byte-tools", +] + +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + +[[package]] +name = "digest" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +dependencies = [ + "generic-array", +] + +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" + +[[package]] +name = "generic-array" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" +dependencies = [ + "typenum", +] + +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" + +[[package]] +name = "num" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfb0800a0291891dd9f4fe7bd9c19384f98f7fbe0cd0f39a2c6b88b9868bbc00" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" +dependencies = [ + "autocfg", +] + +[[package]] +name = "opaque-debug" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" + +[[package]] +name = "pszip" +version = "0.1.0" +dependencies = [ + "hex", + "num", + "sha2", +] + +[[package]] +name = "sha2" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" +dependencies = [ + "block-buffer", + "digest", + "fake-simd", + "opaque-debug", +] + +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..679c3b3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "pszip" +version = "0.1.0" +authors = ["malf"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sha2 = "0.8.1" +num = "*" +hex = "0.3.1" diff --git a/GarbageProofOfConcept.py b/GarbageProofOfConcept.py deleted file mode 100755 index e6e4030..0000000 --- a/GarbageProofOfConcept.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python - -import hashlib -from itertools import combinations - -data = b"abc" -length = len(data) - -hexdigest = hashlib.sha256(data).hexdigest() -print(hexdigest) - -lookupTable = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, - 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, - 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, - 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, - 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, - 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, - 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, - 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, - 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, - 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, - 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, - 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8] - -sum1 = 0 -for i in range(length): - sum1 += lookupTable[bytearray(data)[i]] - -print("The length is: {0}".format(length)) -print("The ones count is: {0}".format(sum1)) - -combinations = list(combinations(range(length * 8),sum1)) -numCombs = len(combinations) - -count = 1 -for comb in combinations: - print("Trying {0}/{1}".format(count, numCombs)) - count += 1 - a = [0] * length - for bit in comb: - a[bit//8] |= 1 << (bit % 8) - if hashlib.sha256(bytearray(a)).hexdigest() == hexdigest: - print("Found! {0}".format(a)) - break -else: - print("failed to find") diff --git a/LICENSE b/LICENSE deleted file mode 100644 index d3d881b..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 Paul Sorensen - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/data.bin b/data.bin new file mode 100644 index 0000000..f2ba8f8 --- /dev/null +++ b/data.bin @@ -0,0 +1 @@ +abc
\ No newline at end of file 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); +} |