forked from CryptoExperts/FV-NFLlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FV.hpp
1082 lines (961 loc) · 28.7 KB
/
FV.hpp
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
/**
*
* This file is part of FV-NFLlib
*
* Copyright (C) 2015, 2016 CryptoExperts
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include <cstddef>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <memory>
#include <nfl.hpp>
namespace FV {
// Parameters (need to be defined before inclusion)
//
// @param poly_t (nfl::poly)
// @param plaintextModulus (mpz_class)
// @param gauss_struct (nfl::gaussian)
// @param fg_prng_sk (FastGaussianNoise)
// @param fg_prng_evk (FastGaussianNoise)
// @param fg_prng_pk (FastGaussianNoise)
// @param fg_prng_enc (FastGaussianNoise)
//
namespace params {
using poly_p =
nfl::poly_p<typename poly_t::value_type, poly_t::degree, poly_t::nmoduli>;
using polyZ_p = nfl::poly<typename poly_t::value_type, poly_t::degree,
poly_t::nmoduli * 2 + 1>;
}
class sk_t;
class pk_t;
class evk_t;
class ciphertext_t;
template <typename T>
class message_t;
using mess_t = message_t<mpz_class>;
} // namespace FV
/**
* Headers of utilitary functions implemented at the end of the document
*/
namespace FV {
namespace util {
inline void center(mpz_t &rop, mpz_t const &op1, mpz_t const &op2,
mpz_t const &op2Div2);
inline void div_and_round(mpz_t &rop, mpz_t const &op1, mpz_t const &op2,
mpz_t const &op2Div2);
template <size_t degree>
inline void reduce(std::array<mpz_t, degree> &coefficients, mpz_t multiplier,
mpz_t const &divisor, mpz_t const &divisorDiv2,
mpz_t const &mod_init, mpz_t const &mod_initDiv2);
inline void lift(std::array<mpz_t, params::polyZ_p::degree> &coefficients,
params::polyZ_p const &c);
void convert(params::polyZ_p &new_c, params::poly_p const &c,
bool ntt_form = true);
template <typename T>
T message_from_mpz_t(mpz_t value);
} // namespace util
} // namespace FV
/**
* Class to store the secret key
*/
namespace FV {
class sk_t {
using P = params::poly_p;
public:
/// The secret key is a polynomial
P value{params::gauss_struct(¶ms::fg_prng_sk)};
P value_shoup;
/// Constructor
sk_t() {
value.ntt_pow_phi(); // store in NTT form
value_shoup = nfl::compute_shoup(value);
}
};
} // namespace FV
/**
* Class to store the evaluation key
*/
namespace FV {
class evk_t {
using P = params::poly_p;
public:
size_t ell; // log_2(q)+1
size_t word_size;
mpz_t word;
mpz_t word_mask;
P **values;
P **values_shoup;
mpz_t qDivBy2;
mpz_t bigmodDivBy2;
/// Constructor
evk_t(sk_t const &sk, size_t word_size) : word_size(word_size) {
mpz_inits(qDivBy2, bigmodDivBy2, word, word_mask, nullptr);
ell = floor(mpz_sizeinbase(P::moduli_product(), 2) / word_size) + 1;
mpz_fdiv_q_ui(qDivBy2, P::moduli_product(), 2);
mpz_fdiv_q_ui(bigmodDivBy2, params::polyZ_p::moduli_product(), 2);
// Word and word mask
mpz_set_ui(word, 1);
mpz_mul_2exp(word, word, word_size);
mpz_sub_ui(word_mask, word, 1);
// Temporary values that will contain word^i
mpz_t tmp_word;
mpz_init_set_ui(tmp_word, 1);
P add;
// Allocate values
values = (P **)malloc(ell * sizeof(P *));
values_shoup = (P **)malloc(ell * sizeof(P *));
for (size_t i = 0; i < ell; ++i) {
values[i] = new P[2];
values[i][1] = nfl::uniform();
values[i][0] = params::gauss_struct(¶ms::fg_prng_evk);
values[i][0].ntt_pow_phi();
values[i][0] = values[i][0] - values[i][1] * sk.value;
add = tmp_word;
add.ntt_pow_phi();
add = add * sk.value * sk.value;
values[i][0] = values[i][0] + add;
values_shoup[i] = new P[2];
values_shoup[i][1] = nfl::compute_shoup(values[i][1]);
values_shoup[i][0] = nfl::compute_shoup(values[i][0]);
// Update for next loop
mpz_mul_2exp(tmp_word, tmp_word, word_size);
}
// Clean
mpz_clear(tmp_word);
}
/// Destructor
~evk_t() {
mpz_clears(qDivBy2, bigmodDivBy2, word, word_mask, nullptr);
for (size_t i = 0; i < ell; i++) {
delete[] values[i];
delete[] values_shoup[i];
}
free(values);
free(values_shoup);
}
};
} // namespace FV
/**
* Class to store the public key
*/
namespace FV {
class pk_t {
using P = params::poly_p;
public:
/// Public key elements
P a, b, delta;
P a_shoup, b_shoup, delta_shoup;
/// Link to evaluation key
evk_t *evk;
/// Interesting value
long noise_max;
/// Constructor
pk_t(sk_t const &sk, evk_t const &evaluation_key) {
// Link to the evaluation key
evk = (evk_t *)&evaluation_key;
// random a (already in NTT form)
a = nfl::uniform();
a_shoup = nfl::compute_shoup(a);
// b = small - a*sk
b = params::gauss_struct(¶ms::fg_prng_pk);
b.ntt_pow_phi(); // transform via NTT
b = b - a * sk.value;
b_shoup = nfl::compute_shoup(b);
// Set the plaintext modulus
noise_max =
mpz_sizeinbase(P::moduli_product(), 2) - 1 -
mpz_sizeinbase(params::plaintextModulus<mpz_class>::value().get_mpz_t(),
2);
// Define delta the polynomial of constant coeff = floor(modulus / plaintext
// modulus)
mpz_class Delta;
mpz_fdiv_q(Delta.get_mpz_t(), P::moduli_product(),
params::plaintextModulus<mpz_class>::value().get_mpz_t());
delta = Delta;
delta.ntt_pow_phi();
delta_shoup = nfl::compute_shoup(delta);
}
};
} // namespace FV
/**
* Class to store a ciphertext
*/
namespace FV {
class ciphertext_t {
using P = params::poly_p;
using PZ = params::polyZ_p;
public:
typedef P type;
/// ciphertext_t contains two polynomials (c0, c1)
P c0, c1;
/// Link to public key
pk_t *pk = nullptr;
/// Boolean if the ciphertext is 0
bool isnull;
/// Constructors
ciphertext_t() : c0(0), c1(0), pk(nullptr), isnull(true) {}
ciphertext_t(ciphertext_t const &ct) : c0(ct.c0), c1(ct.c1) {
if (ct.pk != nullptr) {
pk = ct.pk;
}
isnull = ct.isnull;
}
template <typename T>
ciphertext_t(T const &value) : c0(0), c1(0), pk(nullptr), isnull(true) {
assert(value == 0);
}
template <typename T>
ciphertext_t(pk_t &pk_in, message_t<T> const &m) : c1(0), pk(&pk_in) {
if (m.getValue() == 0) {
c0 = 0;
isnull = true;
} else {
c0 = m.getValue();
c0.ntt_pow_phi();
c0 = nfl::shoup(c0 * pk->delta, pk->delta_shoup);
isnull = false;
}
}
template <typename T>
ciphertext_t(pk_t &pk_in, T const &m) : c1(0), pk(&pk_in) {
if (m == 0) {
c0 = 0;
isnull = true;
} else {
c0 = m;
c0.ntt_pow_phi();
c0 = nfl::shoup(c0 * pk->delta, pk->delta_shoup);
isnull = false;
}
}
/// Assignment
inline ciphertext_t &operator=(ciphertext_t const &ct) {
c0 = ct.c0;
c1 = ct.c1;
if (ct.pk != nullptr) pk = ct.pk;
isnull = ct.isnull;
return *this;
}
template <typename T>
inline ciphertext_t &operator=(message_t<T> const &value) {
if (value != 0) {
assert(pk != nullptr);
P v{value.getValue()};
v.ntt_pow_phi();
c1 = 0;
c0 = nfl::shoup(v * pk->delta, pk->delta_shoup);
isnull = false;
} else {
c0 = 0;
c1 = 0;
isnull = true;
}
return *this;
}
template <typename Tp>
inline ciphertext_t &operator=(Tp const &value) {
if (value != 0) {
assert(pk != nullptr);
P v{value};
v.ntt_pow_phi();
isnull = false;
c1 = 0;
c0 = nfl::shoup(v * pk->delta, pk->delta_shoup);
} else {
c0 = 0;
c1 = 0;
isnull = true;
}
return *this;
}
/// Additions/Substractions
inline ciphertext_t &operator+=(ciphertext_t const &ct) {
if (ct.isnull == false) {
c0 = c0 + ct.c0;
c1 = c1 + ct.c1;
isnull = false;
}
return *this;
}
inline ciphertext_t &operator-=(ciphertext_t const &ct) {
if (ct.isnull == false) {
c0 = c0 - ct.c0;
c1 = c1 - ct.c1;
isnull = false;
}
return *this;
}
friend ciphertext_t operator+(ciphertext_t const &lhs,
ciphertext_t const &rhs) {
ciphertext_t ct(lhs);
return ct += rhs;
}
friend ciphertext_t operator-(ciphertext_t const &lhs,
ciphertext_t const &rhs) {
ciphertext_t ct(lhs);
return ct -= rhs;
}
inline ciphertext_t &operator+=(mpz_class const &value) {
if (value == 0) {
return *this;
}
P v{value};
v.ntt_pow_phi();
return *this += v;
}
inline ciphertext_t &operator-=(mpz_class const &value) {
if (value == 0) {
return *this;
}
P v{value};
v.ntt_pow_phi();
return *this -= v;
}
friend ciphertext_t operator+(ciphertext_t const &lhs, mpz_class const &rhs) {
ciphertext_t ct(lhs);
return ct += rhs;
}
friend ciphertext_t operator-(ciphertext_t const &lhs, mpz_class const &rhs) {
ciphertext_t ct(lhs);
return ct -= rhs;
}
inline ciphertext_t &operator+=(P::value_type const &value) {
if (value == 0) {
return *this;
}
P v{value};
v.ntt_pow_phi();
return *this += v;
}
inline ciphertext_t &operator-=(P::value_type const &value) {
if (value == 0) {
return *this;
}
P v{value};
v.ntt_pow_phi();
return *this -= v;
}
friend ciphertext_t operator+(ciphertext_t const &lhs,
P::value_type const &rhs) {
ciphertext_t ct(lhs);
return ct += rhs;
}
friend ciphertext_t operator-(ciphertext_t const &lhs,
P::value_type const &rhs) {
ciphertext_t ct(lhs);
return ct -= rhs;
}
template <typename T>
inline ciphertext_t &operator+=(message_t<T> const &m) {
if (m.getValue() == 0) {
return *this;
}
return *this += m.getValue();
}
template <typename T>
inline ciphertext_t &operator-=(message_t<T> const &m) {
if (m.getValue() == 0) {
return *this;
}
return *this -= m.getValue();
}
template <typename T>
friend ciphertext_t operator+(ciphertext_t const &lhs,
message_t<T> const &rhs) {
ciphertext_t ct(lhs);
return ct += rhs;
}
template <typename T>
friend ciphertext_t operator-(ciphertext_t const &lhs,
message_t<T> const &rhs) {
ciphertext_t ct(lhs);
return ct -= rhs;
}
/// Addition/Substraction of a polynomial
inline ciphertext_t &operator+=(P const &p) {
assert(pk != nullptr);
c0 = c0 + nfl::shoup(p * pk->delta, pk->delta_shoup);
isnull = false;
return *this;
}
inline ciphertext_t &operator-=(P const &p) {
assert(pk != nullptr);
c0 = c0 - nfl::shoup(p * pk->delta, pk->delta_shoup);
isnull = false;
return *this;
}
friend ciphertext_t operator+(ciphertext_t const &lhs, P const &rhs) {
ciphertext_t ct(lhs);
return ct += rhs;
}
friend ciphertext_t operator-(ciphertext_t const &lhs, P const &rhs) {
ciphertext_t ct(lhs);
return ct -= rhs;
}
/// Multiplication
ciphertext_t &operator*=(ciphertext_t const &ct) {
// Early abort
if (ct.isnull || isnull) {
c0 = 0;
c1 = 0;
isnull = true;
return *this;
}
size_t bits_in_moduli_product = P::bits_in_moduli_product();
// Allocations
PZ c00, c10, c01, c11, c1b;
// View the polynomials as PZ polynomials
util::convert(c00, c0);
util::convert(c01, c1);
util::convert(c10, ct.c0);
util::convert(c11, ct.c1);
// Compute products "over ZZ"
c1b = c00 * c11 + c01 * c10;
c00 = c00 * c10;
c11 = c01 * c11;
// Multiply by 2/q
std::array<mpz_t, P::degree> coefficients;
for (size_t i = 0; i < P::degree; i++) {
mpz_init2(coefficients[i], (bits_in_moduli_product << 2));
}
util::lift(coefficients, c00);
util::reduce<PZ::degree>(
coefficients, params::plaintextModulus<mpz_class>::value().get_mpz_t(),
P::moduli_product(), pk->evk->qDivBy2, PZ::moduli_product(),
pk->evk->bigmodDivBy2);
c0.mpz2poly(coefficients);
c0.ntt_pow_phi();
util::lift(coefficients, c1b);
util::reduce<PZ::degree>(
coefficients, params::plaintextModulus<mpz_class>::value().get_mpz_t(),
P::moduli_product(), pk->evk->qDivBy2, PZ::moduli_product(),
pk->evk->bigmodDivBy2);
c1.mpz2poly(coefficients);
c1.ntt_pow_phi();
util::lift(coefficients, c11);
util::reduce<PZ::degree>(
coefficients, params::plaintextModulus<mpz_class>::value().get_mpz_t(),
P::moduli_product(), pk->evk->qDivBy2, PZ::moduli_product(),
pk->evk->bigmodDivBy2);
// Decompose c2i and multiply by evaluation keys
P c2i;
std::array<mpz_t, P::degree> decomp;
for (size_t i = 0; i < P::degree; i++) {
mpz_init2(decomp[i], pk->evk->word_size);
mpz_mod(coefficients[i], coefficients[i], P::moduli_product());
}
for (size_t i = 0; i < pk->evk->ell; i++) {
for (size_t k = 0; k < P::degree; k++) {
mpz_and(decomp[k], coefficients[k], pk->evk->word_mask);
mpz_fdiv_q_2exp(coefficients[k], coefficients[k], pk->evk->word_size);
}
c2i.mpz2poly(decomp);
c2i.ntt_pow_phi();
c0 = c0 +
nfl::shoup(c2i * pk->evk->values[i][0], pk->evk->values_shoup[i][0]);
c1 = c1 +
nfl::shoup(c2i * pk->evk->values[i][1], pk->evk->values_shoup[i][1]);
}
// Clean
for (size_t i = 0; i < P::degree; i++) {
mpz_clear(decomp[i]);
mpz_clear(coefficients[i]);
}
return *this;
}
friend ciphertext_t operator*(ciphertext_t const &lhs,
ciphertext_t const &rhs) {
ciphertext_t ct(lhs);
return ct *= rhs;
}
inline ciphertext_t &operator*=(mpz_class const &m) {
if (m == 0) {
c0 = 0;
c1 = 0;
isnull = true;
return *this;
}
if (m == 1) {
return *this;
}
ciphertext_t c(*pk, m);
return *this *= c;
}
friend ciphertext_t operator*(ciphertext_t const &lhs, mpz_class const &rhs) {
ciphertext_t ct(lhs);
return ct *= rhs;
}
inline ciphertext_t &operator*=(P::value_type const &m) {
if (m == 0) {
c0 = 0;
c1 = 0;
isnull = true;
return *this;
}
if (m == 1) {
return *this;
}
ciphertext_t c(*pk, m);
return *this *= c;
}
friend ciphertext_t operator*(ciphertext_t const &lhs,
P::value_type const &rhs) {
ciphertext_t ct(lhs);
return ct *= rhs;
}
template <typename T>
inline ciphertext_t &operator*=(message_t<T> const &m) {
if (m.getValue() == 0) {
c0 = 0;
c1 = 0;
isnull = true;
return *this;
}
if (m.getValue() == 1) {
return *this;
}
ciphertext_t c(*pk, m);
return *this *= c;
}
template <typename T>
friend ciphertext_t operator*(ciphertext_t const &lhs,
message_t<T> const &rhs) {
ciphertext_t ct(lhs);
return ct *= rhs;
}
/// Multiplication by a polynomial
inline ciphertext_t &operator*=(P const &multiplier) {
if (isnull == false) {
c0 = c0 * multiplier;
c1 = c1 * multiplier;
}
return *this;
}
friend ciphertext_t operator*(ciphertext_t const &lhs, P const &rhs) {
ciphertext_t ct(lhs);
return ct *= rhs;
}
};
} // namespace FV
/**
* Encrypt a polynomial poly_m
* @param ct ciphertext (passed by reference)
* @param pk public key
* @param poly_m polynomial to encrypt
*/
namespace FV {
template <class PK, class C>
void encrypt_poly(C &ct, const PK &pk, params::poly_p &poly_m) {
using P = params::poly_p;
// Apply the NTT on poly_m
poly_m.ntt_pow_phi();
// Generate a small u
P u{params::gauss_struct(¶ms::fg_prng_enc)};
u.ntt_pow_phi();
// Set the ciphertext pk
ct.pk = (PK *)&pk;
// Generate ct = (c0, c1)
// where c0 = b*u + Delta*m + small error
ct.c0 = params::gauss_struct(¶ms::fg_prng_enc);
ct.c0.ntt_pow_phi();
ct.c0 = ct.c0 + nfl::shoup(u * pk.b, pk.b_shoup) +
nfl::shoup(poly_m * pk.delta, pk.delta_shoup);
// where c1 = a*u + small error
ct.c1 = params::gauss_struct(¶ms::fg_prng_enc);
ct.c1.ntt_pow_phi();
ct.c1 = ct.c1 + nfl::shoup(u * pk.a, pk.a_shoup);
ct.isnull = false;
}
} // namespace FV
/**
* Decryption of a ciphertext and recover the whole polynomial encrypted
* @param poly_mpz pointer to the polynomial (already initialized)
* @param sk secret key
* @param pk public key
* @param ct ciphertext
*/
namespace FV {
template <class SK, class PK, class C>
void decrypt_poly(std::array<mpz_t, params::poly_p::degree> &poly_mpz,
const SK &sk, const PK &pk, const C &ct) {
using P = params::poly_p;
// Get the polynomial
P numerator{ct.c0 + ct.c1 * sk.value};
numerator.invntt_pow_invphi();
numerator.poly2mpz(poly_mpz);
// Reduce the coefficients
util::reduce<P::degree>(
poly_mpz, params::plaintextModulus<mpz_class>::value().get_mpz_t(),
P::moduli_product(), pk.evk->qDivBy2, P::moduli_product(),
pk.evk->qDivBy2);
for (size_t i = 0; i < P::degree; i++) {
mpz_mod(poly_mpz[i], poly_mpz[i],
params::plaintextModulus<mpz_class>::value().get_mpz_t());
}
}
} // namespace FV
/**
* Encrypt a message
* @param ct ciphertext (passed by reference)
* @param pk public key
* @param message message to encrypt
*/
namespace FV {
template <class PK, class C, typename T>
void encrypt(C &ct, const PK &pk, const message_t<T> &message) {
// Store the message in the constant coefficient of a polynomial poly_m
params::poly_p poly_m{message.getValue()};
// Encrypt poly_m
encrypt_poly(ct, pk, poly_m);
}
template <class PK, class C, typename T>
void encrypt_integer(C &ct, const PK &pk, const T &message) {
// Store the message in the constant coefficient of a polynomial poly_m
params::poly_p poly_m{message};
// Encrypt poly_m
encrypt_poly(ct, pk, poly_m);
}
} // namespace FV
/**
* Decryption of a ciphertext whose message is in
* @param message message in the ciphertext passed by reference
* @param sk secret key
* @param pk public key
* @param ct ciphertext
*/
namespace FV {
template <class SK, class PK, class C, class M>
void decrypt(M &message, const SK &sk, const PK &pk, const C &ct) {
auto const degree = params::poly_p::degree;
// Initiate the polynomial of mpz values
std::array<mpz_t, degree> poly_mpz;
for (size_t i = 0; i < degree; i++) {
mpz_init(poly_mpz[i]);
}
// Decrypt the ciphertext
decrypt_poly<SK, PK, C>(poly_mpz, sk, pk, ct);
// Get the message from the constant coefficient
message = util::message_from_mpz_t<typename M::type>(poly_mpz[0]);
// Clear the poly of mpz values
for (size_t i = 0; i < degree; i++) {
mpz_clear(poly_mpz[i]);
}
}
} // namespace FV
/**
* Return the log in base 2 of the noise in the ciphertext
* @param message message encrypted in the ciphertext
* @param sk secret key
* @param pk public key
* @param ct ciphertext
* @return log_2(noise in ct)
*/
namespace FV {
template <class SK, class PK, class C, class M>
size_t noise(M const &message, SK const &sk, PK const &pk, C const &ct) {
using P = params::poly_p;
P poly_m{message.getValue()};
poly_m.ntt_pow_phi();
P numerator{ct.c0 + ct.c1 * sk.value -
nfl::shoup(poly_m * pk.delta, pk.delta_shoup)};
numerator.invntt_pow_invphi();
std::array<mpz_t, P::degree> poly_mpz = numerator.poly2mpz();
size_t logMax = 0;
for (size_t i = 0; i < P::degree; i++) {
util::center(poly_mpz[i], poly_mpz[i], P::moduli_product(),
pk.evk->qDivBy2);
logMax = std::max(logMax, mpz_sizeinbase(poly_mpz[i], 2));
}
// Clean
for (size_t i = 0; i < P::degree; i++) {
mpz_clear(poly_mpz[i]);
}
return logMax;
}
} // namespace FV
/**
* Class to store messages
* templated class (T = mpz_class or T = unsigned long)
*/
namespace FV {
template <typename T>
class message_t {
private:
/// internal value
T _value;
public:
/// type of the value
typedef T type;
/// Constructor
message_t() { _value = 0; }
message_t(message_t const &m) { _value = m._value; }
template <typename Tp>
message_t(Tp const &value) {
_value = T(value) % params::plaintextModulus<T>::value();
}
/// Get the value
inline T getValue() const { return _value; }
/// Set a value
message_t &operator=(message_t const &m) {
_value = m._value;
return *this;
}
template <typename Tp>
message_t &operator=(Tp const &value) {
_value = T(value) % params::plaintextModulus<T>::value();
return *this;
}
/// Random generator
message_t &random() {
gmp_randclass rng(gmp_randinit_default);
rng.seed(rand());
mpz_t rnd;
mpz_init_set(rnd, mpz_class(rng.get_z_range(mpz_class(
params::plaintextModulus<T>::value())))
.get_mpz_t());
message_t m = util::message_from_mpz_t<T>(rnd);
_value = m.getValue();
mpz_clear(rnd);
return *this;
};
/// Operations +, -, *
message_t &operator+=(message_t const &m) {
_value = (_value + m._value) % params::plaintextModulus<T>::value();
return *this;
}
message_t &operator*=(message_t const &m) {
_value = (_value * m._value) % params::plaintextModulus<T>::value();
return *this;
}
message_t &operator-=(message_t const &m) {
_value = (_value - m._value) % params::plaintextModulus<T>::value();
return *this;
}
friend message_t operator+(message_t const &lhs, message_t const &rhs) {
message_t m(lhs);
return m += rhs;
}
friend message_t operator*(message_t const &lhs, message_t const &rhs) {
message_t m(lhs);
return m *= rhs;
}
friend message_t operator-(message_t const &lhs, message_t const &rhs) {
message_t m(lhs);
return m -= rhs;
}
/// Inversion
message_t invert() {
mpz_class inverse;
mpz_class value(_value);
mpz_invert(inverse.get_mpz_t(), _value.get_mpz_t(),
mpz_class(params::plaintextModulus<T>::value()).get_mpz_t());
return message_t(util::message_from_mpz_t<T>(inverse.get_mpz_t()));
}
};
/// == operator
template <typename T>
inline bool operator==(message_t<T> const &lhs, message_t<T> const &rhs) {
return lhs.getValue() == rhs.getValue();
}
template <typename T>
inline bool operator==(message_t<T> const &lhs, T const &rhs) {
return lhs.getValue() == rhs;
}
template <typename T>
inline bool operator==(T const &lhs, message_t<T> const &rhs) {
return lhs == rhs.getValue();
}
/// != operator
template <typename T>
inline bool operator!=(message_t<T> const &lhs, message_t<T> const &rhs) {
return lhs.getValue() != rhs.getValue();
}
template <typename T>
inline bool operator!=(message_t<T> const &lhs, T const &rhs) {
return lhs.getValue() != rhs;
}
template <typename T>
inline bool operator!=(T const &lhs, message_t<T> const &rhs) {
return rhs.getValue() != lhs;
}
namespace util {
template <typename T>
std::string value_to_string(T const &value) {
return std::to_string(value);
}
template <>
std::string value_to_string<mpz_class>(mpz_class const &value) {
return std::string(value.get_str());
}
}
/// << operator
template <typename T>
std::ostream &operator<<(std::ostream &os, message_t<T> const &m) {
return os << util::value_to_string(m.getValue());
}
} // namespace FV
namespace FV {
namespace util {
/// Helper functions for messages conversion
template <>
mpz_class message_from_mpz_t(mpz_t value) {
return mpz_class(value);
};
template <>
unsigned long message_from_mpz_t(mpz_t value) {
return mpz_get_ui(value);
};
/**
* Center op1 modulo op2
* @param rop result
* @param op1 number op1 already reduced modulo op2, i.e. such that 0 <= op1
* < op2-1
* @param op2 modulus
* @param op2Div2 floor(modulus/2)
*/
inline void center(mpz_t &rop, mpz_t const &op1, mpz_t const &op2,
mpz_t const &op2Div2) {
mpz_set(rop, op1);
if (mpz_cmp(op1, op2Div2) > 0) {
mpz_sub(rop, rop, op2);
}
}
/**
* Compute the quotient of op1 divided by op2 for a centered noise
* @param rop result
* @param op1 numerator
* @param op2 denominator
* @param op2Div2 floor(denominator/2)
*/
inline void div_and_round(mpz_t &rop, mpz_t const &op1, mpz_t const &op2,
mpz_t const &op2Div2) {
mpz_t r;
mpz_init2(r, mpz_size(op2) + 1);
// Compute op1 = rop * op2 + r
// where r has the same sign as op1
mpz_tdiv_qr(rop, r, op1, op2);
// Correct "rop" so that r is centered around 0
long sgn = mpz_sgn(r);
mpz_abs(r, r);
if (mpz_cmp(r, op2Div2) >= 0) {
if (sgn > 0) {
mpz_add_ui(rop, rop, 1);
} else {
mpz_sub_ui(rop, rop, 1);
}
}
// Clean
mpz_clear(r);
}
/**
* center the coefficients, multiply them by "multiplier" and then divide by the
* divisor
* @param coefficients pointer to the initialized coefficients
* @param degree number of coefficients to compute
* @param multiplier multiplier for the internal multiplication
* @param divisor denominator
* @param divisorDiv2 floor(denominator/2)
* @param mod_init modulus of the initial coefficients
* @param mod_initDiv2 floor(modulus/2)
*/
template <size_t degree>
inline void reduce(std::array<mpz_t, degree> &coefficients, mpz_t multiplier,
mpz_t const &divisor, mpz_t const &divisorDiv2,
mpz_t const &mod_init, mpz_t const &mod_initDiv2) {
for (unsigned i = 0; i < degree; i++) {
// Center with mod_init
center(coefficients[i], coefficients[i], mod_init, mod_initDiv2);
// Multiply by multiplier
mpz_mul(coefficients[i], coefficients[i], multiplier);
// Divide by divisor
div_and_round(coefficients[i], coefficients[i], divisor, divisorDiv2);
// reduction will be done during the mpz2poly()'s calls
// otherwise one needs to do it
}