-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessor.cpp
1051 lines (929 loc) · 31.1 KB
/
Processor.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
/*
Copyright (c) 2024, Technology Innovation Institute, Yas Island, Abu Dhabi, United Arab Emirates.
Copyright (c) 2017, The University of Bristol, Senate House, Tyndall Avenue, Bristol, BS8 1TH, United Kingdom.
Copyright (c) 2021, COSIC-KU Leuven, Kasteelpark Arenberg 10, bus 2452, B-3001 Leuven-Heverlee, Belgium.
*/
#include "Processor.h"
#include "GC/Garbled.h"
#include "GC/Garbled_Circuit_Exceptions.h"
#include "GC/Garbled_Circuit_Serializer.h"
#include "GC/Q2_Evaluate.h"
#include "Input_Output/Share_DTO.h"
#include "Offline/DABitMachine.h"
#include "Tools/util_containers.h"
extern Base_Circuits Global_Circuit_Store;
extern vector<sacrificed_data> SacrificeD;
template <class SRegint, class SBit>
Processor<SRegint, SBit>::Processor(int thread_num, unsigned int nplayers, Machine<SRegint, SBit> &machine, Player &P)
: online_thread_num(thread_num), iop(nplayers)
{
daBitGen = machine.daBitMachine.new_generator(P, thread_num);
rounds = 0;
sent = 0;
stack_Cp.reserve(16384);
stack_Sp.reserve(16384);
stack_int.reserve(16384);
stack_srint.reserve(16384);
stack_sbit.reserve(16384);
gc_storage = Garbled_Circuit_Storage::create_from_config_file(P.whoami());
converter_storage = Converter_Storage::create_from_config_file(P.whoami());
}
template <class SRegint, class SBit> Processor<SRegint, SBit>::~Processor()
{
fprintf(stderr, "Sent %d elements in %d rounds\n", sent, rounds);
#ifdef VERBOSE
cout << "dabitgen statistics:" << endl;
cout << "Produced " << daBitGen->total << " dabits" << endl;
for (auto &timer : daBitGen->timers)
cout << timer.first << " took time " << timer.second.elapsed() / 1e6 << endl;
#endif
delete daBitGen;
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::POpen_Start(const vector<int> ®, int size, Player &P)
{
// printf("POpen_Start : size = %d reg.size = %lu\n ",size,reg.size());
int sz = reg.size();
Sh_PO.clear();
Sh_PO.reserve(sz * size);
if (size > 1)
{
for (typename vector<int>::const_iterator reg_it = reg.begin(); reg_it != reg.end(); reg_it++)
{
typename vector<Share>::iterator begin = Sp.begin() + *reg_it;
Sh_PO.insert(Sh_PO.end(), begin, begin + size);
}
}
else
{
/*
stringstream os;
Sp[reg[0]].output(os, true);
printf("Share : %d : %s \n",reg[0],os.str().c_str());
*/
for (int i = 0; i < sz; i++)
{
Sh_PO.push_back(get_Sp_ref(reg[i]));
}
}
PO.resize(sz * size);
P.OP->Open_To_All_Begin(PO, Sh_PO, P, 0);
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::POpen_Stop(const vector<int> ®, int size, Player &P)
{
int sz = reg.size();
PO.resize(sz * size);
P.OP->Open_To_All_End(PO, Sh_PO, P, 0);
if (size > 1)
{
typename vector<gfp>::iterator PO_it = PO.begin();
for (typename vector<int>::const_iterator reg_it = reg.begin(); reg_it != reg.end(); reg_it++)
{
for (typename vector<gfp>::iterator C_it = Cp.begin() + *reg_it; C_it != Cp.begin() + *reg_it + size;
C_it++)
{
*C_it = *PO_it;
PO_it++;
}
}
}
else
{
for (unsigned int i = 0; i < reg.size(); i++)
{
get_Cp_ref(reg[i]) = PO[i];
}
}
increment_counters(reg.size() * size);
}
unsigned int calculate_Number_Of_Batches(int size, int batch_size)
{
unsigned int number_of_batches = size / batch_size;
if (size % batch_size != 0)
{
number_of_batches++;
}
return number_of_batches;
}
template <typename T> vector<T> fill_batch(vector<T> original_vector, unsigned int inserted_values)
{
vector<T> batched_vector;
batched_vector.resize(open_values_workaround_batch_size);
batched_vector.assign(original_vector.begin() + inserted_values,
original_vector.begin() + inserted_values + open_values_workaround_batch_size);
return batched_vector;
}
template <typename T>
vector<T> fill_last_batch(vector<T> original_vector, unsigned int inserted_values, unsigned int batch_size)
{
vector<T> batched_vector;
batched_vector.resize(batch_size);
batched_vector.assign(original_vector.begin() + inserted_values, original_vector.end());
return batched_vector;
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::POpen_Start_Batched(const vector<int> ®, int size, Player &P)
{
unsigned long sz = reg.size();
Sh_PO.clear();
Sh_PO.reserve(sz * size);
if (size > 1)
{
for (typename vector<int>::const_iterator reg_it = reg.begin(); reg_it != reg.end(); reg_it++)
{
typename vector<Share>::iterator begin = Sp.begin() + *reg_it;
Sh_PO.insert(Sh_PO.end(), begin, begin + size);
}
}
else
{
/*
stringstream os;
Sp[reg[0]].output(os, true);
printf("Share : %d : %s \n",reg[0],os.str().c_str());
*/
for (unsigned int i = 0; i < sz; i++)
{
Sh_PO.push_back(get_Sp_ref(reg[i]));
}
}
PO.resize(sz * size);
// divide in batches
unsigned long number_of_batches = calculate_Number_Of_Batches(size, open_values_workaround_batch_size);
bool are_there_enough_connections =
number_of_batches < maximum_number_of_ssl_connections - number_of_original_connections;
if (!are_there_enough_connections)
{
throw Not_enough_ssl_connections_exception();
}
vector<vector<gfp>> batched_PO(number_of_batches);
vector<vector<Share>> batched_Sh_PO(number_of_batches);
unsigned int inserted_values = 0;
unsigned int remaining_elements = size * sz;
for (unsigned int i = 0; i < number_of_batches; i++)
{
if (remaining_elements >= open_values_workaround_batch_size)
{
batched_PO[i] = fill_batch(PO, inserted_values);
batched_Sh_PO[i] = fill_batch(Sh_PO, inserted_values);
remaining_elements -= open_values_workaround_batch_size;
inserted_values += open_values_workaround_batch_size;
}
else
{
batched_PO[i] = fill_last_batch(PO, inserted_values, remaining_elements);
batched_Sh_PO[i] = fill_last_batch(Sh_PO, inserted_values, remaining_elements);
}
int connection = i + number_of_original_connections;
P.OP->Open_To_All_Begin(batched_PO[i], batched_Sh_PO[i], P, connection);
}
PO.resize(0);
Sh_PO.resize(0);
for (unsigned int i = 0; i < number_of_batches; i++)
{
PO.insert(PO.end(), batched_PO[i].begin(), batched_PO[i].end());
Sh_PO.insert(Sh_PO.end(), batched_Sh_PO[i].begin(), batched_Sh_PO[i].end());
}
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::POpen_Stop_Batched(const vector<int> ®, int size, Player &P)
{
unsigned long sz = reg.size();
PO.resize(sz * size);
// divide in batches
unsigned long number_of_batches = calculate_Number_Of_Batches(size, open_values_workaround_batch_size);
vector<vector<gfp>> batched_PO(number_of_batches);
vector<vector<Share>> batched_Sh_PO(number_of_batches);
unsigned int inserted_values = 0;
unsigned int remaining_elements = size * sz;
for (unsigned int i = 0; i < number_of_batches; i++)
{
if (remaining_elements >= open_values_workaround_batch_size)
{
batched_PO[i] = fill_batch(PO, inserted_values);
batched_Sh_PO[i] = fill_batch(Sh_PO, inserted_values);
remaining_elements -= open_values_workaround_batch_size;
inserted_values += open_values_workaround_batch_size;
}
else
{
batched_PO[i] = fill_last_batch(PO, inserted_values, remaining_elements);
batched_Sh_PO[i] = fill_last_batch(Sh_PO, inserted_values, remaining_elements);
}
}
Sh_PO.resize(0);
PO.resize(0);
inserted_values = 0;
for (unsigned int i = 0; i < number_of_batches; i++)
{
int connection = i + number_of_original_connections;
P.OP->Open_To_All_End(batched_PO[i], batched_Sh_PO[i], P, connection);
Sh_PO.insert(Sh_PO.begin() + inserted_values, batched_Sh_PO[i].begin(), batched_Sh_PO[i].end());
PO.insert(PO.begin() + inserted_values, batched_PO[i].begin(), batched_PO[i].end());
inserted_values += open_values_workaround_batch_size;
}
P.OP->Increase_Number_Of_Used_Connections(number_of_batches);
if (size > 1)
{
typename vector<gfp>::iterator PO_it = PO.begin();
for (typename vector<int>::const_iterator reg_it = reg.begin(); reg_it != reg.end(); reg_it++)
{
for (typename vector<gfp>::iterator C_it = Cp.begin() + *reg_it; C_it != Cp.begin() + *reg_it + size;
C_it++)
{
*C_it = *PO_it;
PO_it++;
}
}
}
else
{
for (unsigned int i = 0; i < reg.size(); i++)
{
get_Cp_ref(reg[i]) = PO[i];
}
}
increment_counters(reg.size() * size);
}
template <class SRegint, class SBit> void Processor<SRegint, SBit>::clear_registers()
{
for (unsigned int i = 0; i < Cp.size(); i++)
{
Cp[i].assign_zero();
}
for (unsigned int i = 0; i < Sp.size(); i++)
{
Sp[i].assign_zero();
}
for (unsigned int i = 0; i < Ri.size(); i++)
{
Ri[i] = 0;
}
for (unsigned int i = 0; i < srint.size(); i++)
{
srint[i].assign_zero();
}
for (unsigned int i = 0; i < sbit.size(); i++)
{
sbit[i].assign_zero();
}
#ifdef DEBUG
for (unsigned int i = 0; i < rwp.size(); i++)
{
rwp[i] = 0;
}
for (unsigned int i = 0; i < rwi.size(); i++)
{
rwi[i] = 0;
}
for (unsigned int i = 0; i < rwsr.size(); i++)
{
rwsr[i] = 0;
}
for (unsigned int i = 0; i < rwsb.size(); i++)
{
rwsb[i] = 0;
}
#endif
}
// Now the routine to execute a program
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::execute(const Program<SRegint, SBit> &prog, int argument, unsigned int start, Player &P,
Machine<SRegint, SBit> &machine, offline_control_data &OCD)
{
reg_maxb= prog.num_reg(SBIT);
reg_maxc= prog.num_reg(MODP);
reg_maxi= prog.num_reg(INT);
reg_maxp= prog.num_reg(MODP);
// Resizing register sizes externally
if (ignore_memory_sizes) {
if (reg_maxb > REG_MAXB_SIZE) {
reg_maxb = REG_MAXB_SIZE;
}
if (reg_maxc > REG_MAXC_SIZE) {
reg_maxc = REG_MAXC_SIZE;
}
if (reg_maxi > REG_MAXI_SIZE) {
reg_maxi = REG_MAXI_SIZE;
}
if (reg_maxp > REG_MAXP_SIZE) {
reg_maxp = REG_MAXP_SIZE;
}
}
Cp.resize(reg_maxc);
Sp.resize(reg_maxp);
Ri.resize(reg_maxi);
srint.resize(reg_maxi);
sbit.resize(reg_maxb);
for (int i = 0; i < reg_maxp; i++)
{
Sp[i].set_player(P.whoami());
}
#ifdef DEBUG
rwp.resize(2 * reg_maxp);
for (int i = 0; i < 2 * reg_maxp; i++)
{
rwp[i] = 0;
}
rwi.resize(reg_maxi);
rwsr.resize(reg_maxi);
for (int i = 0; i < reg_maxi; i++)
{
rwi[i] = 0;
rwsr[i] = 0;
}
rwsb.resize(reg_maxb);
for (int i = 0; i < reg_maxb; i++)
{
rwsb[i] = 0;
}
#endif
unsigned int size = prog.size();
PC = start;
arg = argument;
// This is a deterministic PRNG, used in some ORAM routines
// Having a fixed seed is therefore no problem
// Used to allow the generation of a pseudo-random CINT
uint8_t seed[SEED_SIZE];
memset(seed, 0, SEED_SIZE);
prng.SetSeedFromRandom(seed);
while (PC < size)
{
bool restart = prog.execute_instr(PC, *this, P, machine, OCD);
if (restart)
{
/* Call trigger
* This halts machine, until something external says OK to go
* Possibly loading a new schedule
*/
machine.get_IO().trigger(machine.schedule);
printf("Restarting...\n");
for (unsigned int connection = 0; connection < maximum_number_of_ssl_connections; connection++)
{
RunOpenCheck(P, machine.get_IO().Get_Check(), connection);
}
// Now parse any new schedule into the main machine
machine.Load_Schedule_Into_Memory();
printf("Loaded programs\n");
fflush(stdout);
PC = size;
}
}
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::convert_sint_to_sregint_small(int i0, int i1, Player &P)
{
unsigned int size = numBits(gfp::pr());
unsigned long p = gfp::pr().get_ui();
SRegint x, y, z, r2;
SBit bit;
word b;
bool done = false;
vector<Share> bpr(size);
vector<SBit> b2r(size);
auto &daBitGen = get_generator();
// Get a set of size daBits, until the value is less than p
while (!done)
{ // Get daBits
daBitV.get_daBits(bpr, b2r, daBitGen, P);
r2.assign_zero();
for (unsigned int i = 0; i < size; i++)
{
r2.set_bit(i, b2r[i]);
}
z.sub(r2, p, P, online_thread_num);
bit = z.less_than_zero();
P.OP2->Open_Bit(b, bit, P);
if (b == 1)
{
done = true;
}
}
// Now create the integer r, which is guaranteed to be uniform mod p
Share r(P.whoami());
r.assign_zero();
for (int i = size - 1; i >= 0; i--)
{
r.add(r); // r=2*r
r.add(bpr[i]);
}
// Now form x-r
vector<Share> S_xr(1);
S_xr[0].sub(read_Sp(i0), r);
// Now open x-r
vector<gfp> gfp_xr(1);
P.OP->Open_To_All_Begin(gfp_xr, S_xr, P, 2);
P.OP->Open_To_All_End(gfp_xr, S_xr, P, 2);
// Now add z=(x-r)+r in the GC routines
bigint bi_xr;
to_bigint(bi_xr, gfp_xr[0]);
z.add(bi_xr.get_ui(), r2, P, online_thread_num);
// Remember to do this modp
y.sub(z, p, P, online_thread_num);
bit = y.less_than_zero();
// z=bit*z+(1-b)*y
z.Bit_AND(z, bit, P, online_thread_num);
bit.negate();
y.Bit_AND(y, bit, P, online_thread_num);
z.Bitwise_XOR(z, y);
// Now compare to p/2 to work out whether we need to negate
y.sub(z, p / 2, P, online_thread_num);
bit = y.less_than_zero();
// Now compute bit*z + (1-bit)*(-z)
y.sub(p, z, P, online_thread_num);
y.negate(y, P, online_thread_num);
z.Bit_AND(z, bit, P, online_thread_num);
bit.negate();
y.Bit_AND(y, bit, P, online_thread_num);
x.Bitwise_XOR(z, y);
// Write back into the processor
write_srint(i1, x);
}
template <>
void Processor<aBitVector, aBit>::convert_sint_to_sregint_sub(const vector<vector<aBit>> &input, int i1, Player &P)
{
vector<vector<aBit>> output(1, vector<aBit>(sreg_bitl));
// Evaluate the garbled circuit
Base_Garbled_Circuit GC;
GC.Garble(Global_Circuit_Store.Circuits[LSSS_to_GC], P, online_thread_num);
GC.Evaluate(output, input, Global_Circuit_Store.Circuits[LSSS_to_GC], P);
// Write back into the processor
write_srint(i1, output[0]);
}
template <>
void Processor<aBitVector2, Share2>::convert_sint_to_sregint_sub(const vector<vector<Share2>> &input, int i1, Player &P)
{
vector<vector<Share2>> output(1, vector<Share2>(sreg_bitl));
// Evaluate the garbled circuit
Evaluate(output, input, Global_Circuit_Store.Circuits[LSSS_to_GC], P, online_thread_num);
// Write back into the processor
aBitVector2 temp;
collapse(temp, output[0]);
write_srint(i1, temp);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
template <>
void Processor<aBitVector2, Share2>::convert_sint_to_sregint_sub(const vector<vector<aBit>> &input, int i1, Player &P)
{
// This is a dummy function to get around a C++ quirk
throw not_implemented();
}
template <>
void Processor<aBitVector, aBit>::convert_sint_to_sregint_sub(const vector<vector<Share2>> &input, int i1, Player &P)
{
// This is a dummy function to get around a C++ quirk
throw not_implemented();
}
#pragma GCC diagnostic pop
template <class SRegint, class SBit> void Processor<SRegint, SBit>::convert_sint_to_sregint(int i0, int i1, Player &P)
{
unsigned int size0 = numBits(gfp::pr());
unsigned int size1 = sreg_bitl + conv_stat_sec;
if (size1 > size0)
{
size1 = size0;
}
if (Global_Circuit_Store.convert_ok == false)
{
if (sreg_bitl < size0)
{
throw cannot_do_conversion();
}
convert_sint_to_sregint_small(i0, i1, P);
return;
}
// Set up arrays for the GC stuff
vector<vector<SBit>> input(2);
input[0].resize(size0);
input[1].resize(size1);
// Get a daBit
vector<Share> bpr(size1);
auto &daBitGen = get_generator();
daBitV.get_daBits(bpr, input[1], daBitGen, P);
// Now form r
Share r(P.whoami());
r.assign_zero();
for (int i = size1 - 1; i >= 0; i--)
{
r.add(r); // r=2*r
r.add(bpr[i]);
}
// Now form x-r
vector<Share> S_xr(1);
S_xr[0].sub(read_Sp(i0), r);
// Now open x-r
vector<gfp> gfp_xr(1);
P.OP->Open_To_All_Begin(gfp_xr, S_xr, P, 2);
P.OP->Open_To_All_End(gfp_xr, S_xr, P, 2);
// Form vector<SBit> of gfp_xr
bigint bi_xr;
to_bigint(bi_xr, gfp_xr[0]);
for (unsigned int i = 0; i < size0; i++)
{
if ((bi_xr & 1) == 0)
{
input[0][i].assign_zero(P.whoami());
}
else
{
input[0][i].assign_one(P.whoami());
}
bi_xr >>= 1;
}
// Evaluate the garbled circuit
convert_sint_to_sregint_sub(input, i1, P);
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::convert_sregint_to_sint_sub(int i0, vector<Share> &apr, Player &P)
{
/* Get sreg_bitl daBits */
vector<SBit> a2r(sreg_bitl);
auto &daBitGen = get_generator();
daBitV.get_daBits(apr, a2r, daBitGen, P);
/* Add onto the input register */
for (unsigned int i = 0; i < sreg_bitl; i++)
{
a2r[i].add(read_srint(i0).get_bit(i));
}
/* Now open these */
vector<word> v(sreg_bitl);
P.OP2->Open_Bits(v, a2r, P);
Share one(1, P.whoami(), P.get_mac_keys());
/* Now form the XOR on the modp side
* apr[i]= apr[i]+v[i]-2*v[i]*apr[i]
* = (v[i]==0) * apr[i] + (v[i]==1)*(-apr[i]+1)
*/
for (unsigned int i = 0; i < sreg_bitl; i++)
{
if (v[i] == 1)
{
apr[i].negate();
apr[i].add(one);
}
}
}
template <class SRegint, class SBit> void Processor<SRegint, SBit>::convert_sregint_to_sint(int i0, int i1, Player &P)
{
vector<Share> apr(sreg_bitl);
convert_sregint_to_sint_sub(i0, apr, P);
/* Now form ans = -2^{sreg_bitl-1} * apr[sreg_bitl-1]
* + sum_{i=0}^sreg_bitl-2 2^i*apr[i]
*/
bigint te = 2;
Share tes, ans = apr[0];
for (unsigned int i = 1; i < sreg_bitl - 1; i++)
{
tes.mul(apr[i], te);
ans.add(tes);
te <<= 1;
}
tes.mul(apr[sreg_bitl - 1], te);
ans.sub(tes);
// Write back into the processor
write_Sp(i1, ans);
}
template <class SRegint, class SBit> void Processor<SRegint, SBit>::convert_suregint_to_sint(int i0, int i1, Player &P)
{
vector<Share> apr(sreg_bitl);
convert_sregint_to_sint_sub(i0, apr, P);
/* Now form ans = sum_{i=0}^sreg_bitl-1 2^i*apr[i]
*/
bigint te = 2;
Share tes, ans = apr[0];
for (unsigned int i = 1; i < sreg_bitl; i++)
{
tes.mul(apr[i], te);
ans.add(tes);
te <<= 1;
}
// Write back into the processor
write_Sp(i1, ans);
}
template <class SRegint, class SBit> void Processor<SRegint, SBit>::convert_sbit_to_sint(int i0, int i1, Player &P)
{
Share apr;
SBit a2r;
/* Get a single daBit */
auto &daBitGen = get_generator();
daBitV.get_daBit(apr, a2r, daBitGen, P);
/* Add onto the input register */
a2r.add(read_sbit(i0));
/* Now open it */
word v;
P.OP2->Open_Bit(v, a2r, P);
Share one(1, P.whoami(), P.get_mac_keys());
/* Now form the XOR on the modp side
* apr= apr+v-2*v*apr
* = (v==0) * apr + (v==1)*(-apr+1)
*/
if ((v & 1) == 1)
{
apr.negate();
apr.add(one);
}
// Write back into the processor
write_Sp(i1, apr);
}
template <class SRegint, class SBit>
void Processor<SRegint, SBit>::convert_sint_to_sbit(int i0, int i1, Player &P, offline_control_data &OCD)
{
Share apr;
SBit a2r;
/* Get a single daBit */
auto &daBitGen = get_generator();
daBitV.get_daBit(apr, a2r, daBitGen, P);
/* Multiply apr by the input register */
// First get the triple
list<Share> la, lb, lc;
Wait_For_Preproc(TRIPLE, 1, online_thread_num, OCD);
OCD.mul_mutex[online_thread_num].lock();
Split_Lists(la, SacrificeD[online_thread_num].TD.ta, 1);
Split_Lists(lb, SacrificeD[online_thread_num].TD.tb, 1);
Split_Lists(lc, SacrificeD[online_thread_num].TD.tc, 1);
OCD.mul_mutex[online_thread_num].unlock();
// Now compute rho and sigma
vector<Share> rho_sigma(2);
Share a = la.front();
Share b = lb.front();
Share c = lc.front();
rho_sigma[0].sub(read_Sp(i0), a);
rho_sigma[1].sub(apr, b);
// Open rho and sigma
vector<gfp> gfp_rho_sigma(2);
P.OP->Open_To_All_Begin(gfp_rho_sigma, rho_sigma, P, 2);
P.OP->Open_To_All_End(gfp_rho_sigma, rho_sigma, P, 2);
// Compute the product
Share te1, te2, product = c;
gfp te3;
te3.mul(gfp_rho_sigma[0], gfp_rho_sigma[1]);
te1.mul(b, gfp_rho_sigma[0]);
te2.mul(a, gfp_rho_sigma[1]);
product.add(te1);
product.add(te2);
product.add(product, te3, P.get_mac_keys());
// Now compute the shared bit on the modp side of apr xor Sp[i0]
vector<Share> bit(1);
bit[0].add(apr, read_Sp(i0));
bit[0].sub(product);
bit[0].sub(product);
// Open this bit
vector<gfp> gfp_bit(1);
P.OP->Open_To_All_Begin(gfp_bit, bit, P, 2);
P.OP->Open_To_All_End(gfp_bit, bit, P, 2);
// Xor the bit on the gf2 side
bigint ans;
to_bigint(ans, gfp_bit[0]);
a2r.add(ans.get_ui());
// Write back into the processor
write_sbit(i1, a2r);
}
template <class SRegint, class SBit> void Processor<SRegint, SBit>::load_daBits(unsigned int number_of_times, const Player &P)
{
unsigned int material_type = Dabit<SBit>::get_ID();
Choicebits choicebits = *Choicebits::get_choicebits(P.whoami());
std::shared_ptr<MySQL_Garbled_Circuit_Storage> db_storage = std::dynamic_pointer_cast<MySQL_Garbled_Circuit_Storage>(gc_storage);
vector<Dabit<SBit>> read_dabits = db_storage->read_dabits<SBit>(number_of_times, choicebits, material_type, P.whoami());
dabits.insert(dabits.end(), read_dabits.begin(), read_dabits.end());
}
template <>
void Processor<aBitVector, aBit>::garble_circuit(unsigned int circuit_number, unsigned int number_of_times, Player &P)
{
Global_Circuit_Store.check(circuit_number);
Choicebits choicebits = *Choicebits::get_choicebits(P.whoami());
vector<Base_Garbled_Circuit> garbled_circuits;
for (unsigned int i = 0; i < number_of_times; i++)
{
Base_Garbled_Circuit GC;
GC.Garble(Global_Circuit_Store.Circuits[circuit_number], P, online_thread_num);
garbled_circuits.push_back(GC);
}
gc_storage->save(garbled_circuits, choicebits, circuit_number, P.whoami());
}
template <>
void Processor<aBitVector2, Share2>::garble_circuit([[maybe_unused]] unsigned int circuit_number,
[[maybe_unused]] unsigned int number_of_times,
[[maybe_unused]] Player &P)
{
throw Instruction_not_supported_exception();
}
template <>
void Processor<aBitVector, aBit>::load_garbled_circuits(unsigned int circuit_number, unsigned int number_of_times,
Player &P)
{
Choicebits choicebits = *Choicebits::get_choicebits(P.whoami());
vector<Base_Garbled_Circuit> read_circuits =
gc_storage->read(number_of_times, choicebits, circuit_number, P.whoami());
circuits.insert(circuits.end(), read_circuits.begin(), read_circuits.end());
}
template <>
void Processor<aBitVector2, Share2>::load_garbled_circuits([[maybe_unused]] unsigned int circuit_number,
[[maybe_unused]] unsigned int number_of_times,
[[maybe_unused]] Player &P)
{
throw Instruction_not_supported_exception();
}
template <> void Processor<aBitVector, aBit>::evaluate_garbled_circuit(unsigned int circuit_number, Player &P)
{
Global_Circuit_Store.check(circuit_number);
// Set up arrays for the IO from the GC and load in the inputs
unsigned int numI = Global_Circuit_Store.Circuits[circuit_number].num_inputs();
unsigned int numO = Global_Circuit_Store.Circuits[circuit_number].num_outputs();
vector<vector<aBit>> input(numI);
vector<vector<aBit>> output(numO);
aBitVector temp;
int regib;
bool empty = true;
// The first argument is at the bottom of the stack, the
// last is on the top. So need to take this into account
for (int i = numI - 1; i >= 0; i--)
{
input[i].resize(Global_Circuit_Store.Circuits[circuit_number].num_iWires(i));
for (int j = input[i].size() - 1; j >= 0; j--)
{
if (empty)
{
pop_srint(temp);
empty = false;
regib = sreg_bitl - 1;
}
input[i][j] = temp.get_bit(regib);
regib--;
if (regib < 0)
{
empty = true;
}
}
}
for (unsigned int i = 0; i < numO; i++)
{
output[i].resize(Global_Circuit_Store.Circuits[circuit_number].num_oWires(i));
}
// Evaluate the garbled circuit
try
{
if (reuse_circuit_zero == 1 && circuits.empty())
{
Base_Garbled_Circuit GC_l;
GC_l.Garble(Global_Circuit_Store.Circuits[circuit_number], P, online_thread_num);
circuits.push_back(GC_l);
}
circuits[0].Evaluate(output, input, Global_Circuit_Store.Circuits[circuit_number], P);
if (reuse_circuit_zero != 1)
{
circuits.erase(circuits.begin());
}
}
catch (const bad_value &error)
{
throw No_available_circuits_to_evaluate_exception(circuit_number);
}
// Now process the outputs
regib = 0;
temp.assign_zero();
for (int i = numO - 1; i >= 0; i--)
{
for (unsigned int j = 0; j < output[i].size(); j++)
{
temp.set_bit(regib, output[i][j]);
regib++;
if (regib == sreg_bitl)
{
regib = 0;
push_srint(temp);
temp.assign_zero();
}
}
}
}
template <>
void Processor<aBitVector2, Share2>::evaluate_garbled_circuit([[maybe_unused]] unsigned int circuit_number,
[[maybe_unused]] Player &P)
{
throw Instruction_not_supported_exception();
}
/* Arguments are obtained from the srint stack, and then outputs
* are pushed back onto the srint stack
*/
template <> void Processor<aBitVector, aBit>::apply_GC(unsigned int circuit_number, Player &P)
{
Global_Circuit_Store.check(circuit_number);
// Set up arrays for the IO from the GC and load in the inputs
unsigned int numI = Global_Circuit_Store.Circuits[circuit_number].num_inputs();
unsigned int numO = Global_Circuit_Store.Circuits[circuit_number].num_outputs();
vector<vector<aBit>> input(numI);
vector<vector<aBit>> output(numO);
aBitVector temp;
int regib;
bool empty = true;
// The first argument is at the bottom of the stack, the
// last is on the top. So need to take this into account
for (int i = numI - 1; i >= 0; i--)
{
input[i].resize(Global_Circuit_Store.Circuits[circuit_number].num_iWires(i));
for (int j = input[i].size() - 1; j >= 0; j--)
{
if (empty)
{
pop_srint(temp);
empty = false;
regib = sreg_bitl - 1;
}
input[i][j] = temp.get_bit(regib);
regib--;
if (regib < 0)
{
empty = true;
}
}
}
for (unsigned int i = 0; i < numO; i++)
{
output[i].resize(Global_Circuit_Store.Circuits[circuit_number].num_oWires(i));
}
// Evaluate the garbled circuit
Base_Garbled_Circuit GC;
GC.Garble(Global_Circuit_Store.Circuits[circuit_number], P, online_thread_num);
GC.Evaluate(output, input, Global_Circuit_Store.Circuits[circuit_number], P);
// Now process the outputs
regib = 0;
temp.assign_zero();
for (int i = numO - 1; i >= 0; i--)
{
for (unsigned int j = 0; j < output[i].size(); j++)
{
temp.set_bit(regib, output[i][j]);
regib++;
if (regib == sreg_bitl)
{
regib = 0;
push_srint(temp);
temp.assign_zero();
}
}
}
}
/* Arguments are obtained from the srint stack, and then outputs
* are pushed back onto the srint stack
*/
template <> void Processor<aBitVector2, Share2>::apply_GC(unsigned int circuit_number, Player &P)
{
// Check circuit is loaded
Global_Circuit_Store.check(circuit_number);
// Set up arrays for the IO from the GC and load in the inputs
unsigned int numI = Global_Circuit_Store.Circuits[circuit_number].num_inputs();
unsigned int numO = Global_Circuit_Store.Circuits[circuit_number].num_outputs();
vector<vector<Share2>> input(numI);
vector<vector<Share2>> output(numO);
aBitVector2 temp;
vector<Share2> temp_bits;
int regib;
bool empty = true;