-
Notifications
You must be signed in to change notification settings - Fork 6
/
unroll.cpp
203 lines (173 loc) · 4.3 KB
/
unroll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "unroll.hpp"
#include <cstdlib>
MTUnroller::MTUnroller(bool php)
: N(624), M(397), a(0x9908b0df), stateSize(19968), currentPos(0), update(true)
{
fillTempering();
varArray = new Equation[stateSize];
//State is actually 19937 bits but word aligning the size allows better indexing
// when generating the equations, thus the -31 in the initial values.
mtPHP = php;
if (php) {
varArray[0].push_back(0);
varArray[31].push_back(1);
for (int i = 32; i < stateSize; i ++)
varArray[i].push_back(i - 30);
} else {
varArray[0].push_back(0);
for (int i = 32; i < stateSize; i ++)
varArray[i].push_back(i - 31);
}
return;
}
void
MTUnroller::latexPrintTempering()
{
unsigned int i,j;
for (i = 0; i < 32; i ++) {
std::cout << "$$ z_{" << i << "} = ";
for (j = 0; j < tempering[i].size()-1; j ++)
std::cout <<"x_{" << tempering[i][j] << "} \\oplus ";
std::cout <<"x_{" << tempering[i][j] << "} $$" << std::endl;
}
return;
}
void
MTUnroller::fillTempering()
{
unsigned short int sizes[] = {4,3,5,2,7,5,5,7,3,5,3,2,4,6,4,2,2,4,4,7,
7,4,9,5,9,7,5,7,3,4,4,6};
unsigned short int t[][10] = {{0, 4, 7, 15},
{1, 5, 16},
{2, 6, 13, 17, 24},
{3, 10},
{0, 4, 8, 11, 15, 19, 26},
{1, 5, 9, 12, 20},
{6, 10, 17, 21, 28},
{3, 7, 11, 14, 18, 22, 29},
{8, 12, 23},
{9, 13, 20, 24, 31},
{6, 10, 17},
{0, 11},
{1, 8, 12, 19},
{2, 9, 13, 17, 20, 28},
{3, 14, 18, 29},
{4, 15},
{5, 16},
{6, 13, 17, 24},
{0, 4, 15, 18},
{1, 5, 8, 15, 16, 19, 26},
{2, 6, 9, 13, 17, 20, 24},
{3, 17, 21, 28},
{0, 4, 8, 15, 18, 19, 22, 26, 29},
{1, 5, 9, 20, 23},
{6, 10, 13, 17, 20, 21, 24, 28, 31},
{3, 7, 11, 18, 22, 25, 29},
{8, 12, 15, 23, 26},
{9, 13, 16, 20, 24, 27, 31},
{6, 10, 28},
{0, 11, 18, 29},
{1, 8, 12, 30},
{2, 9, 13, 17, 28, 31}};
for (int i = 0; i < 32; i ++ )
tempering[i].assign(t[i], t[i] + sizes[i]);
return;
}
void
MTUnroller::generateXi()
{
unsigned short int word = currentPos / 32;
std::vector <Equation> neweq;
unsigned short int adder = (mtPHP == true)? 0 : 1;
for (int i = 0; i < 32; i ++ )
switch (i) {
case 0:
neweq.push_back(varArray[((word + M) % N)*32] +
varArray[((word + adder) % N)*32 + 31]);
break;
case 1:
neweq.push_back(varArray[((word + M) % N)*32 + 1] + varArray[word * 32]);
break;
default:
neweq.push_back(varArray[((word + M) % N)*32 + i] + varArray[((word + 1) % N) * 32 + i - 1]);
if (a & (1 << (31 - i)))
neweq[i] += varArray[((word + adder) % N)*32 + 31];
break;
}
/* If we update directly the original array, we will be using the new
equations instead of the old ones that we need in some bit positions */
for (int i = 0; i < 32; i ++ ){
varArray[word*32 + i] = neweq[i];
}
return;
}
Equation
MTUnroller::getNextEquation()
{
unsigned short int bit = currentPos % 32;
unsigned short int word = currentPos / 32;
Equation eq;
// generate the xis for the current word...
if (update) {
generateXi();
update = false;
}
for (std::vector <unsigned short int>::iterator it = tempering[bit].begin();
it != tempering[bit].end(); it ++) {
eq += varArray[word*32 + *it];
}
if (bit == 31)
update = true;
currentPos = (currentPos + 1) % stateSize;
return eq;
}
/*
* Dump implementation
*/
void
MTUnroller::skipEquations( unsigned short int num )
{
for (int i = 0; i < num; i ++) {
unsigned short int bit = currentPos % 32;
if (update){
generateXi();
update = false;
}
if (bit == 31)
update = true;
currentPos = (currentPos + 1) % stateSize;
}
return;
}
void
MTUnroller::clear()
{
for (int i = 0; i < stateSize; i ++) {
varArray[i].clear();
varArray[i].push_back(i);
}
currentPos = 0;
update = true;
return;
}
void
MTUnroller::dumpVarArray()
{
for (int i = 0; i < stateSize; i ++)
varArray[i].print();
return;
}
#if 0
int main(int argc, char *argv[])
{
MTUnroller roller;
int i = 0,skip = atoi(argv[1]);
while (i < 19937){
for (int j = 0; j < skip; j++) {
roller.getNextEquation().printPython();
i ++;
}
roller.skipEquations(32-skip);
}
}
#endif