forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Secret.h
193 lines (147 loc) · 5.34 KB
/
Secret.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
/*
* EvalSecret.h
*
*/
#ifndef GC_SECRET_H_
#define GC_SECRET_H_
#include "GC/config.h"
#include "BMR/config.h"
#include "BMR/common.h"
#include "GC/Clear.h"
#include "GC/Memory.h"
#include "GC/Access.h"
#include "GC/ArgTuples.h"
#include "Math/gf2nlong.h"
#include "Processor/DummyProtocol.h"
#include "Processor/Instruction.h"
#include "Tools/FixedVector.h"
#include <fstream>
class ProcessorBase;
namespace GC
{
template<class T>
class Secret;
template <class T>
inline void XOR(T& res, const T& left, const T& right)
{
res.XOR(left, right);
}
template<class T> class Processor;
template<class T> class Machine;
template <class T>
class Secret
{
#ifdef FIXED_REGISTERS
typedef FixedVector<T, FIXED_REGISTERS> RegVector;
#else
typedef CheckVector<T> RegVector;
#endif
RegVector registers;
T& get_new_reg();
public:
typedef T part_type;
typedef typename T::DynamicMemory DynamicMemory;
typedef NoShare bit_type;
typedef typename T::Input Input;
typedef typename T::out_type out_type;
static string type_string() { return "evaluation secret"; }
static string phase_name() { return T::name(); }
static int default_length;
static const bool needs_ot = false;
static const bool is_real = true;
static const bool actual_inputs = T::actual_inputs;
static int threshold(int nplayers) { return T::threshold(nplayers); }
static Secret<T> input(party_id_t from, const int128& input, int n_bits = -1);
static Secret<T> input(Processor<Secret<T>>& processor, const InputArgs& args);
void random(int n_bits, int128 share);
void random_bit();
template <class U>
static void store_clear_in_dynamic(U& mem, const vector<ClearWriteAccess>& accesses)
{ T::store_clear_in_dynamic(mem, accesses); }
template<class U, class V>
static void load(vector< ReadAccess<V> >& accesses, const U& mem);
template<class U, class V>
static void store(U& mem, vector< WriteAccess<V> >& accesses);
template<class U>
static void andrs(Processor<U>& processor, const vector<int>& args)
{ T::andrs(processor, args); }
template<class U>
static void ands(Processor<U>& processor, const vector<int>& args)
{ T::ands(processor, args); }
template<class U>
static void andrsvec(Processor<U>& processor, const vector<int>& args)
{ T::andrsvec(processor, args); }
template<class U>
static void xors(Processor<U>& processor, const vector<int>& args)
{ T::xors(processor, args); }
template<class U>
static void inputb(Processor<U>& processor, const vector<int>& args)
{ T::inputb(processor, args); }
template<class U>
static void inputb(Processor<U>& processor, ProcessorBase& input_proc,
const vector<int>& args)
{ T::inputb(processor, input_proc, args); }
template<class U>
static void inputbvec(Processor<U>& processor, ProcessorBase& input_proc,
const vector<int>& args)
{ T::inputbvec(processor, input_proc, args); }
template<class U>
static void reveal_inst(Processor<U>& processor, const vector<int>& args)
{ T::reveal_inst(processor, args); }
template<class U>
static void trans(Processor<U>& processor, int n_inputs, const vector<int>& args);
template<class U>
static void convcbit(Integer& dest, const Clear& source,
Processor<U>& proc)
{ T::convcbit(dest, source, proc); }
template<class U>
static void convcbit2s(Processor<U>& processor, const BaseInstruction& instruction)
{ T::convcbit2s(processor, instruction); }
template<class U>
static void andm(Processor<U>& processor, const BaseInstruction& instruction)
{ T::andm(processor, instruction); }
Secret();
Secret(const Integer& x) { *this = x; }
void load_clear(int n, const Integer& x);
void operator=(const Integer& x) { load_clear(default_length, x); }
Secret<T> operator<<(int i) const;
Secret<T> operator>>(int i) const;
template<class U>
void bitcom(Memory<U>& S, const vector<int>& regs);
template<class U>
void bitdec(Memory<U>& S, const vector<int>& regs) const;
Secret<T> operator+(const Secret<T>& x) const;
Secret<T>& operator+=(const Secret<T>& x) { *this = *this + x; return *this; }
void xor_(int n, const Secret<T>& x, const Secret<T>& y)
{
resize_regs(n);
for (int i = 0; i < n; i++)
XOR<T>(registers[i], x.get_reg(i), y.get_reg(i));
}
void invert(int n, const Secret<T>& x);
void and_(int n, const Secret<T>& x, const Secret<T>& y, bool repeat);
template <class U>
void reveal(size_t n_bits, U& x);
template <class U>
void my_input(U& inputter, BitVec value, int n_bits);
template <class U>
void other_input(U& inputter, int from, int n_bits);
template <class U>
void finalize_input(U& inputter, int from, int n_bits);
int size() const { return registers.size(); }
RegVector& get_regs() { return registers; }
const RegVector& get_regs() const { return registers; }
const T& get_reg(int i) const { return registers.at(i); }
T& get_reg(int i) { return registers.at(i); }
void resize_regs(size_t n);
};
template <class T>
int Secret<T>::default_length = 64;
template <class T>
inline ostream& operator<<(ostream& o, Secret<T>& secret)
{
o << "(" << secret.size() << " secret bits)";
return o;
}
} /* namespace GC */
#endif /* GC_SECRET_H_ */