-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContext.java
More file actions
441 lines (340 loc) · 15.7 KB
/
Context.java
File metadata and controls
441 lines (340 loc) · 15.7 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package ckks;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
public class Context {
final static boolean debug = false;
int slots;
int topLevel;
double defaultScale;
// Roots of the cyclotomic polynomial
Complex cyclotomicRoots[];
// All generated primes, used to check that we didn't took some prime twice
long[] allPrimes;
int lastPrimeIdx = 0;
// RNS primes
long[] primes;
// Level specific data
LevelData[] levelsData;
// Roots of unity modulo each prime
long[][] primesRootsOfUnity;
// VandermondeLU of each prime, used for encoding
long[][][] primesVandermondeLUs;
// in [i][j] the inverse of the j'th prime modulo the i'th prime
long[][] otherPrimesInv;
// temp primes. Used in relinearization
long[] tempPrimes;
// in [i] product of temp primes modulo the i'th regular prime
long[] tempPrimesProdsMod;
// in [i] inverse of product of temp primes modulo the i'th regular prime
long[] tempPrimesProdsInvMod;
// in [i][j] product of temp primes besides the i'th temp prime, modulo the j'th
// regular prime
long[][] otherTempPrimesProdsMods;
// in [i] inverse of product of temp primes besides the i'th temp prime, modulo
// the i'th temp prime
long[] otherTempPrimesProdsInvsMod;
/**
* Initializes an empty context to be loaded with deserialize
*/
public Context() {
}
/**
* Initializes a context containing basic elements required for encoding,
* encrypting, etc.
*
* @param slots Number of slots in each ciphertext. Must be a
* power of 2.
* @param multiplications Number of multiplications the can be applied to a
* ciphertext or a plaintext.
* @param integerPrecision Maximal number of bits (including the sign bit) of
* the integer part of every number in a ciphertext
* or a plaintext.
* @param fractionalPrecision
* @throws Exception
*/
public Context(int slots, int multiplications, int integerPrecision, int fractionalPrecision) {
if (!isPowerOfTwo(slots))
throw new IllegalArgumentException("Number of slots must be power of 2");
if (integerPrecision < 1)
throw new IllegalArgumentException("integerPrecision must be at least 1");
if (integerPrecision + fractionalPrecision > 62)
throw new IllegalArgumentException("integerPrecision + fractionalPrecision can be at most 62");
this.slots = slots;
this.topLevel = multiplications;
this.defaultScale = Math.pow(2, fractionalPrecision);
if (debug)
System.out.println("initCyclotomicRoots()\n");
initCyclotomicRoots();
if (debug)
System.out.println("initPrimes()\n");
initPrimes(multiplications, integerPrecision, fractionalPrecision);
initPrimesData();
initTempPrimes(fractionalPrecision);
initTempPrimesData();
if (debug)
System.out.println("initPrimesRootsOfUnity()\n");
initPrimesRootsOfUnity();
if (debug)
System.out.println("initPrimesVandermondeLUs()\n");
initPrimesVandermondeLUs();
}
public void validateLevelDataExists(int level) {
if (levelsData[level] == null)
levelsData[level] = new LevelData(primes, tempPrimes, level);
}
private boolean isPowerOfTwo(int x) {
// see:
// https://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2
return (x & (x - 1)) == 0;
}
private void initCyclotomicRoots() {
cyclotomicRoots = new Complex[slots];
for (int i = 0; i < slots; i++) {
double theta = (Math.PI * (2 * i + 1)) / (2 * slots);
cyclotomicRoots[i] = new Complex(Math.cos(theta), Math.sin(theta));
}
}
private void initPrimes(int multiplications, int integerPrecision, int fractionalPrecision) {
allPrimes = new long[2 * (multiplications + 1)];
primes = new long[multiplications + 1];
Random rnd = new Random();
// We search for primes that are 1 bit larger than the wanted precision, so that
// the range of valid values for encryption (determined by the precision
// parameters) will be contained inside the modulos range. Also, notice that
// JAVA doesn't have unsigned types, and so we limit our width to 63 bits max.
primes[0] = genPrime(integerPrecision + fractionalPrecision + 1, rnd, slots * 2);
for (int i = 1; i < primes.length; i++)
primes[i] = genPrime(fractionalPrecision + 1, rnd, slots * 2);
}
private void initPrimesData() {
if (debug) {
System.out.println("Primes= " + Arrays.toString(primes) + '\n');
BigInteger modulus = BigInteger.ONE;
for (int i = 0; i < primes.length; i++)
modulus = modulus.multiply(BigInteger.valueOf(primes[i]));
System.out.println("Modulus= " + modulus + '\n');
}
levelsData = new LevelData[primes.length];
otherPrimesInv = new long[primes.length][primes.length];
for (int primeIdx = 0; primeIdx < primes.length; primeIdx++)
for (int otherPrimeIdx = 0; otherPrimeIdx < primes.length; otherPrimeIdx++)
if (primeIdx != otherPrimeIdx)
otherPrimesInv[primeIdx][otherPrimeIdx] = Maths.modInv(primes[otherPrimeIdx], primes[primeIdx]);
}
private void initTempPrimes(int fractionalPrecision) {
tempPrimes = new long[primes.length];
Random rnd = new Random();
for (int i = 0; i < tempPrimes.length; i++)
tempPrimes[i] = genPrime(fractionalPrecision, rnd, slots * 2);
}
private void initTempPrimesData() {
if (debug) {
System.out.println("Temp Primes= " + Arrays.toString(tempPrimes) + '\n');
BigInteger modulus = BigInteger.ONE;
for (int i = 0; i < tempPrimes.length; i++)
modulus = modulus.multiply(BigInteger.valueOf(tempPrimes[i]));
System.out.println("Temp Modulus= " + modulus + '\n');
}
tempPrimesProdsMod = new long[primes.length];
for (int primeIdx = 0; primeIdx < primes.length; primeIdx++) {
tempPrimesProdsMod[primeIdx] = 1;
for (int tmpPrimeIdx = 0; tmpPrimeIdx < tempPrimes.length; tmpPrimeIdx++)
tempPrimesProdsMod[primeIdx] = Maths.modMult(tempPrimesProdsMod[primeIdx], tempPrimes[tmpPrimeIdx],
primes[primeIdx]);
}
if (debug) {
System.out.println("tempPrimesProdsMod= " + Arrays.toString(tempPrimesProdsMod) + '\n');
}
tempPrimesProdsInvMod = new long[primes.length];
for (int primeIdx = 0; primeIdx < primes.length; primeIdx++)
tempPrimesProdsInvMod[primeIdx] = Maths.modInv(tempPrimesProdsMod[primeIdx], primes[primeIdx]);
if (debug) {
System.out.println("tempPrimesProdsInvMod= " + Arrays.toString(tempPrimesProdsInvMod) + '\n');
}
otherTempPrimesProdsMods = new long[tempPrimes.length][primes.length];
for (int primeIdx = 0; primeIdx < primes.length; primeIdx++) {
for (int tempPrimeIdx = 0; tempPrimeIdx < tempPrimes.length; tempPrimeIdx++) {
otherTempPrimesProdsMods[tempPrimeIdx][primeIdx] = 1;
for (int i = 0; i < tempPrimes.length; i++) {
if (i == tempPrimeIdx)
continue;
otherTempPrimesProdsMods[tempPrimeIdx][primeIdx] = Maths
.modMult(otherTempPrimesProdsMods[tempPrimeIdx][primeIdx], tempPrimes[i], primes[primeIdx]);
}
}
}
if (debug) {
System.out.println("otherTempPrimesProdsMods= " + Arrays.deepToString(otherTempPrimesProdsMods) + '\n');
}
otherTempPrimesProdsInvsMod = new long[tempPrimes.length];
for (int tempPrimeIdx = 0; tempPrimeIdx < tempPrimes.length; tempPrimeIdx++) {
otherTempPrimesProdsInvsMod[tempPrimeIdx] = 1;
for (int i = 0; i < tempPrimes.length; i++) {
if (i == tempPrimeIdx)
continue;
otherTempPrimesProdsInvsMod[tempPrimeIdx] = Maths.modMult(otherTempPrimesProdsInvsMod[tempPrimeIdx],
tempPrimes[i], tempPrimes[tempPrimeIdx]);
}
otherTempPrimesProdsInvsMod[tempPrimeIdx] = Maths.modInv(otherTempPrimesProdsInvsMod[tempPrimeIdx],
tempPrimes[tempPrimeIdx]);
}
if (debug) {
System.out.println("otherTempPrimesProdsInvsMod= " + Arrays.toString(otherTempPrimesProdsInvsMod) + '\n');
}
}
private long genPrime(int bits, Random rnd, int N) {
// for each prime p, (p-1) needs to be divisible by 2N (slots * 4)
// so that the cyclotomic polynomial can be factorized completely modulo p.
// see:
// https://en.wikipedia.org/wiki/Cyclotomic_polynomial#Cyclotomic_polynomials_over_a_finite_field_and_over_the_p-adic_integers
BigInteger M = BigInteger.valueOf(2 * N);
long res = 0;
boolean found = false;
int tries = 0;
while (!found && tries < 100) {
tries++;
BigInteger num = BigInteger.probablePrime(bits, rnd);
if (!num.subtract(BigInteger.ONE).mod(M).equals(BigInteger.ZERO))
continue;
res = num.longValue();
found = true;
for (int i = 0; i < lastPrimeIdx; i++)
if (allPrimes[i] == res)
found = false;
}
if (!found)
throw new IllegalStateException(
"Unable to find enough primes for given parameters. Try to increase fractional precision.");
allPrimes[lastPrimeIdx++] = res;
return res;
}
// see:
// https://math.stackexchange.com/questions/158344/how-to-find-the-solutions-for-the-n-th-root-of-unity-in-modular-arithmetic
private void initPrimesRootsOfUnity() {
int N = slots * 2;
primesRootsOfUnity = new long[primes.length][N];
Random rand = new Random();
long generator = 0, res;
for (int primeIdx = 0; primeIdx < primes.length; primeIdx++) {
HashSet<Long> generators = new HashSet<Long>();
res = 1;
// if res == 1 then we didn't got a generator
while (res == 1) {
generator = Maths.mod(rand.nextLong(), primes[primeIdx] - 2) + 1;
if (generators.contains(generator))
continue;
generators.add(generator);
res = Maths.modPow(generator, (primes[primeIdx] - 1) / 2, primes[primeIdx]);
}
for (int rootIdx = 0; rootIdx < N; rootIdx++)
primesRootsOfUnity[primeIdx][rootIdx] = Maths.modPow(generator,
((primes[primeIdx] - 1) / (2 * N)) * (2 * rootIdx + 1), primes[primeIdx]);
if (debug) {
System.out.println("(x^" + N + " + 1) roots modulo " + primes[primeIdx] + ": "
+ Arrays.toString(primesRootsOfUnity[primeIdx]) + '\n');
}
}
}
private void initPrimesVandermondeLUs() {
int N = slots * 2;
primesVandermondeLUs = new long[primes.length][N][N];
for (int primeIdx = 0; primeIdx < primes.length; primeIdx++) {
// init vandermondes
for (int rootIdx = 0; rootIdx < N; rootIdx++) {
long val = 1;
for (int exp = 0; exp < N; exp++) {
primesVandermondeLUs[primeIdx][rootIdx][exp] = val;
val = Maths.modMult(val, primesRootsOfUnity[primeIdx][rootIdx], primes[primeIdx]);
}
}
if (debug) {
System.out.println("vandermonde matrix for prime " + primes[primeIdx]);
System.out.println(Arrays.deepToString(primesVandermondeLUs[primeIdx]));
System.out.println();
}
// LU decomposition
// based on code from https://math.nist.gov/javanumerics/jama/
long[] LUrowi;
long[] LUcolj = new long[N];
// Outer loop.
for (int j = 0; j < N; j++) {
// Make a copy of the j-th column to localize references.
for (int i = 0; i < N; i++)
LUcolj[i] = primesVandermondeLUs[primeIdx][i][j];
// Apply previous transformations.
for (int i = 0; i < N; i++) {
LUrowi = primesVandermondeLUs[primeIdx][i];
int kmax = Math.min(i, j);
long a[] = new long[kmax];
long b[] = new long[kmax];
for (int k = 0; k < kmax; k++) {
a[k] = LUrowi[k];
b[k] = LUcolj[k];
}
long s = 0;
for (int k = 0; k < kmax; k++) {
s += Maths.modMult(LUrowi[k], LUcolj[k], primes[primeIdx]);
s = Maths.mod(s, primes[primeIdx]);
}
LUcolj[i] -= s;
LUcolj[i] = Maths.mod(LUcolj[i], primes[primeIdx]);
LUrowi[j] = LUcolj[i];
}
// Compute multipliers.
if (j < N & primesVandermondeLUs[primeIdx][j][j] != 0) {
for (int i = j + 1; i < N; i++)
primesVandermondeLUs[primeIdx][i][j] = Maths.modDiv(primesVandermondeLUs[primeIdx][i][j],
primesVandermondeLUs[primeIdx][j][j], primes[primeIdx]);
}
}
if (debug) {
System.out.println("LU decomposition of vandermonde matrix for prime " + primes[primeIdx]);
System.out.println(Arrays.deepToString(primesVandermondeLUs[primeIdx]));
System.out.println();
}
}
}
public long[] serialize() {
long[] res = new long[5 + primes.length + tempPrimes.length
+ primesRootsOfUnity.length * primesRootsOfUnity[0].length];
int idx = 0;
res[idx++] = slots;
res[idx++] = topLevel;
res[idx++] = (long) defaultScale;
res[idx++] = primes.length;
res[idx++] = tempPrimes.length;
for (int i = 0; i < primes.length; i++)
res[idx++] = primes[i];
for (int i = 0; i < tempPrimes.length; i++)
res[idx++] = tempPrimes[i];
for (int i = 0; i < primes.length; i++)
for (int j = 0; j < slots * 2; j++)
res[idx++] = primesRootsOfUnity[i][j];
return res;
}
public void deserialize(long[] serialization) {
int idx = 0;
slots = (int) serialization[idx++];
topLevel = (int) serialization[idx++];
defaultScale = (double) serialization[idx++];
primes = new long[(int) serialization[idx++]];
tempPrimes = new long[(int) serialization[idx++]];
initCyclotomicRoots();
for (int i = 0; i < primes.length; i++)
primes[i] = serialization[idx++];
initPrimesData();
for (int i = 0; i < tempPrimes.length; i++)
tempPrimes[i] = serialization[idx++];
initTempPrimesData();
primesRootsOfUnity = new long[primes.length][slots * 2];
for (int i = 0; i < primes.length; i++)
for (int j = 0; j < slots * 2; j++)
primesRootsOfUnity[i][j] = serialization[idx++];
initPrimesVandermondeLUs();
}
public int getNumSlots() {
return slots;
}
}