forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 272
/
TinySecret.h
285 lines (236 loc) · 6.68 KB
/
TinySecret.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
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
/*
* TinySecret.h
*
*/
#ifndef GC_TINYSECRET_H_
#define GC_TINYSECRET_H_
#include "Secret.h"
#include "TinyShare.h"
#include "ShareParty.h"
#include "OT/Rectangle.h"
#include "OT/BitDiagonal.h"
template<class T> class NPartyTripleGenerator;
template<class T> class OTTripleGenerator;
template<class T> class TinyMultiplier;
namespace GC
{
template<class T> class TinyOnlyPrep;
template<class T> class TinyMC;
template<class T> class VectorProtocol;
template<class T> class VectorInput;
template<class T> class CcdPrep;
template<class T>
class VectorSecret : public Secret<T>
{
typedef VectorSecret This;
public:
typedef T part_type;
typedef Secret<part_type> super;
typedef typename part_type::mac_key_type mac_key_type;
typedef BitVec open_type;
typedef BitVec clear;
typedef typename part_type::sacri_type sacri_type;
typedef typename part_type::mac_type mac_type;
typedef typename part_type::mac_share_type mac_share_type;
typedef BitDiagonal Rectangle;
typedef typename T::super check_type;
static const bool dishonest_majority = T::dishonest_majority;
static const bool variable_players = T::variable_players;
static const bool needs_ot = T::needs_ot;
static const bool has_mac = T::has_mac;
static const bool malicious = T::malicious;
static const bool expensive_triples = T::expensive_triples;
static const bool randoms_for_opens = false;
static const int default_length = 64;
static int size()
{
return part_type::size() * default_length;
}
static void specification(octetStream& os)
{
T::specification(os);
}
static void read_or_generate_mac_key(string directory, const Player& P,
mac_key_type& key)
{
T::read_or_generate_mac_key(directory, P, key);
}
template<class U>
static void reveal_inst(U& processor, const vector<int>& args)
{
T::reveal_inst(processor, args);
}
static This constant(BitVec other, int my_num, mac_key_type alphai,
int n_bits = -1)
{
if (n_bits < 0)
n_bits = other.length();
This res;
res.resize_regs(n_bits);
for (int i = 0; i < n_bits; i++)
res.get_reg(i) = part_type::constant(other.get_bit(i), my_num, alphai);
return res;
}
VectorSecret()
{
}
VectorSecret(const super& other) :
super(other)
{
}
VectorSecret(const Clear& other) :
super(other)
{
}
VectorSecret(const part_type& other)
{
this->get_regs().push_back(other);
}
void assign(const char* buffer)
{
this->resize_regs(default_length);
for (int i = 0; i < default_length; i++)
this->get_reg(i).assign(buffer + i * part_type::size());
}
This operator-(const This& other) const
{
return *this + other;
}
This& operator^=(const This& other)
{
return *this = *this + other;
}
This operator*(const BitVec& other) const
{
This res = *this;
for (int i = 0; i < super::size(); i++)
if (not other.get_bit(i))
res.get_reg(i) = {};
return res;
}
This operator&(const BitVec::super& other) const
{
return *this * BitVec(other);
}
void extend_bit(This& res, int n_bits) const
{
auto& regs = res.get_regs();
regs.assign(n_bits, this->get_reg(0));
}
void mask(This& res, int n_bits) const
{
if (this != &res)
res.get_regs().assign(this->get_regs().begin(),
this->get_regs().begin()
+ min(size_t(n_bits), this->get_regs().size()));
res.resize_regs(n_bits);
}
T get_bit(int i) const
{
return this->get_reg(i);
}
void xor_bit(size_t i, const T& bit)
{
if (i < this->get_regs().size())
XOR(this->get_reg(i), this->get_reg(i), bit);
else
{
this->resize_regs(i + 1);
this->get_reg(i) = bit;
}
}
void output(ostream& s, bool human = true) const
{
assert(this->get_regs().size() == default_length);
for (auto& reg : this->get_regs())
reg.output(s, human);
}
void input(istream&, bool)
{
throw not_implemented();
}
template <class U>
void my_input(U& inputter, BitVec value, int n_bits)
{
inputter.add_mine(value, n_bits);
}
template <class U>
void other_input(U& inputter, int from, int n_bits)
{
inputter.add_other(from, n_bits);
}
template <class U>
void finalize_input(U& inputter, int from, int n_bits)
{
inputter.finalize(from, n_bits).mask(*this, n_bits);
}
void random_bit()
{
auto& thread = GC::ShareThread<typename T::whole_type>::s();
*this = thread.DataF.get_part().get_bit();
}
};
template<int S>
class TinySecret : public VectorSecret<TinyShare<S>>
{
typedef VectorSecret<TinyShare<S>> super;
typedef TinySecret This;
public:
typedef TinyMC<This> MC;
typedef MC MAC_Check;
typedef VectorProtocol<This> Protocol;
typedef VectorInput<This> Input;
typedef CcdPrep<This> LivePrep;
typedef Memory<This> DynamicMemory;
typedef OTTripleGenerator<This> TripleGenerator;
typedef typename super::part_type::TripleGenerator InputGenerator;
typedef TinyMultiplier<This> Multiplier;
static string type_short()
{
return "T";
}
static MC* new_mc(typename super::mac_key_type mac_key)
{
return new MC(mac_key);
}
static void store_clear_in_dynamic(Memory<This>& mem,
const vector<ClearWriteAccess>& accesses)
{
auto& party = ShareThread<This>::s();
for (auto access : accesses)
mem[access.address] = super::constant(access.value,
party.P->my_num(), {});
}
static void generate_mac_key(typename This::mac_key_type& dest,
const typename This::mac_key_type& source)
{
dest = source;
}
TinySecret()
{
}
TinySecret(const typename super::super& other) :
super(other)
{
}
TinySecret(const typename super::part_type& other) :
super(other)
{
}
void reveal(size_t n_bits, Clear& x)
{
auto& to_open = *this;
to_open.resize_regs(n_bits);
auto& party = ShareThread<This>::s();
x = party.MC->POpen(to_open, *party.P);
}
};
template<class U>
const int VectorSecret<U>::default_length;
template<class T>
inline VectorSecret<T> operator*(const BitVec& clear, const VectorSecret<T>& share)
{
return share * clear;
}
} /* namespace GC */
#endif /* GC_TINYSECRET_H_ */