forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProducer.h
257 lines (196 loc) · 6.97 KB
/
Producer.h
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
/*
* Producer.h
*
*/
#ifndef FHEOFFLINE_PRODUCER_H_
#define FHEOFFLINE_PRODUCER_H_
#include "Networking/Player.h"
#include "FHE/Plaintext.h"
#include "FHEOffline/EncCommit.h"
#include "FHEOffline/DistDecrypt.h"
#include "FHEOffline/Sacrificing.h"
#include "Protocols/Share.h"
#include "Math/Setup.h"
template <class T>
string prep_filename(string type, int my_num, int thread_num,
bool initial, string dir = PREP_DIR);
template <class T>
string open_prep_file(ofstream& outf, string type, int my_num, int thread_num,
bool initial, bool clear, string dir = PREP_DIR);
template <class FD>
class Producer
{
protected:
int n_slots;
int output_thread;
bool write_output;
string dir;
public:
typedef typename FD::T T;
typedef typename FD::S S;
map<string, Timer> timers;
Producer(int output_thread = 0, bool write_output = true);
virtual ~Producer() {}
virtual string data_type() = 0;
virtual condition required_randomness() { return Full; }
virtual string open_file(ofstream& outf, int my_num, int thread_num, bool initial,
bool clear);
virtual void clear_file(int my_num, int thread_num = 0, bool initial = true);
virtual void clear() = 0;
virtual void run(const Player& P, const FHE_PK& pk,
const Ciphertext& calpha, EncCommitBase<T, FD, S>& EC,
DistDecrypt<FD>& dd, const T& alphai) = 0;
virtual int sacrifice(const Player& P, MAC_Check<T>& MC) = 0;
int num_slots() { return n_slots; }
virtual size_t report_size(ReportType type) { (void)type; return 0; }
};
template <class T, class FD, class S>
class TripleProducer : public TripleSacriFactory< Share<T> >, public Producer<FD>
{
unsigned int i;
public:
Plaintext_<FD> values[3], macs[3];
Plaintext<T,FD,S> &ai, &bi, &ci;
Plaintext<T,FD,S> &gam_ai, &gam_bi, &gam_ci;
string data_type() { return "Triples"; }
TripleProducer(const FD& Field, int my_num, int output_thread = 0,
bool write_output = true, string dir = PREP_DIR);
void clear() { this->triples.clear(); }
void run(const Player& P, const FHE_PK& pk,
const Ciphertext& calpha, EncCommitBase<T, FD, S>& EC,
DistDecrypt<FD>& dd, const T& alphai);
int sacrifice(const Player& P, MAC_Check<T>& MC);
void get(Share<T>& a, Share<T>& b, Share<T>& c);
void reset() { i = 0; }
int num_slots() { return ai.num_slots(); }
size_t report_size(ReportType type);
};
template <class FD>
using TripleProducer_ = TripleProducer<typename FD::T, FD, typename FD::S>;
template <class FD>
class TupleProducer : public Producer<FD>
{
protected:
typedef typename FD::T T;
unsigned int i;
public:
Plaintext_<FD> values[2], macs[2];
TupleProducer(const FD& FieldD, int output_thread = 0,
bool write_output = true);
virtual ~TupleProducer() {}
void compute_macs(const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, Ciphertext& ca,
Ciphertext & cb);
virtual void get(Share<T>& a, Share<T>& b);
};
template <class FD>
class InverseProducer : public TupleProducer<FD>,
public TupleSacriFactory< Share<typename FD::T> >
{
typedef typename FD::T T;
Plaintext_<FD> ab;
TripleProducer_<FD> triple_producer;
bool produce_triples;
public:
Plaintext_<FD> &ai, &bi;
Plaintext_<FD> &gam_ai, &gam_bi;
InverseProducer(const FD& FieldD, int my_num, int output_thread = 0,
bool write_output = true, bool produce_triples = true,
string dir = PREP_DIR);
string data_type() { return "Inverses"; }
void clear() { this->tuples.clear(); }
void run(const Player& P, const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, const T& alphai);
void get(Share<T>& a, Share<T>& b);
int sacrifice(const Player& P, MAC_Check<T>& MC);
};
template <class FD>
class SquareProducer : public TupleProducer<FD>,
public TupleSacriFactory< Share<typename FD::T> >
{
public:
typedef typename FD::T T;
Plaintext_<FD> &ai, &bi;
Plaintext_<FD> &gam_ai, &gam_bi;
SquareProducer(const FD& FieldD, int my_num, int output_thread = 0,
bool write_output = true, string dir = PREP_DIR);
string data_type() { return "Squares"; }
void clear() { this->tuples.clear(); }
void run(const Player& P, const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, const T& alphai);
void get(Share<T>& a, Share<T>& b) { TupleProducer<FD>::get(a, b); }
int sacrifice(const Player& P, MAC_Check<T>& MC);
};
class gfpBitProducer : public Producer<FFT_Data>,
public SingleSacriFactory< Share<gfp> >
{
typedef FFT_Data FD;
unsigned int i;
Plaintext_<FD> vi, gam_vi;
SquareProducer<FFT_Data> square_producer;
bool produce_squares;
public:
vector< Share<gfp> > bits;
gfpBitProducer(const FD& FieldD, int my_num, int output_thread = 0,
bool write_output = true, bool produce_squares = true,
string dir = PREP_DIR);
string data_type() { return "Bits"; }
void clear() { bits.clear(); }
void run(const Player& P, const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, const gfp& alphai);
int output(const Player& P, int thread, int output_thread = 0);
void get(Share<gfp>& a);
int sacrifice(const Player& P, MAC_Check<T>& MC);
};
// glue
template<class FD>
Producer<FD>* new_bit_producer(const FD& FieldD, const Player& P,
const FHE_PK& pk, int covert,
bool produce_squares = true, int thread_num = 0, bool write_output = true,
string dir = PREP_DIR);
class gf2nBitProducer : public Producer<P2Data>
{
typedef P2Data FD;
typedef gf2n_short T;
ofstream outf;
bool write_output;
EncCommit_<FD> ECB;
public:
gf2nBitProducer(const Player& P, const FHE_PK& pk, int covert,
int output_thread = 0, bool write_output = true, string dir = PREP_DIR);
string data_type() { return "Bits"; }
void clear() {}
void run(const Player& P, const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, const T& alphai);
int sacrifice(const Player& P, MAC_Check<T>& MC);
};
template <class FD>
class InputProducer : public Producer<FD>
{
typedef typename FD::T T;
typedef typename FD::S S;
ofstream* outf;
const Player& P;
bool write_output;
public:
vector<vector<InputTuple<Share<T>>>> inputs;
InputProducer(const Player& P, int output_thread = 0, bool write_output = true,
string dir = PREP_DIR);
~InputProducer();
string data_type() { return "Inputs"; }
void clear() {}
void run(const Player& P, const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, const T& alphai)
{
run(P, pk, calpha, EC, dd, alphai, -1);
}
void run(const Player& P, const FHE_PK& pk, const Ciphertext& calpha,
EncCommitBase_<FD>& EC, DistDecrypt<FD>& dd, const T& alphai,
int player);
int sacrifice(const Player& P, MAC_Check<T>& MC);
// no ops
string open_file(ofstream& outf, int my_num, int thread_num, bool initial,
bool clear);
void clear_file(int my_num, int thread_num = 0, bool initial = 0);
};
#endif /* FHEOFFLINE_PRODUCER_H_ */