-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProtocol.cpp
1276 lines (1091 loc) · 35.7 KB
/
Protocol.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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Protocol.h"
Protocol::Protocol(int n, int id, string inputsFile, string outputFile, string circuitFile, string address)
{
GF2X irreduciblePolynomial = BuildSparseIrred_GF2X(8);
GF2E::init(irreduciblePolynomial);
ADDRESS = address;
comm = Communication::getInstance(n, id, address);
N = n;
T = n/3 - 1;
this->inputsFile = inputsFile;
this->outputFile = outputFile;
if(n%3 > 0)
{
T++;
}
m_partyId = id;
s = to_string(m_partyId);
circuit.readCircuit(circuitFile.c_str());
M = circuit.getNrOfGates();
myInputs.resize(M);
}
void Protocol::split(const string &s, char delim, vector<string> &elems) {
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
}
vector<string> Protocol::split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
/**
* Protocol Broadcast:
* 0. Ps holds input vector x = (X1,...,Xn−t).
* 1. ∀j: Ps sends x to Pj . Denote the received vector as x (j) (P j -th view on the sent vector).
* 2. ∀j: Pj applies hyper-invertible matrix M and calculate (y1,...,yn) = M(X1,...,Xn−t,0,...,0), padding t zeroes.
* 3. ∀j,k: Pj sends yk to Pk .
* 4. ∀k: Pk checks whether all received values are equal. If so, be happy, otherwise cry.
*
* This protocol uses when Ps wants to broadcast exactly n−t values.
* if Ps wants more than n-t values we divide the values to buckes.
* Every bucket contains n-t values.
*
* @param myMessage = vector of all the values which Ps wants to broadcast.
* @param recBufsdiff = the values which received from the protocol.
*/
bool Protocol::broadcast(int party_id, string myMessage, vector<string> &recBufsdiff, HIM &mat)
{
int no_buckets;
vector<string> buffers(N); // N parties = N buffers
vector<string> recBufs2(N);
// Ps sends his values to all parties and received there values.
comm->roundfunction2(myMessage, recBufsdiff); // Values are in recBufsdiff
if(flag_print) {
cout << "recBufsdiff" << endl;
for (int i = 0; i < N; i++) {
cout << i << " " << recBufsdiff[i] << endl;
}
}
vector<TFieldElement> X1(N);
vector<TFieldElement> Y1(N);
// calculate total number of values which received
int count = 0;
for(int i=0; i< N; i++)
{
vector<string> arr = {};
arr = split(recBufsdiff[i], '*');
count += arr.size();
}
vector<string> valBufs(count);
int index = 0;
// concatenate everything
for(int l=0; l< N; l++)
{
if(flag_print) {
cout << "recBufsdiff[l] " <<recBufsdiff[l]<< endl; }
vector<string> arr = {};
arr = split(recBufsdiff[l], '*');
for (int i = 0; i < arr.size() ; i++) {
valBufs[index] = arr[i];
index++;
}
}
index = 0;
if(flag_print) {
cout << "valBufs " <<endl;
for(int k = 0; k < count; k++)
{
cout << "valBufs " << k << " " << valBufs[k] << endl;
} }
// nr of buckets
no_buckets = count / (N - T) + 1; // nr of buckets
if(flag_print) {
cout << " before the for " << '\n';}
for(int k = 0; k < no_buckets; k++)
{
for(int i = 0; i < N; i++)
{
if((i < N-T) && (k*(N-T)+i < count))
{
X1[i]= TFieldElement(valBufs[index]);
index++;
}
else
{
// padding zero
X1[i] = TFieldElement(GF2X::zero());
}
}
if(flag_print) {
for(int i = 0; i < N; i++)
{
cout << "X1[i]" << i << " " << X1[i].getElement() << endl;
} }
// x1 contains (up to) N-T values from ValBuf
mat.MatrixMult(X1, Y1); // no cheating: all parties have same y1
if(flag_print) {
cout << "X1[i] after mult" << endl;}
// ‘‘Reconstruct’’ values towards some party (‘‘reconstruct’’ with degree 0)
if(flag_print) {
for(int i = 0; i < N; i++)
{
cout << "X1[i]" << i << " " << X1[i].getElement() << endl;
} }
for(int i = 0; i < N; i++) {
buffers[i] += Y1[i].toString() + "*";
}
for(int i = 0; i < N; i++)
{
X1[i].setPoly(GF2X::zero());
Y1[i].setPoly(GF2X::zero());
}
}
if(flag_print) {
cout << "index 2 time :" << index << '\n';
cout << "before roundfunction3 " << endl;
for(int k=0; k< N; k++) {
cout << k << " " << buffers[k] << endl;
}}
comm->roundfunction3(buffers, recBufs2);
if(flag_print) {
cout << "after roundfunction3 " << endl;
for(int k=0; k< N; k++) {
cout << k << " " << recBufs2[k] << endl;
}
cout << "no_buckets " << no_buckets << endl;}
vector<string> arr = {};
vector<string> arr1 = {};
string temp1;
// no cheating: all parties have same y1
// ‘‘Reconstruct’’ values towards some party (‘‘reconstruct’’ with degree 0)
for(int k=0; k < no_buckets; k++) {
if(flag_print) {
cout << "fff " << k<< endl;}
arr = split(recBufs2[0], '*');
if(arr.size() > 0) {
temp1 = arr[k];
// arr.size()-1
for (int i = 1; i < N; i++) {
arr1 = split(recBufs2[i], '*');
if(temp1 != arr1[k])
{
// cheating detected!!!
if(flag_print) {
cout << " cheating" << endl;}
return false;
}
}
}
}
return true;
}
void Protocol::readMyInputs()
{
ifstream myfile;
int input;
int i =0;
myfile.open(inputsFile);
do {
myfile >> input;
myInputs[i] = input;
i++;
} while(!(myfile.eof()));
myfile.close();
}
void Protocol::run() {
HIM matrix_him(N,N);
VDM matrix_vand(N,N);
HIM m(T, N-T);
matrix_vand.InitVDM();
matrix_him.InitHIM();
comm->ConnectionToServer(s);
initializationPhase(matrix_him, matrix_vand, m);
if(preparationPhase(matrix_vand, matrix_him) == false) {
if(flag_print) {
cout << "cheating!!!" << '\n';}
return;
}
else {
if(flag_print) {
cout << "no cheating!!!" << '\n' << "finish Preparation Phase" << '\n';}
}
if(inputPreparation() == false) {
if(flag_print) {
cout << "cheating!!!" << '\n';}
return;
}
else {
if(flag_print) {
cout << "no cheating!!!" << '\n' << "finish Input Preparation" << '\n';}
}
string sss = "";
auto t1 = high_resolution_clock::now();
auto t1start = high_resolution_clock::now();
inputAdjustment(sss, matrix_him);
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
cout << "time in milliseconds inputAdjustment: " << duration << endl;
if(flag_print) {
cout << "after Input Adjustment " << '\n'; }
t1 = high_resolution_clock::now();
computationPhase(m);
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
cout << "time in milliseconds computationPhase: " << duration << endl;
t1 = high_resolution_clock::now();
outputPhase();
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
cout << "time in milliseconds outputPhase: " << duration << endl;
if(flag_print) {
cout << "after output Phase " << '\n'; }
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
cout << "time in milliseconds : " << duration << endl;
auto t2end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2end-t1start).count();
cout << "time in milliseconds : " << duration << endl;
}
void Protocol::computationPhase(HIM &m) {
int count = 0;
processRandoms();
do {
count = processSmul();
count += processAdditions();
count += processMultiplications(m);
} while(count!=0);
}
/**
* the function implements the second step of Input Phase:
* the party broadcasts for each input gate the difference between
* the random secret and the actual input value.
* @param diff
* @param gateValueArr
* @param circuit
* @param gateShareArr
* @param gateDoneArr
*/
void Protocol::inputAdjustment(string &diff, HIM &mat)
{
int input;
int index = 0;
// read the inputs of the party
for (int k = 0; k < M; k++)
{
if(circuit.getGates()[k].gateType == INPUT && circuit.getGates()[k].party == m_partyId)
{
input = myInputs[index];
index++;
if(flag_print) {
cout << "input " << input << endl;}
// the value is gateValue[k], but should be input.
TFieldElement myinput = TField::getInstance()->GetElementByValue(input);
if(flag_print) {
cout << "gateValueArr "<<k<< " " << gateValueArr[k].toString() << endl;}
TFieldElement different = myinput - gateValueArr[k];
string str = different.toString();
diff += str + "*";
}
}
if(flag_print) {
cout << "try to print diff" << '\n';
cout << diff << '\n';}
vector<string> recBufsdiff(N);
// Broadcast the difference between GateValue[k] to x.
if(broadcast(m_partyId, diff, recBufsdiff, mat) == false) {
if(flag_print) {
cout << "cheating!!!" << '\n';}
return;
}
else {
if(flag_print) {
cout << "no cheating!!!" << '\n' << "finish Broadcast" << '\n';}
}
if(flag_print) {
cout << "recBufsdiff" << endl;
for (int k = 0; k < N; k++) {
cout << "recBufsdiff" << k << " " << recBufsdiff[k] << endl;
}
}
// handle after broadcast
string str;
TFieldElement db;
vector<string> arr = {};
for (int k = 0; k < M; k++)
{
if(circuit.getGates()[k].gateType == INPUT)
{
str = recBufsdiff[circuit.getGates()[k].party - 1];
arr = split(str, '*');
db = TFieldElement(arr[0]);
gateShareArr[k] = gateShareArr[k] + db; // adjustment
recBufsdiff[circuit.getGates()[k].party - 1] = "";
for(int i=1; i<arr.size(); i++) {
recBufsdiff[circuit.getGates()[k].party - 1] += arr[i] + "*";
}
gateDoneArr[k] = true; // gate is processed
}
}
}
/**
* some global variables are initialized
* @param GateValueArr
* @param GateShareArr
* @param GateDoneArr
* @param matrix_him
* @param matrix_vand
* @param alpha
*/
void Protocol::initializationPhase(HIM &matrix_him, VDM &matrix_vand, HIM &m)
{
beta.resize(1);
gateValueArr.resize(M); // the value of the gate (for my input and output gates)
gateShareArr.resize(M); // my share of the gate (for all gates)
alpha.resize(N); // N distinct non-zero field elements
gateDoneArr.resize(M); // true is the gate is processed
vector<TFieldElement> alpha1(N-T);
vector<TFieldElement> alpha2(T);
beta[0] = TField::getInstance()->GetElement(0); // zero of the field
matrix_for_interpolate.allocate(1,N);
// Compute Vandermonde matrix VDM[i,k] = alpha[i]^k
matrix_vand.InitVDM();
// Prepare an N-by-N hyper-invertible matrix
matrix_him.InitHIM();
// N distinct non-zero field elements
for(int i=0; i<N; i++)
{
alpha[i]=(TField::getInstance()->GetElementByValue(i+1));
}
for(int i = 0; i < N-T; i++)
{
alpha1[i] = alpha[i];
}
for(int i = N-T; i < N; i++)
{
alpha2[i - (N-T)] = alpha[i];
}
m.InitHIMByVectors(alpha1, alpha2);
for(int i=0; i<gateDoneArr.size(); i++)
{
gateDoneArr[i] = false;
}
readMyInputs();
matrix_for_interpolate.InitHIMByVectors(alpha, beta);
vector<TFieldElement> alpha_until_t(T + 1);
vector<TFieldElement> alpha_from_t(N - 1 - T);
for(int i=0; i<T+1; i++)
{
alpha_until_t[i]= alpha[i];
}
for (int i = T + 1; i < N; i++) {
alpha_from_t[i - (T + 1)] = alpha[i];
}
// Interpolate first d+1 positions of (alpha,x)
matrix_for_t.allocate(N - 1 - T, T + 1); // slices, only positions from 0..d
matrix_for_t.InitHIMByVectors(alpha_until_t, alpha_from_t);
vector<TFieldElement> alpha_until_2t(2*T + 1);
vector<TFieldElement> alpha_from_2t(N - 1 - 2*T);
for(int i=0; i<2*T+1; i++)
{
alpha_until_2t[i]= alpha[i];
}
for (int i = 2*T + 1; i < N; i++) {
alpha_from_2t[i - (2*T + 1)] = alpha[i];
}
// Interpolate first d+1 positions of (alpha,x)
matrix_for_2t.allocate(N - 1 - 2*T, 2*T + 1); // slices, only positions from 0..d
matrix_for_2t.InitHIMByVectors(alpha_until_2t, alpha_from_2t);
}
/**
* The function compute t shared authentication checks and to reconstruct the n sharings,
* one towards each party, who then computes the secret and sends it to everybody.
* Each party receives n − t secrets and t authentication checks.
* Reconstruct a bunch of degree-d sharings to all parties (into ValBuf)
* @param myShares
* @param alpha
* @param valBuf
*/
void Protocol::publicReconstruction(vector<string> &myShares, int &count, int d, vector<TFieldElement> &valBuf, HIM &m)
{
int no_buckets = count / (N-T) + 1;
if(flag_print) {
cout << "public reconstruction" << endl;
cout << "no buckets" << no_buckets << endl; }
TFieldElement x;
vector<TFieldElement> x1(N);
vector<TFieldElement> y1(N);
vector<TFieldElement> y2(N);
vector<string> sendBufs(N);
vector<string> sendBufs2(N);
vector<string> recBufs(N);
vector<string> recBufs2(N);
for(int i = 0; i < N; i++)
{
recBufs[i] = "";
recBufs2[i] = "";
sendBufs[i] = "";
sendBufs2[i] = "";
}
if(flag_print) {
for (int i = 0; i < myShares.size(); i++) {
cout << "myShares " << i << " " << myShares[i] << endl;
}
}
// init x to be vector of degree-d (d=2*t) shares of n−t secret
for(int k=0; k < no_buckets; k++)
{
for(int i = 0; i < N-T; i++)
{
if( k*(N-T)+i < count)
{
// k*(N-T)+i
x1[i] = TFieldElement(myShares[k*(N-T)+i]);
}
else
{
x1[i] = *TField::getInstance()->GetZero();
}
}
// compute y = M*x and append it to x
m.MatrixMult(x1, y1);
for(int i = 0; i < T; i++)
{
x1[N-T+i] = y1[i];
}
// ∀i, j: Pi sends xj to Pj
for(int i = 0; i < N; i++)
{
sendBufs[i] += x1[i].toString() + "*";
}
}
if(flag_print) {
cout << "sendBufs[i]" << endl;
for (int i = 0; i < N; i++) {
cout << sendBufs[i] << endl;
}
}
// cout << "before roundfunction1" << '\n';
comm->roundfunction1(sendBufs, recBufs);
if(flag_print) {
cout << "recBufs[i]" << endl;
for(int i = 0; i < N; i++)
{
cout << recBufs[i] << endl;
}}
// cout << "after roundfunction1" << '\n';
vector<string> arr = {};
for(int k=0; k < no_buckets; k++) {
for (int i = 0; i < N; i++) {
arr = split(recBufs[i], '*');
x1[i] = TFieldElement(arr[k]);
}
if(flag_print) {
cout << "x1[i]" << endl;
for(int i = 0; i < N; i++)
{
cout << x1[i].toString() << endl;
} }
// checking that {xj}i are d-consistent and interpolate them to x j .
if (!checkConsistency(x1, d)) {
// halt
// cheating detected
if(flag_print) {
cout << "cheating" << '\n';}
}
// interpolate {xj}i to x
x = interpolate(x1);
// send x to all parties
for (int i = 0; i < N; i++) {
sendBufs2[i] += x.toString() + "*";
}
}
if(flag_print) {
cout << "sendBufs2[i]" << endl;
for(int i = 0; i < N; i++)
{
cout << sendBufs2[i] << endl;
} }
comm->roundfunction8(sendBufs2, recBufs2);
if(flag_print) {
cout << "recBufs2[i]" << endl;
for(int i = 0; i < N; i++)
{
cout << recBufs2[i] << endl;
} }
// vector<string> arr = {};
int index = 0;
for(int k=0; k < no_buckets; k++) {
for (int i = 0; i < N; i++) {
arr = split(recBufs2[i], '*');
x1[i] = TFieldElement(arr[k]);
}
// checking that (Xn−t,...,Xn) = M*(X1,...,Xn−t)
m.MatrixMult(x1, y1);
for (int i = 0; i < T; i++) {
if(x1[N-T+i].toString() != y1[i].toString())
{
if(flag_print) {
// halt !
cout << " cheating" << '\n'; }
}
}
for (int i = 0; i < N-T; i++) {
if(k*(N-T)+i < count)
{
valBuf[index] = x1[i];
index++;
}
}
}
}
bool Protocol::preparationPhase(VDM &matrix_vand, HIM &matrix_him)
{
vector<string> recBufs(N);
int robin = 0;
string strX1, strX2, str;
// the number of random double sharings we need altogether
int no_random = circuit.getNrOfMultiplicationGates() + circuit.getNrOfInputGates();
vector<TFieldElement> x1(N),x2(N),y1(N),y2(N);
vector<string> sendBufs(N);
// the number of buckets (each bucket requires one double-sharing
// from each party and gives N-2T random double-sharings)
int no_buckets = (no_random / (N-2*T))+1;
sharingBuf.resize(no_buckets*(N-2*T)); // my shares of the double-sharings
for(int i=0; i < N; i++)
{
sendBufs[i] = "";
}
/**
* generate double sharings.
* first degree t.
* subsequent: degree 2t with same secret.
*/
for(int k=0; k < no_buckets; k++)
{
// generate random degree-T polynomial
for(int i = 0; i < T+1; i++)
{
// A random field element, uniform distribution
x1[i] = TField::getInstance()->Random();
}
// same secret
TFieldElement secret;
secret.setPoly(x1[0].getElement());
//x2[0] = x1[0];
x2[0] = secret;
if(flag_print) {
cout << "k " << k << "s= " << secret.toString() << endl;}
for(int i = 1; i < 2*T+1; i++)
{
// otherwise random
x2[i] = TField::getInstance()->Random();
}
matrix_vand.MatrixMult(x1, y1); // eval poly at alpha-positions
matrix_vand.MatrixMult(x2, y2); // eval poly at alpha-positions
// prepare shares to be sent
for(int i=0; i < N; i++)
{
sendBufs[i] += y1[i].toString() + "*"; // the degree-t shares of my poly
sendBufs[i] += y2[i].toString() + "$"; // the degree 2t shares of my poly
}
}
if(flag_print) {
cout << "sendBufs" << endl;
cout << "N" << N << endl;
cout << "T" << T << endl;
for (int i = 0; i < sendBufs.size(); i++) {
cout << "sendBufs" << i << " :" << sendBufs[i] << endl;
}
}
for(int i=0; i < recBufs.size(); i++)
{
recBufs[i] = "";
}
comm->roundfunction4(sendBufs, recBufs);
if(flag_print) {
for (int i=0; i < recBufs.size();i++)
{
cout << "recBufs" << i << " :" << recBufs[i] << endl;
} }
/**
* Apply hyper-invertible matrix on each bucket.
* From the resulting sharings, 2T are being reconstructed towards some party,
* the remaining N-2T are kept as prepared sharings.
* For balancing, we do round-robin the party how shall reconstruct and check!
*/
vector<string> sendBufs1(N);
for(int i=0; i < N; i++)
{
sendBufs1[i] = "";
}
vector<string> arr1 = {};
vector<string> arr2 = {};
vector<string> arr = {};
// x1 : used for the N degree-t sharings
// x2 : used for the N degree-2t sharings
for(int k=0; k < no_buckets; k++) {
// generate random degree-T polynomial
for (int i = 0; i < N; i++) {
str = recBufs[i];
arr1 = split(str, '$');
arr = split(arr1[k], '*');
if(arr.size() >1 ) {
strX1 = arr[0];
strX2 = arr[1];
x1[i] = TFieldElement(strX1); // my share of the degree-t sharings
x2[i] = TFieldElement(strX2); // my share of the degree-2t sharings
}
else{
if(flag_print) { cout << " problemmmmm" << endl;}
}
}
matrix_him.MatrixMult(x1, y1);
matrix_him.MatrixMult(x2, y2);
// these shall be checked
for (int i = 0; i < 2 * T; i++) {
sendBufs1[robin] += y1[i].toString() + "*";
sendBufs1[robin] += y2[i].toString() + "$";
robin = (robin+1) % N; // next robin
}
// Y1 : the degree-t shares of my poly
// Y2 : the degree 2t shares of my poly
for (int i = 2 * T; i < N; i++) {
sharingBuf[k*(N-2*T) + i - 2*T] = y1[i].toString() + "*" + y2[i].toString();
}
for (int i=0; i < sendBufs.size();i++)
{
x2[0] = *TField::getInstance()->GetZero();
x1[0] = *TField::getInstance()->GetZero();
}
}
if(flag_print) {
cout << "before round" << endl;}
comm->roundfunction5(sendBufs1, recBufs);
if(flag_print) {
cout << "after round" << endl;}
int count = no_buckets * (2*T) / N; // nr of sharings *I* have to check
// got one in the last round
if(no_buckets * (2*T)%N > m_partyId) { // maybe -1
count++;
}
// count--;
for(int k=0; k < count; k++) {
for (int i = 0; i < N; i++) {
str = recBufs[i];
arr1 = split(str, '$');
arr = split(arr1[k], '*');
if (arr.size() == 2) {
strX1 = arr[0];
strX2 = arr[1];
} else {
// change !!!!
strX1 = "";
strX2 = "";
}
x1[i] = TFieldElement(strX1);
x2[i] = TFieldElement(strX2);
}
vector<TFieldElement> x_until_d(N);
for(int i=0; i<T; i++)
{
x_until_d[i] = x1[i];
}
for(int i=T; i<N; i++)
{
x_until_d[i] = *TField::getInstance()->GetZero();
}
if(flag_print) {
// cout <<"k "<<k<< "tinterpolate(x1).toString() " << tinterpolate(x_until_d).toString() << endl;
cout << "k " << k << "interpolate(x1).toString() " << interpolate(x1).toString() << endl;
cout << "k " << k << "interpolate(x2).toString() " << interpolate(x2).toString() << endl;
}
// Check that x1 is t-consistent and x2 is 2t-consistent and secret is the same
if(!checkConsistency(x1,T) || !checkConsistency(x2,2*T) || (interpolate(x1).toString() != interpolate(x2).toString()) ) {
// cheating detected, abort
if(flag_print) {
cout << "k" << k<< endl;}
return false;
}
}
return true;
}
/**
* the function implements the first step of Input Phase:
* for each input gate, a prepared t-sharings is reconstructed
* towards the party giving input
*/
bool Protocol::inputPreparation()
{
vector<string> sendBufs(N); // upper bound
vector<string> recBufs(N); // dito
vector<TFieldElement> x1(N); // vector for the shares of my inputs
TFieldElement elem;
TFieldElement secret;
int i;
for(int k = 0; k < M; k++)
{
if((circuit.getGates())[k].gateType == INPUT)
{
string str = sharingBuf[k]; // use the t-sharing (correction will come)
// change !!!
if(str!="") {
// waste the 2T-sharing
vector<string> arr = {};
arr = split(str, '*');
elem = TFieldElement(arr[0]);
} else {
elem = TFieldElement("[]");
}
gateShareArr[k] = elem;
i = (circuit.getGates())[k].party; // the number of party which has the input
// reconstruct sharing towards input party
sendBufs[i-1] += gateShareArr[k].toString() + "*";
}
}
if(flag_print) {
cout << "sendBufs[i] in input preperation" << endl;
for (int i = 0; i < N; i++) {
cout << sendBufs[i] << endl;
}
}
comm->roundfunction6(sendBufs, recBufs);
if(flag_print) {
for(int k = 0; k < N; k++) {
cout << "roundfunction6 recBufs" << k << " " << recBufs[k] << endl;
} }
// reconstruct my random input values
for(int k = 0; k < M; k++) {
if (circuit.getGates()[k].gateType == INPUT && circuit.getGates()[k].party == m_partyId) {
// my input: reconstruct received shares
for (int i = 0; i < N; i++) {
vector<string> arr = {};
arr = split(recBufs[i], '*');
elem = TFieldElement(arr[0]);
x1[i] = elem;
recBufs[i]="";
for(int l=1; l<arr.size(); l++)
{
recBufs[i] += arr[l] + "*";
}
}
if(!checkConsistency(x1, T))
{
// someone cheated!
return false;
}
// the (random) secret
secret = TFieldElement();
secret.setPoly(interpolate(x1).getElement());
gateValueArr[k] = secret;
if(flag_print) {
cout << " the secret is " << secret.toString() << endl;}
}
}
return true;
}
///**
// * Check whether given points lie on polynomial of degree d. This check is performed by interpolating x on
// * the first d + 1 positions of α and check the remaining positions.
// */
//bool Protocol::checkConsistency(vector<TFieldElement>& x, int d)
//{
// vector<TFieldElement> alpha_until_d(d+1);
// vector<TFieldElement> alpha_from_d(N-1-d);
// vector<TFieldElement> x_until_d(d+1);
// vector<TFieldElement> y(N-1-d); // the result of multiplication
//
// for(int i=0; i<d+1; i++)
// {
// alpha_until_d[i]= alpha[i];
// x_until_d[i] = x[i];
// }
// for(int i=d+1; i<N; i++)
// {
// alpha_from_d[i-(d+1)]= alpha[i];
// }
// // Interpolate first d+1 positions of (alpha,x)
// HIM matrix(N-1-d,d+1); // slices, only positions from 0..d
// matrix.InitHIMByVectors(alpha_until_d, alpha_from_d);
// matrix.MatrixMult(x_until_d, y);
//
// // compare that the result is equal to the according positions in x
// for(int i = 0; i < N-d-1; i++) // n-d-2 or n-d-1 ??
// {
// if(y[i].toString() != x[d+1+i].toString())
// {
// return false;
// }
// }
// return true;
//}
/**
* Check whether given points lie on polynomial of degree d. This check is performed by interpolating x on
* the first d + 1 positions of α and check the remaining positions.
*/
bool Protocol::checkConsistency(vector<TFieldElement>& x, int d)
{
if(d == T)
{
vector<TFieldElement> y(N - 1 - d); // the result of multiplication
vector<TFieldElement> x_until_t(T + 1);
for (int i = 0; i < T + 1; i++) {
x_until_t[i] = x[i];
}
matrix_for_t.MatrixMult(x_until_t, y);
// compare that the result is equal to the according positions in x
for (int i = 0; i < N - d - 1; i++) // n-d-2 or n-d-1 ??
{
if (y[i].toString() != x[d + 1 + i].toString()) {
return false;
}
}
return true;
} else if (d == 2*T)
{
vector<TFieldElement> y(N - 1 - d); // the result of multiplication
vector<TFieldElement> x_until_2t(2*T + 1);
for (int i = 0; i < 2*T + 1; i++) {
x_until_2t[i] = x[i];
}
matrix_for_2t.MatrixMult(x_until_2t, y);
// compare that the result is equal to the according positions in x
for (int i = 0; i < N - d - 1; i++) // n-d-2 or n-d-1 ??