-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObliviousDictionary.cpp
1358 lines (1065 loc) · 44.6 KB
/
ObliviousDictionary.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
//
// Created by moriya on 7/17/19.
//
#include "ObliviousDictionary.h"
#include "Tools.h"
#include <queue>
#define print_timings false
#define print false
void ObliviousDictionaryDB::init() {
vals.clear();
keys.clear();
peelingVector.clear();
peelingCounter = 0;
keys.resize(hashSize);
vals.reserve(hashSize);
for (int i=0; i < hashSize; i++){
keys[i] = prg->getRandom64() >> 3;
vals.insert({keys[i], prg->getRandom64() >> 3});
}
if(print) {
int numKeysToCheck = 10;
cout << "keys to check with the other party" << endl;
for (int i = 0; i < numKeysToCheck; i++) {
cout << "key = " << keys[i] << " val = " << vals[keys[i]] << endl;
}
}
}
void ObliviousDictionaryDB2Tables::init() {
firstSeed = prg->getRandom64();
secondSeed = prg->getRandom64();
ObliviousDictionaryDB::init();
first.clear();
second.clear();
}
ObliviousDictionaryDB2Tables::ObliviousDictionaryDB2Tables(int size, string toolType) : ObliviousDictionaryDB(size) {
auto key = prg->generateKey(128);
prg->setKey(key);
firstSeed = 123456;//prg->getRandom64();
secondSeed = 987654;//prg->getRandom64();
auto start = high_resolution_clock::now();
createSets();
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for create sets: " << duration << endl;
start = high_resolution_clock::now();
first.insert(1);
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for insert first element: " << duration << endl;
start = high_resolution_clock::now();
first.erase(1);
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for erase first element: " << duration << endl;
if(print) {
cout << "after create sets" << endl;
cout << "tableRealSize = " << tableRealSize << endl;
cout << "hashSize = " << hashSize << endl;
}
firstEncValues.resize(tableRealSize, 0);
secondEncValues.resize(tableRealSize, 0);
// keys.resize(hashSize);
// vals.reserve(hashSize);
//
// for (int i=0; i<hashSize; i++){
// keys[i] = prg->getRandom64() >> 3;
// vals.insert({keys[i],prg->getRandom64()>>3});
// }
//
// int numKeysToCheck = 10;
// cout<<"keys to check with the other party"<<endl;
// for (int i=0; i<numKeysToCheck; i++){
// cout<<"key = "<<keys[i]<<" val = "<<vals[keys[i]]<<endl;
// }
if (toolType.compare("poly") == 0){
tool = new PolynomialTool(hashSize);
}
}
void ObliviousDictionaryDB2Tables::createSets(){
first = unordered_set<uint64_t, Hasher>(hashSize, Hasher(firstSeed));
second = unordered_set<uint64_t, Hasher>(hashSize, Hasher(secondSeed));
tableRealSize = first.bucket_count();
if(print)
cout<<"tableRealSize = "<<tableRealSize<<endl;
while(tableRealSize/1.2 < hashSize){
first = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(firstSeed));
second = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(secondSeed));
tableRealSize = first.bucket_count();
if(print)
cout<<"tableRealSize = "<<tableRealSize<<endl;
}
hashSize = tableRealSize/1.2;
if(print)
cout<<"new hashSize = "<<hashSize<<endl;
}
void ObliviousDictionaryDB2Tables::fillTables(){
for (int i=0; i<hashSize; i++){
// cout<<"key is "<<keys[i]<<endl;
// auto pair = first.insert(keys[i]);
first.insert(keys[i]);
second.insert(keys[i]);
// if (pair.second == false){
// cout<<"key = "<<keys[i]<<" i = "<<i<<endl;
// }
}
if(print) {
cout << "first set contains " << first.size() << endl;
cout << "second set contains " << second.size() << endl;
}
}
void ObliviousDictionaryDB2Tables::peeling(){
peelingVector.resize(hashSize);
peelingCounter = 0;
auto t1 = high_resolution_clock::now();
//Goes on the first hash
for (int position = 0; position<tableRealSize; position++){
if (first.bucket_size(position) == 1){
//Delete the vertex from the graph
auto key = *first.begin(position);
// cout<<"remove key "<<key<<endl;
peelingVector[peelingCounter++] = key;
first.erase(key);
//Update the second vertex on the edge
second.erase(key);
}
}
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
if(print_timings)
cout << "time in milliseconds for first loop: " << duration << endl;
t1 = high_resolution_clock::now();
//goes on the second hash
for (int position = 0; position<tableRealSize; position++){
if (second.bucket_size(position) == 1){
//Delete the vertex from the graph
auto key = *second.begin(position);
// peelingVector.push_back(key);
// cout<<"remove key "<<key<<endl;
// second.erase(key);
int secondBucket = 0;
while(secondBucket <= position) {
peelingVector[peelingCounter++] = key;
// cout<<"remove key "<<key<<endl;
second.erase(key);
// if (secondBucket>0) cout<<"loop in peeling"<<endl;
//Update the second vertex on the edge
int bucket = first.bucket(key);
first.erase(key);
if (first.bucket_size(bucket) == 1) {
key = *first.begin(bucket);
// cout<<"remove key from first "<<key<<endl;
peelingVector[peelingCounter++] = key;
first.erase(key);
//Update the second vertex on the edge
secondBucket = second.bucket(key);
second.erase(key);
if (second.bucket_size(secondBucket) == 1) {
key = *second.begin(secondBucket);
// peelingVector.push_back(key);
// cout<<"remove key "<<key<<endl;
// second.erase(key);
} else {
secondBucket = position + 1;
}
} else {
secondBucket = position + 1;
}
}
}
}
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
if(print_timings)
cout << "time in milliseconds for second loop: " << duration << endl;
if (hasLoop()){
cout << "remain loops!!!" << endl;
}
if(print)
cout<<"peelingCounter = "<<peelingCounter<<endl;
}
void ObliviousDictionaryDB2Tables::generateExternalToolValues(){
int polySize = 5*log2(hashSize);
vector<uint64_t> edgesForPolynomial(polySize);
vector<uint64_t> valuesForPolynomial(polySize);
//Assign random values to all vertex in the circles.
for (int i=0; i<tableRealSize; i++){
if (first.bucket_size(i) > 1){
firstEncValues[i] = prg->getRandom64() >> 3;
// cout<<"bucket "<<i<<" got random value in first"<<endl;
}
if (second.bucket_size(i) > 1) {
secondEncValues[i] = prg->getRandom64() >> 3;
// cout<<"bucket "<<i<<" got random value in second"<<endl;
}
}
//Get all the edges that are in the graph's circles and calc the polynomial values that should be for them.
int polyCounter = 0;
for (int i=0; i<tableRealSize; i++){
if (first.bucket_size(i) > 1){
for (auto key = first.begin(i); key!= first.end(i); ++key){
edgesForPolynomial[polyCounter] = *key;
valuesForPolynomial[polyCounter] = firstEncValues[i] ^ secondEncValues[second.bucket(*key)] ^ vals[*key];
polyCounter++;
}
}
}
if(print)
cout<<"circles size = "<<polyCounter<<endl;
if(reportStatistics==1) {
statisticsFile << polyCounter << ", \n";
}
if (polyCounter < polySize){
for (int i=polyCounter; i<polySize; i++){
edgesForPolynomial[polyCounter] = peelingVector[i];
valuesForPolynomial[polyCounter] = prg->getRandom64() >> 3;
polyCounter++;
}
} else if (polyCounter > polySize){
cout<<"error!!! circles size is bigger than polynomial size"<<endl;
}
tool->generateToolValues(edgesForPolynomial, valuesForPolynomial);
}
void ObliviousDictionaryDB2Tables::unpeeling(){
if(print)
cout<<"in unpeeling"<<endl;
uint64_t firstPosition, secondPosition, poliVal, key;
// vector<uint64_t> polyVals(peelingCounter);
// Poly::evalMersenneMultipoint(polyVals, polynomial, peelingVector);
while (peelingCounter > 0){
// cout<<"key = "<<key<<endl;
key = peelingVector[--peelingCounter];
firstPosition = first.bucket(key);
secondPosition = second.bucket(key);
poliVal = tool->getValue(key);
// Poly::evalMersenne((ZpMersenneLongElement*)&poliVal, polynomial, (ZpMersenneLongElement*)&key);
// poliVal = polyVals[peelingCounter];
if (firstEncValues[firstPosition] == 0 && secondEncValues[secondPosition] == 0){
firstEncValues[firstPosition] = prg->getRandom64() >> 3;
// cout<<"set RANDOM value to first in bucket "<<firstPosition<<endl;
}
if (firstEncValues[firstPosition] == 0){
firstEncValues[firstPosition] = vals[key] ^ secondEncValues[secondPosition] ^ poliVal;
// cout<<"set value to first in bucket "<<firstPosition<<endl;
} else {
secondEncValues[secondPosition] = vals[key] ^ firstEncValues[firstPosition] ^ poliVal;
// cout<<"set value to second in bucket "<<secondPosition<<endl;
}
}
if(print)
cout<<"peelingCounter = "<<peelingCounter<<endl;
}
void ObliviousDictionaryDB2Tables::checkOutput(){
uint64_t firstPosition, secondPosition, key, val, poliVal;
for (int i=0; i<hashSize; i++){
key = keys[i];
val = vals[key];
poliVal = tool->getValue(key);
firstPosition = first.bucket(key);
secondPosition = second.bucket(key);
if ((firstEncValues[firstPosition] ^ secondEncValues[secondPosition] ^ poliVal) == val) {
if (i%100000 == 0)
cout<<"good value!!! val = "<<val<<endl;
} else {//if (!hasLoop()){
cout<<"invalid value :( val = "<<val<<" wrong val = "<<(firstEncValues[firstPosition] ^ secondEncValues[secondPosition] ^ poliVal)<<endl;
cout<<"firstEncValues["<<firstPosition<<"] = "<<firstEncValues[firstPosition]<<endl;
cout<<"secondEncValues["<<secondPosition<<"] = "<<secondEncValues[secondPosition]<<endl;
cout<<"poliVal = "<<poliVal<<endl;
}
}
}
bool ObliviousDictionaryDB2Tables::hasLoop(){
for (int position = 0; position<tableRealSize; position++) {
if (first.bucket_size(position) > 1) {
return true;
}
}
return false;
}
void ObliviousDictionaryDB2Tables::sendData(shared_ptr<ProtocolPartyData> otherParty){
//TODO should be deleted!!
// otherParty->getChannel()->write((byte*)&firstSeed, 8);
// otherParty->getChannel()->write((byte*)&secondSeed, 8);
if(print) {
cout << "firstSeed = " << firstSeed << endl;
cout << "secondSeed = " << secondSeed << endl;
cout << "key size in bytes: " << keys.size() * 8 << endl;
}
otherParty->getChannel()->write((byte*)keys.data(), keys.size()*8);
//TODO until here
otherParty->getChannel()->write((byte*)firstEncValues.data(), firstEncValues.size()*8);
otherParty->getChannel()->write((byte*)secondEncValues.data(), secondEncValues.size()*8);
int polySize = tool->getSendableDataSize();
if(print)
cout<<"polySize = "<<polySize/8<<endl;
otherParty->getChannel()->write(tool->getSendableData(), polySize);
}
ObliviousDictionaryQuery2Tables::ObliviousDictionaryQuery2Tables(int hashSize, string toolType) : ObliviousDictionaryQuery(hashSize){
auto key = prg->generateKey(128);
prg->setKey(key);
if (toolType.compare("poly") == 0){
tool = new PolynomialTool(hashSize);
}
}
void ObliviousDictionaryQuery2Tables::createSets(){
first = unordered_set<uint64_t, Hasher>(hashSize, Hasher(firstSeed));
second = unordered_set<uint64_t, Hasher>(hashSize, Hasher(secondSeed));
tableRealSize = first.bucket_count();
if(print)
cout<<"tableRealSize = "<<tableRealSize<<endl;
while(tableRealSize/1.2 < hashSize){
first = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(firstSeed));
second = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(secondSeed));
tableRealSize = first.bucket_count();
if(print)
cout<<"tableRealSize = "<<tableRealSize<<endl;
}
hashSize = tableRealSize/1.2;
if(print)
cout<<"new hashSize = "<<hashSize<<endl;
tool->setHashSize(hashSize);
}
void ObliviousDictionaryQuery2Tables::readData(shared_ptr<ProtocolPartyData> otherParty){
//TODO should be deleted!!
// otherParty->getChannel()->read((byte*)&firstSeed, 8);
// otherParty->getChannel()->read((byte*)&secondSeed, 8);
firstSeed = 123456;//prg->getRandom64();
secondSeed = 987654;
if(print) {
cout << "firstSeed = " << firstSeed << endl;
cout << "secondSeed = " << secondSeed << endl;
}
createSets();
vector<uint64_t> tmpKeys(hashSize);
otherParty->getChannel()->read((byte*)tmpKeys.data(), tmpKeys.size()*8);
int numKeysToCheck = 10;
keys.resize(numKeysToCheck);
for (int i=0; i<numKeysToCheck; i++){
keys[i] = tmpKeys[i];//prg->getRandom32() % hashSize;
}
int polySize = 5*log2(hashSize)*8;
if(print)
cout<<"polySize = "<<polySize/8<<endl;
byte* polynomial = new byte[polySize];
firstEncValues.resize(tableRealSize, 0);
secondEncValues.resize(tableRealSize, 0);
//TODO until here
otherParty->getChannel()->read((byte*)firstEncValues.data(), firstEncValues.size()*8);
otherParty->getChannel()->read((byte*)secondEncValues.data(), secondEncValues.size()*8);
otherParty->getChannel()->read(polynomial, polySize);
tool->setSendableData(polynomial);
delete polynomial;
}
void ObliviousDictionaryQuery2Tables::calcRealValues(){
if(print)
cout<<"vals:"<<endl;
uint64_t firstPosition, secondPosition, poliVal;
int size = keys.size();
vector<uint64_t> vals(size);
for (int i=0; i<size; i++){
poliVal = tool->getValue(keys[i]);
firstPosition = first.bucket(keys[i]);
secondPosition = second.bucket(keys[i]);
vals[i] = firstEncValues[firstPosition] ^ secondEncValues[secondPosition] ^ poliVal;
if(print)
cout<<"key = "<<keys[i]<<"val = "<<vals[i]<<endl;
// cout<<"firstEncValues["<<firstPosition<<"] = "<<firstEncValues[firstPosition]<<endl;
// cout<<"secondEncValues["<<secondPosition<<"] = "<<secondEncValues[secondPosition]<<endl;
// cout<<"poliVal = "<<poliVal<<endl;
}
}
void ObliviousDictionaryQuery2Tables::output(){
}
ObliviousDictionaryDB3Tables::ObliviousDictionaryDB3Tables(int size, string toolType,int batchSize, std::string processId, float tableRatio) : ObliviousDictionaryDB(size) {
this->batchSize = batchSize;
this->processId = processId;
this->tableRatio = tableRatio;
this->hashOriginalSize = size;
circleVec.resize(batchSize);
auto key = prg->generateKey(128);
prg->setKey(key);
firstSeed = prg->getRandom64();
secondSeed = prg->getRandom64();
thirdSeed = prg->getRandom64();
createSets();
if(print) {
cout << "after create sets" << endl;
cout << "tableRealSize = " << tableRealSize << endl;
cout << "hashSize = " << hashSize << endl;
}
firstEncValues.resize(tableRealSize, 0);
secondEncValues.resize(tableRealSize, 0);
thirdEncValues.resize(tableRealSize, 0);
if (toolType.compare("poly") == 0){
tool = new PolynomialTool(hashSize);
}
}
void ObliviousDictionaryDB3Tables::init() {
firstSeed = prg->getRandom64();
secondSeed = prg->getRandom64();
thirdSeed = prg->getRandom64();
ObliviousDictionaryDB::init();
first.clear();
second.clear();
third.clear();
}
void ObliviousDictionaryDB3Tables::createSets(){
first = unordered_set<uint64_t, Hasher>(hashSize*tableRatio/3.0, Hasher(firstSeed));
second = unordered_set<uint64_t, Hasher>(hashSize*tableRatio/3.0, Hasher(secondSeed));
third = unordered_set<uint64_t, Hasher>(hashSize*tableRatio/3.0, Hasher(thirdSeed));
tableRealSize = first.bucket_count();
if(print)
cout<<"tableRealSize = "<<tableRealSize<<endl;
while(tableRealSize*3.0/tableRatio < hashSize){
first = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(firstSeed));
second = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(secondSeed));
third = unordered_set<uint64_t, Hasher>(tableRealSize + 1, Hasher(thirdSeed));
tableRealSize = first.bucket_count();
if(print)
cout<<"tableRealSize = "<<tableRealSize<<endl;
}
first.max_load_factor(3.0/tableRatio + 1);
second.max_load_factor(3.0/tableRatio +1);
third.max_load_factor(3.0/tableRatio + 1);
hashSize = tableRealSize*3.0/tableRatio;
if(print)
cout<<"new hashSize = "<<hashSize<<endl;
}
void ObliviousDictionaryDB3Tables::fillTables(){
for (int i=0; i<hashSize; i++){
// cout<<"key is "<<keys[i]<<endl;
// auto pair = first.insert(keys[i]);
first.insert(keys[i]);
second.insert(keys[i]);
third.insert(keys[i]);
// cout<<"first bucket = "<<first.bucket(keys[i])<<" second bucket = "<<second.bucket(keys[i])<<" third bucket = "<<third.bucket(keys[i])<<endl;
// if (pair.second == false){
// cout<<"key = "<<keys[i]<<" i = "<<i<<endl;
// }
}
if(print) {
cout << "first set contains " << first.size() << endl;
cout << "second set contains " << second.size() << endl;
cout << "third set contains " << third.size() << endl;
}
}
void ObliviousDictionaryDB3Tables::peeling2() {
peelingVector.resize(hashSize);
peelingCounter = 0;
int counterInLoop = 1;
cout << "in peeling" << endl;
// cout<<"first loop"<<endl;
while (counterInLoop > 0){
counterInLoop = 0;
// auto t1 = high_resolution_clock::now();
//Goes on the first hash
for (int position = 0; position < tableRealSize; position++) {
if (first.bucket_size(position) == 1) {
//Delete the vertex from the graph
auto key = *first.begin(position);
// cout << "remove key " << key << endl;
counterInLoop++;
peelingVector[peelingCounter++] = key;
first.erase(key);
//Update the second vertex on the edge
second.erase(key);
third.erase(key);
}
}
// auto t2 = high_resolution_clock::now();
// auto duration = duration_cast<milliseconds>(t2 - t1).count();
// cout << "time in milliseconds for first loop: " << duration << endl;
// t1 = high_resolution_clock::now();
//Goes on the first hash
for (int position = 0; position < tableRealSize; position++) {
if (second.bucket_size(position) == 1) {
//Delete the vertex from the graph
auto key = *second.begin(position);
// cout << "remove key " << key << endl;
counterInLoop++;
peelingVector[peelingCounter++] = key;
second.erase(key);
//Update the second vertex on the edge
first.erase(key);
third.erase(key);
}
}
// t2 = high_resolution_clock::now();
// duration = duration_cast<milliseconds>(t2 - t1).count();
// cout << "time in milliseconds for second loop: " << duration << endl;
// t1 = high_resolution_clock::now();
//Goes on the first hash
for (int position = 0; position < tableRealSize; position++) {
if (third.bucket_size(position) == 1) {
//Delete the vertex from the graph
auto key = *third.begin(position);
// cout << "remove key " << key << endl;
counterInLoop++;
peelingVector[peelingCounter++] = key;
third.erase(key);
//Update the second vertex on the edge
second.erase(key);
first.erase(key);
}
}
// t2 = high_resolution_clock::now();
// duration = duration_cast<milliseconds>(t2 - t1).count();
// cout << "time in milliseconds for third loop: " << duration << endl;
//
// cout<<" counterInLoop = "<< counterInLoop<<endl;
}
/*
// cout<<"second loop"<<endl;
auto t1 = high_resolution_clock::now();
//goes on the second hash
for (int position = 0; position<tableRealSize; position++){
if (second.bucket_size(position) == 1){
//Delete the vertex from the graph
auto key = *second.begin(position);
// peelingVector.push_back(key);
// cout<<"remove key "<<key<<endl;
// second.erase(key);
peelSecondSet(position, key, true, nullptr, key);
}
}
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2-t1).count();
cout << "time in milliseconds for second loop: " << duration << endl;
// cout<<"third loop"<<endl;
t1 = high_resolution_clock::now();
//goes on the second hash
for (int position = 0; position<tableRealSize; position++){
vector<uint64_t> keysToDeleteFromThree;
peelThirdSet(position, &keysToDeleteFromThree);
//
// cout<<"keysToDeleteFromThree size = "<<keysToDeleteFromThree.size()<<endl;
for (int i=0; i<keysToDeleteFromThree.size(); i++){
auto key = keysToDeleteFromThree[i];
int bucket = third.bucket(key);
third.erase(key);
// cout<<"remove key from third "<<key<<endl;
peelThirdSet(bucket, &keysToDeleteFromThree);
// cout<<"keysToDeleteFromThree size = "<<keysToDeleteFromThree.size()<<endl;
}
}
t2 = high_resolution_clock::now();
duration = duration_cast<milliseconds>(t2-t1).count();
cout << "time in milliseconds for third loop: " << duration << endl;
if (hasLoop()){
cout << "remain loops!!!" << endl;
}
*/
if(print) {
cout << "peelingCounter = " << peelingCounter << endl;
cout << "circle size = " << (hashSize - peelingCounter) << endl;
}
// cout<<"peeling vector:"<<endl;
// for (int i=0; i<peelingCounter; i++){
// cout<<peelingVector[i]<<" ";
// }
// cout<<endl;
}
void ObliviousDictionaryDB3Tables::peelThirdSet(int position, vector<uint64_t> * keysToDeleteFromThree){
if (third.bucket_size(position) == 1){
//Delete the vertex from the graph
auto key = *third.begin(position);
peelingVector[peelingCounter++] = key;
// cout<<"remove key from third "<<key<<endl;
third.erase(key);
//Dealing with set number 2
int bucket = second.bucket(key);
second.erase(key);
// cout<<"remove key from second "<<key<<endl;
if (second.bucket_size(bucket) == 1) {
auto secondKey = *second.begin(bucket);
//cout<<"go to peelSecondSet. bucket = "<<bucket<<" key = "<<secondKey<<endl;
peelSecondSet(second.bucket_count(), secondKey, false, keysToDeleteFromThree, key);
}
//Dealing with set number 1
bucket = first.bucket(key);
first.erase(key);
// cout<<"remove key from first "<<key<<endl;
if (first.bucket_size(bucket) == 1) {
auto secondKey = *first.begin(bucket);
peelingVector[peelingCounter++] = secondKey;
keysToDeleteFromThree->push_back(secondKey);
// cout<<"put "<<secondKey<<" in keysToDeleteFromThree"<<endl;
first.erase(secondKey);
// cout<<"remove key from first "<<secondKey<<endl;
bucket = second.bucket(secondKey);
second.erase(secondKey);
if (second.bucket_size(bucket) == 1) {
secondKey = *second.begin(bucket);
peelSecondSet(second.bucket_count(), secondKey, false, keysToDeleteFromThree, key);
}
}
}
}
void ObliviousDictionaryDB3Tables::peelSecondSet(int position, uint64_t key, bool deleteFromThree, vector<uint64_t> * keysToDeleteFromThree, uint64_t originalKey) {
int secondBucket = 0;
while (secondBucket <= position) {
peelingVector[peelingCounter++] = key;
// cout<<"remove key from second "<<key<<endl;
second.erase(key);
if (deleteFromThree) {
third.erase(key);
// cout<<"remove key from third "<<key<<endl;
} else {
keysToDeleteFromThree->push_back(key);
// cout<<"put "<<key<<" in keysToDeleteFromThree"<<endl;
}
// if (secondBucket>0) cout<<"loop in peeling"<<endl;
//Update the second vertex on the edge
int bucket = first.bucket(key);
first.erase(key);
// cout<<"remove key from first "<<key<<endl;
if (first.bucket_size(bucket) == 1) {
key = *first.begin(bucket);
if (key != originalKey) {
// cout << "remove key from first " << key << endl;
peelingVector[peelingCounter++] = key;
first.erase(key);
if (deleteFromThree) {
third.erase(key);
// cout<<"remove key from third "<<key<<endl;
} else {
keysToDeleteFromThree->push_back(key);
// cout<<"put "<<key<<" in keysToDeleteFromThree"<<endl;
}
//Update the second vertex on the edge
secondBucket = second.bucket(key);
second.erase(key);
// cout << "remove key from second " << key << endl;
if (second.bucket_size(secondBucket) == 1) {
key = *second.begin(secondBucket);
} else {
secondBucket = position + 1;
}
} else {
// cout<<"key will be deleted in the future"<<endl;
secondBucket = position + 1;
}
} else {
secondBucket = position + 1;
}
}
}
void ObliviousDictionaryDB3Tables::peeling() {
peelingVector.resize(hashSize);
peelingCounter = 0;
int counterInLoop = 1;
queue<int> queueFirst;
queue<int> queueSecond;
queue<int> queueThird;
// cout << "in peeling" << endl;
// cout<<"first loop"<<endl;
auto start = high_resolution_clock::now();
//Goes on the first hash
for (int position = 0; position < tableRealSize; position++) {
if (first.bucket_size(position) == 1) {
//Delete the vertex from the graph
auto key = *first.begin(position);
// cout << "remove key " << key << endl;
counterInLoop++;
peelingVector[peelingCounter++] = key;
first.erase(key);
//Update the second vertex on the edge
second.erase(key);
third.erase(key);
}
}
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for first peel: " << duration << endl;
start = high_resolution_clock::now();
int bucketInd;
//Goes on the second has
for (int position = 0; position < tableRealSize; position++) {
if (second.bucket_size(position) == 1) {
//Delete the vertex from the graph
auto key = *second.begin(position);
second.erase(key);
// cout << "remove key " << key << endl;
counterInLoop++;
peelingVector[peelingCounter++] = key;
bucketInd = first.bucket(key);
first.erase(key);
if(first.bucket_size(bucketInd)==1)
queueFirst.push(bucketInd);
//Update the second vertex on the edge
third.erase(key);
}
}
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for second peel: " << duration << endl;
start = high_resolution_clock::now();
for (int position = 0; position < tableRealSize; position++) {
if (third.bucket_size(position) == 1) {
//Delete the vertex from the graph
auto key = *third.begin(position);
third.erase(key);
// cout << "remove key " << key << endl;
counterInLoop++;
peelingVector[peelingCounter++] = key;
bucketInd = first.bucket(key);
first.erase(key);
if(first.bucket_size(bucketInd)==1)
queueFirst.push(bucketInd);
bucketInd = second.bucket(key);
second.erase(key);
if(second.bucket_size(bucketInd)==1)
queueSecond.push(bucketInd);
}
}
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for third peel: " << duration << endl;
if(print) {
cout << "peelingCounter : " << peelingCounter << endl;
cout << "hashSize : " << hashSize << endl;
cout << "queueFirst.size() : " << queueFirst.size() << endl;
cout << "queueSecond.size() : " << queueSecond.size() << endl;
cout << "queueThird.size() : " << queueThird.size() << endl;
}
start = high_resolution_clock::now();
//handle the queues one by one
while(queueFirst.size()!=0 ||
queueSecond.size()!=0 ||
queueThird.size()!=0){
handleQueue(queueFirst, first,
queueSecond, second,
queueThird,third);
handleQueue(queueSecond, second,
queueFirst, first,
queueThird,third);
handleQueue(queueThird,third,
queueFirst, first,
queueSecond, second);
}
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end-start).count();
if(print_timings)
cout << "time in milliseconds for peel queues: " << duration << endl;
if(print) {
cout << "peelingCounter : " << peelingCounter << endl;
cout << "hashSize : " << hashSize << endl;
}
// if(reportStatistics==1) {
//
// statisticsFile << "tesintg" << ", \n";
// }
}
void ObliviousDictionaryDB3Tables::handleQueue(queue<int> &queueMain, unordered_set<uint64_t, Hasher> &main,
queue<int> &queueOther1, unordered_set<uint64_t, Hasher> &other1,
queue<int> &queueOther2,unordered_set<uint64_t, Hasher> &other2) {
int bucketInd;
for(int i=0; i < queueMain.size(); i++){
int pos = queueMain.front();
queueMain.pop();
if(main.bucket_size(pos) == 1) {
auto key = *main.begin(pos);
main.erase(key);
// cout << "remove key " << key << endl;
peelingVector[peelingCounter++] = key;
bucketInd = other1.bucket(key);
other1.erase(key);
if (other1.bucket_size(bucketInd) == 1)
queueOther1.push(bucketInd);
bucketInd = other2.bucket(key);
other2.erase(key);
if (other2.bucket_size(bucketInd) == 1)
queueOther2.push(bucketInd);
}
}