-
Notifications
You must be signed in to change notification settings - Fork 0
/
ORAM.cpp
1382 lines (1155 loc) · 49 KB
/
ORAM.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 meysam on 7/18/18.
//
#include <cmath>
#include "ORAM.h"
#include <random>
#include <iostream>
#include <assert.h>
#include <time.h>
#include <list>
//#include "Util.h"
ONC::ONC(int cached_height){
long long int size_cache = (1 << cached_height) - 1;
hit = 0;
miss = 0;
/*for (int i=0; i<size_cache; i++)
{
ONC_umap.insert(std::make_pair(0,false));
}*/
}
ONC::~ONC(){
std::unordered_map<long long int, std::pair< bool, int>>::iterator it;
for (it= ONC_umap.begin(); it != ONC_umap.end();)
{
it = ONC_umap.erase(it);
}
ONC_stat_write();
std::cout << " ONC is distryed\n";
}
void ONC::write_ONC() {
std::unordered_map<long long int, std::pair< bool, int>>::iterator it;
for (it = ONC_umap.begin(); it != ONC_umap.end(); it++)
{
std::cout << std::hex << it->first <<" " << std::dec << it->second.first << " " << it->second.second <<"\n";
}
}
bool ONC::find(long long int tg) {
auto found = ONC_umap.find(tg);
if (found == ONC_umap.end())
{
//std::cout << "not found\n";
return (false);
}
else
if (found->second.first == true) {
// std::cout << "found\n";
return (true);
}
else {
// std::cout << "found invalid\n";
return (false);
}
}
void ONC::insert(long long int tg, int loc ) {
assert(find(tg) == false);
ONC_umap.insert(std::make_pair(tg, std::make_pair(true, loc)));
}
void ONC::delete_node(long long int tg) {
//std::cout <<std::hex<< tg << " in the function --> to be deleted..\n";
if (find(tg)) {
ONC_umap.erase(tg);
// std::cout <<std::hex<< tg << " in the function --> deleted..\n";
}
else
assert(0);
}
void ONC::write_cache() {
std::unordered_map<long long int, std::pair<bool, int>>::iterator it;
std::cout << "----------------ONC-------------------\n";
for (it = ONC_umap.begin(); it != ONC_umap.end(); it++)
if (it->second.first == true)
std::cout << it->first <<"\n";
std::cout << "----------------END-------------------\n";
}
bool ONC::Is_empty() {
return(ONC_umap.empty());
}
void ONC::hit_update() {
hit++;
}
void ONC::miss_update() {
miss++;
}
void ONC::ONC_stat_write() {
std::cout << "hit miss = " <<std::dec<< hit << " "<< miss <<"\n";
//output_file << "hit miss = " << hit << " "<< miss <<"\n";
}
long long int ONC::get_hit() {
return (hit);
}
long long int ONC::get_miss() {
return (miss);
}
void ONC::reset_hit_miss() {
miss = 0;
hit = 0;
}
int ONC::get_location(long long int addr) {
std::unordered_map<long long int, std::pair< bool, int>>::iterator it;
for (it= ONC_umap.begin(); it != ONC_umap.end(); it++) {
if (it->first == addr)
return (it->second.second);
}
assert(0);
}
ORAM::ORAM(const uint32_t num_node, uint32_t zi, int cached_height, int utilization) : ONC(cached_height), my_ONC(cached_height) {
Node_t Oram_N;
//srand(time(NULL));
ORAM_Z = zi;
height = (uint32_t)ceil(log2((double) num_node));
hight_cached = cached_height;
num_leaf = (uint32_t)1 << (height - 1);
//(uint32_t) (1 << (height-1));
leaf_id_count = new long long int[num_leaf];
for (int i=0; i<num_leaf; i++)
{
leaf_id_count[i] = 0;
}
for (int i=0; i<height; i++)
{
histogram[i] = 0;
}
// to generate random number
//std::random_device rd; //Will be used to obtain a seed for the random number engine
//std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
//std::uniform_int_distribution<> rnd(0, num_leaf-1);
pos_map = new uint32_t [num_node*ORAM_Z];
long long int num_block = 0;
for (uint32_t i =0; i< ((1 << height) - 1); i++)
{
for (uint32_t j=0; j<ORAM_Z; j++)
{
Oram_N.dummy = 1; // it is a dummy node
Oram_N.addr = -1; //OFFSET + ((i * ORAM_Z + j)<< 6); // cache line address
Oram_N.leaf_id = -1; //rnd(gen);
pos_map[(i * ORAM_Z + j)]= -1; //Oram_N.leaf_id; //fill the posmap
buckets.push_back(Oram_N); // insert the node into the ORAM_node vector
num_block++;
}
}
// initilize the pos-map with random number for entire memory
for (int i =0; i<(num_node*ORAM_Z* utilization/100); i++ )
{
pos_map[i] = Util::rand_int(num_leaf);
}
VERBOSE = 0;
VERBOSE1 = 0;
VERBOSE2 = 0;
VERBOSE_BAEC = 0;
found_in_stash = 0;
miss_in_stash = 0;
CYCLE_VAL = 0;
data_cache = 0;
data_memory = 0;
access_BE = 0;
std::cout << "ORAM and its pos map are initilized \n";
std::cout << "height ............................ " << height <<"\n";
std::cout << "treetop level ..................... " << hight_cached <<"\n";
std::cout << "num of leaf ....................... " << num_leaf << "\n";
std::cout << "num of blocks ..................... " << num_block << "\n";
std::cout << "ORAM_Z ............................ " << ORAM_Z << "\n";
std::cout << "STASH_SIZE ........................ " << STASH_SIZE << "\n";
std::cout << "STASH_THSHOLD ..................... " << STASH_THSHOLD << "\n";
std::cout << "OFFSET ............................ " << OFFSET << "\n";
}
ORAM::~ORAM() {
//write_buckets();
buckets.clear();
free (pos_map);
write_histogran();
std::cout<< "hit = : "<< my_ONC.get_hit() << "\n";
std::cout<< "miss = : "<< my_ONC.get_miss() << "\n";
std::cout << "found_in_stash = " << found_in_stash << "\n";
std::cout << "missed_in_stash = " << miss_in_stash << "\n";
std::cout << "access BE = " << access_BE << "\n";
// draw the histogram for count
long long int max, min;
max = 0;
min = 1000000;
for (int i=0; i<num_leaf; i++)
{
if (max < leaf_id_count[i])
max = leaf_id_count[i];
if (leaf_id_count[i] < min)
min = leaf_id_count[i];
}
const int lev = 100;
int length_levels = (max - min) / lev;
/*int hist[lev];
for (int i=0; i<lev; i++) hist[i] = 0;
for (int i = 0; i<num_leaf; i++)
{
hist[((leaf_id_count[i] - min)/length_levels)]++;
}*/
// for (int i=0; i<num_leaf; i++)
// std::cout << "leaf_id_count[" << i << " ] = " << leaf_id_count[i] <<"\n";
/*for (int i=0; i<lev; i++)
{
std::cout << "hist[" << i << " ] = " << hist[i] <<"\n";
}*/
std::cout << "ORAM destroyed memory back\n";
}
void ORAM::set_VERBOSE() {
VERBOSE = 1;
VERBOSE1 = 1;
VERBOSE2 = 1;
}
void ORAM::reset_VERBOSE() {
VERBOSE = 0;
VERBOSE1 = 0;
VERBOSE2 = 0;
}
void ORAM::write_ONC_stat() {
my_ONC.ONC_stat_write();
}
bool ORAM::look_in_stash(const long long int &addr) {
bool found = 0;
std::list<std::pair<int, long long int>>::iterator it;
for (it=stash.begin(); it!=stash.end(); it++)
{
if (it->second == addr) {
found = 1;
break;
}
}
return (found);
}
long long int ORAM::get_found_in_stash() {
return (found_in_stash);
}
void ORAM::write_buckets() {
std::vector<Node_t>::iterator it;
long long int num = 0;
for (it=buckets.begin(); it!=buckets.end(); ++it)
{
if (it->dummy == 0)
std::cout <<std::dec<< num << " [ " <<std::dec<< it->dummy << " " <<std::dec<< it->leaf_id << " " <<std::hex<< it->addr << " ]" << "\n";
num++;
}
}
void ORAM::background_eviction() {
// VERBOSE_BAEC = 0;
int lf_id = -1;
//while (lf_id == -1)
//lf_id = pos_map[rand()%1024][rand()%((ORAM_Z*((1<<(height))-1))/1024)];
lf_id = Util::rand_int(num_leaf);//rand()%num_leaf;
//assert(lf_id != -1);
//if (VERBOSE_BAEC)
//{
std::cerr << " ========================== BackGraound Eviction is undergone for ld_id = " << std::dec << lf_id << " stash size = " << std::dec << get_size_stash() <<"\n";
//}
access_BE++;
int Node_in_Path = (1 << (height - 1)) - 1 + lf_id;
int level_node_in_path = height - 1;
while (Node_in_Path >= 0) {
CYCLE_VAL++;
if (VERBOSE_BAEC)
std::cout << "Node_in_Path= " <<std::dec<< Node_in_Path << "\n";
//starting from leaf go up to the root
for (uint32_t j = 0; j < ORAM_Z; j++) {
if (VERBOSE_BAEC)
std::cout << "j= " << j << " bucket[ " <<std::dec<< Node_in_Path * ORAM_Z + j << " ] " <<std::hex<< buckets[Node_in_Path * ORAM_Z + j].addr;
if (level_node_in_path < hight_cached)
{
if (buckets[Node_in_Path * ORAM_Z + j].dummy == 0)
my_ONC.delete_node(buckets[Node_in_Path * ORAM_Z + j].addr);
}
// this is not our target either it is a dummy throw it a way!
// or it is a data put it in the stash
if (buckets[Node_in_Path * ORAM_Z + j].dummy != 1) {
// data
if (VERBOSE_BAEC) {
std::cout << "--> not target but data taken to stash \n";
}
if (VERBOSE_BAEC)
std::cout <<std::dec<<"bucket[" <<Node_in_Path * ORAM_Z + j <<" ] "<< std::hex<<buckets[Node_in_Path * ORAM_Z + j].addr<<"\n";
if (look_in_stash( buckets[Node_in_Path * ORAM_Z + j].addr)) {
std::cout << " --> addr = " << buckets[Node_in_Path * ORAM_Z + j].addr << "\n";
// write_stash();
}
assert(!look_in_stash(buckets[Node_in_Path * ORAM_Z + j].addr));
stash.push_back(std::make_pair(buckets[Node_in_Path * ORAM_Z + j].leaf_id,buckets[Node_in_Path * ORAM_Z + j].addr));
//if (VERBOSE_BAEC)
// std::cout << "+++stash size is " << stash[leaf_id_2_stash_id(buckets[Node_in_Path * ORAM_Z + j].leaf_id)].size()<< "\n";
} else {
// dummy
if (VERBOSE_BAEC)
std::cout << "--> dummy \n";
}
// whether or not it is dummy, it should be converted to a dummy one because it is evacuated into the stash or deleted
buckets[Node_in_Path * ORAM_Z + j].dummy = 1;
buckets[Node_in_Path * ORAM_Z + j].addr = -1;
buckets[Node_in_Path * ORAM_Z + j].leaf_id = -1;
}// for cache lines per bucket
if (Node_in_Path == 0) break;
Node_in_Path = (Node_in_Path - 1) / 2;
level_node_in_path--;
// go to the parent node up to the root
} // while bucket
if (VERBOSE_BAEC)
std::cout << " Back_Evict: eviction from stash back to ORAM starting .....\n";
Node_in_Path = (1 << (height - 1)) - 1 + lf_id;
int level = height - 1; // the maximum level is for leaves in the cache
std::list<std::pair<int, long long int>>::iterator it_j;
int lv = 0; //the max is for the root
while (Node_in_Path >= 0)
{
CYCLE_VAL++;
// find the correct cachelines belonging to correct leaf that can be located in this bucket
if(VERBOSE2)
{
std::cout << "Node_in path = " << Node_in_Path << "\n";
}
int num_cache_per_bucket = 0;
it_j = stash.begin();
for (; it_j != stash.end();){
// std::cout << std::dec << it_j->first <<" "<< lf_id <<" " << (it_j->first ^ lf_id) <<" " << ((1<< lv)-1)<<"\n";
// std::cout << std::dec << it_j->first <<" " <<std::hex<<" " << it_j->second <<"\n";
if ((it_j->first ^ lf_id) <= ((1<< lv)-1))
{
if(VERBOSE2)
std::cout <<std::dec << it_j->first <<" "<< lf_id <<" " << lv <<" "<< ((1<< lv)-1) <<"\n";
if(VERBOSE2)
{
std::cout << "buckets[ " <<std::dec<< Node_in_Path*ORAM_Z+num_cache_per_bucket <<" ]" << "is updated ";
}
// this is good for this node
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].dummy = 0;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].leaf_id = it_j->first;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].addr = it_j->second;
if(VERBOSE2)
{
std::cout << "with " <<std::dec << it_j->first << std::hex <<" "<< it_j->second << "\n";
}
if (level < hight_cached)
{
my_ONC.insert(buckets[Node_in_Path * ORAM_Z + num_cache_per_bucket].addr, Node_in_Path * ORAM_Z + num_cache_per_bucket);
//std::cout << std::dec<< "bucket[" << Node_in_Path * ORAM_Z + num_cache_per_bucket << "] = " <<std::hex <<
// buckets[Node_in_Path * ORAM_Z + num_cache_per_bucket].addr << " inserted in cache \n";
}
it_j = stash.erase(it_j);
num_cache_per_bucket++;
if (num_cache_per_bucket >= ORAM_Z)
break;
}
else ++it_j;
}
//std::cout <<"===========\n";
//getchar();
if (num_cache_per_bucket < ORAM_Z)
{
// not found enough fit canidates go for dummies!
for (int j=(num_cache_per_bucket); j<ORAM_Z; j++)
{
Node_t tmp;
tmp.dummy = 1;
tmp.addr = -2;
tmp.leaf_id = -2;
buckets[Node_in_Path * ORAM_Z + j] = tmp;
if(VERBOSE2)
{
std::cout << "bucket[ " <<std::dec<< Node_in_Path * ORAM_Z + j << " ] is dummy " << "\n";
}
}
}
/* for (int i=0; i<(1<<lv); i++)
{
int leaf_look = ((1<<lv)*(Node_in_Path - ((1<< level)-1)))+i;
// go to the stash and find the cacheline with this leaf_look
if(VERBOSE)
{
std::cout << "leaf_look = " << leaf_look << "\n";
}
it_j = stash.begin();
for (; it_j != stash.end();)
{
if (it_j->first == leaf_look)
{
if(VERBOSE)
{
std::cout << "buckets[ " << Node_in_Path*ORAM_Z+num_cache_per_bucket <<" ]" << "is updated ";
}
// this cacheline should be evicted from stash to this node.
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].dummy = 0;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].leaf_id = leaf_look;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].addr = it_j->second;
if(VERBOSE)
{
std::cout << "with " <<std::dec << leaf_look << std::hex <<" "<< it_j->second << "\n";
}
// std::cout << " before erase ...\n";
stash.erase(it_j);
if (STASH_VERBOSE)
std::cout << "+++stash size is " << stash.size()<< "\n";
// std::cout << " After erase ...\n";
num_cache_per_bucket++;
if (num_cache_per_bucket == ORAM_Z)
{
// ths node is full
break;
}
}
else ++it_j;
}
if (num_cache_per_bucket ==ORAM_Z )
break;
if ((i == (1<<lv)-1) && (num_cache_per_bucket < ORAM_Z))
{
if(VERBOSE)
{
std::cout << "dummy --> num_cache_per_bucket = " << num_cache_per_bucket << "\n";
}
// we could not find enough non-dummy cache lines go for dummies
if (VERBOSE)
std::cout << "num_cache_per_bucket = " << num_cache_per_bucket << "\n";
for (int j=(num_cache_per_bucket); j<ORAM_Z; j++)
{
Node_t tmp;
tmp.dummy = 1;
tmp.addr = -2;
tmp.leaf_id = -2;
buckets[Node_in_Path * ORAM_Z + j] = tmp;
if(VERBOSE)
{
std::cout << "bucket[ " << Node_in_Path * ORAM_Z + j << " ] is dummy " << "\n";
}
}
}
}*/
if (Node_in_Path == 0) break;
Node_in_Path = (Node_in_Path - 1) / 2;
lv++;
level--;
}
/* while (Node_in_Path >= 0)
{
CYCLE_VAL++;
// find the correct cachelines belonging to correct leaf that can be located in this bucket
if(VERBOSE_BAEC)
{
std::cout << "Node_in path = " <<std::dec<< Node_in_Path << "\n";
}
int num_per_buck = 0;
int num_group = (1<<(height-level-1)); // howmany leaf id may fit in to this level of lf_id
int num_cache_per_bucket = 0;
for (int i=0; i<num_group; i++)
{
// this leaf can be fit into this level of lf_id
int cand_leaf_id = num_group * (lf_id / num_group) + i;
std::list<std::pair<int, long long int>>::iterator it_st;
//for (int j = 0; j < ORAM_Z; j++) Z_top[j].top_pair.second.first = -1;
//int min_index = 0; // index of minimum in Z_top
//num_cache_per_bucket = 0;
//if (VERBOSE_BAEC)
// write_a_stash(leaf_id_2_stash_id(cand_leaf_id));
for (it_st = stash.begin(); it_st != stash.end();) {
if (it_st->first == cand_leaf_id) {
if (VERBOSE_BAEC) {
std::cout << "buckets[ " << std::dec << Node_in_Path * ORAM_Z + num_per_buck << " ]"
<< "is updated ";
}
buckets[Node_in_Path * ORAM_Z + num_per_buck].dummy = 0;
buckets[Node_in_Path * ORAM_Z + num_per_buck].leaf_id = it_st->first;
buckets[Node_in_Path * ORAM_Z + num_per_buck].addr = it_st->second;
if (level < hight_cached)
my_ONC.insert(buckets[Node_in_Path * ORAM_Z + num_per_buck].addr);
if (VERBOSE_BAEC) {
std::cout << "with " << std::dec << it_st->first << std::hex << " " << it_st->second
<< " from memory" << "\n";
}
// delete from stash
it_st = stash.erase(it_st);
num_per_buck++;
if (num_per_buck == ORAM_Z)
break;
}
else
it_st++;
}
if (num_per_buck == ORAM_Z)
break;
}
if (num_per_buck <ORAM_Z)
{
// go for dummy
for (int ii=num_per_buck; ii<ORAM_Z;ii++)
{
buckets[Node_in_Path * ORAM_Z + ii].dummy = 1;
buckets[Node_in_Path * ORAM_Z + ii].leaf_id = -1;
buckets[Node_in_Path * ORAM_Z + ii].addr = -1;
if(VERBOSE_BAEC)
{
std::cout << "bucket[ " <<std::dec<< Node_in_Path * ORAM_Z + ii << " ] is dummy " << "\n";
}
}
}
if (Node_in_Path == 0) break;
Node_in_Path = (Node_in_Path - 1) / 2;
// lv++;
level--;
} // while bucket for eviction
*/
// if (VERBOSE_BAEC)
std::cerr << " Back eviction has endded !!!! ====================== " << " stash size = " << std::dec <<get_size_stash() <<"\n";
}
void ORAM::write_ONC() {
my_ONC.write_ONC();
}
int ORAM::access(const char &op, const long long int &addr) {
if (VERBOSE) {
if (op == 'r')
std::cout << "addr = " <<std::hex<< addr << " =========ORAM access started for a read!======================\n";
else
std::cout << "addr = " <<std::hex<< addr << " =========ORAM access started for a write!======================\n";
}
// determine whether or not it already exists in stash
if (look_in_stash(addr)) {
found_in_stash++;
return 0;
}
miss_in_stash++;
if (!my_ONC.Is_empty())
{
if (my_ONC.find(addr) == true)
{
my_ONC.hit_update();
return 0;
}
else
my_ONC.miss_update();
}
else
my_ONC.miss_update();
// determine which path (leaf_id) should be fetched
int lf_id = pos_map[(addr - OFFSET)/64];
/*if (op == 'r')
{
//assert(pos_map[(addr - OFFSET) / 64] != -1);
lf_id = pos_map[(addr - OFFSET) / 64];
if (VERBOSE)
std::cout << "lf_id = " <<std::dec<< lf_id << "\n";
}
else
{
//std::cout <<"wrrrrrrrrite\n";
//write
if (pos_map[(addr - OFFSET) / 64] == -1)
{
// this is the first time to write
pos_map[(addr - OFFSET) / 64] = Util::rand_int(num_leaf); //rand()%num_leaf; //rnd(gen);
leaf_id_count[pos_map[(addr - OFFSET) / 64]]++;
lf_id = pos_map[(addr - OFFSET) / 64];
// std::cout <<"rand_int = " << lf_id <<"\n";
// getchar();
}
else {
lf_id = pos_map[(addr - OFFSET) / 64];
}
}*/
std::vector<std::pair<int, long long int>> stash_buffer; // to tore candidates to evict there is a triple (int,int, long long int)
// to generate random number
// fetch all the blocks along with the path
int Node_in_Path = (1 << (height - 1)) - 1 + lf_id;
if (VERBOSE) {
std::cout << "lf_id = " <<std::dec<< lf_id << " Node_in_Path " << Node_in_Path << "\n";
}
bool found = 0;
int level_node_in_path = height - 1;
while (Node_in_Path >= 0)
{
CYCLE_VAL++;
if (VERBOSE)
std::cout << "Node_in_Path= " <<std::dec<< Node_in_Path << "\n";
//starting from leaf go up to the root
for (uint32_t j = 0; j < ORAM_Z; j++)
{
if (VERBOSE)
std::cout << "j= " << j << " bucket[ " <<std::dec<< Node_in_Path * ORAM_Z + j << " ] " <<std::hex<< buckets[Node_in_Path * ORAM_Z + j].addr;
if (level_node_in_path < hight_cached) // if it exists in treetop
{
if (buckets[Node_in_Path * ORAM_Z + j].dummy == 0)
{
// std::cout << "\nbuckets[" <<std::dec<< Node_in_Path * ORAM_Z + j << "]" << "deleted from the cache\n";
my_ONC.delete_node(buckets[Node_in_Path * ORAM_Z + j].addr);
}
}
if (buckets[Node_in_Path * ORAM_Z + j].addr == addr)
{
found =1;
//update_histogram(level_node_in_path, histogram[level_node_in_path]+ 1 );
histogram[level_node_in_path]++;
// if it is a read it should be valid
if (op == 'r')
assert(buckets[Node_in_Path * ORAM_Z + j].dummy != 1);
// this is our target
// change the leaf id and put in the stash
int new_leaf_id = Util::rand_int(num_leaf); //rand()%num_leaf; //rnd(gen);
leaf_id_count[new_leaf_id]++;
stash.push_back(std::make_pair(new_leaf_id,addr)); // (new_leaf_id, addr)
if (VERBOSE)
std::cout << "\n stash size.................. " <<std::dec<< get_size_stash() <<"\n";
pos_map[(addr - OFFSET) / 64] = new_leaf_id;
if (VERBOSE)
std::cout << "--> our target taken to stash " << " new leaf = " << new_leaf_id << "\n";
} else
{
// this is not our target either it is a dummy throw it a way!
// or it is a data put it in the stash
if (buckets[Node_in_Path * ORAM_Z + j].dummy != 1)
{
// data
if (VERBOSE) {
std::cout << "--> not target but data taken to stash \n";
}
// its exact node will go to the stash because it is not
// our target while it is not dummy either
stash.push_back(std::make_pair(buckets[Node_in_Path * ORAM_Z + j].leaf_id, buckets[Node_in_Path * ORAM_Z + j].addr ));
if (STASH_VERBOSE)
std::cout << "+++stash size is " << stash.size()<< "\n";
if (VERBOSE){
std::cout << "stash size.................. " <<std::dec<< get_size_stash() <<"\n";
write_stash();
}
}
else
{
// dummy
if (VERBOSE)
std::cout << "--> dummy \n";
}
}
// whether or not it is dummy, it should be converted to a dummy one because it is evacuated into the stash or deleted
buckets[Node_in_Path * ORAM_Z + j].dummy = 1;
buckets[Node_in_Path * ORAM_Z + j].addr = -1;
buckets[Node_in_Path * ORAM_Z + j].leaf_id = -1;
}// for cache lines per bucket
if (Node_in_Path == 0) break;
Node_in_Path = (Node_in_Path - 1) / 2;
level_node_in_path--;
// go to the parent node up to the root
} // while bucket
if (op == 'r')
{
if (found == 0)
std::cout << " address " <<std::hex<< addr << " not found in read!\n";
assert(found == 1);
}
else
{
if (found == 0)
{
// this was a write into a new address
// we should change its leaf id and store in the stash
int new_leaf_id = Util::rand_int(num_leaf); //rand()%num_leaf; //rnd(gen);
leaf_id_count[new_leaf_id]++;
pos_map[(addr-OFFSET)/64] = new_leaf_id;
stash.push_back(std::make_pair(new_leaf_id, addr));
if (STASH_VERBOSE)
std::cout << "+++stash size is " << stash.size()<< "\n";
if (VERBOSE)
std::cout << " addr = " << addr << " with leaf id = " <<std::dec << new_leaf_id << "--> stash \n";
}
}
// evict appropriate cache lines back to the tree in a same path
if (VERBOSE2)
std::cout << " eviction from stash back to ORAM starting .....\n";
int found_to_evict = 0;
std::list<std::pair<int, long long int>>::iterator it_j;
Node_in_Path = (1 << (height - 1)) - 1 + lf_id;
if (VERBOSE2)
std::cout << " Node_in_Path = " <<std::dec<<Node_in_Path<<" lf_id= "<< lf_id<<"\n";
int lv = 0; //the max is for the root
int level = height - 1; // the maximum level is for leaves
if (!stash.empty()) {
if (VERBOSE2) {
std::cout << "stash is NOT empty ..\n";
write_stash();
}
// this should happen for every node (bucket)
while (Node_in_Path >= 0)
{
CYCLE_VAL++;
// find the correct cachelines belonging to correct leaf that can be located in this bucket
if(VERBOSE2)
{
std::cout << "Node_in path = " <<std::dec<< Node_in_Path << "\n";
}
int num_cache_per_bucket = 0;
it_j = stash.begin();
for (; it_j != stash.end();)
{
if ((it_j->first ^ lf_id) <= ((1<< lv)-1))
//if ((it_j->first >> lv) == (lf_id >> lv))
{
if(VERBOSE2)
std::cout <<std::dec << it_j->first <<" "<< lf_id <<" " << lv <<" "<< ((1<< lv)-1) <<"\n";
if(VERBOSE2)
{
std::cout << "buckets[ " <<std::dec<< Node_in_Path*ORAM_Z+num_cache_per_bucket <<" ]" << "is updated ";
}
// this is good for this node
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].dummy = 0;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].leaf_id = it_j->first;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].addr = it_j->second;
if(VERBOSE2)
{
std::cout << "with " <<std::dec << it_j->first << std::hex <<" "<< it_j->second << "\n";
}
if (level < hight_cached)
{
my_ONC.insert(buckets[Node_in_Path * ORAM_Z + num_cache_per_bucket].addr, Node_in_Path * ORAM_Z + num_cache_per_bucket);
//std::cout << std::dec<< "bucket[" << Node_in_Path * ORAM_Z + num_cache_per_bucket << "] = " <<std::hex <<
// buckets[Node_in_Path * ORAM_Z + num_cache_per_bucket].addr << " inserted in cache \n";
}
it_j = stash.erase(it_j);
num_cache_per_bucket++;
if (num_cache_per_bucket >= ORAM_Z)
break;
}
else ++it_j;
}
if (num_cache_per_bucket < ORAM_Z)
{
// not found enough fit canidates go for dummies!
for (int j=(num_cache_per_bucket); j<ORAM_Z; j++)
{
Node_t tmp;
tmp.dummy = 1;
tmp.addr = -1;
tmp.leaf_id = -1;
buckets[Node_in_Path * ORAM_Z + j] = tmp;
if(VERBOSE2)
{
std::cout << "bucket[ " <<std::dec<< Node_in_Path * ORAM_Z + j << " ] is dummy " << "\n";
}
}
}
/* for (int i=0; i<(1<<lv); i++)
{
int leaf_look = ((1<<lv)*(Node_in_Path - ((1<< level)-1)))+i;
// go to the stash and find the cacheline with this leaf_look
if(VERBOSE)
{
std::cout << "leaf_look = " << leaf_look << "\n";
}
it_j = stash.begin();
for (; it_j != stash.end();)
{
if (it_j->first == leaf_look)
{
if(VERBOSE)
{
std::cout << "buckets[ " << Node_in_Path*ORAM_Z+num_cache_per_bucket <<" ]" << "is updated ";
}
// this cacheline should be evicted from stash to this node.
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].dummy = 0;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].leaf_id = leaf_look;
buckets[Node_in_Path*ORAM_Z+num_cache_per_bucket].addr = it_j->second;
if(VERBOSE)
{
std::cout << "with " <<std::dec << leaf_look << std::hex <<" "<< it_j->second << "\n";
}
// std::cout << " before erase ...\n";
stash.erase(it_j);
if (STASH_VERBOSE)
std::cout << "+++stash size is " << stash.size()<< "\n";
// std::cout << " After erase ...\n";
num_cache_per_bucket++;
if (num_cache_per_bucket == ORAM_Z)
{
// ths node is full
break;
}
}
else ++it_j;
}
if (num_cache_per_bucket ==ORAM_Z )
break;
if ((i == (1<<lv)-1) && (num_cache_per_bucket < ORAM_Z))
{
if(VERBOSE)
{
std::cout << "dummy --> num_cache_per_bucket = " << num_cache_per_bucket << "\n";
}
// we could not find enough non-dummy cache lines go for dummies
if (VERBOSE)
std::cout << "num_cache_per_bucket = " << num_cache_per_bucket << "\n";
for (int j=(num_cache_per_bucket); j<ORAM_Z; j++)
{
Node_t tmp;
tmp.dummy = 1;
tmp.addr = -2;
tmp.leaf_id = -2;
buckets[Node_in_Path * ORAM_Z + j] = tmp;
if(VERBOSE)
{
std::cout << "bucket[ " << Node_in_Path * ORAM_Z + j << " ] is dummy " << "\n";
}
}
}
}*/
if (Node_in_Path == 0) break;
Node_in_Path = (Node_in_Path - 1) / 2;
lv++;
level--;
}
/*int Node_in_Path = (1 << (height - 1)) - 1 + lf_id;
int leveling = height - 1;
data_cache = 0; data_memory = 0;
while (Node_in_Path >= 0)
{
for (int iii=0; iii<ORAM_Z; iii++)
{
if (buckets[Node_in_Path * ORAM_Z + iii].dummy == 0)
{
if (leveling < hight_cached)
data_cache++;
else
data_memory++;
}
}
if (Node_in_Path == 0) break;
Node_in_Path = (Node_in_Path - 1) / 2;
leveling--;
}*/
/*it_j = stash.begin();
while (it_j != stash.end()) {
if (VERBOSE) {
std::cout << "[ " << it_j->second << " ," << it_j->first << " ]" << " is in the stash\n";
}
if (it_j->first == lf_id) {
// this is a good candate for eviction
if (VERBOSE)
std::cout << "inserting into stash_buffer "<<std::dec<< it_j->first <<" "<<std::hex<< it_j->second <<"\n";
stash_buffer.push_back(std::make_pair(it_j->first, it_j->second));
//std::pair<int, long long int> hh = stash_buffer.back();
//std::cout <<" " << hh.first << " " <<hh.second;
stash.erase(it_j);
//std::cout << "stash size.................. " << get_size_stash() <<"\n";
if (VERBOSE) {
std::cout << it_j->second << " added to stash_buufer\n";
}
}
it_j++;
///std::cout << "it_j added \n";
}*/
///std::cout << " Exit from while .. " << stash_buffer.size() <<"\n";
} else if (VERBOSE)
std::cout << "stash is empty ..\n";
/*int num_candidates;
if (stash_buffer.empty()) {
num_candidates = 0;
if (VERBOSE)
std::cout << "stash_buffer is empty ..\n";
} else
num_candidates = stash_buffer.size();
Node_in_Path = (1 << (height - 1)) - 1 + lf_id;
Node_t tmp;
std::pair<int, long long int> pair_tmp;
while (num_candidates > 0 && Node_in_Path >= 0) {