-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimized-data-driven-control.cpp
executable file
·2279 lines (1806 loc) · 83 KB
/
optimized-data-driven-control.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Code for model-free control with behavioral paradigm.
*
* This is the optimized and updated functionality described in the paper "Data-driven control on encrypted data",
by Andreea B. Alexandru, Anastasios Tsiamis and George J. Pappas.
* Specifically, we reduce the memory from O((S+T)^2) ciphertexts to O((S+T)) ciphertext.
* As a result, this also reduces the number of operations (at least in big Oh notation)
* and the number of rotation keys.
*
* At every time step, we use both the offline precollected input-output trajectory, as well as the
* input-output trajectory collected at the previous time steps to determine the online feedback.
* The offline preprocessing (including a matrix inversion) is considered to be done separately; online,
* matrix-vector encrypted multiplications and summations, along with the inverse of rank-one updated
* matrix are performed.
*
*/
// Define PROFILE to enable TIC-TOC timing measurements
#define PROFILE
#include "palisade.h"
#include "Plant.h"
#include "helperControl.h"
using namespace lbcrypto;
// Specify IDENTITY_FOR_TYPE(T) in order to be able to construct an identity matrix
IDENTITY_FOR_TYPE(complex<double>)
void OnlineFeedback();
void OfflineFeedback();
int main()
{
/*
* Start collecting online measurements to compute the feedback after N steps;
* refresh at every multiple of Trefresh; then, after Tstop steps, do not collect
* new samples anymore. Uses APPROXRESCALE.
* Ask the client to compute 1/mSchur immediately after the server computes mSchur.
* This reduces the amount of computation and memory at the cloud (only computes
* the next M_1 once), reduces the noise in u (since the step where the rotated
* mSchur is multiplied to M_1 is not needed anymore) and reduces the depth of u by 1.
* Cut the levels from ciphertexts and keys as needed (after the final refresh).
* Add more parallelization.
*/
OnlineFbRfSt();
// OfflineFeedback();
// plantInitRoommm(2,5,5,true);
return 0;
}
void OnlineFeedback()
{
TimeVar t0,tt,t1,t2,t3;
TIC(t0);
TIC(tt);
double timeInit(0.0), timeEval(0.0), timeStep(0.0);
double timeClientmSchur(0.0),timeClientUpdate(0.0), timeClientDec(0.0), timeClientEnc(0.0);
double timeServer(0.0), timeServerUpdate(0.0), timeServerRefresh(0.0);
/*
* Simulation parameters
*/
uint32_t Ton = 8; // Number of online collected samples. Make sure Ton > 0.
uint32_t Tcont = 5; // Number of time steps to continue the computation after stopping the collection.
uint32_t Trefresh = 4; // Number of time steps after the server asks for refresh. Make sure 0 < Trefresh.
// If no refresh is wanted, please set Trefresh = Ton.
/*
* Initialize the plant
*/
uint32_t size = 2;
Plant<complex<double>>* plant = plantInitRoommm(size, Ton, Tcont);
uint32_t Tstop = plant->N + Ton;
uint32_t T = Tstop + Tcont;
// Scale M_1 up by a factor = scale (after t >= plant->N)
std::complex<double> scale = 100;
//////////////////////// Get inputs: r, yini, uini ////////////////////////
std::vector<complex<double>> r(plant->getr().GetRows()); // The setpoint
mat2Vec(plant->getr(), r);
std::vector<complex<double>> yini(plant->getyini().GetRows()); // This is the initial yini; from now on, we compute it encrypted
mat2Vec(plant->getyini(), yini);
std::vector<complex<double>> uini(plant->getuini().GetRows()); // This is the initial uini; from now on, we compute it encrypted
mat2Vec(plant->getuini(), uini);
//////////////////////// Got inputs: r, yini, uini ////////////////////////
//////////////////////// These are necessary only for the beginning of the algorithm: Kur, Kyini, Kuini ////////////////////////
// Which diagonals to extract depend on the relationship between the
// # of rows and the # of columns of the matrices
Matrix<complex<double>> Kur = plant->getKur();
Matrix<complex<double>> Kyini = plant->getKyini();
Matrix<complex<double>> Kuini = plant->getKuini();
size_t colsKur = Kur.GetCols(); size_t rowsKur = Kur.GetRows();
size_t colsKyini = Kyini.GetCols(); size_t rowsKyini = Kyini.GetRows();
size_t colsKuini = Kuini.GetCols(); size_t rowsKuini = Kuini.GetRows();
std::vector<std::vector<complex<double>>> dKur;
if (rowsKur >= colsKur) // tall
{
dKur.resize(colsKur);
#pragma omp parallel for
for (size_t i = 0; i < colsKur; i++)
dKur[i] = std::vector<complex<double>>(rowsKur);
mat2HybridDiags(Kur, dKur);
}
else // wide
if (colsKur % rowsKur == 0) // wideEf
{
dKur.resize(rowsKur);
#pragma omp parallel for
for (size_t i = 0; i < rowsKur; i ++)
dKur[i] = std::vector<complex<double>>(colsKur);
mat2HybridDiags(Kur, dKur);
}
else // plain wide
{
dKur.resize(colsKur);
for (size_t i = 0; i < colsKur; i ++)
dKur[i] = std::vector<complex<double>>(colsKur);
mat2Diags(Kur, dKur);
}
std::vector<std::vector<complex<double>>> dKyini;
if (rowsKyini >= colsKyini) // tall
{
dKyini.resize(colsKyini);
#pragma omp parallel for
for (size_t i = 0; i < colsKyini; i++)
dKyini[i] = std::vector<complex<double>>(rowsKyini);
mat2HybridDiags(Kyini, dKyini);
}
else // wide
if (colsKyini % rowsKyini == 0) // wideEf
{
dKyini.resize(rowsKyini);
#pragma omp parallel for
for (size_t i = 0; i < rowsKyini; i ++)
dKyini[i] = std::vector<complex<double>>(colsKyini);
mat2HybridDiags(Kyini, dKyini);
}
else // plain wide
{
dKyini.resize(colsKyini);
for (size_t i = 0; i < colsKyini; i ++)
dKyini[i] = std::vector<complex<double>>(colsKyini);
mat2Diags(Kyini, dKyini);
}
std::vector<std::vector<complex<double>>> dKuini;
if (rowsKuini >= colsKuini) // tall
{
dKuini.resize(colsKuini);
#pragma omp parallel for
for (size_t i = 0; i < colsKuini; i++)
dKuini[i] = std::vector<complex<double>>(rowsKuini);
mat2HybridDiags(Kuini, dKuini);
}
else // wide
if (colsKuini % rowsKuini == 0) // wideEf
{
dKuini.resize(rowsKuini);
#pragma omp parallel for
for (size_t i = 0; i < rowsKuini; i ++)
dKuini[i] = std::vector<complex<double>>(colsKuini);
mat2HybridDiags(Kuini, dKuini);
}
else // plain wide
{
dKuini.resize(colsKuini);
for (size_t i = 0; i < colsKuini; i ++)
dKuini[i] = std::vector<complex<double>>(colsKuini);
mat2Diags(Kuini, dKuini);
}
//////////////////////// These were necessary only for the beginning of the algorithm: Kur, Kyini, Kuini ////////////////////////
//////////////////////// Get inputs: M_1, HY, HU ////////////////////////
Matrix<complex<double>> M_1 = plant->getM_1(); // The initial inverse matrix = Yf'*Q*Yf + Uf'*R*Uf + lamy*Yp'*Yp + lamu*Up'*Up + lamg*I
Matrix<complex<double>> HY = plant->getHY(); // The matrix of Hankel matrix for output measurements used for past and future prediction
Matrix<complex<double>> HU = plant->getHU(); // The matrix of Hankel matrix for input measurements used for past and future prediction
int32_t S = M_1.GetRows();
int32_t Ty = HY.GetRows();
int32_t Tu = HU.GetRows();
// Transform M_1, HY and HU into column representation
std::vector<std::vector<complex<double>>> cM_1(S);
std::vector<std::vector<complex<double>>> cHY(S);
std::vector<std::vector<complex<double>>> cHU(S);
for (int32_t i = 0; i < S; i ++ )
{
cM_1[i] = std::vector<complex<double>>(S);
cHY[i] = std::vector<complex<double>>(Ty);
cHU[i] = std::vector<complex<double>>(Tu);
}
mat2Cols(M_1, cM_1);
mat2Cols(HY, cHY);
mat2Cols(HU, cHU);
// Transform Uf into row representation to use it in the computation of u* without adding an extra masking
Matrix<complex<double>> Uf = HU.ExtractRows(plant->pu, Tu-1);
std::vector<std::vector<complex<double>>> rUf(plant->m);
for (size_t i = 0; i < plant->m; i ++ )
rUf[i] = std::vector<complex<double>>(S);
mat2Rows(Uf.ExtractRows(0,plant->m-1), rUf);
int32_t maxSamp = S + max(Ton, plant->M);
cout << "S = " << S << ", maxSamp = " << maxSamp << endl;
//////////////////////// Got inputs: M_1, HY, HU ////////////////////////
//////////////////////// Get costs: Q, R, lamg, lamy, lamu ////////////////////////
Costs<complex<double>> costs = plant->getCosts();
std::vector<complex<double>> lamQ(plant->py, costs.lamy);
lamQ.insert(std::end(lamQ), std::begin(costs.diagQ), std::end(costs.diagQ));
std::vector<complex<double>> lamR(plant->pu, costs.lamu);
lamR.insert(std::end(lamR), std::begin(costs.diagR), std::end(costs.diagR));
std::vector<complex<double>> lamQ_sc = lamQ;
std::vector<complex<double>> lamR_sc = lamR;
for (size_t i = 0; i < lamQ_sc.size(); i ++)
lamQ_sc[i] /= scale;
for (size_t i = 0; i < lamR_sc.size(); i ++)
lamR_sc[i] /= scale;
//////////////////////// Get costs: Q, R, lamg, lamy, lamu ////////////////////////
// Step 1: Setup CryptoContext
// A. Specify main parameters
/* A1) Multiplicative depth:
* The CKKS scheme we setup here will work for any computation
* that has a multiplicative depth equal to 'multDepth'.
* This is the maximum possible depth of a given multiplication,
* but not the total number of multiplications supported by the
* scheme.
*
* For example, computation f(x, y) = x^2 + x*y + y^2 + x + y has
* a multiplicative depth of 1, but requires a total of 3 multiplications.
* On the other hand, computation g(x_i) = x1*x2*x3*x4 can be implemented
* either as a computation of multiplicative depth 3 as
* g(x_i) = ((x1*x2)*x3)*x4, or as a computation of multiplicative depth 2
* as g(x_i) = (x1*x2)*(x3*x4).
*
* For performance reasons, it's generally preferable to perform operations
* in the shortest multiplicative depth possible.
*/
/* For the online model-free control, we need a multDepth = 2t + 7 to compute
* the control action at time t. In this case, we assume that the client
* transmits uini and yini at each time step.
*/
int32_t multDepth;
if (Tstop < plant->N) // until the trajectories are concatenated, no deep computations are necessary
multDepth = 2;
else
multDepth = 2*(Trefresh-1) + 6; //2*(Tstop-plant->N-1) + 6;
cout << " # of time steps = " << T << ", refresh at time = " << Trefresh << "*k, " <<\
"stop collecting at time = " << Tstop <<", total circuit depth = " << multDepth << endl << endl;
/* A2) Bit-length of scaling factor.
* CKKS works for real numbers, but these numbers are encoded as integers.
* For instance, real number m=0.01 is encoded as m'=round(m*D), where D is
* a scheme parameter called scaling factor. Suppose D=1000, then m' is 10 (an
* integer). Say the result of a computation based on m' is 130, then at
* decryption, the scaling factor is removed so the user is presented with
* the real number result of 0.13.
*
* Parameter 'scaleFactorBits' determines the bit-length of the scaling
* factor D, but not the scaling factor itself. The latter is implementation
* specific, and it may also vary between ciphertexts in certain versions of
* CKKS (e.g., in EXACTRESCALE).
*
* Choosing 'scaleFactorBits' depends on the desired accuracy of the
* computation, as well as the remaining parameters like multDepth or security
* standard. This is because the remaining parameters determine how much noise
* will be incurred during the computation (remember CKKS is an approximate
* scheme that incurs small amounts of noise with every operation). The scaling
* factor should be large enough to both accommodate this noise and support results
* that match the desired accuracy.
*/
uint32_t scaleFactorBits = 53;
/* A3) Number of plaintext slots used in the ciphertext.
* CKKS packs multiple plaintext values in each ciphertext.
* The maximum number of slots depends on a security parameter called ring
* dimension. In this instance, we don't specify the ring dimension directly,
* but let the library choose it for us, based on the security level we choose,
* the multiplicative depth we want to support, and the scaling factor size.
*
* Please use method GetRingDimension() to find out the exact ring dimension
* being used for these parameters. Give ring dimension N, the maximum batch
* size is N/2, because of the way CKKS works.
*/
uint32_t batchSize = maxSamp; // what to display and for EvalSum.
// The ring dimension is 4*slots
uint32_t slots = 4096;
/* A4) Desired security level based on FHE standards.
* This parameter can take four values. Three of the possible values correspond
* to 128-bit, 192-bit, and 256-bit security, and the fourth value corresponds
* to "NotSet", which means that the user is responsible for choosing security
* parameters. Naturally, "NotSet" should be used only in non-production
* environments, or by experts who understand the security implications of their
* choices.
*
* If a given security level is selected, the library will consult the current
* security parameter tables defined by the FHE standards consortium
* (https://homomorphicencryption.org/introduction/) to automatically
* select the security parameters. Please see "TABLES of RECOMMENDED PARAMETERS"
* in the following reference for more details:
* http://homomorphicencryption.org/wp-content/uploads/2018/11/HomomorphicEncryptionStandardv1.1.pdf
*/
// SecurityLevel securityLevel = HEStd_128_classic;
SecurityLevel securityLevel = HEStd_NotSet;
RescalingTechnique rsTech = APPROXRESCALE;//EXACTRESCALE;
KeySwitchTechnique ksTech = HYBRID;
uint32_t dnum = 0;
uint32_t maxDepth = 3;
// This is the size of the first modulus
uint32_t firstModSize = 60;
uint32_t relinWin = 10;
MODE mode = OPTIMIZED; // Using ternary distribution
/*
* The following call creates a CKKS crypto context based on the arguments defined above.
*/
CryptoContext<DCRTPoly> cc =
CryptoContextFactory<DCRTPoly>::genCryptoContextCKKS(
multDepth,
scaleFactorBits,
batchSize,
securityLevel,
slots*4, // set this to zero when security level = HEStd_128_classic
rsTech,
ksTech,
dnum,
maxDepth,
firstModSize,
relinWin,
mode);
uint32_t RD = cc->GetRingDimension();
cout << "CKKS scheme is using ring dimension " << RD << endl;
uint32_t cyclOrder = RD*2;
cout << "CKKS scheme is using the cyclotomic order " << cyclOrder << endl;
cout << "scaleFactorBits = " << scaleFactorBits << ", scale = " << scale.real() << endl << endl;
// Enable the features that you wish to use
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
cc->Enable(LEVELEDSHE);
// B. Step 2: Key Generation
/* B1) Generate encryption keys.
* These are used for encryption/decryption, as well as in generating different
* kinds of keys.
*/
auto keys = cc->KeyGen();
/* B2) Generate the relinearization key
* In CKKS, whenever someone multiplies two ciphertexts encrypted with key s,
* we get a result with some components that are valid under key s, and
* with an additional component that's valid under key s^2.
*
* In most cases, we want to perform relinearization of the multiplicaiton result,
* i.e., we want to transform the s^2 component of the ciphertext so it becomes valid
* under original key s. To do so, we need to create what we call a relinearization
* key with the following line.
*/
cc->EvalMultKeyGen(keys.secretKey);
/* B3) Generate the rotation keys
* CKKS supports rotating the contents of a packed ciphertext, but to do so, we
* need to create what we call a rotation key. This is done with the following call,
* which takes as input a vector with indices that correspond to the rotation offset
* we want to support. Negative indices correspond to right shift and positive to left
* shift. Look at the output of this demo for an illustration of this.
*
* Keep in mind that rotations work on the entire ring dimension, not the specified
* batch size. This means that, if ring dimension is 8 and batch size is 4, then an
* input (1,2,3,4,0,0,0,0) rotated by 2 will become (3,4,0,0,0,0,1,2) and not
* (3,4,1,2,0,0,0,0). Also, as someone can observe in the output of this demo, since
* CKKS is approximate, zeros are not exact - they're just very small numbers.
*/
/*
* Find rotation indices
*/
//////////////////////// These are necessary only for the beginning of the algorithm: rotations for Kur, Kyini, Kuini ////////////////////////
int32_t maxNoRot = max(max(r.size(),yini.size()),uini.size());
std::vector<int> indexVec(maxNoRot-1);
std::iota (std::begin(indexVec), std::end(indexVec), 1);
//////////////////////// These were necessary only for the beginning of the algorithm: rotations for Kur, Kyini, Kuini ////////////////////////
//////////////////////// Rotations for computing new M_1 ////////////////////////
for (int32_t i = 0; i < maxSamp; i ++)
{
indexVec.push_back(i);
for (int32_t j = 0; j < maxSamp; j ++)
indexVec.push_back(i-j);
}
for (int32_t i = S; i < maxSamp; i ++)
{
indexVec.push_back(-i);
}
//////////////////////// Rotations for computing new M_1 ////////////////////////
//////////////////////// Rotations for u = U*M_1*Z ////////////////////////
for (int32_t i = 0; i < (int)plant->fu; i ++)
{
for (int32_t j = S; j <= maxSamp; j ++ )
indexVec.push_back((int)((plant->pu+i)*maxSamp-j));
}
//////////////////////// Rotations for u = U*M_1*Z ////////////////////////
//////////////////////// Rotations for constructing the last columns of HY and HU ////////////////////////
indexVec.push_back(maxSamp*plant->p); indexVec.push_back(maxSamp*plant->m);
indexVec.push_back(int(-Ty+plant->p)*maxSamp); indexVec.push_back(int(-Tu+plant->m)*maxSamp);
//////////////////////// Rotations for constructing the last columns of HY and HU ////////////////////////
// remove any duplicate indices to avoid the generation of extra automorphism keys
sort( indexVec.begin(), indexVec.end() );
indexVec.erase( std::unique( indexVec.begin(), indexVec.end() ), indexVec.end() );
//remove automorphisms corresponding to 0
indexVec.erase(std::remove(indexVec.begin(), indexVec.end(), 0), indexVec.end());
auto EvalRotKeys = cc->GetEncryptionAlgorithm()->EvalAtIndexKeyGen(nullptr, keys.secretKey, indexVec);
//////////////////////// Rotations for refreshing M_1 ////////////////////////
indexVec.clear();
for (int32_t k = 1; k <= int((Ton-1)/Trefresh); k ++)
{
for (int32_t i = 0; i < (int)(S + k*Trefresh); i ++)
{
indexVec.push_back( -(int)(i*(S + k*Trefresh)) );
// indexVec.push_back( -(int)(i*maxSamp) ); // this would be better if all rotation keys would be together
}
}
// remove any duplicate indices to avoid the generation of extra automorphism keys
sort( indexVec.begin(), indexVec.end() );
indexVec.erase( std::unique( indexVec.begin(), indexVec.end() ), indexVec.end() );
//remove automorphisms corresponding to 0
indexVec.erase(std::remove(indexVec.begin(), indexVec.end(), 0), indexVec.end());
// If refreshing is done multiple times, then there should be different keys, as they do not repeat, and could be deleted afterwards
auto EvalPackKeys = cc->GetEncryptionAlgorithm()->EvalAtIndexKeyGen(nullptr, keys.secretKey, indexVec);
if (Trefresh > 1 && Trefresh < Ton)
CompressEvalKeys(*EvalPackKeys, (2*(Trefresh-1)+5));
indexVec.clear();
for (int32_t k = 1; k <= int((Ton-1)/Trefresh); k ++)
{
for (int32_t i = 0; i < (int)(S + k*Trefresh); i ++)
{
indexVec.push_back( (int)(i*(S + k*Trefresh)) );
}
}
// remove any duplicate indices to avoid the generation of extra automorphism keys
sort( indexVec.begin(), indexVec.end() );
indexVec.erase( std::unique( indexVec.begin(), indexVec.end() ), indexVec.end() );
//remove automorphisms corresponding to 0
indexVec.erase(std::remove(indexVec.begin(), indexVec.end(), 0), indexVec.end());
// If refreshing is done multiple times, then there should be different keys, as they do not repeat, and could be deleted afterwards
auto EvalUnpackKeys = cc->GetEncryptionAlgorithm()->EvalAtIndexKeyGen(nullptr,keys.secretKey, indexVec);
//////////////////////// Rotations for refreshing M_1 ////////////////////////
//////////////////////// Rotations for inner products with rotated elements for time steps after Tstop ////////////////////////
indexVec.clear();
// // REDUNDANCY, not all are needed for EvalSumRotBatch - choose one method and compute only the associated rotations
// for (int32_t i = 0; i < max(Tu,Ty); i ++)
// {
// indexVec.push_back(i*maxSamp);
// }
for (int32_t i = std::ceil(std::log2(max(Tu,Ty)))-1; i >= 0; i --)
indexVec.push_back(pow(2,i)*maxSamp);
// remove any duplicate indices to avoid the generation of extra automorphism keys
sort( indexVec.begin(), indexVec.end() );
indexVec.erase( std::unique( indexVec.begin(), indexVec.end() ), indexVec.end() );
//remove automorphisms corresponding to 0
indexVec.erase(std::remove(indexVec.begin(), indexVec.end(), 0), indexVec.end());
auto EvalSumRotKeys = cc->GetEncryptionAlgorithm()->EvalAtIndexKeyGen(nullptr,keys.secretKey, indexVec);
//////////////////////// Rotations for inner products with rotated elements ////////////////////////
//////////////////////// Rotations for constructing the new yini and uini and the last columns of HY and HU ////////////////////////
indexVec.push_back(maxSamp*plant->p); indexVec.push_back(maxSamp*plant->m);
indexVec.push_back(int(-plant->py+plant->p)*maxSamp); indexVec.push_back(int(-plant->pu+plant->m)*maxSamp);
for(int32_t i = 1; i < (int)plant->m; i ++) // for u after t>=Tstop
indexVec.push_back(-i*maxSamp);
// remove any duplicate indices to avoid the generation of extra automorphism keys
sort( indexVec.begin(), indexVec.end() );
indexVec.erase( std::unique( indexVec.begin(), indexVec.end() ), indexVec.end() );
//remove automorphisms corresponding to 0
indexVec.erase(std::remove(indexVec.begin(), indexVec.end(), 0), indexVec.end());
auto EvalIniRotKeys = cc->GetEncryptionAlgorithm()->EvalAtIndexKeyGen(nullptr,keys.secretKey, indexVec);
//////////////////////// Rotations for constructing the new yini and uini and the last columns of HY and HU ////////////////////////
// Step 3: Encoding and encryption of inputs
/*
* Encoding as plaintexts
*/
// Vectors r, yini and uini need to be repeated in the packed plaintext for the first time step and each element has to be repeated for S+Ton times for the following time steps
std::vector<std::complex<double>> rep_r(Fill(r,slots));
Plaintext ptxt_rep_r = cc->MakeCKKSPackedPlaintext(rep_r);
std::vector<std::complex<double>> zero_r(Ty*maxSamp);
for (int32_t i = 0; i < (int)plant->fy; i ++) //// This can be replaced by online rotations if storage is too large
{
for (int32_t j = 0; j < maxSamp; j ++)
zero_r[plant->py*maxSamp + i*maxSamp + j] = r[i];
}
Plaintext ptxt_r = cc->MakeCKKSPackedPlaintext(zero_r);
std::vector<std::complex<double>> rep_yini(Fill(yini,slots));
Plaintext ptxt_rep_yini = cc->MakeCKKSPackedPlaintext(rep_yini);
std::vector<std::complex<double>> repSamp_yini(plant->py*maxSamp);
for (int32_t i = 0; i < (int)plant->py; i ++)
{
for (int32_t j = 0; j < max(S+(int)plant->M,maxSamp); j ++) // each element needs to be repeated as many times as it will remain in yini => S+plant->M
repSamp_yini[i*maxSamp + j] = yini[i];
}
Plaintext ptxt_yini = cc->MakeCKKSPackedPlaintext(repSamp_yini);
std::vector<std::complex<double>> rep_uini(Fill(uini,slots));
Plaintext ptxt_rep_uini = cc->MakeCKKSPackedPlaintext(rep_uini);
std::vector<std::complex<double>> repSamp_uini(plant->pu*maxSamp);
for (int32_t i = 0; i < (int)plant->pu; i ++)
{
for (int32_t j = 0; j < max(S+(int)plant->M,maxSamp); j ++) // each element needs to be repeated as many times as it will remain in uini => S+plant->M
repSamp_uini[i*maxSamp + j] = uini[i];
}
Plaintext ptxt_uini = cc->MakeCKKSPackedPlaintext(repSamp_uini);
Plaintext ptxt_u, ptxt_y;
//////////////////////// These are necessary only for the beginning of the algorithm: encryptions for Kur, Kyini, Kuini ////////////////////////
std::vector<Plaintext> ptxt_dKur(dKur.size());
# pragma omp parallel for
for (size_t i = 0; i < dKur.size(); ++i)
ptxt_dKur[i] = cc->MakeCKKSPackedPlaintext(dKur[i]);
std::vector<Plaintext> ptxt_dKuini(dKuini.size());
# pragma omp parallel for
for (size_t i = 0; i < dKuini.size(); ++i)
ptxt_dKuini[i] = cc->MakeCKKSPackedPlaintext(dKuini[i]);
std::vector<Plaintext> ptxt_dKyini(dKyini.size());
# pragma omp parallel for
for (size_t i = 0; i < dKyini.size(); ++i)
ptxt_dKyini[i] = cc->MakeCKKSPackedPlaintext(dKyini[i]);
//////////////////////// These were necessary only for the beginning of the algorithm: encryptions for Kur, Kyini, Kuini ////////////////////////
//////////////////////// Construct plaintexts for M_1, HY, HU, Q, R, lambdas ////////////////////////
std::vector<Plaintext> ptxt_cHY(S);
for (int32_t i = 0; i < S; ++i)
{
std::vector<std::complex<double>> repSamp_HY(Ty*maxSamp); // repeat entries of each column of HY
for (int32_t j = 0; j < Ty; j ++)
{
for (int32_t k = 0; k < maxSamp; k ++)
repSamp_HY[j*maxSamp + k] = cHY[i][j];
}
ptxt_cHY[i] = cc->MakeCKKSPackedPlaintext(repSamp_HY);
}
std::vector<Plaintext> ptxt_cHU(S);
for (int32_t i = 0; i < S; ++i)
{
std::vector<std::complex<double>> repSamp_HU(Tu*maxSamp); // repeat entries of each column of HU
for (int32_t j = 0; j < Tu; j ++)
{
for (int32_t k = 0; k < maxSamp; k ++)
repSamp_HU[j*maxSamp + k] = cHU[i][j];
}
ptxt_cHU[i] = cc->MakeCKKSPackedPlaintext(repSamp_HU);
}
std::vector<Plaintext> ptxt_M_1(S);
for (int32_t i = 0; i < S; ++i)
{
std::vector<std::complex<double>> scM_1 = cM_1[i];
std::transform(scM_1.begin(), scM_1.end(), scM_1.begin(), [&scale](std::complex<double>& c){return c*scale;});
ptxt_M_1[i] = cc->MakeCKKSPackedPlaintext(scM_1);
}
std::vector<Plaintext> ptxt_rUf(plant->m);
for (size_t i = 0; i < plant->m; ++i)
{
ptxt_rUf[i] = cc->MakeCKKSPackedPlaintext(rUf[i]);
}
std::vector<std::complex<double>> repSamp_lamQ(Ty*maxSamp); // repeat entries of lamQ
for (int32_t i = 0; i < Ty; i ++)
{
for (int32_t j = 0; j < maxSamp; j ++)
repSamp_lamQ[i*maxSamp + j] = lamQ[i];
}
Plaintext ptxt_lamQ = cc->MakeCKKSPackedPlaintext(repSamp_lamQ);
std::vector<std::complex<double>> repSamp_lamR(Tu*maxSamp); // repeat entries of lamR
for (int32_t i = 0; i < Tu; i ++)
{
for (int32_t j = 0; j < maxSamp; j ++)
repSamp_lamR[i*maxSamp + j] = lamR[i];
}
Plaintext ptxt_lamR = cc->MakeCKKSPackedPlaintext(repSamp_lamR);
Plaintext ptxt_lamg = cc->MakeCKKSPackedPlaintext(std::vector<complex<double>>(maxSamp, costs.lamg));
std::vector<Plaintext> ptxt_1(maxSamp); //// This can be replaced by online rotations if storage is too large
std::vector<complex<double>> zero_vec(maxSamp,0);
for (int32_t i = 0; i < maxSamp; i ++)
{
std::vector<complex<double>> elem_vec = zero_vec;
elem_vec[i] = 1;
ptxt_1[i] = cc->MakeCKKSPackedPlaintext(elem_vec);
}
Plaintext ptxt_scale = cc->MakeCKKSPackedPlaintext({scale});
std::vector<complex<double>> ones(maxSamp,1);
Plaintext ptxt_1_v = cc->MakeCKKSPackedPlaintext(ones);
for (int32_t i = 0; i < Ty; i ++) // repeat entries of sclamQ
{
for (int32_t j = 0; j < maxSamp; j ++)
repSamp_lamQ[i*maxSamp + j] = lamQ_sc[i];
}
Plaintext ptxt_sclamQ = cc->MakeCKKSPackedPlaintext(repSamp_lamQ);
for (int32_t i = 0; i < Tu; i ++)
{
for (int32_t j = 0; j < maxSamp; j ++)
repSamp_lamR[i*maxSamp + j] = lamR_sc[i];
}
Plaintext ptxt_sclamR = cc->MakeCKKSPackedPlaintext(repSamp_lamR);
//////////////////////// Constructed plaintexts for M_1, HY, HU, Q, R, lambdas ////////////////////////
/*
* Encrypt the encoded vectors
*/
//////////////////////// The values for the first iterations until trajectory concatenation ////////////////////////
auto ctxt_rep_r = cc->Encrypt(keys.publicKey, ptxt_rep_r);
auto ctxt_rep_yini = cc->Encrypt(keys.publicKey, ptxt_rep_yini);
auto ctxt_rep_uini = cc->Encrypt(keys.publicKey, ptxt_rep_uini);
std::vector<Ciphertext<DCRTPoly>> ctxt_dKur(dKur.size());
# pragma omp parallel for
for (size_t i = 0; i < dKur.size(); ++i){
ctxt_dKur[i] = cc->Encrypt(keys.publicKey, ptxt_dKur[i]);
}
std::vector<Ciphertext<DCRTPoly>> ctxt_dKyini(dKyini.size());
# pragma omp parallel for
for (size_t i = 0; i < dKyini.size(); ++i){
ctxt_dKyini[i] = cc->Encrypt(keys.publicKey, ptxt_dKyini[i]);
}
std::vector<Ciphertext<DCRTPoly>> ctxt_dKuini(dKuini.size());
# pragma omp parallel for
for (size_t i = 0; i < dKuini.size(); ++i){
ctxt_dKuini[i] = cc->Encrypt(keys.publicKey, ptxt_dKuini[i]);
}
//////////////////////// The values for the first iterations until trajectory concatenation ////////////////////////
auto ctxt_r = cc->Encrypt(keys.publicKey, ptxt_r);
Ciphertext<DCRTPoly> ctxt_yini, ctxt_uini;
std::vector<Ciphertext<DCRTPoly>> ctxt_cHY(S);
# pragma omp parallel for
for (int32_t i = 0; i < S; ++ i)
ctxt_cHY[i] = cc->Encrypt(keys.publicKey, ptxt_cHY[i]);
std::vector<Ciphertext<DCRTPoly>> ctxt_cHU(S);
# pragma omp parallel for
for (int32_t i = 0; i < S; ++ i)
ctxt_cHU[i] = cc->Encrypt(keys.publicKey, ptxt_cHU[i]);
std::vector<Ciphertext<DCRTPoly>> ctxt_M_1(S);
# pragma omp parallel for
for (int32_t i = 0; i < S; ++ i)
ctxt_M_1[i] = cc->Encrypt(keys.publicKey, ptxt_M_1[i]);
std::vector<Ciphertext<DCRTPoly>> ctxt_rUf(plant->m);
# pragma omp parallel for
for (size_t i = 0; i < plant->m; ++ i)
ctxt_rUf[i] = cc->Encrypt(keys.publicKey, ptxt_rUf[i]);
Ciphertext<DCRTPoly> ctxt_scale = cc->Encrypt(keys.publicKey, ptxt_scale);
timeInit = TOC(tt);
cout << "Time for offline key generation, encoding and encryption: " << timeInit << " ms" << endl;
// Step 4: Evaluation
TIC(tt);
Ciphertext<DCRTPoly> ctxt_y, ctxt_u;
Ciphertext<DCRTPoly> ctxt_mSchur, ctxt_mSchur_1, ctxt_v_mSchur_1, ctxt_scaled_mSchur_1;
std::vector<Ciphertext<DCRTPoly>> ctxt_mVec(S), ctxt_mVec_s(S);
Ciphertext<DCRTPoly> ctxt_M_1mVec, ctxt_M_1mVec_s;
std::vector<Ciphertext<DCRTPoly>> ctxt_mMMm(S);
Ciphertext<DCRTPoly> ctxt_M_1_packed;
// This is necessary to start creating the next column in HU and HY
std::vector<complex<double>> temp_u = mat2Vec(plant->getHU().ExtractCol(S-1));
std::vector<complex<double>> temp_y = mat2Vec(plant->getHY().ExtractCol(S-1));
// Update temp_u, temp_y with uini and yini
for (size_t j = 0; j < plant->fu; j ++)
temp_u[j] = temp_u[j+plant->pu];
for (size_t j = plant->fu; j < plant->pu+plant->fu; j ++)
temp_u[j] = uini[j-plant->fu];
for (size_t j = 0; j < plant->fy; j ++)
temp_y[j] = temp_y[j+plant->py];
for (size_t j = plant->fy; j < plant->py+plant->fy; j ++)
temp_y[j] = yini[j-plant->fy];
std::vector<std::complex<double>> repSamp_tempy(Ty*maxSamp); // repeat entries of temp_y
for (int32_t i = 0; i < Ty; i ++)
{
for (int32_t j = 0; j < maxSamp; j ++)
repSamp_tempy[i*maxSamp + j] = temp_y[i];
}
Plaintext ptxt_temp_y = cc->MakeCKKSPackedPlaintext(repSamp_tempy);
Ciphertext<DCRTPoly> ctxt_temp_y = cc->Encrypt(keys.publicKey, ptxt_temp_y);
std::vector<std::complex<double>> repSamp_tempu(Tu*maxSamp); // repeat entries of temp_u
for (int32_t i = 0; i < Tu; i ++)
{
for (int32_t j = 0; j < maxSamp; j ++)
repSamp_tempu[i*maxSamp + j] = temp_u[i];
}
Plaintext ptxt_temp_u = cc->MakeCKKSPackedPlaintext(repSamp_tempu);
Ciphertext<DCRTPoly> ctxt_temp_u = cc->Encrypt(keys.publicKey, ptxt_temp_u);
// Start online computations
for (size_t t = 0; t < T; t ++)
{
cout << "t = " << t << endl << endl;
TIC(t2);
TIC(t1);
if (t < plant->N) // until the trajectory concatenation
{
// Matrix-vector multiplication for Kur*r
Ciphertext<DCRTPoly> result_r;
if ( rowsKur >= colsKur ) // tall
result_r = EvalMatVMultTall(ctxt_dKur, ctxt_rep_r, *EvalRotKeys, rowsKur, colsKur);
else // wide
if ( colsKur % rowsKur == 0) // wide Ef
{
result_r = EvalMatVMultWideEf(ctxt_dKur, ctxt_rep_r, *EvalRotKeys, rowsKur, colsKur);
}
else // plain wide
result_r = EvalMatVMultWide(ctxt_dKur, ctxt_rep_r, *EvalRotKeys, rowsKur, colsKur);
// Matrix-vector multiplication for Kyini*yini;
Ciphertext<DCRTPoly> result_y;
if ( rowsKyini >= colsKyini ) // tall
result_y = EvalMatVMultTall(ctxt_dKyini, ctxt_rep_yini, *EvalRotKeys, rowsKyini, colsKyini);
else // wide
if ( colsKyini % rowsKyini == 0) // wide Ef
result_y = EvalMatVMultWideEf(ctxt_dKyini, ctxt_rep_yini, *EvalRotKeys, rowsKyini, colsKyini);
else // plain wide
result_y = EvalMatVMultWide(ctxt_dKyini, ctxt_rep_yini, *EvalRotKeys, rowsKyini, colsKyini);
// Matrix-vector multiplication for Kuini*uini;
Ciphertext<DCRTPoly> result_u;
if ( rowsKuini >= colsKuini ) // tall
result_u = EvalMatVMultTall(ctxt_dKuini, ctxt_rep_uini, *EvalRotKeys, rowsKuini, colsKuini);
else // wide
if ( colsKuini % rowsKuini == 0) // wide Ef
result_u = EvalMatVMultWideEf(ctxt_dKuini, ctxt_rep_uini, *EvalRotKeys, rowsKuini, colsKuini);
else // plain wide
result_u = EvalMatVMultWide(ctxt_dKuini, ctxt_rep_uini, *EvalRotKeys, rowsKuini, colsKuini);
// Add the components
ctxt_u = cc->EvalAdd ( result_u, result_y );
ctxt_u = cc->EvalAdd ( ctxt_u, result_r );
}
else // t>=plant->N
{
if (t < Tstop)
{
ctxt_mSchur = EvalSumRotBatch( cc->EvalAdd (cc->EvalMult( cc->EvalMult( ctxt_cHY[S], ctxt_cHY[S] ), ptxt_lamQ ),\
cc->EvalMult( cc->EvalMult( ctxt_cHU[S], ctxt_cHU[S] ), ptxt_lamR ) ), *EvalSumRotKeys, max(Tu,Ty), maxSamp );
ctxt_mSchur = cc->EvalAdd( ctxt_mSchur, ptxt_lamg );
ctxt_mSchur = cc->Rescale(ctxt_mSchur);
# pragma omp parallel for
for (int32_t i = 0; i < S; i ++)
{
ctxt_mVec[i] = EvalSumRotBatch( cc->EvalAdd (cc->EvalMult( cc->EvalMult( ctxt_cHY[S], ctxt_cHY[i] ), ptxt_lamQ ),\
cc->EvalMult( cc->EvalMult( ctxt_cHU[S], ctxt_cHU[i] ), ptxt_lamR ) ), *EvalSumRotKeys, max(Tu,Ty), maxSamp );
}
# pragma omp parallel for
for (int32_t i=0; i < S; i++)
{
ctxt_mVec[i] = cc->Rescale(cc->Rescale(ctxt_mVec[i]));
}
// scaled copy
# pragma omp parallel for
for (int32_t i = 0; i < S; i ++)
{
ctxt_mVec_s[i] = EvalSumRotBatch( cc->EvalAdd (cc->EvalMult( cc->EvalMult( ctxt_cHY[S], ctxt_cHY[i] ), ptxt_sclamQ ),\
cc->EvalMult( cc->EvalMult( ctxt_cHU[S], ctxt_cHU[i] ), ptxt_sclamR ) ), *EvalSumRotKeys, max(Tu,Ty), maxSamp );
}
# pragma omp parallel for
for (int32_t i=0; i < S; i++)
{
ctxt_mVec_s[i] = cc->Rescale(cc->Rescale(ctxt_mVec_s[i]));
}
std::vector<Ciphertext<DCRTPoly>> ctxt_temp_vec(S+1);
ctxt_M_1mVec = cc->EvalMult(ctxt_M_1[0], ctxt_mVec[0]);
# pragma omp parallel for
for (int32_t i = 1; i < S; i ++)
{
ctxt_temp_vec[i] = cc->EvalMult(ctxt_M_1[i], ctxt_mVec[i]);
}
for (int32_t i = 1; i < S; i ++)
{
ctxt_M_1mVec = cc->EvalAdd(ctxt_M_1mVec, ctxt_temp_vec[i]);
}
ctxt_M_1mVec = cc->Rescale(ctxt_M_1mVec);
// scaled copy
ctxt_M_1mVec_s = cc->EvalMult(ctxt_M_1[0], ctxt_mVec_s[0]);
# pragma omp parallel for
for (int32_t i = 1; i < S; i ++)
{
ctxt_temp_vec[i] = cc->EvalMult(ctxt_M_1[i], ctxt_mVec_s[i]);
}
for (int32_t i = 1; i < S; i ++)
{
ctxt_M_1mVec_s = cc->EvalAdd( ctxt_M_1mVec_s, ctxt_temp_vec[i] );
}
ctxt_M_1mVec_s = cc->Rescale(ctxt_M_1mVec_s);
// in the first step where online samples are collected, it is better to perform mVec * (M_1 * mVec);
// afterwards, it is better to reorder M_1 * (mVec * mVec)
if (ctxt_M_1[0]->GetElements()[0].GetParams()->GetParams().size() > ctxt_mVec[0]->GetElements()[0].GetParams()->GetParams().size())
{
// Keep the smallest depth for precision
auto M_1mVecPrecomp = cc->GetEncryptionAlgorithm()->EvalFastRotationPrecompute( ctxt_M_1mVec );
ctxt_mSchur = cc->EvalSub( ctxt_mSchur, cc->EvalMult( ctxt_mVec_s[0], ctxt_M_1mVec ) );
# pragma omp parallel for
for(int32_t i = 1; i < S; i ++)
{
ctxt_temp_vec[i] = cc->EvalMult( ctxt_mVec_s[i], cc->GetEncryptionAlgorithm()->EvalFastRotation( ctxt_M_1mVec, i, cyclOrder, M_1mVecPrecomp, *EvalRotKeys ) );
}
for(int32_t i = 1; i < S; i ++)
{
ctxt_mSchur = cc->EvalSub( ctxt_mSchur, ctxt_temp_vec[i]);
}
ctxt_mSchur = cc->Rescale(ctxt_mSchur);
}
else
{
Ciphertext<DCRTPoly> ctxt_mVecv_s = cc->EvalMult(ctxt_mVec_s[0], ptxt_1[0]);
# pragma omp parallel for
for (int32_t i = 1; i < S; i ++)
{
ctxt_temp_vec[i] = cc->EvalMult(ctxt_mVec_s[i], ptxt_1[i]) ;
}
for (int32_t i = 1; i < S; i ++)
{
ctxt_mVecv_s = cc->EvalAdd( ctxt_mVecv_s, ctxt_temp_vec[i] );
}
ctxt_mVecv_s = cc->Rescale(ctxt_mVecv_s);
Ciphertext<DCRTPoly> ctxt_tempSum = cc->EvalMult(ctxt_M_1[0], cc->Rescale(cc->EvalMult(ctxt_mVecv_s, ctxt_mVec[0])));
# pragma omp parallel for
for (int32_t i = 1; i < S; i ++)
{
ctxt_temp_vec[i] = cc->EvalMult(ctxt_M_1[i], cc->Rescale(cc->EvalMult(ctxt_mVecv_s, ctxt_mVec[i])));
}
for (int32_t i = 1; i < S; i ++)
{
ctxt_tempSum = cc->EvalAdd( ctxt_tempSum, ctxt_temp_vec[i] );
}
ctxt_mSchur = cc->EvalSub( ctxt_mSchur, EvalSumExact(ctxt_tempSum, *EvalRotKeys, S) ); //cc->EvalSum( ctxt_tempSum, S) ); the latter assumes trailing zeros until the closest power of two to S
ctxt_mSchur = cc->Rescale(ctxt_mSchur);
}
timeServer = TOC(t1);
cout << "Time for computing mSchur at the server at step " << t << ": " << timeServer << " ms" << endl;
///////////// At the client: compute 1/mSchur and send it back.
TIC(t1);
Plaintext ptxt_Schur;
complex<double> mSchur_1;
cc->Decrypt(keys.secretKey, ctxt_mSchur, &ptxt_Schur);
ptxt_Schur->SetLength(1);
mSchur_1 = double(1)/(ptxt_Schur->GetCKKSPackedValue()[0]);
std::vector<complex<double>> v_mSchur_1 = {mSchur_1};
ctxt_v_mSchur_1 = cc->Encrypt(keys.publicKey, cc->MakeCKKSPackedPlaintext(RepeatElements(v_mSchur_1, 1, S+1),1,0));
ctxt_mSchur_1 = cc->Encrypt(keys.publicKey, cc->MakeCKKSPackedPlaintext({mSchur_1},1,0));