This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTDMatrixParams.cpp
290 lines (241 loc) · 8.79 KB
/
TDMatrixParams.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
/* Copyright (C) 2017 IBM Corp.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
/* TDMatrixParams.cpp - Parameters for trapdoor sampling
*/
#include <stdexcept>
#include <NTL/vector.h>
#include "utils/tools.h"
#include "utils/timing.h"
#include "TDMatrixParams.h"
#include <stdexcept>
#include <sys/stat.h>
#include <unistd.h>
//#define DEBUGPRINT
//#define DEBUG
NTL_CLIENT
// A static table of small co-prime factors (6-7.5 bits)
long TDMatrixParams::smallFactors[TDMATRIX_NUM_SMALL_FACTORS]
= { TDMATRIX_SMALL_FACTORS }; // TDMATRIX_SMALL_FACTORS list in TDMatrix.h
// Calculate the sigmaX value. This value must be large enough so
// that sigmaX*I - r*maxFactor * (R/I)*(R^t|I) is positive definite.
// We compute an upper bound s on the singular values of R, then set
// sigmaX = r*maxFactor * s^2.
long getsigmaXVal(long wLen, long mBar, long maxFactor, long r)
{
double s = r*(sqrt(wLen)+sqrt(mBar)+6);
double sigmaZ = r*maxFactor;
long val = ceil(sigmaZ*sigmaZ * s*s);
#ifdef DEBUG
cout << "sigmaX val=" << val << endl;
#endif // DEBUG
return val;
}
// lower bound the dimension to get sec bits of security
long mBound(long qBits, long errBits, long sec)
{
return ceil((qBits-errBits)*(sec+110)/7.2);
}
long mbarBound(long qBits, long n, long sec)
{
return ceil((2+sqrt(sec))*sqrt(n*qBits));
}
long get_qBits(long kFactors, long e)
{
assert(kFactors>0 && e>0);
if (kFactors > TDMATRIX_NUM_SMALL_FACTORS)
kFactors = TDMATRIX_NUM_SMALL_FACTORS;
double logProd = log(TDMatrixParams::smallFactors[0]);
for (long i=1; i<kFactors; i++)
logProd += log(TDMatrixParams::smallFactors[i]);
return ceil(logProd*e/log(2.0));
}
// For each factor f_i, initialize zz_p context for F_i=f_i^e, and also
// compute f_i^{-1} mod F_j for all i<j. Returns max the largest factor
static long setFactors(mat_zz_p& fInv, Vec<zz_pContext>& zzp_context,
const vec_l factors, long e)
{
FHE_TIMER_START;
long kFactors = factors.length();
// For each factor f_i, initialize zz_p context for F_i=f_i^e,
// and also compute f_i^{-1} mod F_j for all i<j
zzp_context.SetLength(kFactors);
fInv.SetDims(kFactors,kFactors);
NTL::zz_pPush ppush; // backup NTL's current modulus
long maxFactor = 0;
for (long j=kFactors-1; j>=0; j--)
{
if (factors[j]>maxFactor) maxFactor = factors[j];
long f2e = NTL::power_long(factors[j],e);
// FIXME: check that this fits in a single-precision integer
NTL::zz_p::init(f2e);
zzp_context[j].save(); // save the current zz_p::modulus()
for (long i=j-1; i>=0; i--)
{
fInv[i][j] = NTL::inv(conv<zz_p>(factors[i])); // f_i^{-1} mod F_j
}
}
return maxFactor;
}
// Initialize the parameters for a modulus q with kk factors,
// each factor is an e-th power of an even smaller number.
void TDMatrixParams::init(long nn, long kk, long ee, long mm, long rr)
{
FHE_TIMER_START;
assert(kk <= TDMATRIX_NUM_SMALL_FACTORS);
factors.SetLength(kk);
for (long i=0; i<kk; i++)
factors[i] = smallFactors[i];
kFactors = kk;
e = ee;
r = rr;
n = nn;
wLen = nn*kk*ee;
if (mm > 0) { // caller supplied a value for m
m = mm;
mBar = m - wLen;
if (mBar < nn) { // check that mBar is not too tiny
mBar = nn;
m = mBar + wLen;
}
}
else { // compute m, mBar using security formulas
long qBits = get_qBits(kk,ee);
m = mBound(qBits, /*errBits=*/7, /*sec=*/80);
mBar = mbarBound(qBits, n, /*sec=*/80);
if (m < mBar+wLen) m = mBar+wLen;
else mBar = m - wLen;
}
// For each factor f_i, initialize zz_p context for F_i=f_i^e,
// and also compute f_i^{-1} mod F_j for all i<j
maxFactor = setFactors(fInv, zzp_context, factors, e);
sigmaX = getsigmaXVal(wLen, mBar, maxFactor, r);//ceil(2*r*r*r*m*maxFactor);
// The Gaussian parameter from which we can sample with a trapdoor
//initialize stash
stash.SetLength(kFactors);
for (long i = 0; i < kFactors; i++)
stash[i].init(factors[i]);
cout << "TDMatrixParams::init: sigmaX="<<sigmaX<<", mBar="<<mBar;
cout << ", |q|="<<NTL::NumBits(this->getQ())<<endl;
}
//returns the produce of all the factors to the power of e, output = product(factors)^e
ZZ TDMatrixParams::getQ() const
{
FHE_TIMER_START;
ZZ q = to_ZZ(1L);
for (long i=0; i<factors.length(); i++) // product of factors
q *= factors[i];
return NTL::power(q,e); // return product^e
}
//outputs the different parameters in the class p into the stream s
ostream& operator<<(ostream &s, const TDMatrixParams& p)
{
s << "[" << p.n << " "
<< p.m << " "
<< p.mBar << " "
<< p.e << " "
<< p.r << "\n "
<< p.factors << "]";
return s;
}
//This function gets an input streams and sets the different parameters in p from this stream
istream& operator>>(istream &s, TDMatrixParams& p)
{
seekPastChar(s, '['); // this function is defined in tools.cpp
s >> p.n;
s >> p.m;
s >> p.mBar;
s >> p.e;
s >> p.r;
s >> p.factors;
p.wLen = p.n * p.kFactors * p.e;
p.maxFactor = setFactors(p.fInv, p.zzp_context, p.factors, p.e);
p.sigmaX = getsigmaXVal(p.wLen, p.mBar, p.maxFactor, p.r);
seekPastChar(s, ']'); // this function is defined in tools.cpp
return s;
}
// binary I/O - write all the variables to the file. The handle of the open file is an input to the function The function returns the number of items written
long TDMatrixParams::writeToFile(FILE* handle) const
{
FHE_TIMER_START;
#ifdef DEBUGPRINT
char cwd[1024];
getcwd(cwd, sizeof(cwd));
cout << "write TDMatrixParams, current directory = " << cwd << endl;
#endif
long count = fwrite(&n, sizeof(n), 1, handle);
count += fwrite(&m, sizeof(m), 1, handle);
count += fwrite(&mBar, sizeof(mBar), 1, handle);
count += fwrite(&e, sizeof(e), 1, handle);
count += fwrite(&r, sizeof(r), 1, handle);
count += fwrite(&kFactors, sizeof(kFactors), 1, handle);
count += fwrite(factors.elts(), sizeof(long), factors.length(), handle);
return count;
}
//Binary IO - reads the class parameters from the file. The open file handle
//is provided as input to the function, and the number of items read is
//returned by the function.
long TDMatrixParams::readFromFile(FILE* handle)
{
FHE_TIMER_START;
long count = 0;
#ifdef DEBUGPRINT
char cwd[1024];
getcwd(cwd, sizeof(cwd));
cout << "read TDMatrixParams from current directory = " << cwd << endl;
#endif
count += fread(&n, sizeof(n), 1, handle);
count += fread(&m, sizeof(m), 1, handle);
count += fread(&mBar, sizeof(mBar), 1, handle);
count += fread(&e, sizeof(e), 1, handle);
count += fread(&r, sizeof(r), 1, handle);
count += fread(&kFactors, sizeof(kFactors), 1, handle);
factors.SetLength(kFactors);
count += fread(factors.elts(), sizeof(long), factors.length(), handle);
//initialize stash
stash.SetLength(kFactors);
for (long i = 0; i < kFactors; i++)
stash[i].init(factors[i]);
wLen = n * kFactors * e;
maxFactor = setFactors(fInv, zzp_context, factors, e);
sigmaX = getsigmaXVal(wLen, mBar, maxFactor, r);
return count;
}
//check if all the variables in the two classes are equal if Yes, return true. Else, false.
bool operator==(const TDMatrixParams& p, const TDMatrixParams& q)
{
return (p.n == q.n && p.m == q.m && p.mBar == q.mBar && p.e == q.e
&& p.r == q.r && p.wLen == q.wLen && p.kFactors == q.kFactors
&& p.factors == q.factors);
}
#if 0
// Deprecated, kept for debuging purposes
TDMatrixParams::TDMatrixParams(vec_l &vFactors, long lm,long rSigma,long llogQ,long lq, long nIn, long eL)
{
FHE_TIMER_START;
factors = vFactors;
r = rSigma;
kFactors = llogQ;
n = nIn;
m = lm;//SMS.NumCols();
mBar = m - n*(kFactors*eL);
wLen = n*kFactors*eL;
e = eL;
// For each factor f_i, initialize zz_p context for F_i=f_i^e,
// and also compute f_i^{-1} mod F_j for all i<j
maxFactor = setFactors(fInv, zzp_context, factors, e);
sigmaX = getsigmaXVal(wLen, mBar, maxFactor, r);
// The Gaussian parameter from which we can sample with a trapdoor
stash.SetLength(kFactors);
for (long i = 0; i < kFactors; i++)
stash[i].init(factors[i]);
}
#endif