-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathIFactory.cs
508 lines (444 loc) · 19.6 KB
/
IFactory.cs
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using MathNet.Numerics.LinearAlgebra;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
namespace HEWrapper
{
/// <summary>
/// The factory is used to:
/// - generate encrypted vectors/matrices
/// - create encryption parameters
/// - save/load encryption parameters
/// </summary>
public interface IFactory
{
/// <summary>
/// generate a plaintext vector
/// </summary>
/// <param name="v">values to populate the vector with</param>
/// <param name="format">type of vector to generate (sparse/dense)</param>
/// <param name="scale">scale is the precision of the values. Each value is multiplied by scale beore rounded to an integer</param>
/// <returns>a vector</returns>
IVector GetPlainVector(Vector<double> v, EVectorFormat format, double scale);
/// <summary>
/// generate a plaintext vector
/// </summary>
/// <param name="v">values to populate the vector with</param>
/// <param name="format">type of vector to generate (sparse/dense)</param>
/// <returns>a vector</returns>
IVector GetPlainVector(IEnumerable<BigInteger> v, EVectorFormat format);
/// <summary>
/// generate an encrypted vector
/// </summary>
/// <param name="v">values to populate the vector with</param>
/// <param name="format">type of vector to generate (sparse/dense)</param>
/// <param name="scale">scale is the precision of the values. Each value is multiplied by scale beore rounded to an integer</param>
/// <returns>a vector</returns>
IVector GetEncryptedVector(Vector<double> v, EVectorFormat format, double scale);
/// <summary>
/// generate an encrypted vector
/// </summary>
/// <param name="v">values to populate the vector with</param>
/// <param name="format">type of vector to generate (sparse/dense)</param>
/// <returns>a vector</returns>
IVector GetEncryptedVector(IEnumerable<BigInteger> v, EVectorFormat format);
/// <summary>
/// Get an unbounded integer from string
/// </summary>
/// <param name="str">input string</param>
/// <returns>unbounded integer</returns>
BigInteger GetValueFromString(string str);
/// <summary>
/// Convert an unbounded integer to string
/// </summary>
/// <param name="value">unbounded integer</param>
/// <returns>string</returns>
string GetStringFromValue(BigInteger value);
/// <summary>
/// Deep copy of a vector
/// </summary>
/// <param name="v">The vector to copy</param>
/// <returns>A new vector</returns>
IVector CopyVector(IVector v);
/// <summary>
/// Load vector from a stream
/// </summary>
/// <param name="str">Stream to load from</param>
/// <returns>The vector</returns>
IVector LoadVector(StreamReader str);
/// <summary>
/// Generate a matrix with plaintext values
/// </summary>
/// <param name="m">The data to use to populate the matrix</param>
/// <param name="format">Matrix format to use</param>
/// <param name="scale">scale is the precision of the values. Each value is multiplied by scale beore rounded to an integer</param>
/// <returns>The matrix</returns>
IMatrix GetPlainMatrix(Matrix<double> m, EMatrixFormat format, double scale);
/// <summary>
/// Generate a matrix with encrypted values
/// </summary>
/// <param name="m">The data to use to populate the matrix</param>
/// <param name="format">Matrix format to use</param>
/// <param name="scale">scale is the precision of the values. Each value is multiplied by scale beore rounded to an integer</param>
/// <returns>The matrix</returns>
IMatrix GetEncryptedMatrix(Matrix<double> m, EMatrixFormat format, double scale);
/// <summary>
/// Creates a matrix from a collection of vectors
/// </summary>
/// <param name="vectors"> the vectors that will form the rows/columns of the matrix</param>
/// <param name="format"> specifies whether the matrix is column major or row major</param>
/// <param name="env"> computational environment</param>
/// <param name="CopyVectors"> by default the vectors are copied. However, if this variable is set to false thy are passed by reference and therefore should not be disposed.</param>
/// <returns> a matrix</returns>
IMatrix GetMatrix(IVector[] vectors, EMatrixFormat format, bool CopyVectors = true);
/// <summary>
/// Load matrix from stream
/// </summary>
/// <param name="str">Stream to load from</param>
/// <returns>Loaded matrix</returns>
IMatrix LoadMatrix(StreamReader str);
/// <summary>
/// Return a computational environment with which it is posible to operate on matrices and vectors
/// </summary>
/// <returns>The computational environment</returns>
IComputationEnvironment AllocateComputationEnv();
/// <summary>
/// De-allocate a computational environment
/// </summary>
/// <param name="env">The computational environment to de-allocate</param>
void FreeComputationEnv(IComputationEnvironment env);
/// <summary>
/// Save the factory to a stream
/// </summary>
/// <param name="stream">The stream to save to</param>
/// <param name="withPrivateKeys">If set to true, the secret keys are saved, otherwise the factory is saved without secret keys</param>
/// <returns>The stream</returns>
Stream Save(Stream stream, bool withPrivateKeys = false);
/// <summary>
/// Save the factory to a file
/// </summary>
/// <param name="FileName">File name to save to</param>
/// <param name="withPrivateKeys">If set to true, the secret keys are saved, otherwise the factory is saved without secret keys</param>
void Save(string FileName, bool withPrivateKeys = false);
}
public class RawComputationalEnvironment : IComputationEnvironment
{
public IFactory ParentFactory { get; set; } = null;
public ulong[] Primes { get; set; }
}
public class RawFactory : IFactory
{
IComputationEnvironment Parent;
readonly ulong BlockSize;
public ulong[] Primes { get; set; }
public RawFactory(ulong BlockSize)
{
this.BlockSize = BlockSize;
}
public IVector GetPlainVector(Vector<double> v, EVectorFormat format, double scale)
{
return new RawVector(v, scale, BlockSize) { Format = format };
}
public IVector GetPlainVector(IEnumerable<BigInteger> v, EVectorFormat format)
{
return new RawVector(v, BlockSize) { Format = format };
}
public IVector GetEncryptedVector(Vector<double> v, EVectorFormat format, double scale)
{
return new RawVector(v, scale, BlockSize) { Format = format };
}
public IVector GetEncryptedVector(IEnumerable<BigInteger> v, EVectorFormat format)
{
return new RawVector(v, BlockSize) { Format = format };
}
public IVector LoadVector(StreamReader str)
{
return RawVector.Read(str);
}
public IVector CopyVector(IVector v) { return new RawVector((RawVector)v); }
public IMatrix GetPlainMatrix(Matrix<double> m, EMatrixFormat format, double scale)
{
return new RawMatrix(m, scale, format, BlockSize);
}
public IMatrix GetEncryptedMatrix(Matrix<double> m, EMatrixFormat format, double scale)
{
return new RawMatrix(m, scale, format, BlockSize);
}
public IMatrix GetMatrix(IVector[] vectors, EMatrixFormat format, bool CopyVectors = true)
{
var scale = vectors[0].Scale;
var columns = vectors.Select(v => (Vector<double>)(v.Data) / scale);
var mat = Matrix<double>.Build.DenseOfColumnVectors(columns);
return new RawMatrix(mat, scale, format, BlockSize);
}
public IMatrix LoadMatrix(StreamReader str)
{
return RawMatrix.Read(str);
}
public IComputationEnvironment AllocateComputationEnv()
{
if (Parent == null) Parent = new RawComputationalEnvironment() { ParentFactory = this, Primes = Primes};
return Parent;
}
public void FreeComputationEnv(IComputationEnvironment env)
{
}
public Stream Save(Stream stream, bool withPrivateKeys = false)
{
using (var sw = new StreamWriter(stream))
{
sw.WriteLine("<RawFactory>");
sw.WriteLine(BlockSize);
sw.WriteLine("</RawFactory>");
}
return stream;
}
public void Save(string FileName, bool withPrivateKeys = false)
{
using (var stream = new FileStream(FileName, FileMode.Create))
{
Save(stream, withPrivateKeys);
}
}
public BigInteger GetValueFromString(string str)
{
return BigInteger.Parse(str);
}
public string GetStringFromValue(BigInteger value)
{
return value.ToString();
}
}
public class EncryptedSealBfvFactory : IFactory
{
ConcurrentQueue<EncryptedSealBfvEnvironment> environmentQueue = new ConcurrentQueue<EncryptedSealBfvEnvironment>();
EncryptedSealBfvEnvironment referenceEnvironment;
const int DefaultDecompositionBitCount = 10;
const int DefaultGaloisDecompositionBitCount = 20;
public EncryptedSealBfvFactory()
{
ulong[] primes = new ulong[] { 40961, 65537, 114689, 147457, 188417 };
EncryptedSealBfvEnvironment eenv = new EncryptedSealBfvEnvironment() { ParentFactory = this };
eenv.GenerateEncryptionKeys(primes, 4096, DefaultDecompositionBitCount, DefaultGaloisDecompositionBitCount);
referenceEnvironment = eenv;
}
public EncryptedSealBfvFactory(ulong[] primes, ulong n, int DecompositionBitCount = DefaultDecompositionBitCount, int GaloisDecompositionBitCount = DefaultGaloisDecompositionBitCount, int SmallModulusCount = -1)
{
EncryptedSealBfvEnvironment eenv = new EncryptedSealBfvEnvironment() { ParentFactory = this };
eenv.GenerateEncryptionKeys(primes, n, DecompositionBitCount, GaloisDecompositionBitCount, SmallModulusCount);
referenceEnvironment = eenv;
}
public EncryptedSealBfvFactory(string fileName)
{
referenceEnvironment = new EncryptedSealBfvEnvironment(fileName)
{
ParentFactory = this
};
}
public EncryptedSealBfvFactory(Stream stream)
{
referenceEnvironment = new EncryptedSealBfvEnvironment(stream)
{
ParentFactory = this
};
}
public IVector CopyVector(IVector v)
{
return Utils.ProcessInEnv((env) => new EncryptedSealBfvVector(v, env), this);
}
public IComputationEnvironment AllocateComputationEnv()
{
environmentQueue.TryDequeue(out EncryptedSealBfvEnvironment env);
if (env == null)
env = new EncryptedSealBfvEnvironment(referenceEnvironment);
return env;
}
public void FreeComputationEnv(IComputationEnvironment env)
{
var lenv = env as EncryptedSealBfvEnvironment;
environmentQueue.Enqueue(lenv);
}
public Stream Save(Stream stream, bool withPrivateKeys = false)
{
return referenceEnvironment.Save(stream, withPrivateKeys);
}
public void Save(string fileName, bool withPrivateKeys = false)
{
referenceEnvironment.Save(fileName, withPrivateKeys);
}
public IVector LoadVector(StreamReader str)
{
return Utils.ProcessInEnv(env => EncryptedSealBfvVector.Read(str, env as EncryptedSealBfvEnvironment), this);
}
public IMatrix LoadMatrix(StreamReader str)
{
return Utils.ProcessInEnv(env => EncryptedSealBfvMatrix.Read(str, env as EncryptedSealBfvEnvironment), this);
}
public IVector GetPlainVector(Vector<double> v, EVectorFormat format, double scale)
{
return Utils.ProcessInEnv(env => new EncryptedSealBfvVector(v, env, scale, EncryptData: false, Format: format), this);
}
public IVector GetPlainVector(IEnumerable<BigInteger> v, EVectorFormat format)
{
return Utils.ProcessInEnv((env) =>
new EncryptedSealBfvVector(v, env, EncryptData: false, Format: format), this);
}
public IVector GetEncryptedVector(Vector<double> v, EVectorFormat format, double scale)
{
return Utils.ProcessInEnv((env) =>
new EncryptedSealBfvVector(v, env, scale, EncryptData: true, Format: format), this);
}
public IVector GetEncryptedVector(IEnumerable<BigInteger> v, EVectorFormat format)
{
return Utils.ProcessInEnv((env) =>
new EncryptedSealBfvVector(v, env, EncryptData: true, Format: format), this);
}
public IMatrix GetPlainMatrix(Matrix<double> m, EMatrixFormat format, double scale)
{
IMatrix res = null;
Utils.ProcessInEnv((env) =>
{
EncryptedSealBfvVector[] vecs = (format == EMatrixFormat.ColumnMajor) ?
m.EnumerateColumns().Select(v => new EncryptedSealBfvVector(v, env, scale, Format: EVectorFormat.dense, EncryptData: false)).ToArray()
: m.EnumerateRows().Select(v => new EncryptedSealBfvVector(v, env, scale, Format: EVectorFormat.dense, EncryptData: false)).ToArray();
res = new EncryptedSealBfvMatrix(vecs, env) { Format = format };
foreach (var v in vecs) v.Dispose();
}, this);
return res;
}
public IMatrix GetEncryptedMatrix(Matrix<double> m, EMatrixFormat format, double scale)
{
IMatrix res = null;
Utils.ProcessInEnv((env) =>
{
EncryptedSealBfvVector[] vecs = null;
if (format == EMatrixFormat.ColumnMajor)
{
vecs = new EncryptedSealBfvVector[m.ColumnCount];
Utils.ParallelProcessInEnv(vecs.Length, env, (penv, taskIndex, k) =>
{
vecs[k] = new EncryptedSealBfvVector(m.Column(k), penv, scale, Format: EVectorFormat.dense, EncryptData: true);
});
}
else
{
vecs = new EncryptedSealBfvVector[m.RowCount];
Utils.ParallelProcessInEnv(vecs.Length, env, (penv, taskIndex, k) =>
{
vecs[k] = new EncryptedSealBfvVector(m.Row(k), penv, scale, Format: EVectorFormat.dense, EncryptData: true);
});
}
res = new EncryptedSealBfvMatrix(vecs, env) { Format = format };
foreach (var v in vecs) v.Dispose();
}, this);
return res;
}
public IMatrix GetMatrix(IVector[] vectors, EMatrixFormat format, bool CopyVectors = true)
{
IMatrix mat = null;
Utils.ProcessInEnv((env) =>
{
mat = new EncryptedSealBfvMatrix(Array.ConvertAll(vectors, v => (EncryptedSealBfvVector)v), env, CopyVectors: CopyVectors)
{
Format = format
};
}, this);
return mat;
}
public BigInteger GetValueFromString(string str)
{
var f = str.Split(',').Select(x => uint.Parse(x)).ToArray();
BigInteger v = new BigInteger();
for (int i = 0; i < f.Length; i++)
v += referenceEnvironment.preComputedCoefficients[i] * f[i];
v = v % referenceEnvironment.bigFactor;
return v;
}
public string GetStringFromValue(BigInteger value)
{
return string.Join(",", this.referenceEnvironment.Environments.Select(e => value % e.plainmodulusValue));
}
}
/// <summary>
/// Note that this class is not fully implemented. It is just intended to allow getting and releasing environments
/// </summary>
public class AtomicEncryptedFactory : IFactory
{
ConcurrentQueue<AtomicSealBfvEncryptedEnvironment> environmentQueue = new ConcurrentQueue<AtomicSealBfvEncryptedEnvironment>();
internal AtomicSealBfvEncryptedEnvironment ReferenceEnvironment { get; set; }
public IComputationEnvironment AllocateComputationEnv()
{
environmentQueue.TryDequeue(out AtomicSealBfvEncryptedEnvironment env);
if (env == null)
env = new AtomicSealBfvEncryptedEnvironment(ReferenceEnvironment);
return env;
}
public void FreeComputationEnv(IComputationEnvironment env)
{
var eenv = env as AtomicSealBfvEncryptedEnvironment;
environmentQueue.Enqueue(eenv);
}
public IVector CopyVector(IVector v)
{
throw new NotImplementedException();
}
public IMatrix GetEncryptedMatrix(Matrix<double> m, EMatrixFormat format, double scale)
{
throw new NotImplementedException();
}
public IVector GetEncryptedVector(Vector<double> v, EVectorFormat format, double scale)
{
throw new NotImplementedException();
}
public IVector GetPlainVector(IEnumerable<BigInteger> v, EVectorFormat format)
{
throw new NotImplementedException();
}
public IVector GetEncryptedVector(IEnumerable<BigInteger> v, EVectorFormat format)
{
throw new NotImplementedException();
}
public IMatrix GetMatrix(IVector[] vectors, EMatrixFormat format, bool CopyVectors = true)
{
throw new NotImplementedException();
}
public IMatrix GetPlainMatrix(Matrix<double> m, EMatrixFormat format, double scale)
{
throw new NotImplementedException();
}
public IVector GetPlainVector(Vector<double> v, EVectorFormat format, double scale)
{
throw new NotImplementedException();
}
public IMatrix LoadMatrix(StreamReader str)
{
throw new NotImplementedException();
}
public IVector LoadVector(StreamReader str)
{
throw new NotImplementedException();
}
public Stream Save(Stream stream, bool withPrivateKeys = false)
{
throw new NotImplementedException();
}
public void Save(string FileName, bool withPrivateKeys = false)
{
throw new NotImplementedException();
}
public BigInteger GetValueFromString(string str)
{
var v = BigInteger.Parse(str) % this.ReferenceEnvironment.plainmodulusValue;
return v;
}
public string GetStringFromValue(BigInteger value)
{
return value.ToString();
}
}
}