OSDN Git Service

Small fix + regenerated MIX table.
[mhash384/mhash384.git] / tools / GenTables / src / gen_table_mix.c
1 /* ----------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Generate tables utility program                                                     */
3 /* Copyright(c) 2016 LoRd_MuldeR <mulder2@gmx.de>                                                  */
4 /*                                                                                                 */
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:                            */
10 /*                                                                                                 */
11 /* The above copyright notice and this permission notice shall be included in all copies or        */
12 /* substantial portions of the Software.                                                           */
13 /*                                                                                                 */
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 /* ----------------------------------------------------------------------------------------------- */
20
21 #include "common.h"
22 #include "thread_utils.h"
23 #include "twister.h"
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <time.h>
29 #include <stdbool.h>
30
31 //-----------------------------------------------------------------------------
32 // Const
33 //-----------------------------------------------------------------------------
34
35 #define HASH_LEN 384
36
37 #define ROW_NUM (UINT8_MAX+1)           /*total number of rows*/
38 #define ROW_LEN (HASH_LEN / CHAR_BIT)   /*number of bits per row*/
39
40 //-----------------------------------------------------------------------------
41 // Globals
42 //-----------------------------------------------------------------------------
43
44 static uint8_t g_table[ROW_NUM][HASH_LEN];
45
46 //-----------------------------------------------------------------------------
47 // Utility Functions
48 //-----------------------------------------------------------------------------
49
50 static inline void build_permutation(uint8_t *const row_buffer, twister_t *const rand)
51 {
52         for (uint32_t i = 0; i < ROW_LEN; i++)
53         {
54                 row_buffer[i] = (uint8_t)(i + (next_rand_range(rand, ROW_LEN - i)));
55         }
56 }
57
58 static inline void apply_permutation(uint8_t *const row_buffe, const uint8_t *const permutation)
59 {
60         uint8_t tmp;
61         for (size_t i = 0; i < ROW_LEN; ++i)
62         {
63                 tmp = row_buffe[permutation[i]];
64                 row_buffe[permutation[i]] = row_buffe[i];
65                 row_buffe[i] = tmp;
66         }
67 }
68
69 static inline bool test_permutation(const uint8_t *const permutation)
70 {
71         uint8_t row_buffe[ROW_LEN];
72         for (size_t i = 0; i < ROW_LEN; ++i)
73         {
74                 row_buffe[i] = ((uint8_t)i);
75         }
76
77         apply_permutation(row_buffe, permutation);
78         for (size_t i = 0; i < ROW_LEN; ++i)
79         {
80                 if (row_buffe[i] == ((uint8_t)i))
81                 {
82                         return false;
83                 }
84         }
85
86         return true;
87 }
88
89 static dump_table(FILE *out)
90 {
91         fputs("uint8_t MHASH_384_TABLE_MIX[UINT8_MAX+1][MHASH_384_LEN] =\n{\n", out);
92         for (size_t i = 0; i < ROW_NUM; i++)
93         {
94                 fputs("\t{ ", out);
95                 for (size_t j = 0; j < ROW_LEN; j++)
96                 {
97                         if (j > 0)
98                         {
99                                 fputc(',', out);
100                         }
101                         fprintf(out, "0x%02X", g_table[i][j]);
102                 }
103                 fprintf(out, " }%s /*%02X*/\n", (i != (ROW_NUM - 1)) ? "," : " ", (uint32_t)(i % 0x100));
104         }
105         fputs("};\n", out);
106 }
107
108 //-----------------------------------------------------------------------------
109 // MAIN
110 //-----------------------------------------------------------------------------
111
112 int main()
113 {
114         FILE *file_out = NULL;
115
116         printf("MHash GenTableMIX [%s]\n\n", __DATE__);
117         printf("HashLen: %d\n\n", HASH_LEN);
118
119         if ((HASH_LEN % (8 * sizeof(uint32_t))) != 0)
120         {
121                 crit_exit("FATAL: Hash length must be a multiple of 32 bits!");
122         }
123
124         twister_t rand;
125         rand_init(&rand, make_seed());
126
127         for (size_t i = 0; i < ROW_NUM; ++i)
128         {
129                 printf("Row %03u of %03u...", (uint32_t)(i + 1U), ROW_NUM);
130                 do
131                 {
132                         fputc('.', stdout);
133                         build_permutation(&g_table[i][0], &rand);
134                 }
135                 while(!test_permutation(&g_table[i][0]));
136                 puts(" done");
137         }
138
139         printf("\n-----\n\n");
140
141         dump_table(stdout);
142         if (fopen_s(&file_out, "table_out.MIX.txt", "w") == 0)
143         {
144                 dump_table(file_out);
145                 fclose(file_out);
146         }
147
148         printf("\n-----\n\n");
149
150         printf("COMPLETED.\n\n");
151         return getchar();
152 }