forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairwiseSetup.cpp
237 lines (212 loc) · 6.56 KB
/
PairwiseSetup.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
/*
* PairwiseSetup.cpp
*
*/
#include <FHEOffline/PairwiseSetup.h>
#include "FHE/NoiseBounds.h"
#include "FHE/NTL-Subs.h"
#include "Math/Setup.h"
#include "FHEOffline/Proof.h"
#include "FHEOffline/PairwiseMachine.h"
#include "FHEOffline/TemiSetup.h"
#include "Tools/Commit.h"
#include "Tools/Bundle.h"
#include "Processor/OnlineOptions.h"
#include "Protocols/LowGearKeyGen.h"
#include "Protocols/Share.hpp"
#include "Protocols/mac_key.hpp"
template <class FD>
void PairwiseSetup<FD>::init(const Player& P, int sec, int plaintext_length,
int& extra_slack)
{
cout << "Finding parameters for security " << sec << " and field size ~2^"
<< plaintext_length << endl;
PRNG G;
G.ReSeed();
octetStream o;
if (P.my_num() == 0)
{
extra_slack =
generate_semi_setup(plaintext_length, sec, params, FieldD, true);
params.pack(o);
FieldD.pack(o);
o.store(extra_slack);
P.send_all(o);
}
else
{
P.receive_player(0, o);
params.unpack(o);
FieldD.unpack(o);
FieldD.init_field();
o.get(extra_slack);
}
alpha = FieldD;
alphai = read_or_generate_mac_key<Share<T>>(P);
alpha.assign_constant(alphai);
}
template <class FD>
void PairwiseSetup<FD>::secure_init(Player& P, PairwiseMachine& machine, int plaintext_length, int sec)
{
::secure_init(*this, P, machine, plaintext_length, sec, params);
alpha = FieldD;
machine.sk = FHE_SK(params, FieldD.get_prime());
for (auto& pk : machine.other_pks)
pk = FHE_PK(params, FieldD.get_prime());
}
template <class T, class U>
void secure_init(T& setup, Player& P, U& machine,
int plaintext_length, int sec, FHE_Params& params)
{
assert(sec >= 0);
machine.sec = sec;
params.set_min_sec(sec);
string filename = PREP_DIR + T::name() + "-"
+ to_string(plaintext_length) + "-" + to_string(sec) + "-"
+ to_string(params.secp()) + "-"
+ to_string(params.get_matrix_dim()) + "-"
+ OnlineOptions::singleton.prime.get_str() + "-"
+ to_string(CowGearOptions::singleton.top_gear()) + "-P"
+ to_string(P.my_num()) + "-" + to_string(P.num_players());
string reason;
try
{
ifstream file(filename);
octetStream os;
os.input(file);
os.get(machine.extra_slack);
setup.unpack(os);
}
catch (exception& e)
{
reason = e.what();
}
try
{
setup.check(P, machine);
}
catch (exception& e)
{
reason = e.what();
}
if (not reason.empty())
{
if (OnlineOptions::singleton.verbose)
cerr << "Generating parameters for security " << sec
<< " and field size ~2^" << plaintext_length
<< " because no suitable material "
"from a previous run was found (" << reason << ")"
<< endl;
setup = {};
setup.generate(P, machine, plaintext_length, sec);
setup.check(P, machine);
octetStream os;
os.store(machine.extra_slack);
setup.pack(os);
ofstream file(filename);
os.output(file);
}
}
template <class FD>
void PairwiseSetup<FD>::generate(Player&, MachineBase& machine,
int plaintext_length, int sec)
{
machine.extra_slack = generate_semi_setup(plaintext_length, sec, params,
FieldD, true);
}
template<class FD>
void PairwiseSetup<FD>::pack(octetStream& os) const
{
params.pack(os);
FieldD.pack(os);
alpha.pack(os);
alphai.pack(os);
}
template<class FD>
void PairwiseSetup<FD>::unpack(octetStream& os)
{
params.unpack(os);
FieldD.unpack(os);
FieldD.init_field();
alpha.unpack(os);
alphai.unpack(os);
}
template <class FD>
void PairwiseSetup<FD>::check(Player& P, PairwiseMachine& machine)
{
Bundle<octetStream> bundle(P);
bundle.mine.store(machine.extra_slack);
params.pack(bundle.mine);
FieldD.hash(bundle.mine);
bundle.compare(P);
}
template <class FD>
void PairwiseSetup<FD>::covert_key_generation(Player& P,
PairwiseMachine& machine, int num_runs)
{
vector<SeededPRNG> G(num_runs);
vector<AllCommitments> commits(num_runs, P);
vector<FHE_KeyPair> my_keys(num_runs, {params, FieldD.get_prime()});
Bundle<octetStream> pks(P);
for (int i = 0; i < num_runs; i++)
{
my_keys[i].generate(G[i]);
my_keys[i].pk.pack(pks.mine);
commits[i].commit({SEED_SIZE, G[i].get_seed()});
}
P.Broadcast_Receive(pks);
int challenge = GlobalPRNG(P).get_uint(num_runs);
machine.sk = my_keys[challenge].sk;
machine.pk = my_keys[challenge].pk;
for (int i = 0; i < num_runs; i++)
if (i != challenge)
commits[i].open({SEED_SIZE, G[i].get_seed()});
for (int i = 0; i < num_runs; i++)
{
for (int j = 0; j < P.num_players(); j++)
if (j != P.my_num())
{
FHE_PK pk(params);
pk.unpack(pks[j]);
if (i == challenge)
machine.other_pks[j] = pk;
else
{
FHE_KeyPair pair(params, FieldD.get_prime());
PRNG prng(commits[i].messages[j]);
pair.generate(prng);
if (pair.pk != pk)
throw bad_keygen("covert pairwise key generation");
}
}
}
}
template <class FD>
void PairwiseSetup<FD>::covert_mac_generation(Player& P,
PairwiseMachine& machine, int num_runs)
{
vector<const FHE_PK*> pks;
for (auto& pk : machine.other_pks)
pks.push_back(&pk);
covert_generation(alpha, machine.enc_alphas, pks, &P, num_runs, Diagonal);
alphai = alpha.element(0);
}
template <class FD>
void PairwiseSetup<FD>::key_and_mac_generation(Player& P,
PairwiseMachine& machine, int num_runs, true_type)
{
covert_key_generation(P, machine, num_runs);
covert_mac_generation(P, machine, num_runs);
}
template <class FD>
void PairwiseSetup<FD>::set_alphai(T alphai)
{
this->alphai = alphai;
alpha.assign_constant(alphai);
}
template class PairwiseSetup<FFT_Data>;
template class PairwiseSetup<P2Data>;
template void secure_init(PartSetup<FFT_Data>&, Player&, MachineBase&, int, int, FHE_Params& params);
template void secure_init(PartSetup<P2Data>&, Player&, MachineBase&, int, int, FHE_Params& params);
template void secure_init(TemiSetup<FFT_Data>&, Player&, MachineBase&, int, int, FHE_Params& params);
template void secure_init(TemiSetup<P2Data>&, Player&, MachineBase&, int, int, FHE_Params& params);