-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaar.cpp
251 lines (226 loc) · 6.85 KB
/
paar.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include"paar.h"
#include"rnbp_a1_a2.h"
extern mt19937 rand_generator;
extern int SIZE;
int hamming_weight(uint64_t input)
{
return __builtin_popcount(input & 0xffffffff) + __builtin_popcount((input >> 32) & 0xffffffff);
}
int Paar_algorithm1(vector<list> &seq, uint64_t NumInputs, uint64_t NumTargets, uint64_t *Target)
{
uint64_t *input_matrix;
input_matrix = (uint64_t *)malloc((SIZE + 1000) * sizeof(uint64_t));
vector<uint64_t> P;
vector<uint64_t> Q;
int randomly_flag = rand() % 2;
if(randomly_flag == 1)
{
P = generate_p(NumTargets);
Q = generate_p(NumInputs);
p_m_q_paar(Target, NumTargets, NumInputs, P, Q);
}
for(int i = 0; i < NumInputs; i++)
{
input_matrix[i] = 0;
for(int j = 0; j < NumTargets; j++)
{
input_matrix[i] += ((Target[j] >> i) & 1ull) << j;
}
}
int xor_count = 0; /*In fact, here we do not need to record the xor_count, since 'seq.size() - SIZE' is equal to it.*/
uint64_t number_of_columns = NumInputs;
int hw_max = 0;
int i_max = 0, j_max = 0;
uint64_t tmp = 0;
int hw = 0;
uint64_t new_column = 0;
vector<pair<int, int>> program;
do {
hw_max = 0;
for (uint64_t i = 0; i < number_of_columns; i++)
{
for (uint64_t j = i + 1; j < number_of_columns; j++)
{
tmp = input_matrix[i] & input_matrix[j];
hw = hamming_weight(tmp);
if (hw > hw_max)
{
hw_max = hw;
i_max = i;
j_max = j;
}
}
}
if (hw_max >= 1)
{
new_column = input_matrix[i_max] & input_matrix[j_max];
input_matrix[number_of_columns] = new_column;
input_matrix[i_max] = new_column ^ input_matrix[i_max];
input_matrix[j_max] = new_column ^ input_matrix[j_max];
xor_count -= (hw_max - 1);
number_of_columns++;
program.push_back(make_pair(i_max, j_max));
}
} while (hw_max >= 1);
int ctr = NumInputs;
for(auto const& prog:program)
{
list row;
row.flag = -1;
row.usd = 0;
row.value.push_back(ctr);
row.value.push_back(prog.first);
row.value.push_back(prog.second);
seq.push_back(row);
ctr++;
}
for(uint64_t i = 0; i < NumTargets; i++)
{
for(uint64_t j = 0; j < number_of_columns; j++)
{
if ((input_matrix[j] & (1ll << (NumTargets - 1 - i))) != 0)
{
for(int k = 0; k < seq.size(); k++)
{
if(seq[k].value[0] == j)
{
seq[k].flag = (NumTargets - 1 - i);
}
}
}
}
}
free(input_matrix);
if(randomly_flag == 1)
{
recover_implementation(seq, P, Q);
}
return xor_count;
}
int RPaar_algorithm1(vector<list> &seq, uint64_t NumInputs, uint64_t NumTargets, uint64_t *Target)
{
uint64_t *input_matrix;
input_matrix = (uint64_t *)malloc((SIZE + 1000) * sizeof(uint64_t));
vector<uint64_t> P;
vector<uint64_t> Q;
int randomly_flag = rand() % 2;
if(randomly_flag == 1)
{
P = generate_p(NumTargets);
Q = generate_p(NumInputs);
p_m_q_paar(Target, NumTargets, NumInputs, P, Q);
}
for(int i = 0; i < NumInputs; i++)
{
input_matrix[i] = 0;
for(int j = 0; j < NumTargets; j++)
{
input_matrix[i] += ((Target[j] >> i) & 1) << j;
}
}
int xor_count = 0;
uint64_t number_of_columns = NumInputs;
int hw_max = 0;;
int i_max = 0, j_max = 0;
uint64_t tmp = 0;
uint64_t new_column = 0;
// compute naive xor count
for (int i = 0; i < NumInputs; i++)
{
xor_count += hamming_weight(input_matrix[i]);
}
xor_count -= NumTargets;
vector<pair<int, int>> program;
do {
vector<pair<int, int>> idx_i_j = find_max_idx(input_matrix, number_of_columns);
i_max = idx_i_j[0].first;
j_max = idx_i_j[0].second;
tmp = input_matrix[i_max] & input_matrix[j_max];
hw_max = hamming_weight(tmp);
if (hw_max >= 1)
{
new_column = input_matrix[i_max] & input_matrix[j_max];
input_matrix[number_of_columns] = new_column;
input_matrix[i_max] = new_column ^ input_matrix[i_max];
input_matrix[j_max] = new_column ^ input_matrix[j_max];
xor_count -= (hw_max - 1);
number_of_columns++;
program.push_back(make_pair(i_max, j_max));
}
} while (hw_max >= 1);
int ctr = NumInputs;
for(auto const& prog:program)
{
list row;
row.flag = -1;
row.usd = 0;
row.value.push_back(ctr);
row.value.push_back(prog.first);
row.value.push_back(prog.second);
seq.push_back(row);
//cout << "x" << ctr << " = x" << prog.first << " + x" << prog.second << endl;
ctr++;
}
for(uint64_t i = 0; i < NumTargets; i++)
{
for(uint64_t j = 0; j < number_of_columns; j++)
{
if ((input_matrix[j] & (1ll << (NumTargets - 1 - i))) != 0)
{
//cout << " = x" << j;
for(int k = 0; k < seq.size(); k++)
{
if(seq[k].value[0] == j)
{
seq[k].flag = (NumTargets - 1 - i);
}
}
}
}
}
free(input_matrix);
if(randomly_flag == 1)
{
recover_implementation(seq, P, Q);
}
return xor_count;
}
vector<pair<int, int>> find_max_idx(uint64_t *input_matrix, int number_of_columns)
{
uint64_t tmp;
int hw = 0;
int i_max_list[10000];
int j_max_list[10000];
int hw_max = 0;
int list_end = 0;
for (int i = 0; i < number_of_columns; i++)
{
for (int j = i + 1; j < number_of_columns; j++)
{
tmp = input_matrix[i] & input_matrix[j];
hw = hamming_weight(tmp);
if (hw > hw_max) {
hw_max = hw;
i_max_list[0] = i;
j_max_list[0] = j;
list_end = 1;
}
else if ((hw == hw_max)&& (hw != 0))
{
i_max_list[list_end] = i;
j_max_list[list_end] = j;
list_end++;
}
}
}
vector<pair<int, int>> idx_i_j_max;
int rand_num = 0;
if(list_end > 1)
{
rand_generator.seed(time(0));
uniform_int_distribution<int> rand_distribution(0, list_end - 1);
rand_num = rand_distribution(rand_generator);
}
idx_i_j_max.push_back(make_pair(i_max_list[rand_num], j_max_list[rand_num]));
return idx_i_j_max;
}