1 /* ----------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Generate tables utility program */
3 /* Copyright(c) 2016-2017 LoRd_MuldeR <mulder2@gmx.de> */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */
6 /* and associated documentation files(the "Software"), to deal in the Software without */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish, */
8 /* distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions: */
11 /* The above copyright notice and this permission notice shall be included in all copies or */
12 /* substantial portions of the Software. */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
16 /* NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19 /* ----------------------------------------------------------------------------------------------- */
22 #include "thread_utils.h"
24 #include "boxmuller.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 #define ROW_NUM 251U /*total number of rows*/
40 #define ROW_LEN (HASH_LEN / CHAR_BIT) /*number of indices per row*/
41 #define DISTANCE_MIN (ROW_LEN - 1U) /*min. hamming distance*/
45 #define __DISTANCE_STR(X) #X
46 #define _DISTANCE_STR(X) __DISTANCE_STR(X)
47 #define DISTANCE_STR _DISTANCE_STR(DISTANCE_MIN)
49 #define MAGIC_NUMBER 0x76A3D06509A73016ui64
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 static uint8_t g_table[ROW_NUM][HASH_LEN];
57 static size_t g_spinpos = 0;
58 static char SPINNER[4] = { '/', '-', '\\', '|' };
60 static const uint8_t INDICES[UINT8_MAX + 1U] =
62 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
63 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
64 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
65 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
66 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
67 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
68 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
69 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
70 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
71 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
72 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
73 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
74 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
75 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
76 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
77 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
85 #define TRACE(X, ...) printf(X "\n", __VA_ARGS__)
87 #define TRACE(X, ...) __noop()
90 static inline void swap(uint8_t *const row_buffer, const uint_fast16_t a, const uint_fast16_t b)
92 const uint8_t temp = row_buffer[a];
93 row_buffer[a] = row_buffer[b];
97 static inline void random_permutation(msws_t rand, uint8_t *const row_buffer)
99 for (uint_fast16_t i = 0; i < ROW_LEN; ++i)
101 const uint_fast16_t j = msws_uint32_max(rand, i + 1U);
102 row_buffer[i] = row_buffer[j];
103 row_buffer[j] = INDICES[i];
107 static inline void swap_multiple_random(msws_t rand, uint8_t *const row_buffer, const uint_fast16_t count)
109 for (uint_fast16_t i = 0U; i < count; ++i)
114 a = msws_uint32_max(rand, ROW_LEN);
115 b = msws_uint32_max(rand, ROW_LEN);
118 swap(row_buffer, a, b);
122 static inline void rotate_row(uint8_t *const row_buffer)
124 const uint8_t temp = row_buffer[0];
125 for (uint_fast16_t k = 0U; k < ROW_LEN - 1U; ++k)
127 row_buffer[k] = row_buffer[k + 1U];
129 row_buffer[ROW_LEN - 1U] = temp;
132 static inline void reverse_row(uint8_t *const row_buffer)
134 uint_fast16_t j = ROW_LEN - 1U;
135 for (uint_fast16_t i = 0U; i < ROW_LEN / 2U; ++i)
137 swap(row_buffer, i, j--);
141 static inline uint_fast16_t row_distance(const uint8_t *const row_a, const uint8_t *const row_b)
143 uint_fast16_t distance = 0U;
144 for (uint_fast16_t j = 0; j < ROW_LEN; ++j)
146 if (row_a[j] != row_b[j])
154 #define ERROR_ACC(X,Y) ((X >= Y) ? ((X << 8U) | Y) : ((Y << 8U) | X))
155 static inline uint_fast16_t check_permutation(const uint_fast16_t index, const uint8_t *const row_buffer, const uint_fast16_t limit)
157 uint_fast16_t error = 0U, failed = 0U;
158 uint_fast16_t distance = row_distance(&INDICES[0], &row_buffer[0]);
159 if (distance < DISTANCE_MIN)
161 error = DISTANCE_MIN - distance;
163 if (ERROR_ACC(failed, error) >= limit)
165 return limit; /*early termination*/
168 for (uint_fast16_t i = 0; i < index; ++i)
170 distance = row_distance(&g_table[i][0], &row_buffer[0]);
171 if (distance < DISTANCE_MIN)
173 error = max_ui16(error, DISTANCE_MIN - distance);
175 if (ERROR_ACC(failed, error) >= limit)
177 return limit; /*early termination*/
181 return ERROR_ACC(failed, error);
184 static void print_row(const uint8_t *const row_buffer)
186 for (uint_fast8_t i = 0; i < ROW_LEN; ++i)
188 printf(i ? ",%02X" : "%02X", row_buffer[i]);
193 static void dump_table(FILE *out)
195 fprintf(out, "uint8_t MHASH_384_TABLE_MIX[%u][MHASH_384_LEN] =\n{\n", ROW_NUM);
196 for (uint_fast16_t i = 0; i < ROW_NUM; i++)
199 for (uint_fast8_t j = 0; j < ROW_LEN; j++)
205 fprintf(out, "0x%02X", g_table[i][j]);
207 fprintf(out, " }%s /*%03u*/\n", (i != (ROW_NUM - 1)) ? "," : " ", (uint32_t)i);
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
216 static bool save_table_data(const wchar_t *const filename, const uint_fast16_t rows_completed_in)
218 FILE *const file = _wfopen(filename, L"wb");
222 const uint64_t magic_number = MAGIC_NUMBER;
223 const uint32_t hash_len = HASH_LEN, distance_min = DISTANCE_MIN, rows_completed = (uint32_t)rows_completed_in;
224 fwrite(&magic_number, sizeof(uint64_t), 1, file);
225 fwrite(&hash_len, sizeof(uint32_t), 1, file);
226 fwrite(&distance_min, sizeof(uint32_t), 1, file);
227 fwrite(&rows_completed, sizeof(uint32_t), 1, file);
228 for (uint32_t i = 0; i < rows_completed; ++i)
230 const uint32_t checksum = adler32(&g_table[i][0], ROW_LEN);
231 fwrite(&checksum, sizeof(uint32_t), 1, file);
232 fwrite(&g_table[i][0], sizeof(uint8_t), ROW_LEN, file);
236 printf("ERROR: Failed to write table data!\n");
244 printf("ERROR: Failed to open table file for writing!\n");
249 static bool load_table_data(const wchar_t *const filename, uint_fast16_t *const rows_completed_out)
251 FILE *const file = _wfopen(filename, L"rb");
255 uint64_t magic_number;
256 uint32_t hash_len, distance_min, rows_completed;
257 fread(&magic_number, sizeof(uint64_t), 1, file);
258 fread(&hash_len, sizeof(uint32_t), 1, file);
259 fread(&distance_min, sizeof(uint32_t), 1, file);
260 fread(&rows_completed, sizeof(uint32_t), 1, file);
261 if (ferror(file) || feof(file))
263 printf("ERROR: Failed to read the table header!\n");
267 if (magic_number != MAGIC_NUMBER)
269 printf("ERROR: Table file format could not be recognized!\n");
273 if ((hash_len != HASH_LEN) || (distance_min != DISTANCE_MIN) || (rows_completed > ROW_NUM))
275 printf("ERROR: Table properties are incompatibe with this instance!\n");
279 for (uint_fast16_t i = 0; i < rows_completed; ++i)
281 uint32_t checksum_expected;
282 if ((fread(&checksum_expected, sizeof(uint32_t), 1, file) != 1) || (fread(&g_table[i][0], sizeof(uint8_t), ROW_LEN, file) != ROW_LEN))
284 printf("ERROR: Failed to read table data from file!\n");
288 if (adler32(&g_table[i][0], ROW_LEN) != checksum_expected)
290 printf("ERROR: Table checksum does *not* match table contents!\n");
294 if (check_permutation(i, &g_table[i][0], UINT16_MAX) != 0U)
296 printf("ERROR: Table distance verification has failed!\n");
303 if (success && rows_completed_out)
305 *rows_completed_out = (uint_fast16_t)rows_completed;
311 printf("ERROR: Failed to open table file for reading!\n");
315 //-----------------------------------------------------------------------------
317 //-----------------------------------------------------------------------------
319 int wmain(int argc, wchar_t *argv[])
321 FILE *file_out = NULL;
322 uint_fast16_t initial_row_index = 0;
323 char time_string[64];
325 printf("MHash GenTableMIX [%s]\n\n", __DATE__);
326 printf("HashLen: %d, Distance Min: %d\n\n", HASH_LEN, DISTANCE_MIN);
328 if ((HASH_LEN % (8 * sizeof(uint32_t))) != 0)
330 crit_exit("FATAL: Hash length must be a multiple of 32 bits!");
333 for (uint_fast8_t i = 0; i < ROW_LEN; ++i)
335 if (INDICES[i] != ((uint8_t)i))
337 crit_exit("FATAL: Invalid indices array detected!");
343 printf("Table file not specified!\n\n");
345 printf(" GenTables_MIX.exe <table_file>\n\n");
350 msws_init(rand, make_seed());
353 gaussian_noise_init(&bxmller);
355 if (_waccess(argv[1], 4) == 0)
357 printf("Loading existing table data and proceeding...\n");
358 if (!load_table_data(argv[1], &initial_row_index))
364 for (uint_fast16_t i = initial_row_index; i < ROW_NUM; ++i)
366 printf("Row %03u of %03u [%c]", (uint32_t)(i + 1U), ROW_NUM, SPINNER[g_spinpos]);
367 uint8_t temp[ROW_LEN];
368 uint_fast16_t counter = 0U;
371 random_permutation(rand, &g_table[i][0]);
372 uint_fast16_t error = check_permutation(i, &g_table[i][0], UINT16_MAX);
375 static const int_fast16_t MAX_RETRY = 1999U;
376 for (int_fast16_t retry = 0; retry < MAX_RETRY; ++retry)
378 if (!((++counter) & 0x3))
380 TRACE("Randomize round %u of %u", retry, MAX_RETRY);
381 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
382 g_spinpos = (g_spinpos + 1) % 4;
384 for (uint_fast16_t rand_init = 0U; rand_init < 9973U; ++rand_init)
386 random_permutation(rand, &temp[0]);
387 const uint_fast16_t error_next = check_permutation(i, &temp[0], error);
388 if (error_next < error)
390 TRACE("Improved by rand init! (%04X -> %04X)", error, error_next);
392 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
393 if (!((error = error_next) > 0U))
398 for (uint_fast8_t rotate = 0U; rotate < ROW_LEN; ++rotate)
400 rotate_row(&temp[0]);
401 const uint_fast16_t error_next = check_permutation(i, &temp[0], error);
402 if (error_next < error)
404 TRACE("Improved by rand init + rotate! (%04X -> %04X)", error, error_next);
406 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
407 if (!((error = error_next) > 0U))
412 reverse_row(&temp[0]);
413 const uint_fast16_t error_next_reverse = check_permutation(i, &temp[0], error);
414 if (error_next_reverse < error)
416 TRACE("Improved by rand init + rotate + reverse! (%04X -> %04X)", error, error_next_reverse);
418 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
419 if (!((error = error_next_reverse) > 0U))
426 reverse_row(&temp[0]);
431 for (int_fast16_t retry = 0; retry < MAX_RETRY; ++retry)
433 TRACE("Optimizer round %u of %u", retry, MAX_RETRY);
436 msws_init(rand, make_seed());
437 TRACE("First round!");
438 for (uint_fast8_t swap_x1 = 0; swap_x1 < ROW_LEN; ++swap_x1)
440 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
441 g_spinpos = (g_spinpos + 1) % 4;
442 for (uint_fast8_t swap_y1 = swap_x1 + 1U; swap_y1 < ROW_LEN; ++swap_y1)
444 bool revert_1 = true;
445 swap(&g_table[i][0], swap_x1, swap_y1);
446 const uint_fast16_t error_next = check_permutation(i, &g_table[i][0], error);
447 if (error_next < error)
449 TRACE("Improved by swap-1! (%04X -> %04X)", error, error_next);
452 if (!((error = error_next) > 0U))
457 for (uint_fast8_t swap_x2 = 0; swap_x2 < ROW_LEN; ++swap_x2)
459 for (uint_fast8_t swap_y2 = swap_x2 + 1U; swap_y2 < ROW_LEN; ++swap_y2)
461 bool revert_2 = true;
462 swap(&g_table[i][0], swap_x2, swap_y2);
463 const uint_fast16_t error_next = check_permutation(i, &g_table[i][0], error);
464 if (error_next < error)
466 TRACE("Improved by swap-2! (%04X -> %04X)", error, error_next);
467 revert_1 = revert_2 = false;
469 if (!((error = error_next) > 0U))
474 for (uint_fast8_t swap_x3 = 0; swap_x3 < ROW_LEN; ++swap_x3)
476 for (uint_fast8_t swap_y3 = swap_x3 + 1U; swap_y3 < ROW_LEN; ++swap_y3)
478 swap(&g_table[i][0], swap_x3, swap_y3);
479 const uint_fast16_t error_next = check_permutation(i, &g_table[i][0], error);
480 if (error_next >= error)
482 swap(&g_table[i][0], swap_x3, swap_y3); /*revert*/
486 TRACE("Improved by swap-3! (%04X -> %04X)", error, error_next);
487 revert_1 = revert_2 = false;
489 if (!((error = error_next) > 0U))
498 swap(&g_table[i][0], swap_x2, swap_y2); /*revert*/
504 swap(&g_table[i][0], swap_x1, swap_y1); /*revert*/
509 const double sigma = 3.14159 * (1.0 + (retry / (double)MAX_RETRY));
510 for (int_fast16_t loop = 0; loop < 9973U; ++loop)
512 const uint_fast8_t swap_count = (uint_fast8_t)gaussian_noise_next(rand, &bxmller, sigma, 4U, (ROW_LEN / 2U));
513 if (!((++counter) & 0x3FFF))
515 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
516 g_spinpos = (g_spinpos + 1) % 4;
518 for (int_fast16_t round = 0; round < 997U; ++round)
520 memcpy(&temp[0], &g_table[i][0], sizeof(uint8_t) * ROW_LEN);
521 swap_multiple_random(rand, &temp[0], swap_count);
522 const uint_fast16_t error_next = check_permutation(i, &temp[0], error);
523 if (error_next < error)
525 TRACE("Improved by swap-N! (N=%u, %04X -> %04X)", swap_count, error, error_next);
526 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
528 if (!((error = error_next) > 0U))
537 msws_init(rand, make_seed());
546 if (check_permutation(i, &g_table[i][0], UINT16_MAX))
548 fprintf(stderr, "ERROR MISCOMPARE!\n");
552 get_time_str(time_string, 64);
553 printf("\b\b\b[#] - %s\n", time_string);
555 if (!save_table_data(argv[1], i + 1U))
557 return 1; /*failed to save current table data*/
561 printf("\n-----\n\n");
564 if (fopen_s(&file_out, "table_MIX." DISTANCE_STR ".txt", "w") == 0)
566 dump_table(file_out);
570 printf("\n-----\n\n");
572 printf("COMPLETED.\n\n");