-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProtocolParty.h
More file actions
4570 lines (3687 loc) · 174 KB
/
ProtocolParty.h
File metadata and controls
4570 lines (3687 loc) · 174 KB
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
#ifndef PROTOCOLPARTY_H_
#define PROTOCOLPARTY_H_
#include <stdlib.h>
#include <libscapi/include/primitives/Matrix.hpp>
#include <libscapi/include/cryptoInfra/Protocol.hpp>
#include <libscapi/include/circuits/ArithmeticCircuit.hpp>
#include <libscapi/include/infra/Measurement.hpp>
#include <vector>
#include <bitset>
#include <iostream>
#include <fstream>
#include <chrono>
#include <libscapi/include/primitives/Mersenne.hpp>
#include <libscapi/include/comm/MPCCommunication.hpp>
#include <libscapi/include/infra/Common.hpp>
#include <libscapi/include/primitives/Prg.hpp>
#include <emmintrin.h>
#include <thread>
#include <libscapi/include/primitives/HashOpenSSL.hpp>
#include <omp.h>
//#ifdef __NVCC__
#include "cudaGemm.h"
#include "utils.h"
#include <cuda_runtime.h>
//#endif
#include <algorithm>
#define flag_print false
#define flag_print_timings true
#define flag_print_output true
using namespace std;
using namespace std::chrono;
template <class FieldType>
class ProtocolParty : public MPCProtocol, public HonestMajority{
private:
/**
* N - number of parties
* M - number of gates
* T - number of malicious
*/
int N, M, T, m_partyId;//number of servers
int times; //number of times to run the run function
int iteration; //number of the current iteration
vector<long> shiftbyOne;
//
long l;
long numClients;
int numServers;
int securityParamter = 40;
long sqrtR;
long sqrtU;
int numThreads;
vector<PrgFromOpenSSLAES> prgs;
long batchSize;
vector<FieldType> msgsVectorsFlat;
vector<FieldType> squaresVectorsFlat;
vector<FieldType> countersVectorsFlat;
vector<FieldType> unitVectorsFlat;
// vector<FieldType> msgsVectorsShiftedFlat;
// vector<FieldType> squaresVectorsShiftedFlat;
// vector<FieldType> countersVectorsShiftedFlat;
// vector<FieldType> unitVectorsShiftedFlat;
// vector<FieldType> sum1;
// vector<FieldType> sum0;
// vector<long> sum01;
vector<FieldType> sumOfElementsVecs;
vector<FieldType> openedSumOfElementsVecs;
// vector<FieldType> sumsForConsistensyTestOpened;
vector<FieldType> bigRVec;
Measurement* timer;
VDM<FieldType> matrix_vand;
TemplateField<FieldType> *field;
vector<shared_ptr<ProtocolPartyData>> parties;
vector<FieldType> randomTAnd2TShares;
vector<FieldType> randomSharesArray;
vector<FieldType> bigR;
vector<byte> h;//a string accumulated that should be hashed in the comparing views function.
int offset = 0;
int randomSharesOffset = 0;
string s;
vector<FieldType> beta;
HIM<FieldType> matrix_for_interpolate;
HIM<FieldType> matrix_for_t;
HIM<FieldType> matrix_for_2t;
vector<FieldType> y_for_interpolate;
HIM<FieldType> matrix_him;
VDMTranspose<FieldType> matrix_vand_transpose;
HIM<FieldType> m;
boost::asio::io_service io_service;
vector<FieldType> alpha; // N distinct non-zero field elements
thread t;
bool toUmount;
bool toSimulate;
public:
ProtocolParty(int argc, char* argv[]);
void roundFunctionSync(vector<vector<byte>> &sendBufs, vector<vector<byte>> &recBufs, int round);
void exchangeData(vector<vector<byte>> &sendBufs,vector<vector<byte>> &recBufs, int first, int last);
void roundFunctionSyncElements(vector<vector<FieldType>> &sendBufs, vector<vector<FieldType>> &recBufs, int round);
void exchangeDataElements(vector<vector<FieldType>> &sendBufs,vector<vector<FieldType>> &recBufs, int first, int last);
/**
* This method runs the protocol:
* 1. Preparation Phase
* 2. Input Phase
* 3. Computation Phase
* 4. Verification Phase
* 5. Output Phase
*/
void run() override;
bool hasOffline() {
return true;
}
bool hasOnline() override {
return true;
}
/**
* This method runs the protocol:
* Preparation Phase
*/
void runOffline() override;
/**
* This method runs the protocol:
* Input Phase
* Computation Phase
* Verification Phase
* Output Phase
*/
void runOnline() override;
/**
* We describe the protocol initialization.
* In particular, some global variables are declared and initialized.
*/
void initializationPhase();
/**
* A random double-sharing is a pair of two sharings of the same random value, where the one sharing is
* of degree t, and the other sharing is of degree 2t. Such random double-sharing are of big help in the
* multiplication protocol.
* We use hyper-invertible matrices to generate random double-sharings. The basic idea is as follows:
* Every party generates one random double-sharing. These n double-sharings are processes through a
* hyper-invertible matrix. From the resulting n double-sharings, t are checked to be valid (correct degree,
* same secret), and t are then kept as “good” double-sharings. This is secure due to the diversion property
* of hyper-invertible matrix: We know that n − t of the input double-sharings are good. So, if there are t
* valid output double-sharings, then all double-sharings must be valid. Furthermore, the adversary knows
* his own up to t input double-sharings, and learns t output double sharings. So, n − 2t output double
* sharings are random and unknown to the adversary.
* For the sake of efficiency, we do not publicly reconstruct t of the output double-sharings. Rather, we
* reconstruct 2t output double sharings, each to one dedicated party only. At least t of these parties are
* honest and correctly validate the reconstructed double-sharing.
*
* The goal of this phase is to generate “enough” double-sharings to evaluate the circuit. The double-
* sharings are stored in a buffer SharingBuf , where alternating a degree-t and a degree-2t sharing (of the same secret)
* is stored (more precisely, a share of each such corresponding sharings is stored).
* The creation of double-sharings is:
*
* Protocol Generate-Double-Sharings:
* 1. ∀i: Pi selects random value x-(i) and computes degree-t shares x1-(i) and degree-2t shares x2-(i).
* 2. ∀i,j: Pi sends the shares x1,j and X2,j to party Pj.
* 3. ∀j: Pj applies a hyper-invertible matrix M on the received shares, i.e:
* (y1,j,..., y1,j) = M(x1,j,...,x1,j)
* (y2,j,...,y2,j) = M (x2,j,...,x2,)
* 4. ∀j, ∀k ≤ 2t: Pj sends y1,j and y2,j to Pk.
* 5. ∀k ≤ 2t: Pk checks:
* • that the received shares (y1,1,...,y1,n) are t-consistent,
* • that the received shares (y2,1,...,y2,n) are 2t-consistent, and
* • that both sharings interpolate to the same secret.
*
* We use this algorithm, but extend it to capture an arbitrary number of double-sharings.
* This is, as usual, achieved by processing multiple buckets in parallel.
*/
bool preparationPhase();
void inputPhase();
/**
* This protocol is secure only in the presence of a semi-honest adversary.
*/
void DNHonestMultiplication(FieldType *a, FieldType *b, vector<FieldType> &cToFill, int numOfTrupples);
void readclientsinputs(vector<FieldType> &msgsVectorsFlat, vector<FieldType> &squaresVectorsFlat, vector<FieldType> &countersVectorsFlat, vector<FieldType> &unitVectorsFlat);
void readServerFile(string fileName, FieldType* msg, FieldType* squares, FieldType* counters, FieldType* unitVector, FieldType * e);
// int validMsgsTest(vector<vector<FieldType>> &msgsVectors, vector<vector<FieldType>> &unitVectors);
int validMsgsTestFlat(vector<FieldType> &msgsVectors, vector<FieldType> &msgsVectorsSquares, vector<FieldType> & counters, vector<FieldType> &unitVectors);
// int unitVectorsTestFlat(vector<FieldType> &vecs, int size, FieldType *randomElements, vector<FieldType> &sumsForConsistensyTest, bool toSplit);
int fasterUnitVectorsTestFlat(vector<FieldType> &vecs, int size,FieldType *randomElements);
void prepareVecsForUnitTestThreads(vector<vector<FieldType>> & randomVecs, vector<FieldType> & vecs, vector<FieldType> & messagesShares,
vector<FieldType>& msgByRandomSum, vector<FieldType>& VecByRandomSum, FieldType* randomElements, int size, int start, int end);
void processSums(FieldType* sum, FieldType* constRandomBits, int size, FieldType* vecs, int device);
// void processSumsThreads(vector<FieldType> & sum0, vector<FieldType> & constRandomBits0,
// vector<FieldType> & sum1, vector<FieldType> & constRandomBits1,vector<FieldType> & vecs ,int size,int start, int end, int deviceID);
// int unitVectorsTest(vector<vector<FieldType>> &vecs, FieldType *randomElements,vector<FieldType> &sumsForConsistensyTest);
// int unitWith1VectorsTest(vector<vector<FieldType>> &vecs);
// int generateSharedMatrices(vector<vector<FieldType>> &msgsVectors, vector<vector<FieldType>> &unitVectors,
// vector<FieldType> &accMats,
// vector<FieldType> &accFieldCountersMat);
// int generateSharedMatricesForTesting(vector<vector<FieldType>> &shiftedMsgsVectors,
// vector<vector<FieldType>> &shiftedMsgsVectorsSquares,
// vector<vector<FieldType>> &shiftedMsgsVectorsCounters,
// vector<vector<FieldType>> &shiftedUnitVectors,
// vector<FieldType> &accMsgsMat,
// vector<FieldType> &accMsgsSquareMat,
// vector<FieldType> &accCountersMat);
// int generateSharedMatricesOptimized(vector<vector<FieldType>> &shiftedMsgsVectors,
// vector<vector<FieldType>> &shiftedMsgsVectorsSquares,
// vector<vector<FieldType>> &shiftedMsgsVectorsCounters,
// vector<vector<FieldType>> &shiftedUnitVectors,
// vector<FieldType> &accMsgsMat,
// vector<FieldType> &accMsgsSquareMat,
// vector<FieldType> &accCountersMat);
int generateSharedMatricesOptimizedFlat(vector<FieldType> &shiftedMsgsVectors,
vector<FieldType> &shiftedMsgsVectorsSquares,
vector<FieldType> &shiftedMsgsVectorsCounters,
vector<FieldType> &shiftedUnitVectors,
vector<FieldType> &accMsgsMat,
vector<FieldType> &accMsgsSquareMat,
vector<FieldType> &accCountersMat);
//#ifdef __NVCC__
int generateSharedMatricesForGPU(vector<FieldType> &shiftedMsgsVectors,
vector<FieldType> &shiftedMsgsVectorsSquares,
vector<FieldType> &shiftedMsgsVectorsCounters,
vector<FieldType> &shiftedUnitVectors,
vector<FieldType> &accMsgsMat,
vector<FieldType> &accMsgsSquareMat,
vector<FieldType> &accCountersMat);
//#endif
void matrixMulTN(FieldType *C, int ldc, const FieldType *A, int lda, const FieldType *B, int ldb, int hA, int wA, int wB);
void regMatrixMulTN(FieldType *C, FieldType *A, int rowa, int cola, FieldType *B, int rowb, int colb);
// void multiplyVectors(vector<vector<FieldType>> & input,
// vector<vector<FieldType>> & unitVectors,
// vector<FieldType> & output,
// int numOfRows,
// int numOfCols);
//
// void multMatrices(vector<vector<FieldType>> & input, vector<vector<FieldType>> & unitVectors,
// vector<long> & outputDouble, int newNumRows, int newNumCols, int i, __m256i mask);
void multMatricesFlat(vector<FieldType> & input, int inputSize, vector<FieldType> & unitVectors,
vector<long> & outputDouble, int newNumRows, int newNumCols, int i, __m256i mask);
void reduceMatrix(vector<long> & outputDouble, int newNumRows, int newNumCols, __m256i mask, __m256i p);
// void multiplyVectorsWithThreads(vector<vector<FieldType>> & input, vector<vector<FieldType>> & unitVectors,
// vector<FieldType> & output, int numOfRows, int numOfCols);
//
// void multiplyVectorsPerThread(vector<vector<FieldType>> & input, vector<vector<FieldType>> & unitVectors, vector<long> & outputDouble,
// int newNumRows, int newNumCols, int start, int end);
void multiplyVectorsWithThreadsFlat(vector<FieldType> & input, int inputSize, vector<FieldType> & unitVectors,
vector<FieldType> & output, int numOfRows, int numOfCols);
void multiplyVectorsPerThreadFlat(vector<FieldType> & input, int inputSize, vector<FieldType> & unitVectors, vector<long> & outputDouble,
int newNumRows, int newNumCols, int start, int end);
// void assignSumsPerThread(vector<long> & sum01, vector<vector<FieldType>> & vecs, byte* constRandomBitsPrim,
// vector<vector<FieldType>> & randomVecs, int start, int end);
void multRandomsByThreads(vector<vector<FieldType>> & randomVecs, vector<FieldType> & vecs,
FieldType* randomElements, int size, int start, int end);
// void assignSumsPerThreadFlat(vector<long> & sum01, vector<FieldType> & vecs, int size, byte* constRandomBitsPrim,
// vector<vector<FieldType>> & randomVecs, int start, int end);
int generateClearMatricesForTesting(vector<FieldType> &accMsgsMat,
vector<FieldType> &accMsgsSquareMat,
vector<FieldType> &accCountersMat,
vector<int> &accIntCountersMat);
int generateClearMatrices(vector<FieldType> &accMats, vector<FieldType> &accFieldCountersMat,vector<int> &accIntCountersMat);
void generateRandomShiftingindices(vector<int> &randomShiftingVec);
// void splitShift(vector<vector<FieldType>> &msgsVectors, vector<vector<FieldType>> &unitVectors,
// vector<vector<FieldType>> &msgsVectorsSquare, vector<vector<FieldType>> &msgsVectorsCounter);
void splitShiftFlat(vector<FieldType> &msgsVectors, vector<FieldType> &squaresVectors, vector<FieldType> &countersVectors, vector<FieldType> &unitVectors);
void splitShiftByThreads(vector<int> & randomShiftingIndices, vector<FieldType> & originalArr, long size, long l, long position, long start, long end);
// void copyBackToVectors();
//#ifdef __NVCC__
// void splitShiftForGPU(vector<vector<FieldType>> &msgsVectors, vector<vector<FieldType>> &unitVectors,
// vector<FieldType> &msgsVectorsVec, vector<FieldType> &unitVectorsVec,
// vector<FieldType> &msgsVectorsSquare, vector<FieldType> &msgsVectorsCounter);
//#endif
void commitOnMatrices(vector<FieldType> &accMats, vector<FieldType> &accFieldCountersMat,
vector<vector<byte>> &recBufsBytes);
void extractMessagesForTesting(vector<FieldType> &accMsgsMat,
vector<FieldType> &accMsgsSquareMat,
vector<int> &accIntCountersMat, int numMsgs);
void calcPairMessages(FieldType & a, FieldType & b, int counter);
void printOutputMessages(vector<FieldType> &accMats, vector<int> &accIntCountersMat);
void printOutputMessagesForTesting(vector<FieldType> &accMsgsMat,
vector<FieldType> &accMsgsMat2,
vector<int> &accIntCountersMat, int numMsgs);
void offlineDNForMultiplication(int numOfTriples);
/**
* The input phase proceeds in two steps:
* First, for each input gate, the party owning the input creates shares for that input by choosing a random coefficients for the polynomial
* Then, all the shares are sent to the relevant party
*/
// void prepareForUnitTest(vector<FieldType> &randomElements, vector<vector<FieldType>> &msgsVectors,
// vector<FieldType> &sumXandSqaure, vector<vector<FieldType>> &msgsVectorsForUnitTest, int start, int end);
void prepareForUnitTestFlat(vector<FieldType> &randomElements, vector<FieldType> &msgsVectors, vector<FieldType> &msgsVectorsSquares, vector<FieldType> & counters,
vector<FieldType> &sumXandSqaure, vector<FieldType> &msgsVectorsForUnitTest, int start, int end);
void generateRandomShares(int numOfRandoms, vector<FieldType> &randomElementsToFill);
void getRandomShares(int numOfRandoms, vector<FieldType> &randomElementsToFill);
void generateRandomSharesWithCheck(int numOfRnadoms, vector<FieldType>& randomElementsToFill);
void generateRandom2TAndTShares(int numOfRandomPairs, vector<FieldType>& randomElementsToFill);
void calcSendBufElements(vector<vector<FieldType>> & sendBufsElements, PrgFromOpenSSLAES & prg, int start, int end);
void calcRecBufElements(vector<vector<FieldType>> & recBufsElements, vector<FieldType> & randomElementsToFill, int start, int end);
/**
* 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 checkConsistency(vector<FieldType>& x, int d);
FieldType reconstructShare(vector<FieldType>& x, int d);
void openShare(int numOfRandomShares, vector<FieldType> &Shares, vector<FieldType> &secrets, int d);
void openShareSetRecBuf(int numOfRandomShares, vector<FieldType> &Shares, vector<FieldType> &secrets,
int d, vector<vector<byte>> &recBufsBytes);
/**
* The cheap way: Create a HIM from the αi’s onto ZERO (this is actually a row vector), and multiply
* this HIM with the given x-vector (this is actually a scalar product).
* The first (and only) element of the output vector is the secret.
*/
FieldType interpolate(vector<FieldType>& x);
/**
* Walk through the circuit and verify the multiplication gates.
* We first generate the random elements using a common AES key that was generated by the parties,
* run the relevant verification algorithm and return accept/reject according to the output
* of the verification algorithm.
*/
int verificationPhase();
vector<byte> generateCommonKey();
void generatePseudoRandomElements(vector<byte> & aesKey, vector<FieldType> &randomElementsToFill, int numOfRandomElements);
/**
* Walk through the circuit and reconstruct output gates.
*/
void outputPhase();
~ProtocolParty();
void batchConsistencyCheckOfShares(const vector<FieldType> &inputShares);
};
template <class FieldType>
ProtocolParty<FieldType>::ProtocolParty(int argc, char* argv[]) : MPCProtocol("MPCAnonymuosBlogging", argc, argv,false)
{
l = stoi(this->getParser().getValueByKey(arguments, "l"));
m_partyId = stoi(this->getParser().getValueByKey(arguments, "partyID"));
numServers = stoi(this->getParser().getValueByKey(arguments, "numServers"));
numClients = stoi(this->getParser().getValueByKey(arguments, "numClients"));
numThreads = stoi(this->getParser().getValueByKey(arguments, "numThreads"));
string fieldType = this->getParser().getValueByKey(arguments, "fieldType");
this->times = stoi(this->getParser().getValueByKey(arguments, "internalIterationsNumber"));
toUmount = (this->getParser().getValueByKey(arguments, "toUmount").compare("true") == 0);
toSimulate = (this->getParser().getValueByKey(arguments, "toSimulate").compare("true") == 0);
vector<string> subTaskNames{"Offline", "preparationPhase", "Online", "inputPhase", "ComputePhase",
"VerificationPhase", "outputPhase", "generateSharedMatricesOptimized",
"extractMessagesForTesting"};
timer = new Measurement(*this, subTaskNames);
if(fieldType.compare("ZpMersenne31") == 0) {
field = new TemplateField<FieldType>(2147483647);
} else if(fieldType.compare("ZpMersenne61") == 0) {
field = new TemplateField<FieldType>(0);
}
N = numServers;
T = stoi(this->getParser().getValueByKey(arguments, "T"));
//this->inputsFile = this->getParser().getValueByKey(arguments, "inputFile");
//this->outputFile = this->getParser().getValueByKey(arguments, "outputFile");
sqrtR = (int)((sqrt(l*2.7 * numClients)))/l+1;
sqrtU = (int)(sqrt(l*2.7 * numClients))+1;
batchSize = numClients;
s = to_string(m_partyId);
/*
int threads_per_device = 2;
int num_devices = 1;
cudaSafeCall(cudaGetDeviceCount(&num_devices));
printf("%d devices used\n", num_devices);
std::vector<int> devices(num_devices*threads_per_device);
for (int device = 0; device < num_devices; ++device)
{
for (int i = 0; i < threads_per_device; ++i){
devices[threads_per_device*device +i] = device;
cout<<"vec is "<<device<<endl;
}
}
vector<FieldType> A{1, 2, 3,4,5,6};
vector<FieldType> B{9,8,7,6,5,4, 3, 2};
vector<FieldType> C(12);
processNN31((merssene31_t *)C.data(),
(merssene31_t *)B.data(), 2, 4,
(merssene31_t *)A.data(), 3, 2,
devices);
for(int i=0; i<C.size(); i++){
cout<<"C[i] is "<<C[i];
}
cout<<"--------- reg result ----------------------------"<<endl;
regMatrixMulTN(C.data(),
A.data(), 3, 3,
B.data(), 3,3);
for(int i=0; i<C.size(); i++){
cout<<"C[i] is "<<C[i];
}
*/
string partiesFile = this->getParser().getValueByKey(arguments, "partiesFile");
parties = comm.setCommunication(io_service, m_partyId, N, partiesFile);
prgs.resize(numThreads);
int* keyBytes = new int[4];
for (int i=0; i<numThreads; i++){
for (int j=0; j<4; j++){
keyBytes[j] = field->Random().elem;
}
SecretKey key((byte*)keyBytes, 16, "");
prgs[i].setKey(key);
}
delete [] keyBytes;
//
//
//
//
auto t1 = high_resolution_clock::now();
initializationPhase(/*matrix_him, matrix_vand, m*/);
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds initializationPhase: " << duration << endl;
}
//
//
shiftbyOne.resize(securityParamter);
for(int i=0; i<securityParamter; i++){
shiftbyOne[i] = 1 << i;
}
//
//
string tmp = "init times";
//cout<<"before sending any data"<<endl;
byte tmpBytes[20];
for (int i=0; i<parties.size(); i++){
if (parties[i]->getID() < m_partyId){
parties[i]->getChannel()->write(tmp);
parties[i]->getChannel()->read(tmpBytes, tmp.size());
} else {
parties[i]->getChannel()->read(tmpBytes, tmp.size());
parties[i]->getChannel()->write(tmp);
}
}
}
template <class FieldType>
void ProtocolParty<FieldType>::run() {
for (iteration=0; iteration<times; iteration++){
auto t1start = high_resolution_clock::now();
timer->startSubTask("Offline", iteration);
runOffline();
timer->endSubTask("Offline", iteration);
timer->startSubTask("Online", iteration);
runOnline();
timer->endSubTask("Online", iteration);
auto t2end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2end-t1start).count();
cout << "time in milliseconds for protocol: " << duration << endl;
}
}
template <class FieldType>
void ProtocolParty<FieldType>::runOffline() {
auto t1 = high_resolution_clock::now();
timer->startSubTask("preparationPhase", iteration);
if(preparationPhase() == false) {
if(flag_print) {
cout << "cheating!!!" << '\n';}
return;
}
else {
if(flag_print) {
cout << "no cheating!!!" << '\n' << "finish Preparation Phase" << '\n';}
}
timer->endSubTask("preparationPhase", iteration);
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds preparationPhase: " << duration << endl;
}
}
template <class FieldType>
void ProtocolParty<FieldType>::runOnline() {
auto t1 = high_resolution_clock::now();
timer->startSubTask("inputPhase", iteration);
inputPhase();
timer->endSubTask("inputPhase", iteration);
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds inputPhase: " << duration << endl;
}
t1 = high_resolution_clock::now();
timer->startSubTask("VerificationPhase", iteration);
t = thread(&ProtocolParty::verificationPhase, this);
//auto flag = verificationPhase();
timer->endSubTask("VerificationPhase", iteration);
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds verificationPhase: " << duration << endl;
}
t1 = high_resolution_clock::now();
timer->startSubTask("outputPhase", iteration);
outputPhase();
timer->endSubTask("outputPhase", iteration);
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds outputPhase: " << duration << endl;
}
//t.join();
}
template <class FieldType>
void ProtocolParty<FieldType>::batchConsistencyCheckOfShares(const vector<FieldType> &inputShares) {//first generate the common aes key
auto key = generateCommonKey();
//print key
if (flag_print) {
for (int i = 0; i < key.size(); i++) {
cout << "key[" << i << "] for party :" << m_partyId << "is : " << (int) key[i] << endl;
}
}
//calc the number of times we need to run the verification -- ceiling
int iterations = (5 + field->getElementSizeInBytes() - 1) / field->getElementSizeInBytes();
vector<FieldType> randomElements(inputShares.size()*iterations);
generatePseudoRandomElements(key, randomElements, inputShares.size());
for(int j=0; j<iterations;j++) {
vector<FieldType> r(1);//vector holding the random shares generated
vector<FieldType> v(1);
vector<FieldType> secret(1);
getRandomShares(1, r);
for (int i = 0; i < inputShares.size(); i++)
v[0] += randomElements[i+j*inputShares.size()] * inputShares[i];
v[0] += r[0];
//if all the the parties share lie on the same polynomial this will not abort
openShare(1, v, secret, T);
}
}
template <class FieldType>
void ProtocolParty<FieldType>::generateRandomSharesWithCheck(int numOfRandoms, vector<FieldType>& randomElementsToFill){
getRandomShares(numOfRandoms, randomElementsToFill);
batchConsistencyCheckOfShares(randomElementsToFill);
}
template <class FieldType>
void ProtocolParty<FieldType>::generateRandomShares(int numOfRandoms, vector<FieldType> &randomElementsToFill) {
int index = 0;
int robin = 0;
int no_random = numOfRandoms;
vector<FieldType> x1(N),y1(N), x2(N),y2(N), t1(N), r1(N), t2(N), r2(N);
vector<vector<FieldType>> sendBufsElements(N);
vector<vector<FieldType>> recBufsElements(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 - T)) + 1;
//sharingBufTElements.resize(no_buckets*(N-2*T)); // my shares of the double-sharings
//sharingBuf2TElements.resize(no_buckets*(N-2*T)); // my shares of the double-sharings
//maybe add some elements if a partial bucket is needed
randomElementsToFill.resize(no_buckets*(N - T));
for(int i=0; i < N; i++)
{
sendBufsElements[i].resize(no_buckets);
recBufsElements[i].resize(no_buckets);
}
/**
* generate random sharings.
* first degree t.
*
*/
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, note that x1[0] is the secret which is also random
x1[i] = field->Random();
}
matrix_vand.MatrixMult(x1, y1, T + 1); // eval poly at alpha-positions
// prepare shares to be sent
for(int i=0; i < N; i++)
{
//cout << "y1[ " <<i<< "]" <<y1[i] << endl;
sendBufsElements[i][k] = y1[i];
}
}
if(flag_print) {
for (int i = 0; i < N; i++) {
for (int k = 0; k < sendBufsElements[0].size(); k++) {
// cout << "before roundfunction4 send to " <<i <<" element: "<< k << " " << sendBufsElements[i][k] << endl;
}
}
cout << "sendBufs" << endl;
cout << "N" << N << endl;
cout << "T" << T << endl;
}
roundFunctionSyncElements(sendBufsElements, recBufsElements, 4);
for(int k=0; k < no_buckets; k++) {
for (int i = 0; i < N; i++) {
t1[i] = recBufsElements[i][k];
}
matrix_vand_transpose.MatrixMult(t1, r1, N - T);
//copy the resulting vector to the array of randoms
for (int i = 0; i < N - T; i++) {
randomElementsToFill[index] = r1[i];
index++;
}
}
}
template <class FieldType>
void ProtocolParty<FieldType>::getRandomShares(int numOfRandoms, vector<FieldType> &randomElementsToFill){
randomElementsToFill.assign (randomSharesArray.begin() + randomSharesOffset,
randomSharesArray.begin() + randomSharesOffset + numOfRandoms);
randomSharesOffset += numOfRandoms;
}
template <class FieldType>
void ProtocolParty<FieldType>::generateRandom2TAndTShares(int numOfRandomPairs, vector<FieldType>& randomElementsToFill){
auto t1 = high_resolution_clock::now();
int robin = 0;
int no_random = numOfRandomPairs;
// 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-T))+1;
vector<vector<FieldType>> sendBufsElements(N, vector<FieldType>(no_buckets*2));
vector<vector<FieldType>> recBufsElements(N, vector<FieldType>(no_buckets*2));
//maybe add some elements if a partial bucket is needed
randomElementsToFill.resize(no_buckets*(N-T)*2);
vector<FieldType> randomElementsOnlyTshares (no_buckets*(N-T) );
int sizeForEachThread;
if (no_buckets <= numThreads){
numThreads = no_buckets;
sizeForEachThread = 1;
} else{
sizeForEachThread = (no_buckets + numThreads - 1)/ numThreads;
}
vector<thread> threads(numThreads);
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds header: " << duration << endl;
}
t1 = high_resolution_clock::now();
for (int t=0; t<numThreads; t++) {
if ((t + 1) * sizeForEachThread <= no_buckets) {
threads[t] = thread(&ProtocolParty::calcSendBufElements, this, ref(sendBufsElements), ref(prgs[t]), t * sizeForEachThread, (t + 1) * sizeForEachThread);
} else {
threads[t] = thread(&ProtocolParty::calcSendBufElements, this, ref(sendBufsElements), ref(prgs[t]), t * sizeForEachThread, no_buckets);
}
}
for (int t=0; t<numThreads; t++){
threads[t].join();
}
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds calcSendBufElements: " << duration << endl;
}
t1 = high_resolution_clock::now();
roundFunctionSyncElements(sendBufsElements, recBufsElements,4);
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds round function: " << duration << endl;
}
t1 = high_resolution_clock::now();
for (int t=0; t<numThreads; t++) {
if ((t + 1) * sizeForEachThread <= no_buckets) {
threads[t] = thread(&ProtocolParty::calcRecBufElements, this, ref(recBufsElements), ref(randomElementsToFill), t * sizeForEachThread, (t + 1) * sizeForEachThread);
} else {
threads[t] = thread(&ProtocolParty::calcRecBufElements, this, ref(recBufsElements), ref(randomElementsToFill), t * sizeForEachThread, no_buckets);
}
}
for (int t=0; t<numThreads; t++){
threads[t].join();
}
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds calcRecBufElements: " << duration << endl;
}
//check validity of the t-shares. 2t-shares do not have to be checked
//copy the t-shares for checking
t1 = high_resolution_clock::now();
for(int i=0; i<randomElementsOnlyTshares.size(); i++){
randomElementsOnlyTshares[i] = randomElementsToFill[2*i];
}
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds copy: " << duration << endl;
}
t1 = high_resolution_clock::now();
batchConsistencyCheckOfShares(randomElementsOnlyTshares);
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(flag_print_timings) {
cout << "time in milliseconds batch consistency: " << duration << endl;
}
}
template <class FieldType>
void ProtocolParty<FieldType>::calcSendBufElements(vector<vector<FieldType>> & sendBufsElements, PrgFromOpenSSLAES & prg, int start, int end){
vector<FieldType> x1(N),y1(N), x2(N),y2(N);
int* tempInt;
for(int k=start; k < end; k++)
{
// generate random degree-T polynomial
tempInt = (int*)prg.getPRGBytesEX((T+1)*4);
for(int i = 0; i < T+1; i++)
{
// A random field element, uniform distribution, note that x1[0] is the secret which is also random
x1[i] = field->GetElement(tempInt[i]);
}
matrix_vand.MatrixMult(x1, y1,T+1); // eval poly at alpha-positions
x2[0] = x1[0];
// generate random degree-T polynomial
tempInt = (int*)prg.getPRGBytesEX((2*T+1)*4);
for(int i = 1; i < 2*T+1; i++)
{
// A random field element, uniform distribution, note that x1[0] is the secret which is also random
x2[i] = field->GetElement(tempInt[i]);
}
matrix_vand.MatrixMult(x2, y2,2*T+1);
// prepare shares to be sent
for(int i=0; i < N; i++)
{
//cout << "y1[ " <<i<< "]" <<y1[i] << endl;
sendBufsElements[i][2*k] = y1[i];
sendBufsElements[i][2*k + 1] = y2[i];
}
}
}
template <class FieldType>
void ProtocolParty<FieldType>::calcRecBufElements(vector<vector<FieldType>> & recBufsElements, vector<FieldType> & randomElementsToFill, int start, int end){
vector<FieldType> t1(N), r1(N), t2(N), r2(N);
for(int k=start; k < end; k++) {
for (int i = 0; i < N; i++) {
t1[i] = recBufsElements[i][2*k];
t2[i] = recBufsElements[i][(2*k +1)];
}
matrix_vand_transpose.MatrixMult(t1, r1,N-T);
matrix_vand_transpose.MatrixMult(t2, r2,N-T);
//copy the resulting vector to the array of randoms
for (int i = 0; i < (N - T); i++) {
randomElementsToFill[k*2] = r1[i];
randomElementsToFill[k*2 +1] = r2[i];
}
}
}
/**
* some global variables are initialized
* @param GateValueArr
* @param GateShareArr
* @param matrix_him
* @param matrix_vand
* @param alpha
*/
template <class FieldType>
void ProtocolParty<FieldType>::initializationPhase()
{
bigR.resize(1);
cout<<"requested size is "<<numClients*sqrtR*l<<endl;
msgsVectorsFlat.resize(numClients*sqrtR*l);
squaresVectorsFlat.resize(numClients*sqrtR*l);
countersVectorsFlat.resize(numClients*sqrtR);
unitVectorsFlat.resize(numClients*sqrtU);
// msgsVectorsShiftedFlat.resize(batchSize*sqrtR*l);
// squaresVectorsShiftedFlat.resize(batchSize*sqrtR*l);
// countersVectorsShiftedFlat.resize(batchSize*sqrtR);
// unitVectorsShiftedFlat.resize(batchSize*sqrtU);