From 977317cb3ecf79695f3750f5d88df8ed223b4b22 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Tue, 30 Nov 2021 23:48:52 -0500 Subject: Commit main C file The current 'best' parameters by my testing are SHA1 for the hash and bithack01 as the iterator. Expect more options to arrive over time... The string to compress/decompress is taken as the first (and only) command-line argument. Signed-off-by: Malfurious --- main.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..3b6ec7a --- /dev/null +++ b/main.c @@ -0,0 +1,64 @@ +/* TODO: build system + * gcc -std=gnu99 -W -Wall -Werror -O3 main.c mpz.c -o pszip -lgmp -lcrypto */ + +#include +#include +#include +#include + +#include "mpz.h" + +/* Hash is currently downgraded to SHA1 for now, as it is faster and there is + * not yet a reason for anything stronger. */ +#define HASH_LENGTH SHA_DIGEST_LENGTH +static void hash_mpz(void *output, const mpz_t n, size_t size) +{ + SHA1((void *)mpz_limbs_read(n), size, output); +} + +int main(int argc, char **argv) +{ + unsigned char original_hash[HASH_LENGTH]; + unsigned char iteration_hash[HASH_LENGTH]; + struct pszip_mpz_state mpzs; + mpz_t original; + + if (argc < 2) + { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + /* Process original data */ + size_t size = strlen(argv[1]); + pszip_mpz_init_data(original, argv[1], size); + mp_bitcnt_t popcnt = mpz_popcount(original); + hash_mpz(original_hash, original, size); + mpz_clear(original); + + /* Display metadata */ + printf("%s\n", argv[1]); + for (int i = 0; i < HASH_LENGTH; i++) + printf("%02x", original_hash[i]); + printf("\nsize = %zu\n", size); + printf("popcnt = %llu\n", (long long unsigned)popcnt); + + /* Search for solution (1 thread) */ + pszip_mpz_state_init(&mpzs, popcnt, size); + + while (1) + { + hash_mpz(iteration_hash, mpzs.current, size); + if (memcmp(iteration_hash, original_hash, HASH_LENGTH) == 0) + break; + pszip_mpz_iterate_bithack01(&mpzs); + } + + /* Results */ + printf("\nFound!\n"); + fwrite(mpz_limbs_read(mpzs.current), 1, size, stdout); + printf("\n"); + + pszip_mpz_state_clear(&mpzs); + return 0; +} -- cgit v1.2.3