-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
179 lines (163 loc) · 4.5 KB
/
test.c
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
#include "bgn.h"
#include <string>
void RandMat(mat_ZZ & mat, ZZ upper) {
mat[0][0] = RandomBnd(upper);
/*for (int i = 0; i < mat.NumRows(); i++) {
for (int j = 0; j < mat.NumCols(); j++) {
mat[i][j] = RandomBnd(upper);
}
}*/
}
void Check(mat_ZZ & one, mat_ZZ & two, string name) {
if (one == two) {
cout << "YAY!!!" << endl;
} else {
cout << name << " DOESN'T MATCH ======================" << endl;
cout << "orig: " << endl << one << endl;
cout << "decr: " << endl << two << endl;
}
}
// encrypt
void EncryptAll(mat_ZZ enc_arr[], ZZ arr[], int num) {
mat_ZZ pt;
pt.SetDims(m, 1);
for (int i = 0; i < num; i++) {
pt[0][0] = arr[i];
Encrypt(enc_arr[i], pt);
}
}
// calculate sum
void CalculateSum(mat_ZZ & sum, mat_ZZ enc_arr[], int num) {
if (num == 0) {
return;
}
sum.SetDims(m, 1);
for (int i = 0; i < num; i++) {
Sum(sum, sum, enc_arr[i]);
}
}
// calculate variance
void CalculateVar(mat_ZZ & var, mat_ZZ enc_arr[], int num) {
if (num == 0) {
return;
}
mat_ZZ sq_of_sum;
sq_of_sum.SetDims(m, 1);
mat_ZZ sum_of_sq;
sum_of_sq.SetDims(m, m);
mat_ZZ temp;
for (int i = 0; i < num; i++) {
Sum(sq_of_sum, sq_of_sum, enc_arr[i]);
Multiply(temp, enc_arr[i], enc_arr[i]);
Sum(sum_of_sq, sum_of_sq, temp);
}
Multiply(sq_of_sum, sq_of_sum, sq_of_sum);
mat_ZZ n_sum_of_sq;
n_sum_of_sq = num * sum_of_sq;
for (int i = 0; i < n_sum_of_sq.NumRows(); i++) {
for (int j = 0; j < n_sum_of_sq.NumCols(); j++) {
n_sum_of_sq[i][j] %= q;
}
}
mat_ZZ dec_n_sum_of_sq;
mat_ZZ dec_sum_of_sq;
mat_ZZ dec_sq_of_sum;
cout << "- decrypting sum_of_sq: ";
//cout << sum_of_sq << endl;
DecryptProd(dec_sum_of_sq, sum_of_sq);
cout << dec_sum_of_sq[0][0] << endl;
cout << "- decrypting num* sum_of_sq: ";
//cout << n_sum_of_sq << endl;
DecryptProd(dec_n_sum_of_sq, n_sum_of_sq);
cout << dec_n_sum_of_sq[0][0] << endl;
cout << "- decrypting sq_of_sum: ";
DecryptProd(dec_sq_of_sum, sq_of_sum);
cout << dec_sq_of_sum[0][0] << endl;
var = num*sum_of_sq - sq_of_sum;
}
int main()
{
Init();
GenerateKeys();
int num = 100;
ZZ upper = to_ZZ(sqrt(to_RR(num_Records/num)))/num;
ZZ arr[num];
for (int i = 0; i < num; i++) {
arr[i] = RandomBnd(upper);
}
mat_ZZ enc_arr[num];
// encrcypt
cout << "Encrypting" << endl;
EncryptAll(enc_arr, arr, num);
// calculate sum
cout << "Sum" << endl;
ZZ sum;
for (int i = 0; i < num; i++) {
sum += arr[i];
}
cout << "real sum = " << sum << endl;
mat_ZZ enc_sum;
mat_ZZ dec_sum;
cout << "- calculating sum" << endl;
CalculateSum(enc_sum, enc_arr, num);
cout << "- decrypting sum" << endl;
Decrypt(dec_sum, enc_sum);
cout << "encr sum = " << dec_sum[0][0] << endl;
// calculate variance
cout << "num^2 * Var" << endl;
ZZ sum_of_sq, sq_of_sum, var;
for (int i = 0; i < num; i++) {
sq_of_sum += arr[i];
sum_of_sq += arr[i]*arr[i];
}
sq_of_sum *= sq_of_sum;
var = num*sum_of_sq - sq_of_sum;
cout << "real num^2 * var = num * " << sum_of_sq << " - " << sq_of_sum << " = " << var << endl;
mat_ZZ enc_var;
mat_ZZ dec_var;
cout << "- calculating var" << endl;
CalculateVar(enc_var, enc_arr, num);
cout << "- decrypting var" << endl;
DecryptProd(dec_var, enc_var);
cout << "encr var = " << dec_var[0][0] << endl;
mat_ZZ pt1, pt2, pt_sum, pt_prod;
mat_ZZ ct1, ct2, ct_sum, ct_prod;
mat_ZZ pt1_check, pt2_check, pt_sum_check, pt_prod_check;
pt1.SetDims(m, 1);
pt2.SetDims(m, 1);
pt_sum.SetDims(m, 1);
pt_prod.SetDims(m, m);
ct1.SetDims(m, 1);
ct2.SetDims(m, 1);
ct_sum.SetDims(m, 1);
ct_prod.SetDims(m, m);
pt1_check.SetDims(m, 1);
pt2_check.SetDims(m, 1);
pt_sum_check.SetDims(m, 1);
pt_prod_check.SetDims(m, m);
RandMat(pt1, to_ZZ(sqrt(to_RR(num_Records))));
RandMat(pt2, to_ZZ(sqrt(to_RR(num_Records))));
pt_sum = pt1 + pt2;
pt_prod = pt1 * transpose(pt2);
cout << "Encrypting" << endl;
Encrypt(ct1, pt1);
Encrypt(ct2, pt2);
cout << "Summing" << endl;
Sum(ct_sum, pt1, pt2);
cout << "Multiplying" << endl;
Multiply(ct_prod, pt1, pt2);
cout << "Decrypting pt1" << endl;
Decrypt(pt1_check, ct1);
cout << "Decrypting pt2" << endl;
Decrypt(pt2_check, ct2);
cout << "Decrypting sum" << endl;
Decrypt(pt_sum_check, ct_sum);
cout << "Decrypting prod" << endl;
DecryptProd(pt_prod_check, ct_prod);
cout << "Checking" << endl;
Check(pt1, pt1_check, "pt1");
Check(pt2, pt2_check, "pt2");
Check(pt_sum, pt_sum_check, "sum");
Check(pt_prod, pt_prod_check, "sum");
return 1;
}