-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
208 lines (167 loc) · 7.69 KB
/
main.cpp
File metadata and controls
208 lines (167 loc) · 7.69 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
#include "FHE.h"
#include <time.h>
#include "EncryptedArray.h"
int main(int argc, char **argv)
{
/*** BEGIN INITIALIZATION ***/
clock_t tStart = clock();
long m = 0; // Specific modulus
long p = 1021; // Plaintext base [default=2], should be a prime number
long r = 1; // Lifting [default=1]
long L = 16; // Number of levels in the modulus chain [default=heuristic]
long c = 3; // Number of columns in key-switching matrix [default=2]
long w = 64; // Hamming weight of secret key
long d = 0; // Degree of the field extension [default=1]
long k = 128; // Security parameter [default=80]
long s = 0; // Minimum number of slots [default=0]
string output;
std::cout << "Finding m... " << std::flush;
m = FindM(k, L, c, p, d, s, 0); // Find a value for m given the specified values
std::cout << "m = " << m << std::endl;
std::cout << "Initializing context... " << std::flush;
FHEcontext context(m, p, r); // Initialize context
buildModChain(context, L, c); // Modify the context, adding primes to the modulus chain
std::cout << "OK!" << std::endl;
std::cout << "Creating polynomial... " << std::flush;
ZZX G = context.alMod.getFactorsOverZZ()[0]; // Creates the polynomial used to encrypt the data
std::cout << "OK!" << std::endl;
std::cout << "Generating keys... " << std::flush;
FHESecKey secretKey(context); // Construct a secret key structure
const FHEPubKey& publicKey = secretKey; // An "upcast": FHESecKey is a subclass of FHEPubKey
secretKey.GenSecKey(w); // Actually generate a secret key with Hamming weight w
std::cout << "OK!" << std::endl;
/*** END INITIALIZATION ***/
printf("Time taken for initialization: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
tStart = clock();
Ctxt ctx1(publicKey); // Initialize the first ciphertext (ctx1) using publicKey
Ctxt ctx2(publicKey); // Initialize the first ciphertext (ctx2) using publicKey
Ctxt ctx3(publicKey);
Ctxt ctx4(publicKey);
int sumvalue = 4;
int multiplicator = 5;
publicKey.Encrypt(ctx1, to_ZZX(multiplicator)); // Encrypt the value 5
publicKey.Encrypt(ctx2, to_ZZX(sumvalue)); // Encrypt the value 4
publicKey.Encrypt(ctx3, to_ZZX(sumvalue));
publicKey.Encrypt(ctx4, to_ZZX(sumvalue));
Ctxt ctSum = ctx1; // Create a ciphertext to hold the sum and initialize it with Enc(2)
ZZX ptSum; // Create a plaintext to hold the plaintext of the sum
printf("Time taken for creating ciphertext: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
output = to_string(multiplicator)+" * (";
for (int i= 0;i<5;i++)
{
tStart = clock();
ctSum = ctx1;
ctSum *= ctx3; // Perform Enc(5) * (Enc(4) + Enc(4) + Enc(4) + ...)
ctx3+=ctx2;
if (i>0) output+= " + ";
output += to_string(sumvalue);
std::cout << "Perform Enc(5) * (Enc(4) + Enc(4) + Enc(4) + ...) => " << output << ") "<< std::endl;
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
secretKey.Decrypt(ptSum, ctSum); // Decrypt the ciphertext ctSum into the plaintext ptSum using secretKey
std::cout << "Result: " << ptSum[0] << std::endl;
}
ctSum = ctx1;
ctx3 = ctx4;
output = to_string(multiplicator)+" * ";
for (int i= 0;i<3;i++)
{
tStart = clock();
ctSum = ctx1;
ctSum *= ctx3; // Perform Enc(5) * Enc(4) * Enc(4) * ..
ctx3*=ctx4;
if (i>0) output+= " * ";
output += to_string(sumvalue);
std::cout << "Perform Enc(5) * Enc(4) * Enc(4) * ...=> " << output << " "<< std::endl;
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
secretKey.Decrypt(ptSum, ctSum); // Decrypt the ciphertext ctSum into the plaintext ptSum using secretKey
std::cout << "Result: " << ptSum[0] << std::endl;
}
ctSum = ctx1;
ctx3 = ctx4;
output = "Using multByConstant "+ to_string(multiplicator)+" * ";
for (int i= 0;i<3;i++)
{
tStart = clock();
ctSum = ctx1;
ctSum *= ctx3;
ctx3.multByConstant(to_ZZX(4)); // Perform Enc(5) * Enc(4) * Enc(4) * ...
if (i>0) output+= " * ";
output += to_string(sumvalue);
std::cout << output << " "<< std::endl;
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
secretKey.Decrypt(ptSum, ctSum); // Decrypt the ciphertext ctSum into the plaintext ptSum using secretKey
std::cout << "Result: " << ptSum[0] << std::endl;
}
ctSum = ctx1;
ctx3 = ctx4;
// using reLineration
output = "Using relineration "+ to_string(multiplicator)+" * ";
for (int i= 0;i<3;i++)
{
tStart = clock();
ctSum = ctx1;
ctSum.multiplyBy(ctx3);
ctx3.multiplyBy(ctx4);
if (i>0) output+= " * ";
output += to_string(sumvalue);
std::cout << output << " "<< std::endl;
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
secretKey.Decrypt(ptSum, ctSum); // Decrypt the ciphertext ctSum into the plaintext ptSum using secretKey
std::cout << "Result: " << ptSum[0] << std::endl;
}
// Sample code: Pack into coefficients
tStart = clock();
long v[4] = {1,2,3,4};
long u[4] = {1,2,3,4};
ZZX V, U;
V.SetLength(4);
U.SetLength(4);
for (int i = 0; i<4;i++) {
SetCoeff(V, i, v[i]);
SetCoeff(U, 3 - i, u[i]);
}
// V = 1 + 2x + 3x^2 + 4x^3
// U = 4 + 3x + 2x^3 + 1x^3
printf("\nPack into coefficients.\nV = 1 + 2x + 3x^2 + 4x^3\nU = 4 + 3x + 2x^3 + 1x^3\n" );
Ctxt encV(publicKey), encU(publicKey);
publicKey.Encrypt(encV, V);
publicKey.Encrypt(encU, U);
// encV * encU;
printf("Pack into coefficients. Time taken for encryption: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
encV.multiplyBy(encU);
ZZX result;
printf("Pack into coefficients. Time taken for calculation:\nencV * encU: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
secretKey.Decrypt(result,encV);
printf("Pack into coefficients. Time taken for decryption: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
cout << "Result " << result[2] << std::endl; // 3rd coeff, 30 mod p^r
// Codes for CRT-packing
std::vector<long> u1 = {1, 2, 3, 4};
std::vector<long> v1 = {4, 3, 2, 1};
//ZZX F = context.alMod.getFactorsOverZZ()[0];
EncryptedArray ea(context, G);
//Ctxt encvU(publicKey);
//ZZX ZV, ZU;
//ea.encode (ZV, v1); ea.encode(ZU, u1);
// V = ??, U = ??
//publicKey.Encrypt(encV, Z); publicKey.Encrypt(encU, Z);
//ea.encrypt(encV, publicKey, v1); ea.encrypt(encU, publicKey, u1);
tStart = clock();
encV *= encU;
printf("CRT packing. Time taken for calculation:\nencV * encU: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
//ZZX result;
secretKey.Decrypt(result, encV); // result = ??
std::vector<long> decoded;
ea.decode(decoded, result); // decoded = {4, 6, 6, 4};
//ea.decode(ea, decoded, secretKey, encV);
cout << "Result: " << decoded << std::endl;
// Sample codes for other HELib routines
//Ctxt encU(publicKey);
/**
ea.encrypt(encU, publicKey, u1);
ea.rotate(encU, 1);
ea.rotate(encU, -2);
ea.shift(encU, 1);
runningSums(ea, encU);
totalSums(ea, encU);**/
return 0;
}