This repository has been archived by the owner on May 30, 2023. It is now read-only.
forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proof.cpp
186 lines (170 loc) · 4.17 KB
/
Proof.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
/*
* Proof.cpp
*
*/
#include "Proof.h"
#include "FHE/P2Data.h"
#include "FHEOffline/EncCommit.h"
#include "Math/Z2k.hpp"
double Proof::dist = 0;
bigint Proof::slack(int slack, int sec, int phim)
{
switch (slack)
{
case NONINTERACTIVE_SPDZ1_SLACK:
cout << "Computing slack for non-interactive SPDZ1 proof" << endl;
return NonInteractiveProof::slack(sec, phim);
case INTERACTIVE_SPDZ1_SLACK:
cout << "Computing slack for interactive SPDZ1 proof" << endl;
return InteractiveProof::slack(sec, phim);
case COVERT_SPDZ2_SLACK:
cout << "No slack for covert SPDZ2 proof" << endl;
return 0;
case ACTIVE_SPDZ2_SLACK:
cout << "Computing slack for active SPDZ2 proof" << endl;
return EncCommit_<FFT_Data>::active_slack(phim);
default:
if (slack < 0)
throw runtime_error("slack type unknown");
return bigint(1) << slack;
}
}
void Proof::set_challenge(const octetStream& ciphertexts)
{
octetStream hash = ciphertexts.hash();
PRNG G;
assert(hash.get_length() >= SEED_SIZE);
G.SetSeed(hash.get_data());
set_challenge(G);
}
void Proof::set_challenge(PRNG& G)
{
unsigned int i;
if (top_gear)
{
W.resize(V, vector<int>(U));
for (i = 0; i < V; i++)
for (unsigned j = 0; j < U; j++)
W[i][j] = G.get_uint(2 * phim) - 1;
}
else
{
e.resize(sec);
for (i = 0; i < sec; i++)
{
e[i] = G.get_bit();
}
}
}
void Proof::generate_challenge(const Player& P)
{
GlobalPRNG G(P);
set_challenge(G);
}
template<class T>
class AbsoluteBoundChecker
{
T bound, neg_bound;
public:
AbsoluteBoundChecker(T bound) : bound(bound), neg_bound(-this->bound) {}
bool outside(const T& value, double& dist)
{
(void)dist;
#ifdef PRINT_MIN_DIST
dist = max(dist, abs(value.get_d()) / bound.get_d());
#endif
return value > bound || value < neg_bound;
}
};
bool Proof::check_bounds(T& z, X& t, int i) const
{
(void)i;
unsigned int j,k;
// Check Bound 1 and Bound 2
AbsoluteBoundChecker<bound_type> plain_checker(plain_check * n_proofs);
AbsoluteBoundChecker<typename Int_Random_Coins::rand_type> rand_checker(
rand_check * n_proofs);
for (j=0; j<phim; j++)
{
auto& te = z[j];
if (plain_checker.outside(te, dist))
{
#ifdef VERBOSE
cout << "Fail on Check 1 " << i << " " << j << endl;
cout << te << " " << plain_check << endl;
cout << tau << " " << sec << " " << n_proofs << endl;
#endif
return false;
}
}
for (k=0; k<3; k++)
{
auto& coeffs = t[k];
for (j=0; j<coeffs.size(); j++)
{
auto& te = coeffs.at(j);
if (rand_checker.outside(te, dist))
{
#ifdef VERBOSE
cout << "Fail on Check 2 " << k << " : " << i << " " << j << endl;
cout << te << " " << rand_check << endl;
cout << rho << " " << sec << " " << n_proofs << endl;
#endif
return false;
}
}
}
return true;
}
Proof::Preimages::Preimages(int size, const FHE_PK& pk, const bigint& p, int n_players) :
r(size, pk.get_params())
{
m.resize(size, pk.get_params().phi_m());
// extra limb for addition
bigint limit = p << (64 + n_players);
m.allocate_slots(limit);
r.allocate_slots(n_players);
m_tmp = m[0][0];
r_tmp = r[0][0];
}
void Proof::Preimages::add(octetStream& os)
{
check_sizes();
unsigned int size;
os.get(size);
if (size != m.size())
throw length_error("unexpected size received");
for (size_t i = 0; i < m.size(); i++)
{
m[i].add(os, m_tmp);
r[i].add(os, r_tmp);
}
}
void Proof::Preimages::pack(octetStream& os)
{
check_sizes();
os.store((unsigned int)m.size());
for (size_t i = 0; i < m.size(); i++)
{
m[i].pack(os);
r[i].pack(os);
}
}
void Proof::Preimages::unpack(octetStream& os)
{
unsigned int size;
os.get(size);
m.resize(size);
assert(not r.empty());
r.resize(size, r[0]);
for (size_t i = 0; i < m.size(); i++)
{
m[i].unpack(os);
r[i].unpack(os);
}
}
void Proof::Preimages::check_sizes()
{
if (m.size() != r.size())
throw runtime_error("preimage sizes don't match");
}