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
/
GGH15.cpp
791 lines (651 loc) · 21.8 KB
/
GGH15.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/* 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.
*/
/*****************************************************************************
* GGH15.h - an implementation of the graph-based multilinear maps from
* [Gentry-Gorbunov-Halevi, TCC 2015]
*****************************************************************************/
#include <stdexcept>
#include <unistd.h>
#include <sys/stat.h>
#if defined(__unix__) || defined(__unix) || defined(unix)
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <NTL/BasicThreadPool.h>
#include "mat_l.h"
#include "GGH15.h"
#include "utils/tools.h"
bool bGGH15threading=1;
//#define DEBUGPRINT
//#define PRINTDOT
#ifdef PRINTDOT
#define printDot cerr << "." << std::flush
#else
#define printDot
#endif
// A lower bound on |q|/2^10 for an L-edge chain. We need to bound the size
// of a product of L matrices of dim m-by-m, where L-1 have entries drawn
// with parameter sqrt{sigmaX) and one has entries with parameter sec.
// The formula we use is q/2^10 > largest-singular-val(product), which
// translates to
// q/2^10 > (2*sqrt(m)*sqrt(sigmaX))^{L-1} * lambda*sqrt(m+n)
// \approx lambda * sigmaX^{(L-1)/2} * m^{L/2} * 2^L
long qBound(long sigmaX, long m, long L)
{
// cerr << "@ L="<<L<<", sigmaX="<<sigmaX
// <<",log_2(sqrt(sigmaX))="<<(log(sigmaX)/(log(2)*2))
// <<", log_2(m)="<<(log(m)/log(2)) << endl;
double logQ = ((L-1)*log(sigmaX) + L*log(m))/2.0;
return ceil(logQ/log(2.0)) + L + 16;
}
// Computing basic parameters for length-'diam' MMAPs. In particular,
// assuming that "fresh" sampled encodings use "varince" sigmaX, we need
// to ensure that
// (a) log q > qBound(sigmaX, m, L, sec) (qBound from above)
// (b) m > mBound(log q, errBits=7, sec) (mBound from TDMatrixParams)
//
// Since the size of q is determined by the number of small factors, i.e.,
// q = \prod_{i=0}^{k-1} p_i^e, we just try to find the smallest k for which
// the above constraints are satisfied.
//
// In addition, we also need to ensure that m >= n*k*e + mBar, where
// n = nStates + ceil(sqrt(sec)/2) and mBar = mbarBound(log q, n, sec)
// (from TDMatrixParams).
GGH15basicParams::GGH15basicParams(COMP_PARAMS_TYPE comp, //=COMP_GGH15PRMS
long nStates, long L, long sec, long ee)
{
FHE_TIMER_START;
e = ee;
n = nStates + ceil(sqrt(sec)/2);
// k will never be smaller than L
for (k=L; k<TDMATRIX_NUM_SMALL_FACTORS; k++) {
long qBits = get_qBits(k, e);
m = mBound(qBits, /*errBits=*/7, sec);
long mBar = mbarBound(qBits, n, sec);
long wLen = n*k*e;
if (m < mBar+wLen) m = mBar+wLen;
else mBar = m-wLen;
long sigmaX = getsigmaXVal(wLen, mBar, /*maxFactor=*/181);
long qbound = qBound(sigmaX,m,L);
if (qBits >= qbound) // all constraints are satisfied
break;
// otherwise we try a larger value of k
}
if (k >= TDMATRIX_NUM_SMALL_FACTORS) { // could not find a solution
throw std::logic_error("Cannot set GGH15 parameters");
}
}
// Initialize a single node, choose A w/ trapdoor and T, Tinv
void GGH15node::initGGH15node(TDMatrixParams& prms, long idx)
{
#ifdef DEBUGPRINT
fprintf(stderr,"initGGH15node %s %d\n", __FILE__, __LINE__);
#endif // DEBUGPRINT
FHE_TIMER_START;
index = idx;
params = &prms;
A.initTDmatrix(prms); // Choose matrix A with trapdoor
#ifdef DEBUGPRINT
fprintf(stderr,"initGGH15node %s %d\n", __FILE__, __LINE__);
#endif // DEBUGPRINT
// Choose a small n-by-n matrix P, set Pinv as its inverse mod q
Pinv.params = &prms;
do {
setSmall(P, prms.n, prms.n, /*howSmall=*/prms.maxFactor);
#ifdef DEBUGPRINT
fprintf(stderr,"initGGH15node %s %d\n", __FILE__, __LINE__);
#endif // DEBUGPRINT
}
while (!Pinv.invert(P)); // Repeat until you get an invertible P mod q
#ifdef DEBUGPRINT
fprintf(stderr,"initGGH15node %s %d\n", __FILE__, __LINE__);
#endif // DEBUGPRINT
// choose T, Tinv as random inverses of each other mod q
T.params = Tinv.params = &prms;
generateMatrixPair(T, Tinv, prms.m);
}
// Initilize an instance with n nodes
GGH15mmaps::GGH15mmaps(TDMatrixParams& p, long n)
{
FHE_TIMER_START;
assert (n>0);
params = &p;
nodes.SetLength(n-2);
//the flag allows parallelizing when it is set to false
NTL_GEXEC_RANGE(!bGGH15threading, n-2, first, last)
for (long i=first; i< last; i++)
{
#ifdef DEBUGPRINT
cout << "initializing node:" << i << endl;
#if (defined(__unix__) || defined(__unix) || defined(unix))
struct rusage rusage;
getrusage( RUSAGE_SELF, &rusage );
cout << " rusage.ru_maxrss="<<rusage.ru_maxrss << endl;
#endif
#endif // DEBUGPRINT
nodes[i].initGGH15node(p, i+1);
#ifdef DEBUGPRINT
cout << "after initializing node:" << i << endl;
#endif // DEBUGPRINT
}
NTL_GEXEC_RANGE_END
#ifdef DEBUGPRINT
cout << "before randomfill:" << endl;
#endif // DEBUGPRINT
// The right bracket is a random non-small n-by-1 matrix
randomFill(An, /*rows=*/p.n, /*cols=*/1, p);
#ifdef DEBUGPRINT
cout << "after randomfill:" << endl;
#endif // DEBUGPRINT
}
long GGH15mmaps::writeToFile(FILE* handle)
{
FHE_TIMER_START;
long count = params->writeToFile(handle);
count += An.writeToFile(handle);
long nNodes = numNodes()-2;
count += fwrite(&nNodes,sizeof(long),1,handle);
for (long i = 0; i < nNodes; i++)
{
count+= nodes[i].writeToFile(handle);
}
return count;
}
long GGH15mmaps::readFromFile(FILE* handle, TDMatrixParams* prmBuf)
{
FHE_TIMER_START;
assert(params != NULL || prmBuf != NULL); // some pointer must be provided
long count=0;
if (prmBuf != NULL)
{
TDMatrixParams p;
count = p.readFromFile(handle);
assert(p == *prmBuf); // sanity check
params = prmBuf; // point to given params
}
else
count = params->readFromFile(handle); // overwrite params from input
count += An.readFromFile(handle,params);
// Read the actual nodes from disk, one at a time
long nNodes;
count += fread(&nNodes,sizeof(nNodes),1,handle);
nodes.SetLength(nNodes);
for (long i = 0; i < (nNodes); i++)
{
count+= nodes[i].readFromFile(handle, params);
}
return count;
}
// Write one GGH15 node to output
long GGH15node::writeToActualFile(FILE* handle)
{
FHE_TIMER_START;
long count = params->writeToFile(handle);
count += fwrite(&index, sizeof(index),1, handle);
count += A.writeToFile(handle);
count += ::writeToFile(P,handle);
count += T.writeToFile(handle);
count += Tinv.writeToFile(handle);
count += Pinv.writeToFile(handle);
return count;
}
long GGH15node::readFromFile(FILE* handle, TDMatrixParams* prmBuf)
{
FHE_TIMER_START;
assert(params != NULL || prmBuf != NULL); // some pointer must be provided
long count = 0;
if (prmBuf != NULL)
{
TDMatrixParams p;
count = p.readFromFile(handle);
assert(p == *prmBuf); // sanity check
params = prmBuf; // point to given params
}
else
count = params->readFromFile(handle); // overwrite params from input
count += fread(&index, sizeof(index),1, handle);
count += A.readFromFile(handle, params);
count += ::readFromFile(P,handle);
count += T.readFromFile(handle, params);
count += Tinv.readFromFile(handle, params);
count += Pinv.readFromFile(handle, params);
return count;
}
long GGH15encoding::writeToFile(FILE* handle)
{
FHE_TIMER_START;
long count = fwrite(&from, sizeof(from),1, handle);
count += fwrite(&to, sizeof(to),1, handle);
count += data.writeToFile(handle);
return count;
}
long GGH15encoding::readFromFile(FILE* handle, TDMatrixParams& prms)
{
FHE_TIMER_START;
long count = fread(&from, sizeof(from),1, handle);
count += fread(&to, sizeof(to),1, handle);
return count + data.readFromFile(handle, &prms);
}
/*************************************************************/
/** Encding routines: there are four such routines, **/
/** depending on whether or not i=0 and whether or not j=n. **/
/*************************************************************/
/* GGH15 encoding for 0<i,j<n. Set C = Tinv_i * CC * T_j, where
* CC satisfies A_i*CC = (Pinv_i*S*P_j)*A_j+E (mod q). We assume
* that toA was already multiplied by P_j.
*/
static void encodeGGH15(CRTmatrix& C, const mat_l& S,
const GGH15node& from, const GGH15node& to,
const CRTmatrix& toA)
{
FHE_TIMER_START;
const TDMatrixParams* prms = from.getPrms();
// Set B = (Pinv_i x S x P_j) x A + E
CRTmatrix B = from.getPinv(); // Pinv_i
B *= S; // Pinv_i x S
B *= toA; // (Pinv_i x S x P_j) x A
mat_l E; // Choose a small n-by-m noise matrix E
setSmall(E, S.NumRows(), toA.NumCols(), /*sigma=*/prms-> r);
B += E; // (Pinv_i x S x P_j) x A + E
// Sample small C s.t. fromA x CC = B
mat_l CC(INIT_SIZE, prms->m, B.NumCols());
#ifdef DEBUGPRINT
cout << "encoding" << endl;
#endif
//NTL_GEXEC_RANGE(!bGGH15threading, B.NumCols(), first, last)
GEXEC_RANGE(!bGGH15threading, B.NumCols(), first, last)
for (long j=first; j< last; j++)
{
// Sample vector x_j s.t. A*x_j = j'th column of B
Vec<vec_zz_p> colMod;
B.getColumn(colMod, j); // The j'th column of B in CRT format
vec_l sampledColumn; // this is where we put the vector x_j
from.getA().sampleWithTrapdoor(sampledColumn, colMod);// sample a column
for (long i=0; i<CC.NumRows(); i++) // store it in CC
CC[i][j] = sampledColumn[i];
}
//NTL_GEXEC_RANGE_END
GEXEC_RANGE_END(!bGGH15threading)
// Compute C = Tinv_i * CC * T_j
C = from.getTinv();
C *= CC;
C *= to.getT();
}
//------------------------------------------------------------------//
/* GGH15 encoding for i=0<j<n. Set C =(S*P_j*A_j +E) *T_j (mod q).
* We assume that toA it was already multiplied by P_j.
*/
static void encodeGGH15first(CRTmatrix& C, const mat_l& S,
const GGH15node& to, const CRTmatrix& toA)
{
FHE_TIMER_START;
const TDMatrixParams* prms = to.getPrms();
// Set C = S x P_j x A + E
const mat_l& tmpS = S;
mat_l E; // Choose a small 1-by-m noise matrix E
setSmall(E, tmpS.NumRows(), toA.NumCols(), /*sigma=*/prms->r);
C = toA; // P_j x A
C.leftMultBy(tmpS); // S x P_j x A
C += E; // S x P_j x A + E
C *= to.getT();
}
//------------------------------------------------------------------//
/* GGH15 encoding for 0<i<j=n. Set C = Tinv_i * CC, where CC
* satisfies A_i*CC = (Pinv_i*S)*A_n + E (mod q).
*/
static void encodeGGH15last(CRTmatrix& C, const mat_l& S,
const GGH15node& from, const CRTmatrix& toA)
{
FHE_TIMER_START;
const TDMatrixParams* prms = from.getPrms();
// Set B = (Pinv_i x S) x A_n + E
CRTmatrix B = from.getPinv(); // Pinv_i
B *= S; // Pinv_i x S
B *= toA; // Pinv_i x S x A_n
mat_l E; // Choose a small n-by-m noise matrix E
setSmall(E, B.NumRows(), B.NumCols(), /*sigma=*/prms->r);
B += E; // Pinv_i x S x A_n + E
// Sample small CC s.t. fromA x CC = B
mat_l CC(INIT_SIZE, prms->m, B.NumCols());
for (long j=0; j<B.NumCols(); j++)
{
// Sample vector x_j s.t. A*x_j = j'th column of B
Vec<vec_zz_p> colMod;
B.getColumn(colMod, j); // The j'th column of B in CRT format
vec_l sampledColumn; // this is where we put the vector x_j
from.getA().sampleWithTrapdoor(sampledColumn, colMod);// sample a column
for (long i=0; i<CC.NumRows(); i++) // store it in CC
CC[i][j] = sampledColumn[i];
}
// Compute C = Tinv_i * CC
C = from.getTinv();
C *= CC;
}
//------------------------------------------------------------------//
/* GGH15 encoding for i=0<j=n, set C =(S*A_n +E) (mod q).
*/
static void encodeGGH15_0_n(CRTmatrix& C, const mat_l& S, const CRTmatrix& toA)
{
FHE_TIMER_START;
const mat_l& tmpS = S;
C = toA; // A_n
C.leftMultBy(tmpS); // u x S x A
mat_l E; // Choose a small 1-by-m noise matrix E
setSmall(E, tmpS.NumRows(), toA.NumCols(), /*sigma=*/toA.params->r);
C += E; // u x S x A + E
}
bool encodeMatrix(const std::string& dirName, GGH15encoding& C, const mat_l& S,
long i, long j, GGH15node* iNode, GGH15node* jNode,
CRTmatrix& An, TaggedCRTmatrix* toAptr,
TDMatrixParams *params, int nNodes)
{
#ifdef DEBUGPRINT
cout << "begin encode matrix" << endl;
#endif // DEBUGPRINT
FHE_TIMER_START;
#ifdef DEBUGPRINT
cout << "j=" << ToString(j) << endl;
#endif
// FIXME: should we enforce i<j ??
assert(i>=0 && i<nNodes-1);
assert(j>0 && j<nNodes);
// Get the destination matrix A in explicit CRT form
TaggedCRTmatrix toA(params);
if (toAptr==NULL) toAptr = &toA;
bool isLast = (nNodes-1 == j);
#ifdef DEBUGPRINT
cout << "after reading jNode" << endl;
//getcwd(cwd, sizeof(cwd));
//cout << "read node info, current directory = " << cwd << endl;
#endif
// If the caller did not supply a good toA, then compute it now
if (toAptr->tag != j && !isLast) // If target is NOT the last node
{
jNode->getCRTA(toAptr->data);
toAptr->data.leftMultBy( jNode->getP() ); // set toA = P_j x A_j
toAptr->tag = j;
}
// Do the actual encoding
C.setFrom(i);
C.setTo(j);
printDot;
if (i == 0) // encoding wrt 0 -> ...
{
if (isLast) // encoding wrt 0 -> n
{
#ifdef DEBUGPRINT
cout << "encoding encodeGGH15_0_n" << endl;
#endif
encodeGGH15_0_n(C.getData(), S, An);
}
else // encoding wrt 0 -> j<n
{
#ifdef DEBUGPRINT
cout << "encoding first" << endl;
#endif
encodeGGH15first(C.getData(), S, *jNode, toAptr->data);
}
}
else // encoding wrt 0<i -> ...
{
if (isLast) // encoding wrt 0<i -> n
{
#ifdef DEBUGPRINT
cout << "Encoding last" << endl;
#endif
encodeGGH15last(C.getData(), S, *iNode, An);
}
else // encoding wrt 0<i -> j<n
{
encodeGGH15(C.getData(), S, *iNode, *jNode, toAptr->data);
}
}
return true;
}
bool readEncodeMatrix(const std::string& dirName, GGH15encoding& C, const mat_l& S, long i, long j,
TaggedCRTmatrix* toAptr, TDMatrixParams *params, int nNodes)
{
#ifdef DEBUGPRINT
cout << "begin encode matrix" << endl;
#endif // DEBUGPRINT
FHE_TIMER_START;
// FIXME: should we enforce i<j ??
assert(i>=0 && i<nNodes-1);
assert(j>0 && j<nNodes);
// Get the destination matrix A in explicit CRT form
TaggedCRTmatrix toA(params);
if (toAptr==NULL) toAptr = &toA;
bool isLast = (nNodes-1 == j);
GGH15node jNode, iNode;
#ifdef DEBUGPRINT
char cwd[1024];
getcwd(cwd, sizeof(cwd));
cout << "read node info, current directory = " << cwd << endl;
cout << "reading jNode" << endl;
#endif
// load the correct node
std::string fileName = "node"+ToString(j)+ ".dat";
FILE* handle = fopen(fileName.c_str(), "rb");
if (handle != 0)
{
jNode.readFromFile(handle, params);
fclose(handle);
}
#ifdef DEBUGPRINT
cout << "after reading jNode" << endl;
getcwd(cwd, sizeof(cwd));
cout << "read node info, current directory = " << cwd << endl;
#endif
//load An
CRTmatrix An;// The An matrix (actually n-by-1 "matrix")
fileName = "nAn.dat";
handle = fopen(fileName.c_str(), "rb");
if (handle != 0)
{
long n;
long count = fread(&n, sizeof(n),1, handle);
count+=An.readFromFile(handle, params);
fclose(handle);
}
else NTL::Error("Cannot read An");
// If the caller did not supply a good toA, then compute it now
if (toAptr->tag != j && !isLast) // If target is NOT the last node
{
jNode.getCRTA(toAptr->data);
toAptr->data.leftMultBy( jNode.getP() ); // set toA = P_j x A_j
toAptr->tag = j;
}
// Do the actual encoding
C.setFrom(i);
C.setTo(j);
printDot;
if (i == 0) // encoding wrt 0 -> ...
{
if (isLast) // encoding wrt 0 -> n
{
#ifdef DEBUGPRINT
cout << "encoding encodeGGH15_0_n" << endl;
#endif
encodeGGH15_0_n(C.getData(), S, An);
}
else // encoding wrt 0 -> j<n
{
#ifdef DEBUGPRINT
cout << "encoding first" << endl;
#endif
encodeGGH15first(C.getData(), S, jNode, toAptr->data);
}
}
else // encoding wrt 0<i -> ...
{
//get iNode - we are already in the correct directory
fileName = "node"+ToString(i)+ ".dat";
handle = fopen(fileName.c_str(), "rb");
if (handle != 0)
{
iNode.readFromFile(handle , params);
fclose(handle);
}
if (isLast) // encoding wrt 0<i -> n
{
#ifdef DEBUGPRINT
cout << "Encoding last" << endl;
#endif
encodeGGH15last(C.getData(), S, iNode, An);
}
else // encoding wrt 0<i -> j<n
{
encodeGGH15(C.getData(), S, iNode, jNode, toAptr->data);
}
}
return true;
}
// Encode plaintext matrix wrt the path i -> j. The toAptr argument is
// an optimization:
// + If toAptr is non-NULL and toAptr->tag==j, then it is assumed
// that to toAptr->data is the matrx P_j x A_j.
// + If toAptr is non-NULL and toAptr->tag!=j, then toA->data is set
// P_j x A_j and toAptr->tag is set to j.
bool GGH15mmaps::encode(GGH15encoding& C, const mat_l& S, long i, long j,
TaggedCRTmatrix* toAptr)
{
FHE_TIMER_START;
// FIXME: should we enforce i<j ??
assert(i>=0 && i<numNodes()-1);
assert(j>0 && j<numNodes());
// Get the destination matrix A in explicit CRT form
TaggedCRTmatrix toA(params);
if (toAptr==NULL) toAptr = &toA;
bool isLast = (numNodes()-1 == j);
// If the caller did not supply a good toA, then compute it now
if (toAptr->tag != j && !isLast) // If target is NOT the last node
{
nodes[j-1].getCRTA(toAptr->data);
toAptr->data.leftMultBy( nodes[j-1].getP() ); // set toA = P_j x A_j
toAptr->tag = j;
}
// Do the actual encoding
C.from = i;
C.to = j;
printDot;
if (i == 0) // encoding wrt 0 -> ...
{
if (isLast) // encoding wrt 0 -> n
encodeGGH15_0_n(C.data, S, An);
else // encoding wrt 0 -> j<n
encodeGGH15first(C.data, S, nodes[j-1], toAptr->data);
}
else // encoding wrt 0<i -> ...
{
if (isLast) // encoding wrt 0<i -> n
encodeGGH15last(C.data, S, nodes[i-1], An);
else // encoding wrt 0<i -> j<n
encodeGGH15(C.data, S, nodes[i-1], nodes[j-1], toAptr->data);
}
return true;
}
// A debugging method to check that an encoding was done right
bool GGH15mmaps::verifyEncoding(const GGH15encoding& C, const mat_l& S) const
{
FHE_TIMER_START;
long i = C.fromNode();
long j = C.toNode();
CRTmatrix CC(params), SS(params), toA(params), fromA(params);
if (i!=0)
{
nodes[i-1].getCRTA(fromA);
if (j!=(numNodes()-1)) // 0<i -> j<n
{
nodes[j-1].getCRTA(toA);
CC = nodes[i-1].getT();
CC *= C.data;
CC *= nodes[j-1].getTinv();
SS = nodes[i-1].getPinv();
SS *= S;
SS *= nodes[j-1].getP();
}
else // 0<i -> j=n
{
toA = An;
CC = nodes[i-1].getT();
CC *= C.data;
SS = nodes[i-1].getPinv();
SS *= S;
}
}
else // i==0
{
if (j!=(numNodes()-1)) // 0=i -> j<n
{
nodes[j-1].getCRTA(toA);
CC = C.data;
CC *= nodes[j-1].getTinv();
SS = nodes[j-1].getP();
SS.leftMultBy(S);
}
else // 0=i -> j=n
{
toA = An;
CC = C.data;
SS = S;
}
}
if (i>0) // Check that CC and EE = fromA*CC - SS*toA are small
{
fromA *= CC;
SS *= toA;
fromA -= SS;
return (CC.isSmall() && fromA.isSmall());
}
else // Check that EE = CC - SS*toA is small
{
SS *= toA;
CC -= SS;
return CC.isSmall();
}
}
bool operator==(const GGH15mmaps& A, const GGH15mmaps& B)
{
FHE_TIMER_START;
if (A.getParams()!=B.getParams())
return false;
if (A.numNodes()!=B.numNodes())
return false;
for (long iNode = 0; iNode < (A.numNodes()-2); iNode++)
{
if (A.getNode(iNode)!=B.getNode(iNode))
return false;
}
if (A.getAn()!=B.getAn())
return false;
return true;
}
// checks whether two nodes are identical, i.e., have the exact same
// variable values in them. if so, returns true.
bool operator==(const GGH15node& A, const GGH15node& B)
{
if (A.getIndex()!=B.getIndex())
return false;
if (A.getPrms()!=B.getPrms())
return false;
if (A.getA()!=B.getA())
return false;
if (A.getP()!=B.getP())
return false;
if (A.getPinv()!=B.getPinv())
return false;
if (A.getT()!=B.getT())
return false;
if (A.getTinv()!=B.getTinv())
return false;
return true;
}