-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprida.cpp
More file actions
285 lines (189 loc) · 7.96 KB
/
prida.cpp
File metadata and controls
285 lines (189 loc) · 7.96 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
#include <iostream>
#include <openssl/rand.h>
#include <stdlib.h>
#include <random>
using namespace std;
int main(){
int numberofReceivers = 2;
std::cout << numberofReceivers << " Receivers" << endl;
int numberofDataOwners = 5;
std::cout << numberofDataOwners << " Data Owners" << endl;
std::cout << "Data Preparation for the Beaver triplets Started" << endl;
// choice vector
int choice_vector[numberofDataOwners][numberofReceivers] = {};
int choice_vector1[numberofDataOwners][numberofReceivers] = {};
int choice_vector2[numberofDataOwners][numberofReceivers] = {};
// sensitive data
int data[numberofDataOwners][numberofReceivers] = {};
int data1[numberofDataOwners][numberofReceivers] = {};
int data2[numberofDataOwners][numberofReceivers] = {};
// random data for Beaver's triplets
int alpha[numberofDataOwners][numberofReceivers] = {};
int alpha1[numberofDataOwners][numberofReceivers] = {};
int alpha2[numberofDataOwners][numberofReceivers] = {};
int beta[numberofDataOwners][numberofReceivers] = {};
int beta1[numberofDataOwners][numberofReceivers] = {};
int beta2[numberofDataOwners][numberofReceivers] = {};
int gamma[numberofDataOwners][numberofReceivers] = {};
int gamma1[numberofDataOwners][numberofReceivers] = {};
int gamma2[numberofDataOwners][numberofReceivers] = {};
int epsilon[numberofDataOwners][numberofReceivers] = {};
int epsilon1[numberofDataOwners][numberofReceivers] = {};
int epsilon2[numberofDataOwners][numberofReceivers] = {};
int delta[numberofDataOwners][numberofReceivers] = {};
int delta1[numberofDataOwners][numberofReceivers] = {};
int delta2[numberofDataOwners][numberofReceivers] = {};
int partialAdd[numberofDataOwners][numberofReceivers] = {};
int partialAdd1[numberofDataOwners][numberofReceivers] = {};
int partialAdd2[numberofDataOwners][numberofReceivers] = {};
int result[numberofDataOwners][numberofReceivers] = {};
int Modulus = 32771; //32771>2^15 // Modulus should be a prime number
// for random numbers
std::random_device rd;
std::uniform_int_distribution<long> dist;
for(int i=0; i<numberofDataOwners; i++){
for(int j=0; j<numberofReceivers; j++){
// choosing Receivers for each DO
choice_vector[i][j] = dist(rd) % 2;
//std::cout << "random number for choice: " << choice_vector[i][j] << endl;
choice_vector1[i][j] = dist(rd) % Modulus;
//std::cout << "random share 1 for choice: " << choice_vector1[i][j] << endl;
choice_vector2[i][j] = choice_vector[i][j] - choice_vector1[i][j];
//std::cout << "random share 2 for choice: " << choice_vector2[i][j] << endl;
// data of each DO
if(choice_vector[i][j] == 1)
data[i][j] = 36;
else
data[i][j] = dist(rd) % Modulus;
//std::cout << "data: " << i << j << "\t" << data[i][j] << endl;
data1[i][j] = dist(rd) % Modulus;
//std::cout << "random share 1 of data: " << data1[i][j] << endl;
data2[i][j] = data[i][j] - data1[i][j];
//std::cout << "random share 1 of data: " << data2[i][j] << endl;
// generating alpha
alpha[i][j] = dist(rd) % Modulus;
//std::cout << "alpha: " << alpha[i][j] << endl;
alpha1[i][j] = dist(rd) % Modulus;
//std::cout << "random share 1 of alpha: " << alpha1[i][j] << endl;
alpha2[i][j] = alpha[i][j] - alpha1[i][j];
//std::cout << "random share 1 of alpha: " << alpha2[i][j] << endl;
// generating beta
beta[i][j] = dist(rd) % Modulus;
//std::cout << "beta: " << beta[i][j] << endl;
beta1[i][j] = dist(rd) % Modulus;
//std::cout << "random share 1 of beta: " << beta1[i][j] << endl;
beta2[i][j] = beta[i][j] - beta1[i][j];
//std::cout << "random share 1 of beta: " << beta2[i][j] << endl;
//constracting gamma and its shares
gamma[i][j] = (alpha[i][j]) * (beta[i][j]);
//std::cout << "gamma: " << gamma[i][j] << endl;
gamma1[i][j] = dist(rd) % Modulus;
//std::cout << "random share 1 of gamma: " << gamma1[i][j] << endl;
gamma2[i][j] = gamma[i][j] - gamma1[i][j];
//std::cout << "random share 1 of gamma: " << gamma2[i][j] << endl;
}
}
std::cout << "Data Preparation for the Beaver triplets Done" << endl;
std::cout << "Learning authorised Receivers Started" << endl;
int authorisedRj[numberofReceivers] = {};
int authorisedRj1[numberofReceivers] = {};
int authorisedRj2[numberofReceivers] = {};
// Agg - the addition of shared choice vector1
for (int j = 0; j < numberofReceivers; ++j){
for (int i = 0; i < numberofDataOwners; ++i){
authorisedRj1[j] += choice_vector1[i][j];
}
}
// Dec - the addition of shared choice vector2
for (int j = 0; j < numberofReceivers; ++j){
for (int i = 0; i < numberofDataOwners; ++i){
authorisedRj2[j] += choice_vector2[i][j];
}
}
std::cout << "Learning authorised Receivers by Agg and Dec Started" << endl;
// Agg and Dec compute the number of DOs for Rj
for (int j = 0; j < numberofReceivers; ++j){
authorisedRj[j] = authorisedRj1[j] + authorisedRj2[j];
}
int t = 0;
int min = authorisedRj[0];
int max = authorisedRj[0];
// automatic deciding t
for(int i=1; i<numberofReceivers; i++)
{
// If current element is greater than max
if(authorisedRj[i] > max)
max = authorisedRj[i];
// If current element is smaller than min
if(authorisedRj[i] < min)
min = authorisedRj[i];
}
t = (min + max) / 2;
//std::cout << "min t: " << min << endl;
//std::cout << "max t: " << max << endl;
std::cout << "threshold t:"<< "\t" << t << endl;
int dataAggforAuthorisedRj[numberofReceivers] = {};
for (int i = 0; i < numberofReceivers; ++i)
{
if (authorisedRj[i] >= t)
{
dataAggforAuthorisedRj[i] = 1;
//std:: cout << " Authorised R[" << i <<"] has " << authorisedRj[i] << " votes" << endl;
}
}
std::cout << "Learning authorised Receivers Done" << endl;
std::cout << "Aggregation Started" << endl;
for (int i = 0; i < numberofDataOwners; ++i){
for (int j = 0; j < numberofReceivers; ++j){
if (dataAggforAuthorisedRj[j]==1){
////////////////
//by Agg
////////////////
epsilon1[i][j]= data1[i][j] + alpha1[i][j];
delta1[i][j] = choice_vector1[i][j] + beta1[i][j];
////////////////
// by Dec
////////////////
epsilon2[i][j]= data2[i][j] + alpha2[i][j];
delta2[i][j] = choice_vector2[i][j] + beta2[i][j];
////////////////
// both
////////////////
epsilon[i][j] = epsilon1[i][j] + epsilon2[i][j];
delta[i][j] = delta1[i][j] + delta2[i][j];
////////////////
//by Agg
////////////////
partialAdd1[i][j] = gamma1[i][j] + epsilon[i][j]*choice_vector1[i][j]+delta[i][j]*data1[i][j];
////////////////
// by Dec
////////////////
partialAdd2[i][j] = gamma2[i][j] + epsilon[i][j]*choice_vector2[i][j]+delta[i][j]*data2[i][j];
////////////////
// by Dec
////////////////
partialAdd[i][j] = partialAdd1[i][j] + partialAdd2[i][j];
result[i][j] = partialAdd[i][j] - epsilon[i][j]*delta[i][j];
}
}
}
int AggforRj[numberofReceivers] = {0};
for (int i = 0; i < numberofReceivers; ++i){
//std::cout << "error inside for1" << endl;
if (dataAggforAuthorisedRj[i]==1){
//std::cout << "error inside if" << endl;
for (int j = 0; j < numberofDataOwners; ++j){
//std::cout << "error inside for2" << endl;
AggforRj[i] += result[j][i];
//std::cout << "error"<< i << j << endl;
}
}
}
for (int i = 0; i < numberofReceivers; ++i){
if (dataAggforAuthorisedRj[i]==1){
std::cout << "Authorised R["<< i <<"] has " << authorisedRj[i] << " votes. Resulting Data Aggregation is:" << "\t" << AggforRj[i] % Modulus << endl;
}
}
std::cout << "Aggregation Done" << endl;
return 0;
}