forked from homenc/HElib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Replicate.cpp
More file actions
268 lines (197 loc) · 6.08 KB
/
Test_Replicate.cpp
File metadata and controls
268 lines (197 loc) · 6.08 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
/* Copyright (C) 2012,2013 IBM Corp.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "FHE.h"
#include "replicate.h"
#include "timing.h"
#include <NTL/lzz_pXFactoring.h>
#include <cassert>
NTL_CLIENT
// k=10 p=5 r=2 -- 3 dimensions: 12, 3, !2
// p=11 -- 2 dimensions: 16, !4
// Test_General_x p=23 s=100 z=20 ## a large, realistic example, 48 x 4
// Test_General_x p=2 r=4 k=10 z=10 m=4369 ## 16 x !16
// Test_General_x p=2 r=4 k=10 z=10 m=1247 ## 42
// Test_General_x p=2 r=4 k=10 z=10 m=8191 ## 630
// Test_General_x p=2 r=4 k=10 z=10 m=3133 ## 60 x !2
ZZX makeIrredPoly(long p, long d)
{
assert(d >= 1);
assert(ProbPrime(p));
if (d == 1) return ZZX(1, 1); // the monomial X
zz_pBak bak; bak.save();
zz_p::init(p);
return to_ZZX(BuildIrred_zz_pX(d));
}
class StopReplicate { };
class ReplicateTester : public ReplicateHandler {
public:
const FHESecKey& sKey;
const EncryptedArray& ea;
const PlaintextArray& pa;
long M;
double t_last, t_total;
long pos;
ReplicateTester(const FHESecKey& _sKey, const EncryptedArray& _ea,
const PlaintextArray& _pa, long _M)
: sKey(_sKey), ea(_ea), pa(_pa), M(_M)
{
t_last = GetTime();
t_total = 0.0;
pos = 0;
}
virtual void handle(const Ctxt& ctxt) {
double t_new = GetTime();
double t_elapsed = t_new - t_last;
t_total += t_elapsed;
cerr << "*** " << pos << " t=" << t_elapsed
<< ", t_total=" << t_total
<< ", level=" << ctxt.getLevel()
<< ", log(noise/modulus)~" << ctxt.log_of_ratio()
<< "\n";
PlaintextArray pa1 = pa;
pa1.replicate(pos);
PlaintextArray pa2(ea);
ea.decrypt(ctxt, sKey, pa2);
if (!pa1.equals(pa2)) {
cerr << "error:\n";
pa2.print(cerr); cerr << "\n";
}
t_last = GetTime();
pos++;
if (M > 0 && pos >= M) throw StopReplicate();
}
};
void TestIt(long p, long r, long d, long c, long k, long w,
long L, long m, long bnd, long M, long v)
{
cerr << "*** TestIt: "
<< " p=" << p
<< ", r=" << r
<< ", d=" << d
<< ", c=" << c
<< ", k=" << k
<< ", w=" << w
<< ", L=" << L
<< ", m=" << m
<< ", bnd=" << bnd
<< ", M=" << bnd
<< ", v=" << v
<< endl;
FHEcontext context(m, p, r);
buildModChain(context, L, c);
context.zMStar.printout();
cerr << endl;
FHESecKey secretKey(context);
const FHEPubKey& publicKey = secretKey;
secretKey.GenSecKey(w); // A Hamming-weight-w secret key
ZZX G;
if (d == 0)
G = context.alMod.getFactorsOverZZ()[0];
else
G = makeIrredPoly(p, d);
cerr << "G = " << G << "\n";
cerr << "generating key-switching matrices... ";
addSome1DMatrices(secretKey); // compute key-switching matrices that we need
cerr << "done\n";
printAllTimers();
resetAllTimers();
cerr << "computing masks and tables for rotation...";
EncryptedArray ea(context, G);
cerr << "done\n";
PlaintextArray xp0(ea), xp1(ea);
xp0.random();
xp1.random();
Ctxt xc0(publicKey);
ea.encrypt(xc0, publicKey, xp0);
ZZX poly_xp1;
ea.encode(poly_xp1, xp1);
double t;
cerr << "multiplication test:\n";
t = GetTime();
for (long i = 0; i < ea.size(); i++) {
Ctxt xc2 = xc0;
xc2.multByConstant(poly_xp1);
}
t = GetTime()-t;
cerr << "time = " << t << "\n";
Ctxt xc1 = xc0;
CheckCtxt(xc1, "before replicate");
t = GetTime();
replicate(ea, xc1, ea.size()/2);
t = GetTime()-t;
CheckCtxt(xc1, "after replicate");
cerr << "time: " << t << "\n";
replicateVerboseFlag = v;
ReplicateHandler *handler = new ReplicateTester(secretKey, ea, xp0, M);
try {
replicateAll(ea, xc0, handler, bnd);
}
catch (StopReplicate) {
}
delete handler;
}
void usage(char *prog)
{
cerr << "Usage: "<<prog<<" [ optional parameters ]...\n";
cerr << " p is the plaintext base [default=2]" << endl;
cerr << " r is the lifting [default=1]" << endl;
cerr << " d is the degree of the field extension [default==1]\n";
cerr << " (d == 0 => factors[0] defined the extension)\n";
cerr << " c is number of columns in the key-switching matrices [default=2]\n";
cerr << " k is the security parameter [default=80]\n";
cerr << " z is the # of primes [default=4]\n";
cerr << " s is the minimum number of slots [default=4]\n";
cerr << " m is a specific modulus\n";
cerr << " bnd is a recursion bound for replication\n";
cerr << " M is a bound for # of replications [default=0 => all]\n";
cerr << " v for verbose [default=0]\n";
exit(0);
}
int main(int argc, char *argv[])
{
argmap_t argmap;
argmap["p"] = "2";
argmap["r"] = "1";
argmap["d"] = "1";
argmap["c"] = "2";
argmap["k"] = "80";
argmap["z"] = "4";
argmap["s"] = "0";
argmap["m"] = "0";
argmap["bnd"] = "64";
argmap["M"] = "0";
argmap["v"] = "0";
if (!parseArgs(argc, argv, argmap)) usage(argv[0]);
long p = atoi(argmap["p"]);
long r = atoi(argmap["r"]);
long d = atoi(argmap["d"]);
long c = atoi(argmap["c"]);
long k = atoi(argmap["k"]);
long z = atoi(argmap["z"]);
long s = atoi(argmap["s"]);
long chosen_m = atoi(argmap["m"]);
long bnd = atoi(argmap["bnd"]);
long M = atoi(argmap["M"]);
long v = atoi(argmap["v"]);
long w = 64; // Hamming weight of secret key
long L = z; // number of levels
long m = FindM(k, L, c, p, d, s, chosen_m, true);
setTimersOn();
TestIt(p, r, d, c, k, w, L, m, bnd, M, v);
cerr << endl;
printAllTimers();
cerr << endl;
}