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-2 /*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 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
65 #define TRACE(X, ...) printf(X "\n", __VA_ARGS__)
67 #define TRACE(X, ...) __noop()
70 static inline void swap(uint8_t *const row_buffer, const size_t a, const size_t b)
72 const uint8_t temp = row_buffer[a];
73 row_buffer[a] = row_buffer[b];
77 static inline void random_permutation(twister_t *const rand, uint8_t *const row_buffer)
79 for (uint32_t i = 0; i < ROW_LEN; ++i)
81 row_buffer[i] = ((uint8_t)i);
83 for (uint32_t i = 0; i < ROW_LEN - 1U; ++i)
85 swap(row_buffer, i, next_rand_range(rand, ROW_LEN - i) + i);
89 static inline void swap_multiple_random(twister_t *const rand, uint8_t *const row_buffer, const size_t count)
91 const size_t count_min = min(count, ROW_LEN / 2U);
93 memset(&map[0], 0, sizeof(bool) * ROW_LEN);
94 for (size_t i = 0U; i < count_min; ++i)
99 a = next_rand_range(rand, ROW_LEN);
100 b = next_rand_range(rand, ROW_LEN);
102 while(map[a] || (a == b));
103 map[a] = map[b] = true;
104 swap(row_buffer, a, b);
108 static inline void rotate_row(uint8_t *const row_buffer)
110 const uint8_t temp = row_buffer[0];
111 for (uint32_t k = 0U; k < ROW_LEN - 1U; ++k)
113 row_buffer[k] = row_buffer[k + 1U];
115 row_buffer[ROW_LEN - 1U] = temp;
118 static inline void reverse_row(uint8_t *const row_buffer)
120 size_t j = ROW_LEN - 1U;
121 for (size_t i = 0U; i < ROW_LEN / 2U; ++i)
123 swap(row_buffer, i, j--);
127 static inline uint32_t check_permutation(const size_t index, const uint8_t *const row_buffer, const uint32_t limit)
130 for (size_t i = 0; i < ROW_LEN; ++i)
132 if (row_buffer[i] == ((uint8_t)i))
134 if (++error >= limit)
136 break; /*early termination*/
142 return ROW_LEN + error;
144 for (size_t i = 0; i < index; ++i)
146 uint32_t distance = 0U;
147 for (size_t j = 0; j < ROW_LEN; ++j)
149 if (g_table[i][j] != row_buffer[j])
154 if (distance < DISTANCE_MIN)
156 if ((error = max_ui32(error, DISTANCE_MIN - distance)) >= limit)
158 break; /*early termination*/
165 static void print_row(const uint8_t *const row_buffer)
167 for (size_t i = 0; i < ROW_LEN; ++i)
169 printf(i ? ",%02X" : "%02X", row_buffer[i]);
174 static inline void permutation_to_shuffle_indices(const uint8_t *const row_buffer, uint8_t *const indices_out)
176 uint8_t reference[ROW_LEN];
177 for (uint32_t i = 0U; i < ROW_LEN; ++i)
179 reference[i] = ((uint8_t)i);
181 for (uint32_t i = 0U; i < ROW_LEN; ++i)
183 indices_out[i] = UINT8_MAX;
184 for (uint32_t j = i; j < ROW_LEN; ++j)
186 if (reference[j] == row_buffer[i])
188 swap(&reference[0], i, j);
189 indices_out[i] = ((uint8_t)j);
194 for (uint32_t i = 0; i < ROW_LEN; ++i)
196 if ((indices_out[i] == UINT8_MAX) || (reference[i] != row_buffer[i]))
198 puts("ERROR: Failed to derive shuffle indices!\n");
203 static dump_table(FILE *out)
205 fprintf(out, "uint8_t MHASH_384_TABLE_MIX[%u][MHASH_384_LEN] =\n{\n", ROW_NUM);
206 for (size_t i = 0; i < ROW_NUM; i++)
209 for (size_t j = 0; j < ROW_LEN; j++)
215 fprintf(out, "0x%02X", g_table[i][j]);
217 fprintf(out, " }%s /*%03u*/\n", (i != (ROW_NUM - 1)) ? "," : " ", (uint32_t)i);
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
226 static bool save_table_data(const wchar_t *const filename, const size_t rows_completed_in)
228 FILE *const file = _wfopen(filename, L"wb");
232 const uint64_t magic_number = MAGIC_NUMBER;
233 const uint32_t hash_len = HASH_LEN, distance_min = DISTANCE_MIN, rows_completed = (uint32_t)rows_completed_in;
234 fwrite(&magic_number, sizeof(uint64_t), 1, file);
235 fwrite(&hash_len, sizeof(uint32_t), 1, file);
236 fwrite(&distance_min, sizeof(uint32_t), 1, file);
237 fwrite(&rows_completed, sizeof(uint32_t), 1, file);
238 for (uint32_t i = 0; i < rows_completed; ++i)
240 const uint32_t checksum = adler32(&g_table[i][0], ROW_LEN);
241 fwrite(&checksum, sizeof(uint32_t), 1, file);
242 fwrite(&g_table[i][0], sizeof(uint8_t), ROW_LEN, file);
246 printf("ERROR: Failed to write table data!\n");
254 printf("ERROR: Failed to open table file for writing!\n");
259 static bool load_table_data(const wchar_t *const filename, size_t *const rows_completed_out)
261 FILE *const file = _wfopen(filename, L"rb");
265 uint64_t magic_number;
266 uint32_t hash_len, distance_min, rows_completed;
267 fread(&magic_number, sizeof(uint64_t), 1, file);
268 fread(&hash_len, sizeof(uint32_t), 1, file);
269 fread(&distance_min, sizeof(uint32_t), 1, file);
270 fread(&rows_completed, sizeof(uint32_t), 1, file);
271 if (ferror(file) || feof(file))
273 printf("ERROR: Failed to read the table header!\n");
277 if (magic_number != MAGIC_NUMBER)
279 printf("ERROR: Table file format could not be recognized!\n");
283 if ((hash_len != HASH_LEN) || (distance_min != DISTANCE_MIN) || (rows_completed > ROW_NUM))
285 printf("ERROR: Table properties are incompatibe with this instance!\n");
289 for (size_t i = 0; i < rows_completed; ++i)
291 uint32_t checksum_expected;
292 if ((fread(&checksum_expected, sizeof(uint32_t), 1, file) != 1) || (fread(&g_table[i][0], sizeof(uint8_t), ROW_LEN, file) != ROW_LEN))
294 printf("ERROR: Failed to read table data from file!\n");
298 if (adler32(&g_table[i][0], ROW_LEN) != checksum_expected)
300 printf("ERROR: Table checksum does *not* match table contents!\n");
304 if (check_permutation(i, &g_table[i][0], 0U) != 0U)
306 printf("ERROR: Table distance verification has failed!\n");
313 if (success && rows_completed_out)
315 *rows_completed_out = (size_t)rows_completed;
321 printf("ERROR: Failed to open table file for reading!\n");
325 //-----------------------------------------------------------------------------
327 //-----------------------------------------------------------------------------
329 int wmain(int argc, wchar_t *argv[])
331 FILE *file_out = NULL;
332 size_t initial_row_index = 0;
333 char time_string[64];
335 printf("MHash GenTableMIX [%s]\n\n", __DATE__);
336 printf("HashLen: %d, Distance Min: %d\n\n", HASH_LEN, DISTANCE_MIN);
338 if ((HASH_LEN % (8 * sizeof(uint32_t))) != 0)
340 crit_exit("FATAL: Hash length must be a multiple of 32 bits!");
345 printf("Table file not specified!\n\n");
347 printf(" GenTables_MIX.exe <table_file>\n\n");
352 rand_init(&rand, make_seed());
355 gaussian_noise_init(&bxmller);
357 if (_waccess(argv[1], 4) == 0)
359 printf("Loading existing table data and proceeding...\n");
360 if (!load_table_data(argv[1], &initial_row_index))
366 for (size_t i = initial_row_index; i < ROW_NUM; ++i)
368 printf("Row %03u of %03u [%c]", (uint32_t)(i + 1U), ROW_NUM, SPINNER[g_spinpos]);
369 uint8_t temp[ROW_LEN];
370 uint_fast16_t counter = 0U;
373 random_permutation(&rand, &g_table[i][0]);
374 uint32_t error = check_permutation(i, &g_table[i][0], 2U * ROW_LEN);
379 for (int_fast16_t retry = 0; retry < 4999; ++retry)
381 if (!((++counter) & 0x3F))
383 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
384 g_spinpos = (g_spinpos + 1) % 4;
386 for (uint_fast16_t rand_init = 0U; rand_init < 9973U; ++rand_init)
388 random_permutation(&rand, &temp[0]);
389 const uint32_t error_next = check_permutation(i, &temp[0], error);
390 if (error_next < error)
392 TRACE("Improved by rand init!");
394 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
395 if (!((error = error_next) > 0U))
400 for (uint_fast8_t rotate = 0U; rotate < ROW_LEN; ++rotate)
402 rotate_row(&temp[0]);
403 const uint32_t error_next = check_permutation(i, &temp[0], error);
404 if (error_next < error)
406 TRACE("Improved by rotate!");
408 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
409 if (!((error = error_next) > 0U))
416 reverse_row(&temp[0]);
417 const uint32_t error_next_reverse = check_permutation(i, &temp[0], error);
418 if (error_next_reverse < error)
420 TRACE("Improved by reverse!");
422 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
423 if (!((error = error_next_reverse) > 0U))
433 for (int_fast16_t retry = 0; retry < 4999; ++retry)
435 TRACE("Optimizer round %u of 4999", retry);
438 rand_init(&rand, make_seed());
439 TRACE("First round!");
440 for (uint_fast8_t swap_x1 = 0; swap_x1 < ROW_LEN; ++swap_x1)
442 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
443 g_spinpos = (g_spinpos + 1) % 4;
444 for (uint_fast8_t swap_y1 = swap_x1 + 1U; swap_y1 < ROW_LEN; ++swap_y1)
446 bool revert_1 = true;
447 swap(&g_table[i][0], swap_x1, swap_y1);
448 const uint32_t error_next = check_permutation(i, &g_table[i][0], error);
449 if (error_next < error)
451 TRACE("Improved by swap-1!");
454 if (!((error = error_next) > 0U))
459 for (uint_fast8_t swap_x2 = 0; swap_x2 < ROW_LEN; ++swap_x2)
461 for (uint_fast8_t swap_y2 = swap_x2 + 1U; swap_y2 < ROW_LEN; ++swap_y2)
463 bool revert_2 = true;
464 swap(&g_table[i][0], swap_x2, swap_y2);
465 const uint32_t error_next = check_permutation(i, &g_table[i][0], error);
466 if (error_next < error)
468 TRACE("Improved by swap-2!");
469 revert_1 = revert_2 = false;
471 if (!((error = error_next) > 0U))
476 for (uint_fast8_t swap_x3 = 0; swap_x3 < ROW_LEN; ++swap_x3)
478 for (uint_fast8_t swap_y3 = swap_x3 + 1U; swap_y3 < ROW_LEN; ++swap_y3)
480 swap(&g_table[i][0], swap_x3, swap_y3);
481 const uint32_t error_next = check_permutation(i, &g_table[i][0], error);
482 if (error_next >= error)
484 swap(&g_table[i][0], swap_x3, swap_y3); /*revert*/
488 TRACE("Improved by swap-3!");
489 revert_1 = revert_2 = false;
491 if (!((error = error_next) > 0U))
500 swap(&g_table[i][0], swap_x2, swap_y2); /*revert*/
506 swap(&g_table[i][0], swap_x1, swap_y1); /*revert*/
511 const double sigma = 3.14159 * (1.0 + (retry / 4999.0));
512 for (int_fast16_t loop = 0; loop < 9973U; ++loop)
514 const uint32_t swap_count = gaussian_noise_next(&rand, &bxmller, sigma, 4U, (ROW_LEN / 2U));
515 if (!((++counter) & 0x3FFF))
517 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
518 g_spinpos = (g_spinpos + 1) % 4;
520 for (int_fast16_t round = 0; round < 997U; ++round)
522 memcpy(&temp[0], &g_table[i][0], sizeof(uint8_t) * ROW_LEN);
523 swap_multiple_random(&rand, &temp[0], swap_count);
524 const uint32_t error_next = check_permutation(i, &temp[0], error);
525 if (error_next < error)
527 TRACE("Improved by swap-n (%u)!", swap_count);
528 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
530 if (!((error = error_next) > 0U))
546 if (check_permutation(i, &g_table[i][0], 2U * ROW_LEN))
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");