-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss.c
543 lines (472 loc) · 12.5 KB
/
rss.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <string.h>
#include <stdlib.h>
#include "rss.h"
#include "arith.h"
#include "fips202.h"
unsigned long long factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return num * factorial(num - 1);
}
}
unsigned long long binomialCoefficient(int n, int k)
{
if (k < 0 || k > n)
{
return 0; // Invalid input
}
return factorial(n) / (factorial(k) * factorial(n - k));
}
void generateTuples(int set[], int tuple[], int tuples[][T], int n, int t, int index, int tupleIndex, int *totalTuples)
{
if (tupleIndex == t)
{
for (int i = 0; i < t; i++)
{
tuples[*totalTuples][i] = tuple[i];
}
(*totalTuples)++;
return;
}
for (int i = index; i < n; i++)
{
tuple[tupleIndex] = set[i];
generateTuples(set, tuple, tuples, n, t, i + 1, tupleIndex + 1, totalTuples);
}
}
void printTuples(int tuples[][2], int t, int totalTuples)
{
for (int i = 0; i < totalTuples; i++)
{
printf("(");
for (int j = 0; j < t; j++)
{
printf("%d", tuples[i][j]);
if (j < t - 1)
{
printf(", ");
}
}
printf(")\n");
}
}
void shareDistribution(int shareDistribution[][N_SHARES], int tuples[][T])
{
int not;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
not = 0;
for (int k = 0; k < T; k++)
{
if((i + 1) == tuples[j][k])
{
not = 1;
}
}
if (!not)
{
shareDistribution[i][j] = 1;
}
else
{
shareDistribution[i][j] = 0;
}
}
}
}
void printShareDistribution(int shareDistr[][N_SHARES])
{
for (int i = 0; i < N_SHARES; i++)
{
printf("(");
for (int j = 0; j < N; j++)
{
printf("%d ", shareDistr[j][i]);
}
printf(")\n");
}
}
void shareCounting(int shareCount[][N_SHARES], int shareDistribution[][N_SHARES])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
if (shareDistribution[i][j] == 1)
{
shareCount[j][j] += 1;
for (int k = j + 1; k < N_SHARES; k++)
{
if (shareDistribution[i][k] == 1)
{
shareCount[j][k] += 1;
}
}
}
}
}
for (int i = 1; i < N_SHARES; i++)
{
for (int j = 0; j < i; j++)
{
shareCount[i][j] = shareCount[j][i];
}
}
}
void printShareCounting(int shareCount[][N_SHARES])
{
for (int i = 0; i < N_SHARES; i++)
{
printf("(");
for (int j = 0; j < N_SHARES; j++)
{
printf("%d ", shareCount[i][j]);
}
printf(")\n");
}
}
void shareMapping(int shareMap[][N_SHARES_P_SERVER], int shareDistribution[][N_SHARES])
{
int ind;
for (int i = 0; i < N; i++)
{
ind = 0;
for (int j = 0; j < N_SHARES; j++)
{
if (shareDistribution[i][j] == 1)
{
shareMap[i][ind] = j + 1;
ind++;
}
}
}
}
void printShareMapping(int shareMap[][N_SHARES_P_SERVER])
{
for (int i = 0; i < N; i++)
{
printf("(");
for (int j = 0; j < N_SHARES_P_SERVER; j++)
{
printf("%d ", shareMap[i][j]);
}
printf(")\n");
}
}
void generateFractions(f_elm_t fractionMatrix[][N_SHARES], int shareCount[][N_SHARES])
{
uint64_t one = 1;
f_elm_t tmp_mtrx[N_SHARES][N_SHARES];
for (int i = 0; i < N_SHARES; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
f_from_ui(tmp_mtrx[i][j], shareCount[i][j]);
to_mont(tmp_mtrx[i][j], fractionMatrix[i][j]);
f_inv(fractionMatrix[i][j]);
}
}
}
void secretSharing(f_elm_t shares[], f_elm_t secret, int n_shares)
{
f_elm_t partial_res[n_shares];
for (int i = 0; i < (n_shares - 1); i++)
{
f_rand(shares[i]);
}
f_copy(secret, partial_res[0]);
for (int i = 0; i < (n_shares - 1); i++)
{
f_sub(partial_res[i], shares[i], partial_res[i + 1]);
}
f_copy(partial_res[n_shares - 1], shares[n_shares - 1]);
}
void printSecretSharing(f_elm_t secretShares, int n_shares)
{
for (int i = 0; i < n_shares; i++)
{
printf("Share %02d ; ", i + 1);
print_f_elm_l(secretShares[i]);
printf("\n");
}
printf("\n");
}
void secretOpening(f_elm_t share[], f_elm_t secret, int n_shares)
{
f_elm_t partial_res[n_shares - 1];
f_add(share[0], share[1], partial_res[0]);
for (int i = 2; i < n_shares; i++)
{
f_add(share[i], partial_res[i - 2], partial_res[i - 1]);
}
f_copy(partial_res[n_shares - 2], secret);
}
void secretSharingServers(f_elm_t sharesServer[][N_SHARES_P_SERVER], f_elm_t shares[],
int shareMap[][N_SHARES_P_SERVER])
{
int ind;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N_SHARES_P_SERVER; j++)
{
ind = shareMap[i][j];
f_copy(shares[ind - 1], sharesServer[i][j]);
}
}
}
void printSecretSharingServers(f_elm_t sharesServer[][N_SHARES_P_SERVER])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N_SHARES_P_SERVER; j++)
{
print_f_elm_l(sharesServer[i][j]);
}
printf("\n");
}
}
void keySharingServersN(f_elm_t sharesServer[][N_BITS][N_SHARES_P_SERVER], f_elm_t keys[],
int shareMap[][N_SHARES_P_SERVER])
{
int ind;
f_elm_t shares[N_SHARES];
for (int r = 0; r < N_BITS; r++)
{
secretSharing(shares, keys[r], N_SHARES);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N_SHARES_P_SERVER; j++)
{
ind = shareMap[i][j];
f_copy(shares[ind - 1], sharesServer[i][r][j]);
}
}
}
}
void shareMultMaskN(f_elm_t multMaskServ[][N_BITS][N_SHARES_P_SERVER * N_SHARES_P_SERVER],
int shareDistr[][N_SHARES])
{
int servInd[N];
uint64_t zero_ui = 0;
f_elm_t zero;
f_from_ui(zero, zero_ui);
f_elm_t multMask[N_SHARES * N_SHARES];
for (int r = 0; r < N_BITS; r++)
{
memset(servInd, 0, sizeof(servInd));
secretSharing(multMask, zero, N_SHARES * N_SHARES);
for (int i = 0; i < N_SHARES; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
for (int k = 0; k < N; k++)
{
if ((shareDistr[k][i] == 1) && (shareDistr[k][j] == 1))
{
f_copy(multMask[i * N_SHARES + j], multMaskServ[k][r][servInd[k]]);
servInd[k] += 1;
}
}
}
}
}
}
void shareElmMaskN(f_elm_t elmMaskServ[][N_BITS][N_SHARES_P_SERVER * N_SHARES_P_SERVER],
int shareDistr[][N_SHARES], int shareCount[][N_SHARES])
{
int servInd[N];
uint64_t zero_ui = 0;
f_elm_t zero;
f_from_ui(zero, zero_ui);
int n_row_shares;
int indRowShare;
for (int r = 0; r < N_BITS; r++)
{
memset(servInd, 0, sizeof(servInd));
for (int i = 0; i < N_SHARES; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
n_row_shares = shareCount[i][j];
f_elm_t rowShares[n_row_shares];
secretSharing(rowShares, zero, n_row_shares);
indRowShare = 0;
for (int k = 0; k < N; k++)
{
if ((shareDistr[k][i] == 1) && (shareDistr[k][j] == 1))
{
f_copy(rowShares[indRowShare], elmMaskServ[k][r][servInd[k]]);
servInd[k] += 1;
indRowShare += 1;
}
}
}
}
}
}
void aggregateResN(f_elm_t resServ[][N_BITS][N_SHARES_P_SERVER * N_SHARES_P_SERVER],
f_elm_t expAggrRes[][N_SHARES * N_SHARES], int shareDistr[][N_SHARES])
{
int servInd[N];
memset(servInd, 0, sizeof(servInd));
uint64_t zero_ui = 0;
f_elm_t zero;
f_from_ui(zero, zero_ui);
f_elm_t sum;
for (int r = 0; r < N_BITS; r++)
{
for (int i = 0; i < (N_SHARES * N_SHARES); i++)
{
f_copy(zero, expAggrRes[r][i]);
}
}
for (int i = 0; i < N_SHARES; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
for (int k = 0; k < N; k++)
{
if ((shareDistr[k][i] == 1) && (shareDistr[k][j] == 1))
{
for (int r = 0; r < N_BITS; r++)
{
f_add(resServ[k][r][servInd[k]], expAggrRes[r][i * N_SHARES + j], sum);
f_copy(sum, expAggrRes[r][i * N_SHARES + j]);
}
servInd[k] += 1;
}
}
}
}
}
void aggregateVectorN(f_elm_t expAggrRes[][N_SHARES * N_SHARES], f_elm_t res[])
{
f_elm_t sum_1, sum_2;
uint64_t zero_ui = 0;
f_elm_t zero;
f_from_ui(zero, zero_ui);
for (int r = 0; r < N_BITS; r++)
{
f_copy(zero, sum_1);
for (int i = 0; i < (N_SHARES * N_SHARES); i++)
{
f_add(expAggrRes[r][i], sum_1, sum_2);
f_copy(sum_2, sum_1);
}
f_copy(sum_2, res[r]);
}
}
void aggregateRowsN(f_elm_t resSharesServ[][N_BITS], f_elm_t res[])
{
f_elm_t sum_1, sum_2;
uint64_t zero_ui = 0;
f_elm_t zero;
f_from_ui(zero, zero_ui);
for (int r = 0; r < N_BITS; r++)
{
f_copy(zero, sum_1);
for (int i = 0; i < N; i++)
{
f_add(resSharesServ[i][r], sum_1, sum_2);
f_copy(sum_2, sum_1);
}
f_copy(sum_2, res[r]);
}
}
void mergeHashesN(f_elm_t hashes[][N_BITS][N_SHARES_P_SERVER * N_SHARES_P_SERVER],
f_elm_t mergedHashes[][N_SHARES * N_SHARES], int shareDistr[][N_SHARES])
{
int servInd[N];
memset(servInd, 0, sizeof(servInd));
for (int i = 0; i < N_SHARES; i++)
{
for (int j = 0; j < N_SHARES; j++)
{
for (int k = 0; k < N; k++)
{
if ((shareDistr[k][i] == 1) && (shareDistr[k][j] == 1))
{
for (int r = 0; r < N_BITS; r++)
{
f_copy(hashes[k][r][servInd[k]], mergedHashes[r][i * N_SHARES + j]);
}
servInd[k] += 1;
}
}
}
}
}
void compareHashesN(f_elm_t expAggrRes[][N_SHARES * N_SHARES], f_elm_t mergedHashes[][N_SHARES * N_SHARES],
int checkHash[][N_SHARES * N_SHARES])
{
f_elm_t curr_hash;
char *ptr1, *ptr2;
int equal;
for (int r = 0; r < N_BITS; r++)
{
for (int i = 0; i < (N_SHARES * N_SHARES); i++)
{
shake128((unsigned char *)curr_hash, WORDS_FIELD * 8,
(unsigned char *)expAggrRes[r][i], WORDS_FIELD * 8);
equal = 1;
ptr1 = (unsigned char *)curr_hash;
ptr2 = (unsigned char *)mergedHashes[r][i];
for (int j = 0; j < 8; ++j)
{
if (ptr1[j] != ptr2[j])
{
equal = 0; // Set to 0 if elements are not equal
break; // No need to continue checking
}
}
checkHash[r][i] = equal;
}
}
}
void checkHashesN(int checkHash[][N_SHARES * N_SHARES])
{
int hashesPass = 1;
for (int r = 0; r < N_BITS; r++)
{
for (int i = 0; i < (N_SHARES * N_SHARES); i++)
{
if (checkHash[r][i] == 0)
{
hashesPass = 0;
}
}
}
if (hashesPass == 1)
{
printf("All hashes pass!\n");
}
else
{
printf("Error in hashes\n");
}
}
void printResN(f_elm_t res[])
{
for (int i = 0; i < N_BITS; i++)
{
printf("%03d: ", i + 1);
print_f_elm_l(res[i]);
printf("\n");
}
}
void calculateOPRF(f_elm_t resElm[], unsigned char *resOPRF)
{
f_elm_t resElm_mont;
for (int i = 0; i < N_BITS; i++)
{
to_mont(resElm[i], resElm_mont);
f_leg(resElm_mont, resOPRF + i);
}
}