-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvespo_library.inl
1716 lines (1429 loc) · 54.6 KB
/
vespo_library.inl
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
// ==========================================================================
// VESPo: Verified Evaluation of Secret Polynomials
// Reference: [ https://arxiv.org/abs/2110.02022
// J-G. Dumas, A. Maignan, C. Pernet, D. S. Roche ]
// Authors: J-G Dumas
// Time-stamp: <04 Oct 23 09:54:10 [email protected]>
// ==========================================================================
/****************************************************************
* VESPO Library inline implementations
****************************************************************/
#include "vespo_library.h"
/****************************************************************
* Median deviation
****************************************************************/
template<typename Vect> inline double mediandeviation(const Vect& v) {
assert(v.size()>0);
typename Vect::value_type median(v[v.size()/2]);
double t1( median-v.front() );
double t2( v.back()-median );
return 100.*std::max(t1,t2)/median;
}
/****************************************************************
* relic bn_t utilities
****************************************************************/
inline std::ostream& operator<<(std::ostream& out, const bn_t& numb) {
size_t bits = bn_size_str(numb, 10);
char str[RLC_BN_BITS + 2];
bn_write_str(str, bits, numb, 10);
return out << str;
}
inline void vespo_init_set(bn_t& a, const bn_t b) {
bn_null(a); bn_new(a); bn_copy(a,b);
}
inline void vespo_init_set_ui(bn_t& a, const dig_t b) {
bn_null(a); bn_new(a); bn_set_dig(a,b);
}
// Set a to a + b * c.
inline void vespo_addinmul(bn_t& a, const bn_t b, const bn_t c) {
bn_t tmp; bn_new(tmp);
bn_mul(tmp,b,c);
bn_add(a,a,tmp);
bn_free(tmp);
}
//=====================================================================
// VeSPo data structures
// Polynomial types
//=====================================================================
template<> inline void freeer<bn_t>(bn_t& e) { bn_free(e); }
template<> inline void freeer<g1_t>(g1_t& e) { g1_free(e); }
template<> inline void freeer<g2_t>(g2_t& e) { g2_free(e); }
template<> inline void freeer<gt_t>(gt_t& e) { gt_free(e); }
template<> inline void freeer<paillier_ciphertext_t>(paillier_ciphertext_t& e) { }
template<> inline void newer<bn_t>(bn_t& e) { bn_null(e); bn_new(e); }
template<> inline void newer<g1_t>(g1_t& e) { g1_null(e); g1_new(e); }
template<> inline void newer<g2_t>(g2_t& e) { g2_null(e); g2_new(e); }
template<> inline void newer<gt_t>(gt_t& e) { gt_null(e); gt_new(e); }
template<> inline void newer<paillier_ciphertext_t>(paillier_ciphertext_t& e) { }
template<> inline std::ostream& printer<bn_t>(std::ostream& o, const bn_t& e) { return o<<e;}
template<> inline std::ostream& printer<g1_t>(std::ostream& o, const g1_t& e) { g1_print(e); return o;}
template<> inline std::ostream& printer<g2_t>(std::ostream& o, const g2_t& e) { g2_print(e); return o;}
template<> inline std::ostream& printer<gt_t>(std::ostream& o, const gt_t& e) { gt_print(e); return o;}
template<typename Element>
inline Polynomial<Element>::Polynomial(const int64_t degree,
const bn_t& modulus) :
mod(modulus), d(degree), data(new Element[degree+1]) {
for (int64_t i = 0; i <= degree; ++i) {
newer(this->data[i]);
}
}
template<typename Element>
inline Polynomial<Element>::~Polynomial() {
for (int64_t i = 0; i <= this->d; ++i) {
freeer(this->data[i]);
}
delete [] data;
}
template<typename Element>
inline const Element& Polynomial<Element>::operator[](size_t i) const {
#ifdef DEBUG
if ( (int64_t)i > this->degree()) {
std::cerr << "****************** ERROR ******************* Polynomial access at " << i << " out of " << this->degree() << std::endl;
throw(1);
}
#endif
return this->data[i];
}
template<typename Element>
inline Element& Polynomial<Element>::operator[](size_t i) {
#ifdef DEBUG
if ( (int64_t)i > this->degree()) {
std::cerr << "****************** ERROR ******************* Polynomial access at " << i << " out of " << this->degree() << std::endl;
throw(1);
}
#endif
return this->data[i];
}
template<typename Element>
inline std::ostream& operator<<(std::ostream& out, const Polynomial<Element>& P) {
printer(out,P.data[0]) << " + ";
for (int64_t i = 1; i < P.d; ++i) {
printer(out,P.data[i]) << "X^" << i << " + ";
}
return printer(out,P.data[P.d]) << "X^" << P.d;
}
template<> inline void Polynomial<bn_t>::random() {
std::clog << "[Rand P] BEG: " << this->d << ", mod " << this->modulus() << std::endl;
for(int64_t i = 0; i <= this->d; ++i) {
bn_rand_mod(this->data[i], this->modulus());
}
std::clog << "[Rand P] END" << std::endl;
}
template<> inline void Polynomial<bn_t>::eval(bn_t& eval, const bn_t& s) const {
#ifdef DEBUG
std::clog << "[Horner] BEG: " << this->d << std::endl;
#endif
bn_copy(eval,this->data[this->d]);
#ifdef DEBUG
printf("mod : "); bn_print(this->modulus()); printf("\n");
printf("P_%lu: ", this->d); bn_print(eval); printf("\n");
#endif
for(int64_t i = this->d; i>0; --i) { // Horner
bn_mul(eval,eval,s);
bn_add(eval,eval,this->data[i-1]);
bn_mod(eval,eval, this->modulus());
}
#ifdef DEBUG
bn_print(eval); printf(" = P("); bn_print(s); printf(")\n");
std::clog << "[Horner] END" << std::endl;
#endif
}
/****************************************************************
* Paillier utilities
****************************************************************/
inline int paillier_mul(paillier_ciphertext_t& res,
const paillier_ciphertext_t& a,
const paillier_ciphertext_t& b,
const paillier_pubkey_t& kpub) {
int result = RLC_OK;
bn_mul(res.c, a.c, b.c);
bn_mod(res.c, res.c, kpub.nsq);
return result;
}
/****************************************************************
* Paillier generation
****************************************************************/
//=====================================================================
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
// if VESPO_NOTSECURE is defined, Paillier masks are reused.
// Reusing the same randomness for several Paillier encryption.
// This is for accelerating setup, but it is NOT SECURE,
// however does not change the client nor server execution time
// should be used only for benchmarking the audit part of the protocol
//=====================================================================
// #define VESPO_NOTSECURE 100
inline void random_precompute(Polynomial<bn_t>& reusedrands,
const paillier_pubkey_t& pub,
const paillier_prvkey_t& prv,
const int64_t psize) {
if (reusedrands.degree()<(psize-1)) {
std::clog << "*WARNING WARNING* Paillier randomness reused\n"
<< "*WARNING WARNING* " << (reusedrands.degree()+1)
<< " instead of " << psize
<< "\n*WARNING WARNING* insecure, only benchmarking"
<< std::endl;
}
#pragma omp parallel for shared(reusedrands,prv,pub)
for (int64_t i = 0; i <= reusedrands.degree(); ++i) {
bn_rand_mod(reusedrands[i], prv->a);
bn_mxp(reusedrands[i], prv->gn, reusedrands[i], pub.nsq);
}
}
//=====================================================================
//=====================================================================
// VeSPo: linear algebra
//=====================================================================
inline void vector::init() {
bn_null(this->first);
bn_null(this->second);
bn_new(this->first);
bn_new(this->second);
}
inline vector::vector() { init(); }
inline vector::~vector() {
bn_free(this->first);
bn_free(this->second);
}
inline vector::vector(const bn_t& f, const bn_t& s) : vector() {
bn_copy(this->first, f);
bn_copy(this->second,s);
}
inline vector::vector(const vector& v) : vector() {
this->copy(v);
}
inline void vector::randomize(const bn_t mod) {
bn_rand_mod(this->first, mod);
bn_rand_mod(this->second, mod);
}
inline void vector::modin(const bn_t& r) {
bn_mod(this->first, this->first, r);
bn_mod(this->second, this->second, r);
}
inline void vector::copy(const vector& v) {
bn_copy(this->first, v.first);
bn_copy(this->second,v.second);
}
inline void vector::addin(const vector& v) {
bn_add(this->first, this->first, v.first);
bn_add(this->second, this->second, v.second);
}
inline bool vector::areEqual(const vector& v) const {
return ( (bn_cmp(this->first,v.first)== RLC_EQ) &&
(bn_cmp(this->second,v.second)== RLC_EQ) );
}
inline std::ostream& operator<<(std::ostream& out, const vector& v) {
return out << '<' << v.first << ',' << v.second << '>';
}
inline void matrix::init() {
bn_null(a);bn_null(b);bn_null(c);bn_null(d);
bn_new(a); bn_new(b); bn_new(c); bn_new(d);
}
inline matrix::~matrix() { bn_free(a); bn_free(b); bn_free(c); bn_free(d); }
inline matrix::matrix() { init(); }
inline matrix::matrix(const bn_t& Aa, const bn_t& Ab, const bn_t& Ac, const bn_t& Ad) : matrix() {
bn_copy(a, Aa);
bn_copy(b, Ab);
bn_copy(c, Ac);
bn_copy(d, Ad);
}
inline matrix::matrix(const matrix& A) : matrix(A.a,A.b,A.c,A.d) {}
inline void matrix::randomize(const bn_t& mod) {
bn_rand_mod(a, mod);
bn_rand_mod(b, mod);
bn_rand_mod(c, mod);
bn_rand_mod(d, mod);
}
inline void matrix::mulin(const bn_t& r) {
bn_mul(a, a, r);
bn_mul(b, b, r);
bn_mul(c, c, r);
bn_mul(d, d, r);
}
inline void matrix::modin(const bn_t& r) {
bn_mod(a, a, r);
bn_mod(b, b, r);
bn_mod(c, c, r);
bn_mod(d, d, r);
}
inline void matrix::copy(const matrix& A) {
bn_copy(a, A.a);
bn_copy(b, A.b);
bn_copy(c, A.c);
bn_copy(d, A.d);
}
std::ostream& operator<<(std::ostream& out, const matrix& m) {
return out << "<<" << m.a << '|' << m.b << ">,<" << m.c << '|' << m.d << ">>";
}
inline void sqr_modcharp(bn_t& s0, bn_t& s1,
const bn_t& p0, const bn_t& p1,
const bn_t& t0, const bn_t& t1,
const bn_t& mod) {
// (t0 + t1 X)^2 mod p0 + p1 X + X^2
// --> t0^2 + 2t0t1 X + t1^2 ( -p0 -p1 X)
// --> (t0^2 -p0t1^2) + (2t0t1 -p1t1^2) X
bn_sqr(s0, t0); // T0^2
bn_mod(s0, s0, mod);
bn_mul(s1, t0, t1); // T0T1
bn_mod(s1, s1, mod);
bn_add(s1, s1, s1); // 2T0T1
bn_t tmp, smp; bn_new(tmp); bn_new(smp);
bn_sqr(tmp, t1); // T1^2
bn_mod(tmp, tmp, mod);
bn_mul(smp, tmp, p1); // T1^2P1
bn_mod(smp, smp, mod);
bn_mul(tmp, tmp, p0); // T1^2P0
bn_mod(tmp, tmp, mod);
bn_sub(s0, s0, tmp); // T0^2-T1^2P0
bn_mod(s0, s0, mod);
bn_sub(s1, s1, smp); // 2T0T1-T1^2P1
bn_mod(s1, s1, mod);
bn_free(tmp);bn_free(smp);
}
inline void mulinx_modcharp(bn_t& s0, bn_t& s1,
const bn_t& p0, const bn_t& p1,
const bn_t& mod) {
// (s0 + s1 X) X mod p0 + p1 X + X^2
// --> s0 X + s1 (-p0 -p1 X)
// --> (-p0 s1) + (s0 - s1p1) X
bn_t tmp; bn_t smp; bn_new(tmp); bn_new(smp);
bn_mul(tmp, s1, p1); // S1P1
bn_mod(tmp, tmp, mod);
bn_sub(smp, s0, tmp); // S0-S1P1
bn_mul(tmp, s1, p0); // S1P0
bn_neg(tmp, tmp); // -S1P0
bn_mod(s0, tmp, mod); // Can now be overwritten
bn_mod(s1, smp, mod); // Can now be overwritten
bn_free(tmp); bn_free(smp);
}
inline void TMMP(bn_t& s0, bn_t& s1,
const bn_t& p0, const bn_t& p1,
const int64_t d, const bn_t& mod) {
// Computes s0+s1Z=Z^d modulo P(X)=p0+p1Z+Z^2
#ifdef DEBUG
std::clog << "[2-MMP] " << d << " BEG" << std::endl;
#endif
if (d<=1) {
if (d==1) {
bn_set_dig(s0,0);
bn_set_dig(s1,1);
return;
}
if (d==0) {
bn_set_dig(s0,1);
bn_set_dig(s1,0);
return;
}
if (d<0) {
std::cerr << "**** ERROR **** negative exponent not implemented\n";
return;
}
}
bn_t t0, t1; bn_null(t0); bn_null(t1); bn_new(t0); bn_new(t1);
TMMP(t0, t1, p0, p1, d>>1, mod); // Recursive call
sqr_modcharp(s0, s1, p0, p1, t0, t1, mod); // T^2 mod P
bn_free(t0);bn_free(t1);
if (d & 0x1) {
mulinx_modcharp(s0, s1, p0, p1, mod); // X T^2 mod P
}
#ifdef DEBUG
std::clog << "[2MMP] " << d << " END" << std::endl;
#endif
}
inline void moddet(bn_t& det,
const bn_t& a, const bn_t& b, const bn_t& c, const bn_t& d,
const bn_t& mod) {
// Determinant of <<a|b>,<c|d>>
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_mul(det, a, d); // ad
bn_mul(tmp, b, c); // bc
bn_sub(det, det, tmp); // ad-bc
bn_mod(det, det, mod);
bn_free(tmp);
}
inline void modcharpoly(bn_t& p0, bn_t& p1,
const bn_t& a, const bn_t& b, const bn_t& c, const bn_t& d,
const bn_t& mod) {
// Characteristic polynomial of <<a|b>,<c|d>>
// (ad-bc) - (a+d) X + X^2
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_add(p1, a, d); // a+d
bn_neg(p1, p1);
bn_mod(p1, p1, mod);
bn_free(tmp);
moddet(p0, a, b, c, d, mod);
}
inline void matvectmod(bn_t& u0, bn_t& u1,
const bn_t& a, const bn_t& b, const bn_t& c, const bn_t& d,
const bn_t& v0, const bn_t& v1,
const bn_t& mod) {
// <u0,u1> <-- <<a|b>,<c|d>> . <v0,v1>
#ifdef DEBUG
std::clog << matrix(a,b,c,d) << '.' << vector(v0,v1) << " - ";
#endif
// < a v0 + b v1 , c v0 + d v1 >
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_mul(u0, a, v0);
bn_mul(tmp, b, v1);
bn_add(u0, u0, tmp);
bn_mod(u0, u0, mod);
bn_mul(u1, c, v0);
bn_mul(tmp, d, v1);
bn_add(u1, u1, tmp);
bn_mod(u1, u1, mod);
bn_free(tmp);
#ifdef DEBUG
std::clog << vector(u0,u1) << " mod " << mod << ';' << std::endl;
#endif
}
inline void PMGS(vector& u, const matrix& M,
const uint64_t k,
const vector& v, const bn_t& mod) {
// Projected matrix geometric sum
// u <-- sum_{i=0}^k M^i . v
#ifdef DEBUG
std::clog << "[PMGS] " << k << " BEG" << std::endl;
#endif
// Characteristic polynomial
bn_t p0, p1; bn_null(p0); bn_null(p1); bn_new(p0); bn_new(p1);
modcharpoly(p0, p1, M.a, M.b, M.c, M.d, mod);
// (A-I2)^{-1} --> 1/det < < d-1 | -b >, < -c | a-1 > >
bn_t dm, am;
bn_null(dm); bn_null(am);
bn_new(dm); bn_new(am);
bn_sub_dig(dm, M.d, 1); // d-1
bn_sub_dig(am, M.a, 1); // a-1
// Determinant( A-I2 ) --> am dm - b c
bn_t idet; bn_null(idet); bn_new(idet);
moddet(idet, am, M.b, M.c, dm, mod);
bn_mod_inv(idet, idet, mod);
bn_t t0, t1; bn_null(t0); bn_null(t1); bn_new(t0); bn_new(t1);
bn_t w0, w1; bn_null(w0); bn_null(w1); bn_new(w0); bn_new(w1);
// w <-- (idet)^{-1}(A-I2)^{-1} v
// Direct computation: w = < (d-1)v0 - b v1 , (a-1)v1 -c v0 >
bn_mul(t0, dm, v.first); // (d-1)v0
bn_mul(t1, M.b, v.second); // b v1
bn_sub(w0, t0, t1); // (d-1)v0 - b v1
bn_mod(w0, w0, mod);
bn_mul(t0, am, v.second); // (a-1)v1
bn_mul(t1, M.c, v.first); // c v0
bn_sub(w1, t0, t1); // (a-1)v1 - c v0
bn_mod(w1, w1, mod);
// Apply the inversed determinant
bn_mul(w0, w0, idet);
bn_mod(w0, w0, mod);
bn_mul(w1, w1, idet);
bn_mod(w1, w1, mod);
bn_free(idet);
#ifdef DEBUG
std::clog << "1/(" << M << "-IdentityMatrix(2))." << v << " - " << vector(w0,w1) << " mod " << mod << ';' << std::endl;
#endif
// Polynomial Geometric sum
bn_t f0, f1; bn_null(f0); bn_null(f1); bn_new(f0); bn_new(f1);
TMMP(f0, f1, p0, p1, k+1, mod);
bn_free(p0);bn_free(p1);
// F(A) - I2 --> << a*f1+f0-1 | f1*b >, < f1*c | d*f1+f0-1 >>
bn_sub_dig(dm, f0, 1); // f0-1
bn_mul(am, M.a, f1); // a*f1
bn_mod(am, am, mod);
bn_add(am, am, dm); // a*f1+f0-1
bn_mul(t0, M.d, f1); // d*f1
bn_mod(t0, t0, mod);
bn_add(dm, t0, dm); // d*f1+f0-1
bn_mul(t0, M.b, f1); // f1*b
bn_mod(t0, t0, mod);
bn_mul(t1, M.c, f1); // f1*c
bn_mod(t1, t1, mod);
bn_free(f0); bn_free(f1);
matvectmod(u.first, u.second, am, t0, t1, dm, w0, w1, mod);
bn_free(dm);bn_free(t0);bn_free(t1);bn_free(am);
bn_free(t0);bn_free(t1);
bn_free(w0);bn_free(w1);
#ifdef VESPO_CHECKERS
{
vector r, s, t;
t.copy(v); s.copy(v);
for(uint64_t i = 0; i<k; ++i) {
matvectmod(r, M, t, mod);
s.addin(r);
t.copy(r);
}
s.modin(mod);
if (u.areEqual(s)) {
std::clog << "[ PMGS ] \033[1;32mOK\033[0m" << std::endl;
} else {
std::cerr << "[ PMGS ] \033[1;31m****** FAIL ******\033[0m" << std::endl;
}
}
#endif
#ifdef DEBUG
std::clog << "[PMGS] " << k << " END" << std::endl;
#endif
}
inline void mat_geometric_sum(vector& c,
const bn_t& r, const matrix& A, const uint64_t k,
const vector& b,
const bn_t& mod) {
// Projected matrix geometric sum
// c <-- sum_{i=0}^k (rA)^i . b
matrix M(A);
M.mulin(r);
M.modin(mod);
PMGS(c, M, k, b, mod);
}
//=====================================================================
// VeSPo data structures
// Client/Server
//=====================================================================
inline client_t::client_t(const int64_t degree) : d(degree) {
g1_null(this->g1);g1_new(this->g1);
g2_null(this->g2);g2_new(this->g2);
gt_null(this->e_T);gt_new(this->e_T);
paillier_newprvkey(this->prv);
paillier_newpubkey(this->pub);
bn_null(this->s);bn_new(this->s);
gt_null(this->K1_bT);gt_new(this->K1_bT);
gt_null(this->K2_bT);gt_new(this->K2_bT);
}
inline client_t::~client_t() {
g1_free(this->g1);
g2_free(this->g2);
gt_free(this->e_T);
paillier_freeprvkey(this->prv);
paillier_freepubkey(this->pub);
bn_free(this->s);
gt_free(this->K1_bT);
gt_free(this->K2_bT);
}
inline uint64_t client_t::private_size(uint64_t elements_size) const {
// Groups, prime order and pubkey are supposed public data
// The degree of the polynomial must be stored
uint64_t size=0;
size += sizeof(this->d)*8; // degree
// s,K_bT,valpha,vbeta,msigma + MerkleTreeRoot
size += elements_size*(1+2+2+2+4 + 1);
size += paillier_sizeprvkey( this->prv );
return size;
}
inline uint64_t client_t::privpub_size(uint64_t elements_size) const {
// adding keys
return private_size(elements_size)
+ paillier_sizepubkey( this->pub );
}
//=====================================================================
// VeSPo: key generation
//=====================================================================
inline void client_t::keygen(uint64_t pailliersize) {
pc_get_ord( prv->a );
cp_shpe_gen( pub.rlc, prv, bn_bits( prv->a ), pailliersize);
bn_sqr(pub.nsq, pub.rlc->crt->n); // store n^2 too
}
inline int64_t setup_block_chunks(
std::vector<Polynomial<paillier_ciphertext_t>>& W,
const int64_t degree, const int64_t blocks, const bn_t& modulus) {
// Cut W into 'blocks' chunks
// last ones of size [ (degree+1)/Blocks ]
// first ones of that size + 1
// so that sum of sizes is 'degree+1'
int64_t sW(degree+1);
W.reserve(blocks);
int64_t sdeg(sW/blocks);
int64_t bigblocks = (sW-sdeg*blocks);
const int64_t ldeg(sdeg); --sdeg;
int64_t i=0; for(; i<bigblocks;++i) {
W.emplace_back(ldeg, modulus);
}
for( ; i<blocks; ++i){
W.emplace_back(sdeg, modulus);
}
// If no bigblocks, actually all are of same (big) size
if (bigblocks == 0) bigblocks=blocks;
return bigblocks;
}
inline server_t::server_t(const int64_t degree, const int64_t blocks,
const bn_t& modulus) :
H1_b(degree-1,modulus), H2_b(degree-1,modulus),
S(degree, modulus) {
// For parallelism:
// Cut W into 'blocks' chunks
// last ones of size [ (degree+1)/Blocks ]
// first ones of that size + 1
// so that sum of sizes is 'degree+1'
this->bigblocks=setup_block_chunks(this->W, degree, blocks, modulus);
}
//=====================================================================
// VeSPo: checkers implementations
//=====================================================================
inline void check_dp(g1_t& dp, const Polynomial<g1_t>& gS, const Polynomial<bn_t>& P) {
// dp <-- \oplus [ p_i ] gS_i
g1_t tmp; g1_new(tmp);
g1_mul(dp, gS[0], P[0]);
for(int64_t i=1; i <= P.degree(); ++i) {
g1_mul(tmp, gS[i], P[i]);
g1_add(dp, dp, tmp);
}
g1_free(tmp);
}
inline bool check_orders(const client_t& client, const bn_t& pairing_r) {
// pairing sanity checks
#ifdef DEBUG
printer(std::clog << "g1 : ", client.g1) << std::endl;
printer(std::clog << "g2 : ", client.g2) << std::endl;
#endif
g1_t o1; g1_new(o1);
g1_mul_gen(o1, pairing_r);
g2_t o2; g2_new(o2);
g2_mul_gen(o2, pairing_r);
gt_t ot; gt_new(ot);
gt_exp_gen(ot, pairing_r);
gt_t generatorT;
gt_new(generatorT);
pc_map(generatorT, client.g1, client.g2); // g_t
const bool pass( (! g1_is_infty(client.g1)) &&
(! g2_is_infty(client.g2)) &&
(! gt_is_unity(client.e_T)) &&
g1_is_infty(o1) && g2_is_infty(o2) && gt_is_unity(ot) &&
(gt_cmp(client.e_T, generatorT) == RLC_EQ)
);
if(pass) {
std::clog << "[GOrders ] \033[1;32mOK\033[0m" << std::endl;
} else {
std::clog << "[GOrders ] \033[1;31m****** FAIL ******\033[0m" << std::endl;
#ifdef DEBUG
printf("o1 (should be zero): "); g1_print(o1); printf("\n");
printf("o2 (should be zero): "); g2_print(o2); printf("\n");
printf("gt : "); gt_print(generatorT); printf("\n");
printf("et : "); gt_print(client.e_T); printf("\n");
#endif
}
g1_free(o1); g2_free(o2);
return pass;
}
inline bool check_pubkey(const client_t& client, const server_t& server,
const Polynomial<bn_t>& P_b,
const bn_t& eval, const g2_t& K_b) {
// K_b sanity checks
g1_t test,chet;
g1_new(test);
check_dp(test, server.S, P_b);
g1_new(chet);
g1_mul_gen(chet, eval); // g1^{P_b(s)}
if(g1_cmp(test, chet) == RLC_EQ) {
std::clog << "[ PowerP ] \033[1;32mOK\033[0m" << std::endl;
} else {
# ifdef DEBUG
{
printf("g1 test : "); g1_print(test); printf("\n");
printf("g1 chet : "); g1_print(chet); printf("\n");
}
# endif
std::cerr << "[ PowerP ] \033[1;31m****** FAIL ******\033[0m" << std::endl;
}
gt_t left, right;
gt_new(left);
gt_new(right);
pc_map(left, client.g1, K_b); // e(g1;K_b)
pc_map(right, test, client.g2); // e(dp;g2)
# ifdef DEBUG
{
printf("left : "); gt_print(left); printf("\n");
printf("right : "); gt_print(right);printf("\n");
}
# endif
const bool pass( gt_cmp(left,right) == RLC_EQ );
if(pass) {
std::clog << "[ KeyPub ] \033[1;32mOK\033[0m" << std::endl;
} else {
std::cerr << "[ KeyPub ] \033[1;31m****** FAIL ******\033[0m" << std::endl;
}
g1_free(test);
gt_free(left); gt_free(right);
g1_free(chet);
return pass;
}
inline bool check_g2_omxp(const g2_t& T, const Polynomial<g2_t>& S,
const bn_t& r, const int64_t deg, const bn_t& mod) {
bool pass(true);
g2_t U;
g2_copy(U, S[0]);
for (int64_t i = 1; i <= deg; ++i) {
g2_mul(U, U, r); // t^r
g2_add(U, U, S[i]); // S_{i-1} t^r
}
if ( (g2_cmp(U,T) != RLC_EQ)) {
printer(printer(std::cerr << "check_g2_omxp:", U) << " != ", T) << std::endl;
pass = false;
}
return pass;
}
inline bool check_g1_hmxp(const Polynomial<g1_t>& T, const Polynomial<g1_t>& S,
const bn_t& r, const int64_t from, const int64_t length,
const bn_t& mod) {
const int64_t deg(from+length-1);
bool pass(true);
g1_t U;
g1_copy(U, S[0]);
if (g1_cmp(U, T[0]) != RLC_EQ) {
printer(printer(std::cerr << "check_g1_hmxp: U[0] != T[0]: ", U) << ' ', T[0]) << std::endl;
pass = false;
}
for (int64_t i = 1; i <= deg; ++i) {
g1_mul(U, U, r); // t^r
g1_add(U, U, S[i]); // S_{i-1} t^r
if ( (i>= from) && (g1_cmp(U, T[i]) != RLC_EQ)) {
printer(printer(std::cerr << "check_g1_hmxp: U[" << i << "] != T[" << i << "]: ", U) << ' ', T[i]) << std::endl;
pass = false;
}
}
return pass;
}
inline void check_g1_msl(g1_t& r, const g1_t* P, const bn_t* K, int N) {
g1_t q; g1_null(q); g1_new(q);
g1_set_infty(r);
for (int j = 0; j < N; ++j) {
g1_mul(q, P[j], K[j]);
g1_add(r, r, q);
g1_mul_sim_lot(q,P,K,j+1);
if ( ! (g1_cmp(q,r) == RLC_EQ) ) {
std::cerr << "\033[1;31m****** FAIL: g1_mul_sim_lot " << (j+1) << " ******\033[0m" << std::endl;
}
}
g1_free(q);
}
inline void check_g1_mul_sim_lot(g1_t& RES, const g1_t* P, const bn_t* K, int N) {
#ifdef VESPO_CHECKERS
Chrono c_step; c_step.start();
#endif
g1_mul_sim_lot(RES,P,K,N);
#ifdef VESPO_CHECKERS
g1_t r; g1_null(r); g1_new(r);
check_g1_msl(r, P, K, N);
if ( ! (g1_cmp(RES,r) == RLC_EQ) ) {
std::cerr << "\033[1;31m****** FAIL: g1_mul_sim_lot " << N << " last ******\033[0m" << std::endl;
}
g1_free(r);
#endif
#ifdef VESPO_CHECKERS
std::clog << " Group 1 MulSim : " << c_step.stop() << " (" << N << " operations)" << std::endl;
#endif
}
inline bool check_ciph_horner(gt_t sxi_b, const int64_t deg, const server_t& server,
const Polynomial<g2_t>& H_b, const bn_t& rmod) {
// Server computation: check xi
gt_t xi_b, tmpT;
g1_t tmpG1;
gt_new(tmpT);
gt_new(xi_b);
g1_new(tmpG1);
gt_set_unity(xi_b);
bn_t zero; vespo_init_set_ui(zero,0);
g1_rand(tmpG1); g1_mul(tmpG1,tmpG1,zero);
for (int64_t i = 0; i < deg; ++i) {
g1_mul(tmpG1,tmpG1, rmod); // t^r
g1_add(tmpG1,server.S[i], tmpG1); // S_{i-1} t^r
#ifdef DEBUG
printf("H[%lu]: ", i); g2_print(H_b[i]); printf("\n");
printf("T[%lu]: ", i); g1_print(tmpG1); printf("\n");
#endif
pc_map(tmpT, tmpG1, H_b[i]); // e(H_i;t)
gt_mul(xi_b, xi_b, tmpT); // xi e(H_i;t)
}
const bool pass(gt_cmp(xi_b, sxi_b) == RLC_EQ);
if (pass) {
std::clog << "[CiHorner] \033[1;32mOK\033[0m" << std::endl;
} else {
std::cerr << "[CiHorner] \033[1;31m****** FAIL ******\033[0m" << std::endl;
#ifdef DEBUG
printf("xi_b : "); gt_print(xi_b); printf("\n");
printf("sxi_b: "); gt_print(sxi_b); printf("\n");
#endif
}
gt_free(xi_b);
g1_free(tmpG1);
gt_free(tmpT);
return pass;
}
//=====================================================================
// VeSPo: masking
//=====================================================================
inline void vhide_poly(Polynomial<bn_t> & P1, Polynomial<bn_t>& P2,
const vector & valpha, const vector& vbeta,
const matrix& msigma, const Polynomial<bn_t> & P) {
// <P1,P2> <-- sum X^i ( p_i valpha + msigma^i vbeta)
std::clog << "[VHide P] BEG: d°" << P.degree() << std::endl;
#ifdef VESPO_SUB_TIMINGS
Chrono c_vhide; c_vhide.start();
#endif
#ifdef DEBUG
std::clog << P << std::endl;
#endif
vector sigmapow, tmp; sigmapow.copy(vbeta);
// p_0 alpha + beta
bn_mul(P1[0], valpha.first, P[0]);
bn_mul(P2[0], valpha.second, P[0]);
bn_add(P1[0], P1[0], vbeta.first);
bn_add(P2[0], P2[0], vbeta.second);
bn_mod(P1[0], P1[0], P.modulus());
bn_mod(P2[0], P2[0], P.modulus());
for(int64_t i = 1; i <= P.degree(); ++i) {
bn_mul(P1[i], valpha.first, P[i]); // alpha p_i
bn_mul(P2[i], valpha.second, P[i]); // alpha p_i
matvectmod(tmp, msigma, sigmapow, P.modulus()); // Sigma^i beta
sigmapow.copy(tmp);
bn_add(P1[i], P1[i], sigmapow.first);
bn_mod(P1[i], P1[i], P.modulus()); // alpha p_i + beta sigma^i
bn_add(P2[i], P2[i], sigmapow.second);
bn_mod(P2[i], P2[i], P.modulus()); // alpha p_i + beta sigma^i
}
#ifdef VESPO_SUB_TIMINGS
std::clog << " SETUP VHide polynom. : " << c_vhide.stop() << " (" << (P.degree()+1) << " operations)" << std::endl;
#endif
#ifdef VESPO_CHECKERS
bn_t ttmp, etmp; bn_new(ttmp); bn_new(etmp);
vector check, htmp;
bn_rand_mod(etmp, P.modulus()); bn_mod(etmp, etmp, P.modulus());
P.eval(ttmp, etmp);
mat_geometric_sum(check, etmp, msigma, P.degree(), vbeta, P.modulus());
vespo_addinmul(check.first, valpha.first, ttmp);
vespo_addinmul(check.second, valpha.second, ttmp);
check.modin(P.modulus());
P1.eval(htmp.first, etmp);
P2.eval(htmp.second, etmp);
if( htmp.areEqual(check) ) {
std::clog << "[ VHidden ] \033[1;32mOK\033[0m" << std::endl;
} else {
std::cerr << "[ VHidden ] \033[1;31m****** FAIL ******\033[0m" << std::endl;
}
bn_free(ttmp); bn_free(etmp);
#endif
std::clog << "[VHide P] END" << std::endl;
}
//=====================================================================
// VeSPo: Paillier
//=====================================================================
inline void geo_progression(Polynomial<bn_t>& pows_r,
const int64_t bigsdegree, const bn_t& r1,
const int64_t totaldegree, const bn_t& r2,
const bn_t& mod, double& time_r) {
// Computes 1, r1, ..., r1^bigsdegree,
// r1^bigsdegree r2, ..., r1^bigsdegree r2^(totaldegree-bigsdegree)
#ifdef VESPO_CHECKERS
std::clog << "[PGeoProg] BEG: d°" << totaldegree << " (1 then " << bigsdegree << " then " << (totaldegree-bigsdegree) << ')' << std::endl;
#endif
#ifdef VESPO_TIMINGS
Chrono c_step; c_step.start();
#endif
bn_set_dig(pows_r[0],1);
int64_t i = 1; for (; i <= bigsdegree; ++i) {
bn_mul(pows_r[i], pows_r[i-1], r1);
bn_mod(pows_r[i], pows_r[i], mod);
}
for (; i <= totaldegree; ++i) {
bn_mul(pows_r[i], pows_r[i-1], r2);
bn_mod(pows_r[i], pows_r[i], mod);
}
#ifdef VESPO_TIMINGS
time_r = c_step.stop();
std::clog << " SERVER geom. prog: " << time_r << " (" << (totaldegree+1) << " Group mul operations)" << std::endl;
#endif
#ifdef VESPO_CHECKERS
std::clog << "[PGeoProg] END" << std::endl;
#endif
}
inline void paillier_hom_dp(paillier_ciphertext_t& eval,
const paillier_pubkey_t& kpub,
const Polynomial<paillier_ciphertext_t>& Wi,
const Polynomial<bn_t>& pows_r,
const int64_t degree) {