forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 272
/
PostSacriBin.cpp
119 lines (101 loc) · 2.68 KB
/
PostSacriBin.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
/*
* Abfllnoww.cpp
*
*/
#include "PostSacriBin.h"
#include "Processor/Processor.h"
#include "Protocols/Replicated.hpp"
#include "Protocols/MaliciousRepMC.hpp"
#include "Protocols/MalRepRingPrep.hpp"
#include "Protocols/ReplicatedPrep.hpp"
#include "ShareSecret.hpp"
namespace GC
{
PostSacriBin::PostSacriBin(Player& P) :
ReplicatedBase(P), honest(P)
{
}
PostSacriBin::~PostSacriBin()
{
if (not inputs.empty())
{
cerr << "unchecked ANDs" << endl;
terminate();
}
}
void PostSacriBin::init_mul()
{
if ((int) inputs.size() >= OnlineOptions::singleton.batch_size)
check();
honest.init_mul();
}
void PostSacriBin::prepare_mul(const T& x, const T& y, int n)
{
if (n == -1)
n = T::default_length;
honest.prepare_mul(x, y, n);
inputs.push_back({{x.mask(n), y.mask(n)}});
}
void PostSacriBin::exchange()
{
honest.exchange();
}
PostSacriBin::T PostSacriBin::finalize_mul(int n)
{
if (n == -1)
n = T::default_length;
auto res = honest.finalize_mul(n);
outputs.push_back({res, n});
return res;
}
void PostSacriBin::check()
{
vector<array<T, 3>> to_check;
assert(inputs.size() == outputs.size());
for (size_t i = 0; i < inputs.size(); i++)
to_check.push_back({{inputs[i][0], inputs[i][1], outputs[i].first}});
GlobalPRNG G(P);
for (size_t i = 0; i < inputs.size(); i++)
to_check.push_back(get_d1_triple(G, outputs[i].second));
HashMaliciousRepMC<T> MC;
vector<array<T, 3>> _(N);
TripleShuffleSacrifice<T>(2, 6).triple_sacrifice(_, to_check, P, MC, 0, inputs.size());
MC.Check(P);
inputs.clear();
outputs.clear();
}
array<PostSacriBin::T, 3> PostSacriBin::get_d1_triple(GlobalPRNG& G, int n_bits)
{
while (d1.size() < N)
d1.push_back(get_d2_triple(T::N_BITS));
int i = G.get_uint(N);
auto tmp = d1.at(i).mask(n_bits);
d1[i] <<= n_bits;
d1[i] ^= get_d2_triple(n_bits);
array<T, 3> res({{tmp[0], tmp[1], tmp[2]}});
return res;
}
array<PostSacriBin::T, 3> PostSacriBin::get_d2_triple(int n_bits)
{
return get_triple_no_count(n_bits);
}
void PostSacriBin::get(Dtype type, T* res)
{
assert(type == DATA_TRIPLE);
if (d2.empty())
{
TripleShuffleSacrifice<T> sacrifice(2, 6);
vector<array<T, 3>> check_triples;
// optimistic triple generation
Replicated<T> protocol(P);
generate_triples(check_triples, 2 * N + 6, &protocol, T::N_BITS);
HashMaliciousRepMC<T> MC;
sacrifice.triple_sacrifice(d2, check_triples, P, MC, 0);
MC.Check(P);
assert(d2.size() == N);
}
for (int i = 0; i < 3; i++)
res[i] = d2.back()[i];
d2.pop_back();
}
} /* namespace GC */