-
Notifications
You must be signed in to change notification settings - Fork 131
/
booleancircuits.cpp
3255 lines (2704 loc) · 98.4 KB
/
booleancircuits.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
/**
\file booleancircuits.cpp
\author michael.zohner@ec-spride.de
\copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
Copyright (C) 2019 Engineering Cryptographic Protocols Group, TU Darmstadt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ABY 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
\brief A collection of boolean circuits for boolean and yao sharing in the ABY framework
*/
#include "booleancircuits.h"
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#ifdef HW_DEBUG
#include <memory>
#endif
void BooleanCircuit::Init() {
m_nShareBitLen = 1;
m_nNumANDSizes = 1;
m_vANDs = (non_lin_vec_ctx*) malloc(sizeof(non_lin_vec_ctx) * m_nNumANDSizes);
m_vANDs[0].bitlen = 1;
m_vANDs[0].numgates = 0;
//Instantiate with regular 1 output AND gate
m_vTTlens.resize(1);
m_vTTlens[0].resize(1);
m_vTTlens[0][0].resize(1);
m_vTTlens[0][0][0].tt_len = 4;
m_vTTlens[0][0][0].numgates = 0;
m_vTTlens[0][0][0].out_bits = 1;
//m_vTTlens = (non_lin_vec_ctx*) malloc(sizeof(tt_lens_ctx) * m_nNumTTSizes);
m_nGates = 0;
if (m_eContext == S_BOOL) {
m_nRoundsAND = 1;
m_nRoundsXOR = 0;
m_nRoundsIN.resize(2, 1);
m_nRoundsOUT.resize(3, 1);
} else if(m_eContext == S_SPLUT) {
m_nRoundsAND = 1;
m_nRoundsXOR = 0;
m_nRoundsIN.resize(2, 1);
m_nRoundsOUT.resize(3, 1);
} else if (m_eContext == S_YAO || m_eContext == S_YAO_REV) {
m_nRoundsAND = 0;
m_nRoundsXOR = 0;
m_nRoundsIN.resize(2);
m_nRoundsIN[0] = 1;
m_nRoundsIN[1] = 2;
m_nRoundsOUT.resize(3, 1);
m_nRoundsOUT[1] = 0; //the client already holds the output bits from the start
} else {
std::cerr << "Sharing type not implemented for Boolean circuit" << std::endl;
std::exit(EXIT_FAILURE);
}
m_nB2YGates = 0;
m_nA2YGates = 0;
m_nNumXORVals = 0;
m_nNumXORGates = 0;
m_nYSwitchGates = 0;
m_nUNIVGates = 0;
}
/*void BooleanCircuit::UpdateANDsOnLayers() {
}*/
void BooleanCircuit::Cleanup() {
//TODO implement completely
free(m_vANDs);
// should not be necessary:
// m_vTTlens[0][0].clear();
// m_vTTlens[0].clear();
// m_vTTlens.clear();
// m_nRoundsIN.clear();
// m_nRoundsOUT.clear();
}
uint32_t BooleanCircuit::PutANDGate(uint32_t inleft, uint32_t inright) {
uint32_t gateid;
if(m_eContext != S_SPLUT) {
gateid = m_cCircuit->PutPrimitiveGate(G_NON_LIN, inleft, inright, m_nRoundsAND);
if (m_eContext == S_BOOL) {
UpdateInteractiveQueue(gateid);
} else if (m_eContext == S_YAO || m_eContext == S_YAO_REV) {
//if context == YAO, no communication round is required
UpdateLocalQueue(gateid);
} else {
std::cerr << "Context not recognized" << std::endl;
}
if (m_vGates[gateid].nvals != INT_MAX) {
m_vANDs[0].numgates += m_vGates[gateid].nvals;
} else {
std::cerr << "INT_MAX not allowed as nvals" << std::endl;
}
} else {
std::vector<uint32_t> in(2);
uint64_t andttable=8;
in[0] = inleft;
in[1] = inright;
gateid = PutTruthTableGate(in, 1, &andttable);
}
return gateid;
}
std::vector<uint32_t> BooleanCircuit::PutANDGate(std::vector<uint32_t> inleft, std::vector<uint32_t> inright) {
PadWithLeadingZeros(inleft, inright);
uint32_t resultbitlen = inleft.size();
std::vector<uint32_t> out(resultbitlen);
for (uint32_t i = 0; i < resultbitlen; i++){
out[i] = PutANDGate(inleft[i], inright[i]);
}
return out;
}
share* BooleanCircuit::PutANDGate(share* ina, share* inb) {
return new boolshare(PutANDGate(ina->get_wires(), inb->get_wires()), this);
}
uint32_t BooleanCircuit::PutVectorANDGate(uint32_t choiceinput, uint32_t vectorinput) {
if (m_eContext != S_BOOL) {
std::cerr << "Building a vector AND gate is currently only possible for GMW!" << std::endl;
//TODO: prevent error by putting repeater gate on choiceinput and an AND gate between choiceinput and vectorinput
return 0;
}
uint32_t gateid = m_cCircuit->PutNonLinearVectorGate(G_NON_LIN_VEC, choiceinput, vectorinput, m_nRoundsAND);
UpdateInteractiveQueue(gateid);
//std::cout << "Putting a vector and gate between a gate with " << m_vGates[choiceinput].nvals << " and " <<
// m_vGates[vectorinput].nvals << ", res gate has nvals = " << m_vGates[gateid].nvals << std::endl;
if (m_vGates[gateid].nvals != INT_MAX) {
//Update vector AND sizes
//find location of vector AND bitlength
//int pos = FindBitLenPositionInVec(m_vGates[gateid].nvals, m_vANDs, m_nNumANDSizes);
int pos = FindBitLenPositionInVec(m_vGates[gateid].gs.avs.bitlen, m_vANDs, m_nNumANDSizes);
if (pos == -1) {
//Create new entry for the bit-length
m_nNumANDSizes++;
non_lin_vec_ctx* temp = (non_lin_vec_ctx*) malloc(sizeof(non_lin_vec_ctx) * m_nNumANDSizes);
memcpy(temp, m_vANDs, (m_nNumANDSizes - 1) * sizeof(non_lin_vec_ctx));
free(m_vANDs);
m_vANDs = temp;
//m_vANDs[m_nNumANDSizes - 1].bitlen = m_vGates[gateid].nvals;
m_vANDs[m_nNumANDSizes - 1].bitlen = m_vGates[gateid].gs.avs.bitlen;
m_vANDs[m_nNumANDSizes - 1].numgates = m_vGates[choiceinput].nvals; //1
} else {
//increase number of vector ANDs for this bitlength by one
m_vANDs[pos].numgates+=m_vGates[choiceinput].nvals;
}
}
return gateid;
}
share* BooleanCircuit::PutXORGate(share* ina, share* inb) {
return new boolshare(PutXORGate(ina->get_wires(), inb->get_wires()), this);
}
uint32_t BooleanCircuit::PutXORGate(uint32_t inleft, uint32_t inright) {
//std::cout << "inleft = " << inleft << ", inright = " << inright << std::endl;
uint32_t gateid = m_cCircuit->PutPrimitiveGate(G_LIN, inleft, inright, m_nRoundsXOR);
UpdateLocalQueue(gateid);
m_nNumXORVals += m_vGates[gateid].nvals;
m_nNumXORGates += 1;
return gateid;
}
std::vector<uint32_t> BooleanCircuit::PutXORGate(std::vector<uint32_t> inleft, std::vector<uint32_t> inright) {
PadWithLeadingZeros(inleft, inright);
uint32_t resultbitlen = inleft.size();
std::vector<uint32_t> out(resultbitlen);
for (uint32_t i = 0; i < resultbitlen; i++){
out[i] = PutXORGate(inleft[i], inright[i]);
}
return out;
}
uint32_t BooleanCircuit::PutINGate(e_role src) {
uint32_t gateid = m_cCircuit->PutINGate(m_eContext, 1, m_nShareBitLen, src, m_nRoundsIN[src]);
UpdateInteractiveQueue(gateid);
switch (src) {
case SERVER:
m_vInputGates[0].push_back(gateid);
m_vInputBits[0] += m_vGates[gateid].nvals;
break;
case CLIENT:
m_vInputGates[1].push_back(gateid);
m_vInputBits[1] += m_vGates[gateid].nvals;
break;
case ALL:
m_vInputGates[0].push_back(gateid);
m_vInputGates[1].push_back(gateid);
m_vInputBits[0] += m_vGates[gateid].nvals;
m_vInputBits[1] += m_vGates[gateid].nvals;
break;
default:
std::cerr << "Role not recognized" << std::endl;
break;
}
return gateid;
}
uint32_t BooleanCircuit::PutSIMDINGate(uint32_t ninvals, e_role src) {
uint32_t gateid = m_cCircuit->PutINGate(m_eContext, ninvals, m_nShareBitLen, src, m_nRoundsIN[src]);
UpdateInteractiveQueue(gateid);
switch (src) {
case SERVER:
m_vInputGates[0].push_back(gateid);
m_vInputBits[0] += m_vGates[gateid].nvals;
break;
case CLIENT:
m_vInputGates[1].push_back(gateid);
m_vInputBits[1] += m_vGates[gateid].nvals;
break;
case ALL:
m_vInputGates[0].push_back(gateid);
m_vInputGates[1].push_back(gateid);
m_vInputBits[0] += m_vGates[gateid].nvals;
m_vInputBits[1] += m_vGates[gateid].nvals;
break;
default:
std::cerr << "Role not recognized" << std::endl;
break;
}
return gateid;
}
share* BooleanCircuit::PutDummyINGate(uint32_t bitlen) {
std::vector<uint32_t> wires(bitlen);
for(uint32_t i = 0; i < bitlen; i++) {
wires[i] = PutINGate((e_role) !m_eMyRole);
}
return new boolshare(wires, this);
}
share* BooleanCircuit::PutDummySIMDINGate(uint32_t nvals, uint32_t bitlen) {
std::vector<uint32_t> wires(bitlen);
for(uint32_t i = 0; i < bitlen; i++) {
wires[i] = PutSIMDINGate(nvals, (e_role) !m_eMyRole);
}
return new boolshare(wires, this);
}
uint32_t BooleanCircuit::PutSharedINGate() {
uint32_t gateid = m_cCircuit->PutSharedINGate(m_eContext, 1, m_nShareBitLen);
UpdateLocalQueue(gateid);
return gateid;
}
uint32_t BooleanCircuit::PutSharedSIMDINGate(uint32_t ninvals) {
uint32_t gateid = m_cCircuit->PutSharedINGate(m_eContext, ninvals, m_nShareBitLen);
UpdateLocalQueue(gateid);
return gateid;
}
uint32_t BooleanCircuit::PutINGate(uint64_t val, e_role role) {
//return PutINGate(nvals, &val, role);
uint32_t gateid = PutINGate(role);
if (role == m_eMyRole) {
//assign value
GATE* gate = &(m_vGates[gateid]);
gate->gs.ishare.inval = (UGATE_T*) calloc(ceil_divide(1 * m_nShareBitLen, sizeof(UGATE_T) * 8), sizeof(UGATE_T));
memcpy(gate->gs.ishare.inval, &val, ceil_divide(1 * m_nShareBitLen, 8));
gate->instantiated = true;
}
return gateid;
}
uint32_t BooleanCircuit::PutSharedINGate(uint64_t val) {
uint32_t gateid = PutSharedINGate();
//assign value
GATE* gate = &(m_vGates[gateid]);
gate->gs.val = (UGATE_T*) calloc(ceil_divide(1 * m_nShareBitLen, sizeof(UGATE_T) * 8), sizeof(UGATE_T));
memcpy(gate->gs.val, &val, ceil_divide(1 * m_nShareBitLen, 8));
gate->instantiated = true;
return gateid;
}
uint32_t BooleanCircuit::PutSIMDINGate(uint32_t nvals, uint64_t val, e_role role) {
//return PutINGate(nvals, &val, role);
uint32_t gateid = PutSIMDINGate(nvals, role);
if (role == m_eMyRole) {
//assign value
GATE* gate = &(m_vGates[gateid]);
gate->gs.ishare.inval = (UGATE_T*) calloc(ceil_divide(nvals * m_nShareBitLen, sizeof(UGATE_T) * 8), sizeof(UGATE_T));
memcpy(gate->gs.ishare.inval, &val, ceil_divide(nvals * m_nShareBitLen, 8));
gate->instantiated = true;
}
return gateid;
}
uint32_t BooleanCircuit::PutSharedSIMDINGate(uint32_t nvals, uint64_t val) {
//return PutINGate(nvals, &val, role);
uint32_t gateid = PutSharedSIMDINGate(nvals);
//assign value
GATE* gate = &(m_vGates[gateid]);
gate->gs.val = (UGATE_T*) calloc(ceil_divide(nvals * m_nShareBitLen, sizeof(UGATE_T) * 8), sizeof(UGATE_T));
memcpy(gate->gs.val, &val, ceil_divide(nvals * m_nShareBitLen, 8));
gate->instantiated = true;
return gateid;
}
uint32_t BooleanCircuit::PutYaoSharedSIMDINGate(uint32_t nvals, yao_fields keys) {
uint32_t gateid = PutSharedSIMDINGate(nvals);
//assign value
GATE* gate = &(m_vGates[gateid]);
//TODO: fixed to 128-bit security atm. CHANGE
uint8_t keybytelen = ceil_divide(128, 8);
if(m_eMyRole == SERVER) {
gate->gs.yinput.outKey = (uint8_t*) malloc(keybytelen * nvals);
memcpy(gate->gs.yinput.outKey, keys.outKey, keybytelen * nvals);
gate->gs.yinput.pi = (uint8_t*) malloc(nvals);
memcpy(gate->gs.yinput.pi, keys.pi, nvals);
} else {
gate->gs.yval = (uint8_t*) malloc(keybytelen * nvals);
memcpy(gate->gs.yval, keys.outKey, keybytelen * nvals);
}
gate->instantiated = true;
return gateid;
}
share* BooleanCircuit::PutYaoSharedSIMDINGate(uint32_t nvals, yao_fields* keys, uint32_t bitlen) {
share* shr = new boolshare(bitlen, this);
for(uint32_t i = 0; i < bitlen; i++) {
shr->set_wire_id(i, PutYaoSharedSIMDINGate(nvals, keys[i]));
}
return shr;
}
uint32_t BooleanCircuit::PutOUTGate(uint32_t parentid, e_role dst) {
uint32_t gateid = m_cCircuit->PutOUTGate(parentid, dst, m_nRoundsOUT[dst]);
UpdateInteractiveQueue(gateid);
switch (dst) {
case SERVER:
m_vOutputGates[0].push_back(gateid);
m_vOutputBits[0] += m_vGates[gateid].nvals;
break;
case CLIENT:
m_vOutputGates[1].push_back(gateid);
m_vOutputBits[1] += m_vGates[gateid].nvals;
break;
case ALL:
m_vOutputGates[0].push_back(gateid);
m_vOutputGates[1].push_back(gateid);
m_vOutputBits[0] += m_vGates[gateid].nvals;
m_vOutputBits[1] += m_vGates[gateid].nvals;
break;
default:
std::cerr << "Role not recognized" << std::endl;
break;
}
return gateid;
}
share* BooleanCircuit::PutOUTGate(share* parent, e_role dst) {
return new boolshare(PutOUTGate(parent->get_wires(), dst), this);
}
std::vector<uint32_t> BooleanCircuit::PutOUTGate(std::vector<uint32_t> parentids, e_role dst) {
std::vector<uint32_t> gateid = m_cCircuit->PutOUTGate(parentids, dst, m_nRoundsOUT[dst]);
//TODO: optimize
for (uint32_t i = 0; i < gateid.size(); i++) {
UpdateInteractiveQueue(gateid[i]);
switch (dst) {
case SERVER:
m_vOutputGates[0].push_back(gateid[i]);
m_vOutputBits[0] += m_vGates[gateid[i]].nvals;
break;
case CLIENT:
m_vOutputGates[1].push_back(gateid[i]);
m_vOutputBits[1] += m_vGates[gateid[i]].nvals;
break;
case ALL:
m_vOutputGates[0].push_back(gateid[i]);
m_vOutputGates[1].push_back(gateid[i]);
m_vOutputBits[0] += m_vGates[gateid[i]].nvals;
m_vOutputBits[1] += m_vGates[gateid[i]].nvals;
break;
default:
std::cerr << "Role not recognized" << std::endl;
break;
}
}
return gateid;
}
std::vector<uint32_t> BooleanCircuit::PutSharedOUTGate(std::vector<uint32_t> parentids) {
std::vector<uint32_t> out = m_cCircuit->PutSharedOUTGate(parentids);
for(uint32_t i = 0; i < out.size(); i++) {
UpdateLocalQueue(out[i]);
}
return out;
}
share* BooleanCircuit::PutSharedOUTGate(share* parent) {
return new boolshare(PutSharedOUTGate(parent->get_wires()), this);
}
share* BooleanCircuit::PutCONSGate(UGATE_T val, uint32_t bitlen) {
return PutSIMDCONSGate(1, val, bitlen);
}
share* BooleanCircuit::PutCONSGate(uint8_t* val, uint32_t bitlen) {
return PutSIMDCONSGate(1, val, bitlen);
}
share* BooleanCircuit::PutCONSGate(uint32_t* val, uint32_t bitlen) {
return PutSIMDCONSGate(1, val, bitlen);
}
share* BooleanCircuit::PutSIMDCONSGate(uint32_t nvals, UGATE_T val, uint32_t bitlen) {
share* shr = new boolshare(bitlen, this);
for(uint32_t i = 0; i < bitlen; ++i) {
shr->set_wire_id(i, PutConstantGate((val >> i) & 1, nvals));
}
return shr;
}
share* BooleanCircuit::PutSIMDCONSGate(uint32_t nvals, uint8_t* val, uint32_t bitlen) {
share* shr = new boolshare(bitlen, this);
for(uint32_t i = 0; i < bitlen; ++i) {
uint32_t shift = i % 8;
shr->set_wire_id(i, PutConstantGate((val[(i / 8)] & (1 << shift)) >> shift, nvals));
}
return shr;
}
share* BooleanCircuit::PutSIMDCONSGate(uint32_t nvals, uint32_t* val, uint32_t bitlen) {
share* shr = new boolshare(bitlen, this);
for(uint32_t i = 0; i < bitlen; ++i) {
uint32_t shift = i % 32;
shr->set_wire_id(i, PutConstantGate((val[(i / 32)] & (1 << shift)) >> shift, nvals));
}
return shr;
}
uint32_t BooleanCircuit::PutConstantGate(UGATE_T val, uint32_t nvals) {
uint32_t gateid = m_cCircuit->PutConstantGate(m_eContext, val, nvals, m_nShareBitLen);
UpdateLocalQueue(gateid);
return gateid;
}
uint32_t BooleanCircuit::PutINVGate(uint32_t parentid) {
uint32_t gateid = m_cCircuit->PutINVGate(parentid);
UpdateLocalQueue(gateid);
return gateid;
}
std::vector<uint32_t> BooleanCircuit::PutINVGate(std::vector<uint32_t> parentid) {
std::vector<uint32_t> out(parentid.size());
for (uint32_t i = 0; i < out.size(); i++)
out[i] = PutINVGate(parentid[i]);
return out;
}
share* BooleanCircuit::PutINVGate(share* parent) {
return new boolshare(PutINVGate(parent->get_wires()), this);
}
uint32_t BooleanCircuit::PutY2BCONVGate(uint32_t parentid) {
std::vector<uint32_t> in(1, parentid);
uint32_t gateid = m_cCircuit->PutCONVGate(in, 1, S_BOOL, m_nShareBitLen);
m_vGates[gateid].depth++;
UpdateLocalQueue(gateid);
//a Y input gate cannot be parent to a Y2B gate. Alternatively, put a Boolean input gate
assert(m_vGates[parentid].type != G_IN);
return gateid;
}
uint32_t BooleanCircuit::PutB2YCONVGate(uint32_t parentid) {
std::vector<uint32_t> in(1, parentid);
uint32_t gateid = m_cCircuit->PutCONVGate(in, 2, S_YAO, m_nShareBitLen);
UpdateInteractiveQueue(gateid);
//treat similar to input gate of client and server
m_nB2YGates += m_vGates[gateid].nvals;
return gateid;
}
uint32_t BooleanCircuit::PutYSwitchRolesGate(uint32_t parentid) {
std::vector<uint32_t> in(1, parentid);
assert(m_eContext == S_YAO || m_eContext == S_YAO_REV);
assert(m_vGates[in[0]].context != m_eContext);
uint32_t gateid = m_cCircuit->PutCONVGate(in, 2, m_eContext, m_nShareBitLen);
UpdateInteractiveQueue(gateid);
//treat similar to input gate of client and server
m_nYSwitchGates += m_vGates[gateid].nvals;
return gateid;
}
std::vector<uint32_t> BooleanCircuit::PutYSwitchRolesGate(std::vector<uint32_t> parentid) {
std::vector<uint32_t> out(parentid.size());
for (uint32_t i = 0; i < parentid.size(); i++) {
out[i] = PutYSwitchRolesGate(parentid[i]);
}
return out;
}
std::vector<uint32_t> BooleanCircuit::PutY2BCONVGate(std::vector<uint32_t> parentid) {
std::vector<uint32_t> out(parentid.size());
for (uint32_t i = 0; i < parentid.size(); i++) {
out[i] = PutY2BCONVGate(parentid[i]);
}
return out;
}
std::vector<uint32_t> BooleanCircuit::PutB2YCONVGate(std::vector<uint32_t> parentid) {
std::vector<uint32_t> out(parentid.size());
for (uint32_t i = 0; i < parentid.size(); i++) {
out[i] = PutB2YCONVGate(parentid[i]);
}
return out;
}
share* BooleanCircuit::PutY2BGate(share* ina) {
return new boolshare(PutY2BCONVGate(ina->get_wires()), this);
}
share* BooleanCircuit::PutB2YGate(share* ina) {
return new boolshare(PutB2YCONVGate(ina->get_wires()), this);
}
share* BooleanCircuit::PutYSwitchRolesGate(share* ina) {
return new boolshare(PutYSwitchRolesGate(ina->get_wires()), this);
}
std::vector<uint32_t> BooleanCircuit::PutA2YCONVGate(std::vector<uint32_t> parentid) {
std::vector<uint32_t> srvshares(m_vGates[parentid[0]].sharebitlen);
std::vector<uint32_t> clishares(m_vGates[parentid[0]].sharebitlen);
for (uint32_t i = 0; i < m_vGates[parentid[0]].sharebitlen; i++) {
srvshares[i] = m_cCircuit->PutCONVGate(parentid, 1, S_YAO, m_nShareBitLen);
m_vGates[srvshares[i]].gs.pos = 2 * i;
m_vGates[srvshares[i]].depth++; //increase depth by 1 since yao is evaluated before arith
UpdateInteractiveQueue(srvshares[i]);
clishares[i] = m_cCircuit->PutCONVGate(parentid, 2, S_YAO, m_nShareBitLen);
m_vGates[clishares[i]].gs.pos = 2 * i + 1;
m_vGates[clishares[i]].depth++; //increase depth by 1 since yao is evaluated before arith
UpdateInteractiveQueue(clishares[i]);
}
m_nA2YGates += m_vGates[parentid[0]].nvals * m_vGates[parentid[0]].sharebitlen;
return PutAddGate(srvshares, clishares);
}
share* BooleanCircuit::PutA2YGate(share* ina) {
return new boolshare(PutA2YCONVGate(ina->get_wires()), this);
}
uint32_t BooleanCircuit::PutStructurizedCombinerGate(std::vector<uint32_t> input, uint32_t pos_start,
uint32_t pos_incr, uint32_t nvals) {
uint32_t gateid = m_cCircuit->PutStructurizedCombinerGate(input, pos_start, pos_incr, nvals);
UpdateLocalQueue(gateid);
return gateid;
}
share* BooleanCircuit::PutStructurizedCombinerGate(share* input, uint32_t pos_start,
uint32_t pos_incr, uint32_t nvals) {
share* out= new boolshare(1, this);
nstructcombgates++;
out->set_wire_id(0, PutStructurizedCombinerGate(input->get_wires(), pos_start, pos_incr, nvals));
return out;
}
uint32_t BooleanCircuit::PutUniversalGate(uint32_t a, uint32_t b, uint32_t op_id) {
uint32_t gateid;
if(m_eContext == S_YAO) { //In case of Yao, put universal gate
gateid = m_cCircuit->PutUniversalGate(a, b, op_id, m_nRoundsAND);
UpdateLocalQueue(gateid);
m_nUNIVGates+=m_vGates[gateid].nvals;
} else if (m_eContext == S_BOOL) { //In case of GMW, replace universal gate by sub-circuit
gateid = PutUniversalGateCircuit(a, b, op_id);
} else {
std::cerr << "Context not recognized in PutUniversalGate" << std::endl;
std::exit(EXIT_FAILURE);
}
return gateid;
}
std::vector<uint32_t> BooleanCircuit::PutUniversalGate(std::vector<uint32_t> a, std::vector<uint32_t> b, uint32_t op_id) {
uint32_t niters = std::min(a.size(), b.size());
std::vector<uint32_t> output(niters);
for(uint32_t i = 0; i < niters; i++) {
output[i] = PutUniversalGate(a[i], b[i], op_id);
}
return output;
}
share* BooleanCircuit::PutUniversalGate(share* a, share* b, uint32_t op_id) {
return new boolshare(PutUniversalGate(a->get_wires(), b->get_wires(), op_id), this);
}
uint32_t BooleanCircuit::PutCallbackGate(std::vector<uint32_t> in, uint32_t rounds, void (*callback)(GATE*, void*),
void* infos, uint32_t nvals) {
uint32_t gateid = m_cCircuit->PutCallbackGate(in, rounds, callback, infos, nvals);
if(rounds > 0) {
UpdateInteractiveQueue(gateid);
} else {
UpdateLocalQueue(gateid);
}
return gateid;
}
share* BooleanCircuit::PutCallbackGate(share* in, uint32_t rounds, void (*callback)(GATE*, void*),
void* infos, uint32_t nvals) {
return new boolshare(PutCallbackGate(in->get_wires(), rounds, callback, infos, nvals), this);
}
/*uint64_t* transposeTT(uint32_t dima, uint32_t dimb, uint64_t* ttable) {
uint32_t longbits = sizeof(uint64_t) * 8;
uint64_t* newtt = (uint64_t*) calloc(bits_in_bytes(dima * dimb), sizeof(uint8_t));
std::cout << "dima = " << dima << ", dimb = " << dimb << std::endl;
std::cout << "Before Transposing: " << (hex) << std::endl;
for(uint32_t i = 0; i < ceil_divide(dima * dimb, longbits); i++) {
std::cout << ttable[i] << " ";
}
std::cout << (dec) << std::endl;
for(uint32_t i = 0; i < dima; i++) {
for(uint32_t j = 0; j < dimb; j++) {
uint32_t idxsrc = (i * dimb + j);
uint32_t idxdst = (j * dima + i);
newtt[idxdst / longbits] |= (((ttable[idxsrc / longbits] >> (idxsrc % longbits)) & 0x01) << (idxdst % longbits));
}
}
std::cout << "After Transposing: " << (hex) << std::endl;
for(uint32_t i = 0; i < ceil_divide(dima * dimb, longbits); i++) {
std::cout << newtt[i] << " ";
}
std::cout << (dec) << std::endl;
return newtt;
}*/
std::vector<uint32_t> BooleanCircuit::PutTruthTableMultiOutputGate(std::vector<uint32_t> in, uint32_t out_bits,
uint64_t* ttable) {
//assert(m_eContext == S_BOOL_NO_MT);
//uint32_t tmpgate = m_cCircuit->PutTruthTableGate(in, 1, out_bits, ttable);
//UpdateInteractiveQueue(tmpgate);
//Transpose truth table
//ttable = transposeTT(1<<in.size(), out_bits, ttable);
uint32_t tmpgate = PutTruthTableGate(in, out_bits, ttable);
std::vector<uint32_t> bitlens(out_bits, m_vGates[in[0]].nvals);
//assert(out_bits <= 8);
std::vector<uint32_t> output = m_cCircuit->PutSplitterGate(tmpgate, bitlens);
for(uint32_t i = 0; i < output.size(); i++) {
UpdateLocalQueue(output[i]);
}
return output;
}
share* BooleanCircuit::PutTruthTableMultiOutputGate(share* in, uint32_t output_bitlen, uint64_t* ttable) {
return new boolshare(PutTruthTableMultiOutputGate(in->get_wires(), output_bitlen, ttable), this);
}
uint32_t BooleanCircuit::PutTruthTableGate(std::vector<uint32_t> in, uint32_t out_bits, uint64_t* ttable) {
assert(m_eContext == S_SPLUT || m_eContext == S_BOOL);
uint32_t gateid = m_cCircuit->PutTruthTableGate(in, 1, out_bits, ttable);
UpdateTruthTableSizes(1<<in.size(), gateid, out_bits);
UpdateInteractiveQueue(gateid);
return gateid;
}
share* BooleanCircuit::PutTruthTableGate(share* in, uint64_t* ttable) {
boolshare* out = new boolshare(1, this);
out->set_wire_id(0, PutTruthTableGate(in->get_wires(), 1, ttable));
return out;
}
//check if the len exists, otherwise allocate new and update
void BooleanCircuit::UpdateTruthTableSizes(uint32_t len, uint32_t gateid, uint32_t out_bits) {
//check depth and resize if required
uint32_t depth = m_vGates[gateid].depth;
uint32_t nvals = m_vGates[gateid].nvals/out_bits;
if(depth >= m_vTTlens.size()) {
uint32_t old_depth = m_vTTlens.size();
uint32_t nlens = m_vTTlens[0].size();
m_vTTlens.resize(depth+1);
//copy old values from 0-pos
for(uint32_t i = old_depth; i < m_vTTlens.size(); i++) {
m_vTTlens[i].resize(nlens);
for(uint32_t j = 0; j < nlens; j++) {
uint32_t nouts = m_vTTlens[0][j].size();
m_vTTlens[i][j].resize(nouts);
for(uint32_t k = 0; k < nouts; k++) {
m_vTTlens[i][j][k].numgates = 0;
m_vTTlens[i][j][k].tt_len = m_vTTlens[0][j][k].tt_len;
m_vTTlens[i][j][k].out_bits = m_vTTlens[0][j][k].out_bits;
}
}
}
}
//check whether the address for the input sizes already exist
bool ins_exist = false;
bool outs_exist = false;
uint32_t id;
for(uint32_t i = 0; i < m_vTTlens[0].size() && !ins_exist; i++) {
if(len == m_vTTlens[depth][i][0].tt_len) {
//check whether the bitlen already exists for the input size
ins_exist = true;
id = i;
for(uint32_t j = 0; j < m_vTTlens[depth][i].size() && !outs_exist; j++) {
if(m_vTTlens[depth][i][j].out_bits == out_bits) {
outs_exist = true;
m_vTTlens[depth][i][j].numgates += nvals;
//In case of OP-LUT, also save the truth table which is needed in the setup phase
if(m_eContext == S_BOOL) {
for(uint32_t n = 0; n < nvals; n++) {
m_vTTlens[depth][i][j].ttable_values.push_back(m_vGates[gateid].gs.tt.table);
}
}
}
}
}
}
//the input size does not exist, create new one!
if(!ins_exist) {
uint32_t old_in_lens = m_vTTlens[0].size();
for(uint32_t i = 0; i < m_vTTlens.size(); i++) {
m_vTTlens[i].resize(old_in_lens+1);
m_vTTlens[i][old_in_lens].resize(1);
m_vTTlens[i][old_in_lens][0].tt_len = len;
m_vTTlens[i][old_in_lens][0].numgates = 0;
m_vTTlens[i][old_in_lens][0].out_bits = out_bits;
}
//m_vTTlens[depth][old_lens].tt_len = len;//should work without this too
m_vTTlens[depth][old_in_lens][0].numgates = nvals;
//In case of OP-LUT, also save the truth table which is needed in the setup phase
if(m_eContext == S_BOOL) {
for(uint32_t n = 0; n < nvals; n++) {
m_vTTlens[depth][old_in_lens][0].ttable_values.push_back(m_vGates[gateid].gs.tt.table);
}
}
outs_exist = true;
}
//the out size do not exist; create new
if(!outs_exist) {
uint32_t old_out_lens = m_vTTlens[0][id].size();
for(uint32_t i = 0; i < m_vTTlens.size(); i++) {
m_vTTlens[i][id].resize(old_out_lens+1);
m_vTTlens[i][id][old_out_lens].tt_len = len;
m_vTTlens[i][id][old_out_lens].numgates = 0;
m_vTTlens[i][id][old_out_lens].out_bits = out_bits;
}
//m_vTTlens[depth][id][old_out_lens].tt_len = len;//should work without this too
m_vTTlens[depth][id][old_out_lens].numgates = nvals;
//In case of OP-LUT, also save the truth table which is needed in the setup phase
if(m_eContext == S_BOOL) {
for(uint32_t n = 0; n < nvals; n++) {
m_vTTlens[depth][id][old_out_lens].ttable_values.push_back(m_vGates[gateid].gs.tt.table);
}
}
outs_exist = true;
}
}
//enqueue interactive gate queue
void BooleanCircuit::UpdateInteractiveQueue(uint32_t gateid) {
if (m_vGates[gateid].depth + 1 > m_vInteractiveQueueOnLvl.size()) {
m_vInteractiveQueueOnLvl.resize(m_vGates[gateid].depth + 1);
if (m_vGates[gateid].depth + 1 > m_nMaxDepth) {
m_nMaxDepth = m_vGates[gateid].depth + 1;
}
}
m_vInteractiveQueueOnLvl[m_vGates[gateid].depth].push_back(gateid);
m_nGates++;
}
//enqueue locally evaluated gate queue
void BooleanCircuit::UpdateLocalQueue(uint32_t gateid) {
if (m_vGates[gateid].depth + 1 > m_vLocalQueueOnLvl.size()) {
//std::cout << "increasing size of local queue" << std::endl;
m_vLocalQueueOnLvl.resize(m_vGates[gateid].depth + 1);
if (m_vGates[gateid].depth + 1 > m_nMaxDepth) {
m_nMaxDepth = m_vGates[gateid].depth + 1;
}
}
m_vLocalQueueOnLvl[m_vGates[gateid].depth].push_back(gateid);
m_nGates++;
}
share* BooleanCircuit::PutLeftShifterGate(share* in, uint32_t pos) {
return new boolshare(PutLeftShifterGate(in->get_wires(), in->get_max_bitlength(), pos, in->get_nvals()), this);
}
//shift val by pos positions to the left and fill lower wires with zeros
std::vector<uint32_t> BooleanCircuit::PutLeftShifterGate(std::vector<uint32_t> val, uint32_t max_bitlen, uint32_t pos, uint32_t nvals) {
assert(pos < max_bitlen); // cannot shift beyond last bit
uint32_t zerogate = PutConstantGate(0, nvals);
std::vector<uint32_t> out(pos, zerogate);
uint32_t newsize = pos + val.size();
uint32_t extra_bits = newsize > max_bitlen ? (newsize - max_bitlen) : 0;
out.reserve(newsize - extra_bits);
out.insert(out.end(), val.cbegin(), val.cend() - extra_bits);
return out;
}
// Builds a universal gate that output op_id depending on the circuit
uint32_t BooleanCircuit::PutUniversalGateCircuit(uint32_t a, uint32_t b, uint32_t op_id) {
uint32_t nvals = std::max(m_vGates[a].nvals, m_vGates[b].nvals);
uint32_t c0 = PutConstantGate(op_id & 0x01, nvals);
uint32_t c1 = PutConstantGate((op_id>>1) & 0x01, nvals);
uint32_t c2 = PutConstantGate((op_id>>2) & 0x01, nvals);
uint32_t c3 = PutConstantGate((op_id>>3) & 0x01, nvals);
uint32_t c0c1 = PutXORGate(c0, c1);
uint32_t c2c3 = PutXORGate(c2, c3);
uint32_t bc0c1 = PutANDGate(b, c0c1);
uint32_t bc2c3 = PutANDGate(b, c2c3);
uint32_t o0 = PutXORGate(c0, bc0c1);
uint32_t o1 = PutXORGate(c2, bc2c3);
uint32_t o0o1 = PutXORGate(o0, o1);
uint32_t ao0o1 = PutANDGate(a, o0o1);
return PutXORGate(o0, ao0o1);
}
share* BooleanCircuit::PutADDGate(share* ina, share* inb) {
//also output the carry of the result as long as the additional carry does not exceed the maximum bit length of the higher of both inputs
bool carry = std::max(ina->get_bitlength(), inb->get_bitlength()) < std::max(ina->get_max_bitlength(), inb->get_max_bitlength());
return new boolshare(PutAddGate(ina->get_wires(), inb->get_wires(), carry), this);
}
std::vector<uint32_t> BooleanCircuit::PutAddGate(std::vector<uint32_t> left, std::vector<uint32_t> right, BOOL bCarry) {
PadWithLeadingZeros(left, right);
if (m_eContext == S_BOOL) {
return PutDepthOptimizedAddGate(left, right, bCarry);
} if (m_eContext == S_SPLUT) {
return PutLUTAddGate(left, right, bCarry);
} else {
return PutSizeOptimizedAddGate(left, right, bCarry);
}
}
//a + b, do we need a carry?
std::vector<uint32_t> BooleanCircuit::PutSizeOptimizedAddGate(std::vector<uint32_t> a, std::vector<uint32_t> b, BOOL bCarry) {
// left + right mod (2^Rep)
// Construct C[i] gates
PadWithLeadingZeros(a, b);
uint32_t inputbitlen = a.size();// + (!!bCarry);
std::vector<uint32_t> C(inputbitlen);
uint32_t axc, bxc, acNbc;
C[0] = PutXORGate(a[0], a[0]);//PutConstantGate(0, m_vGates[a[0]].nvals); //the second parameter stands for the number of vals
uint32_t i = 0;
for (; i < inputbitlen - 1; i++) {
//===================
// New Gates
// a[i] xor c[i]
axc = PutXORGate(a[i], C[i]);
// b[i] xor c[i]
bxc = PutXORGate(b[i], C[i]);
// axc AND bxc
acNbc = PutANDGate(axc, bxc);
// C[i+1]
C[i + 1] = PutXORGate(C[i], acNbc);
}
#ifdef ZDEBUG
std::cout << "Finished carry generation" << std::endl;
#endif
if (bCarry) {
axc = PutXORGate(a[i], C[i]);
// b[i] xor c[i]
bxc = PutXORGate(b[i], C[i]);
// axc AND bxc
acNbc = PutANDGate(axc, bxc);
}
#ifdef ZDEBUG
std::cout << "Finished additional carry generation" << std::endl;
#endif
// Construct a[i] xor b[i] gates
std::vector<uint32_t> AxB(inputbitlen);
for (uint32_t i = 0; i < inputbitlen; i++) {
// a[i] xor b[i]
AxB[i] = PutXORGate(a[i], b[i]);
}
#ifdef ZDEBUG
std::cout << "Finished parity on inputs" << std::endl;
#endif
// Construct Output gates of Addition
std::vector<uint32_t> out(inputbitlen + (!!bCarry));
for (uint32_t i = 0; i < inputbitlen; i++) {
out[i] = PutXORGate(C[i], AxB[i]);
}
#ifdef ZDEBUG
std::cout << "Finished parity on inputs xor carries" << std::endl;
#endif
if (bCarry)
out[inputbitlen] = PutXORGate(C[i], acNbc);
#ifdef ZDEBUG
std::cout << "Finished parity on additional carry and inputs" << std::endl;
#endif
return out;
}
//TODO: there is a bug when adding 3 and 1 as two 2-bit numbers and expecting a carry
std::vector<uint32_t> BooleanCircuit::PutDepthOptimizedAddGate(std::vector<uint32_t> a, std::vector<uint32_t> b, BOOL bCARRY, bool vector_and) {
PadWithLeadingZeros(a, b);