-
Notifications
You must be signed in to change notification settings - Fork 1
/
secCompMultiParty.c
303 lines (245 loc) · 8.5 KB
/
secCompMultiParty.c
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* secCompMultiParty.c
*
* Created on: Mar 31, 2015
* Author: froike
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "secCompMultiParty.h"
#include <emmintrin.h>
Gate GateCreator(const unsigned int inputBit1, const unsigned int inputBit2, const unsigned int outputBit, TruthTable TTable, const unsigned int flags)
{
Gate g;
g.inputBit1 = inputBit1;
g.inputBit2 = inputBit2;
g.outputBit = outputBit;
g.truthTable = TTable;
g.flags = flags;
return g;
}
inline unsigned int charToBooleanValue(char v){
if (v == '1') {
return true;
}
return false;
}
TruthTable createTruthTablefFromChars(char FF, char FT, char TF, char TT){
TruthTable TrueT;
TrueT.FF = charToBooleanValue(FF);
TrueT.FT = charToBooleanValue(FT);
TrueT.TF = charToBooleanValue(TF);
TrueT.TT = charToBooleanValue(TT);
TrueT.Y1 = TrueT.FF;
TrueT.Y2 = TrueT.FF ^ TrueT.TF;
TrueT.Y3 = TrueT.FF ^ TrueT.FT;
TrueT.Y4 = TrueT.FF ^ TrueT.FT ^ TrueT.TF ^ TrueT.TT;
return TrueT;
}
Cycle * readCycleFromFile(char * path)
{
unsigned int gateAmount = 0;
unsigned int playerAmount = 0;
unsigned int lineCount = 1;
unsigned int playerCounter = 0;
unsigned int gateCounter = 0;
unsigned int specialGatesAmount = 0;
unsigned int i;
unsigned int tempPlayerID;
unsigned int tempAmountOfBits;
unsigned int tempInput1;
unsigned int tempInput2;
unsigned int tempOutput;
unsigned int tempStatus;
char tempTT;
char tempFT;
char tempTF;
char tempFF;
TruthTable tempTruthTable;
unsigned int gotOutputsBits = false;
Cycle * cycleTR;
char lineBuff[STRING_BUFFER_SIZE];
char lineBuffCopy[STRING_BUFFER_SIZE];
FILE * cycleFile = fopen(path, "r");
if (cycleFile == NULL){
printf("Error: Bad file path...");
return NULL;
}
if(!fgets(lineBuff, STRING_BUFFER_SIZE, cycleFile)){
printf("Error: Seems to be empty file...");
return NULL;
}
while(fgets(lineBuff, STRING_BUFFER_SIZE, cycleFile)){
//check for notes lines or empty lines. if found, continue.
strcpy(lineBuffCopy,lineBuff);
removeSpacesAndTubs(lineBuffCopy);
if(lineBuffCopy[0] == 0 || lineBuffCopy[0] == '#' || lineBuffCopy[0] == '\n' ) continue ;
//will append in the first line and just one time.
if(gateAmount == 0){
if(sscanf(lineBuff,"%u %u", &gateAmount, &playerAmount)!=2){
printf("Error: First line in the file had to be in format of '<amount of gates> <amount of players>'\ne.g '32123 3'");
return NULL;
}
if(gateAmount <= 0){
printf("Error: Amount of gates need to be more than 0");
return NULL;
}
// Init Circle struct
cycleTR = (Cycle*) malloc(sizeof(Cycle));
cycleTR -> gateArray = (Gate *) malloc(sizeof(Gate)*gateAmount);
cycleTR -> playerArray = (Player **) malloc(sizeof(Player*)*playerAmount);
for (i = 0; i < playerAmount; ++i) {
cycleTR -> playerArray[i] = NULL;
}
cycleTR -> amountOfGates = gateAmount;
cycleTR -> amountOfPlayers = playerAmount;
}else if (playerCounter < playerAmount){
if (sscanf(lineBuff,"P%u %u",&tempPlayerID,&tempAmountOfBits) == 2){ //
cycleTR -> playerArray[tempPlayerID] = (Player*) malloc(sizeof(Player)); // Init new player....
cycleTR -> playerArray[tempPlayerID] -> playerBitAmount = tempAmountOfBits; //
cycleTR -> playerArray[tempPlayerID] -> playerBitArray = (unsigned int*) malloc(sizeof(unsigned int)*tempAmountOfBits);
//printf("amount: %u",tempAmountOfBits);
for (i = 0; i < tempAmountOfBits; ++i) {
if (!fgets(lineBuff, STRING_BUFFER_SIZE, cycleFile)) { printf("Error: in line %u bit serial expected... but the file is ended.", lineCount); return NULL;}
lineCount++;
if(sscanf(lineBuff,"%u",&cycleTR -> playerArray[tempPlayerID] -> playerBitArray[i]) != 1) {printf("Error: in line %u expected for bit serial... ", lineCount); return NULL;}
}
playerCounter++;
}else{
printf("Error: Player header expected. e.g P1 32");
return NULL;
}
}else if(!gotOutputsBits){
gotOutputsBits = true;
if (sscanf(lineBuff,"Out %u",&tempAmountOfBits) == 1){
cycleTR -> outputbits.playerBitAmount = tempAmountOfBits;
cycleTR -> outputbits.playerBitArray = (unsigned int*) malloc(sizeof(unsigned int)*tempAmountOfBits);
for (i = 0; i < cycleTR -> outputbits.playerBitAmount; ++i) {
if (!fgets(lineBuff, STRING_BUFFER_SIZE, cycleFile)) { printf("Error: in line %u bit serial expected... but the file is ended.", lineCount); return NULL;}
lineCount++;
if(sscanf(lineBuff,"%u",&cycleTR -> outputbits.playerBitArray[i]) != 1) {printf("Error: in line %u expected for bit serial... ", lineCount); return NULL;}
}
}else{
printf("Error: Outputs header expected. e.g O 32");
return NULL;
}
}else{//if done with Players and outputs bits, lets collect gates
if ((tempStatus = sscanf(lineBuff,"%u %u %u %c%c%c%c\n",&tempInput1, &tempInput2, &tempOutput, &tempFF, &tempFT, &tempTF, &tempTT)) >=7){
tempStatus = flagNone;
/** if(tempStatus >= 5){
if(strcmp(lineBuffCopy,dispFreeNor) == 0) tempStatus = flagFreeNor;
else if(strcmp(lineBuffCopy,dispFreeXor) == 0) tempStatus = flagFreeXor;
else tempStatus = flagNone;
}else tempStatus = flagNone; **/
/**if(tempTTable == 110) {
tempStatus = flagXor;
specialGatesAmount++;
}else if(tempTTable == 1001){
tempStatus = flagXnor;
specialGatesAmount++;
}else tempStatus = flagNone;*/
tempTruthTable = createTruthTablefFromChars(tempFF,tempFT,tempTF,tempTT);
cycleTR-> gateArray[gateCounter] = GateCreator(tempInput1, tempInput2, tempOutput, tempTruthTable , tempStatus);
gateCounter++;
}else{
printf("Error: Gate header expected.. format: 1 2 3 1001");
return NULL;
}
}
lineCount++;
}
if (gateCounter < gateAmount) {
printf("Error: expected to %u gates, but got only %u...",gateAmount,gateCounter);
return NULL;
}
cycleTR -> specialGates = specialGatesCollector(cycleTR->gateArray,gateAmount,specialGatesAmount);
printf("test: %u",specialGatesAmount);
return cycleTR;
}
char * truthTableToString(TruthTable TTB, char * charbuff){
unsigned int count = 0;
if(TTB.FF) charbuff[count++] = '1'; else charbuff[count++] = '0';
if(TTB.FT) charbuff[count++] = '1'; else charbuff[count++] = '0';
if(TTB.TF) charbuff[count++] = '1'; else charbuff[count++] = '0';
if(TTB.TT) charbuff[count++] = '1'; else charbuff[count++] = '0';
charbuff[count++] = ' ';
if(TTB.Y1) charbuff[count++] = '1'; else charbuff[count++] = '0';
if(TTB.Y2) charbuff[count++] = '1'; else charbuff[count++] = '0';
if(TTB.Y3) charbuff[count++] = '1'; else charbuff[count++] = '0';
if(TTB.Y4) charbuff[count++] = '1'; else charbuff[count++] = '0';
charbuff[count++] = '\0';
return charbuff;
}
void printCycle(const Cycle * c)
{
char trueTBuffer[30];
char * flagsFriendlyNames[] = {"",dispXor,dispXnor};
int p,i;
if (c == NULL) {
printf("Error: got NULL...\n");
}else{
printf("Gates amount: %u, players: %u\n",c->amountOfGates,c->amountOfPlayers);
for(p = 0; p < c->amountOfPlayers; p++ ){
printf("P%d %u\n",p,c->playerArray[p]->playerBitAmount);
for (i = 0; i < c->playerArray[p]->playerBitAmount; ++i) {
printf("%u\n",c->playerArray[p]->playerBitArray[i]);
}
}
printf("Out %u\n",c->outputbits.playerBitAmount);
for (i = 0; i < c->outputbits.playerBitAmount; ++i) {
printf("%u\n",c->outputbits.playerBitArray[i]);
}
for(i=0;i<c->amountOfGates;i++){
printf("%u %u %u %s %s\n",c->gateArray[i].inputBit1,c->gateArray[i].inputBit2,c->gateArray[i].outputBit,
truthTableToString(c->gateArray[i].truthTable,trueTBuffer),flagsFriendlyNames[c->gateArray[i].flags]);
}
}
}
void removeSpacesAndTubs(char* source)
{
char* i = source;
char* j = source;
while(*j != 0)
{
*i = *j++;
if(*i != ' ' && *i != '\t')
i++;
}
*i = 0;
}
void freeCircle(Cycle * c)
{
if (!c){
printf("Error: expected for circle , but got NULL...");
return;
}
int i;
for (i = 0; i < c->amountOfPlayers; ++i) {
free(c->playerArray[i]->playerBitArray);
}
free(c->playerArray);
free(c->gateArray);
free(c);
}
Gate ** specialGatesCollector(Gate * GatesArray, const unsigned int arraySize, const unsigned int specialGatesAmount){
unsigned int i;
unsigned int count = 0;
Gate ** sarray = (Gate**) malloc(sizeof (Gate*) * specialGatesAmount);
for (i = 0; i < arraySize; ++i) {
if(GatesArray[i].flags != flagNone){
sarray[count++] = &GatesArray[i];
}
if(count>specialGatesAmount){
printf("Error: specialGatesCollector - count>specialGatesAmount");
return NULL;
}
}
return sarray;
}
__m128i getRandom128(){
__m128i rndVec;
_mm
return rndVec;
}
//unsigned int truthTableConvert()