ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
circuit.h
Go to the documentation of this file.
1 
20 #ifndef CIRCUIT_H_
21 #define CIRCUIT_H_
22 
23 #include "abycircuit.h"
24 
25 class share;
26 class boolshare;
27 class arithshare;
28 
30 class Circuit {
31 
32 public:
34  Circuit(ABYCircuit* aby, e_sharing context, e_role myrole, uint32_t bitlen, e_circuit circ) :
35  m_cCircuit(aby), m_eContext(context), m_eMyRole(myrole), m_nShareBitLen(bitlen), m_eCirctype(circ) {
36  Init();
37  }
38  ;
40  virtual ~Circuit() {
41  }
42  ;
43 
48  void Init();
49 
51  void Cleanup();
52 
54  void Reset();
55 
56  /* organizational routines */
57 
62  uint32_t GetShareBitLen() {
63  return m_nShareBitLen;
64  }
65  ;
66 
70  uint32_t GetMaxDepth() {
71  return m_nMaxDepth;
72  }
73  ;
79  deque<uint32_t> GetLocalQueueOnLvl(uint32_t lvl) {
80 
81  if (lvl < m_vLocalQueueOnLvl.size())
82  return m_vLocalQueueOnLvl[lvl];
83  else
84  return EMPTYQUEUE;
85  }
86  ;
87 
93  deque<uint32_t> GetInteractiveQueueOnLvl(uint32_t lvl) {
94  if (lvl < m_vInteractiveQueueOnLvl.size())
95  return m_vInteractiveQueueOnLvl[lvl];
96  else
97  return EMPTYQUEUE;
98  }
99  ;
100 
105  uint32_t GetNumLocalLayers() {
106  return m_vLocalQueueOnLvl.size();
107  }
108 
114  return m_vInteractiveQueueOnLvl.size();
115  }
116 
122  uint32_t GetNumInputBitsForParty(e_role party) {
123  return m_vInputBits[party];
124  }
125  ;
132  return m_vOutputBits[party];
133  }
134  ;
140  deque<uint32_t> GetInputGatesForParty(e_role party) {
141  return m_vInputGates[party];
142  }
143  ;
149  deque<uint32_t> GetOutputGatesForParty(e_role party) {
150  return m_vOutputGates[party];
151  }
152  ;
153 
154  e_sharing GetContext() {
155  return m_eContext;
156  }
157  ;
158  uint32_t GetNumGates() {
159  return m_nGates;
160  }
161  ;
162 
163  UGATE_T* GetOutputGateValue(uint32_t gateid);
164  uint32_t GetOutputGateValue(uint32_t gateid, UGATE_T*& outval);
165  template<class T> void GetOutputGateValue(uint32_t gateid, T& val);
166  uint32_t GetNumVals(uint32_t gateid) {
167  assert(gateid < m_cCircuit->GetGateHead());
168  return m_pGates[gateid].nvals;
169  }
170  ;
171 
172  /* Common gate-building routines */
173  virtual share* PutCONSGate(uint32_t nvals, UGATE_T val, uint32_t bitlen) = 0;
174  virtual share* PutCONSGate(uint32_t nvals, uint32_t* val, uint32_t bitlen) = 0;
175  virtual share* PutCONSGate(uint32_t nvals, uint8_t* val, uint32_t bitlen) = 0;
176  virtual uint32_t PutConstantGate(UGATE_T val, uint32_t nvals = 1) = 0;
177 
178  //virtual int PutINGate(int nvals, ROLE src) = 0;
179  virtual share* PutINGate(uint32_t nvals, uint64_t val, uint32_t bitlen, e_role role) = 0;
180  virtual share* PutINGate(uint32_t nvals, uint32_t* val, uint32_t bitlen, e_role role) = 0;
181  virtual share* PutINGate(uint32_t nvals, uint8_t* val, uint32_t bitlen, e_role role) = 0;
182 
183  virtual share* PutADDGate(share* ina, share* inb) = 0;
184  virtual share* PutSUBGate(share* ina, share* inb) = 0;
185  virtual share* PutANDGate(share* ina, share* inb) = 0;
186  virtual share* PutXORGate(share* ina, share* inb) = 0;
187  virtual share* PutMULGate(share* ina, share* inb) = 0;
188  virtual share* PutGEGate(share* ina, share* inb) = 0;
189  virtual share* PutEQGate(share* ina, share* inb) = 0;
190  virtual share* PutMUXGate(share* ina, share* inb, share* sel) = 0;
191  virtual share* PutY2BGate(share* ina) = 0;
192  virtual share* PutB2AGate(share* ina) = 0;
193  virtual share* PutB2YGate(share* ina) = 0;
194  virtual share* PutA2YGate(share* ina) = 0;
195  virtual share* PutANDVecGate(share* ina, share* inb) = 0;
196  virtual share* PutCallbackGate(share* in, uint32_t rounds, void (*callback)(GATE*, void*), void* infos, uint32_t nvals) = 0;
197  share* PutCombinerGate(share* ina);
198  share* PutSplitterGate(share* ina);
199  share* PutRepeaterGate(uint32_t nvals, share* ina);
200 
201  //Templates may not be virtual, hence use dummy functions
202  template<class T> uint32_t PutINGate(uint32_t nvals, T val) {
203  cout << "IN gate not implemented in super-class, stopping!" << endl;
204  return -1;
205  }
206  ;
207  template<class T> uint32_t PutINGate(uint32_t nvals, T val, e_role role) {
208  cout << "IN gate not implemented in super-class, stopping!" << endl;
209  return -1;
210  }
211  ;
212  //virtual int PutOUTGate(int parent, ROLE dst) = 0;
213  virtual share* PutOUTGate(share* parent, e_role dst) =0;
214  // TODO FIXME PutOUTGate seems to work only for role ALL. SERVER causes the client to segfault at src/abycore/circuit/circuit.cpp:71: UGATE_T* Circuit::GetOutputGateValue(uint32_t): Assertion `m_pGates[gateid].instantiated' failed.
215 
216 
217  virtual uint32_t PutINVGate(uint32_t parentid) = 0;
218  e_circuit GetCircuitType() {
219  return m_eCirctype;
220  }
221  ;
222 
223 protected:
224  virtual void UpdateInteractiveQueue(uint32_t gateid) = 0;
225  virtual void UpdateLocalQueue(uint32_t gateid) = 0;
226 
227  void UpdateInteractiveQueue(share* gateid);
228  void UpdateLocalQueue(share* gateid);
229 
230  ABYCircuit* m_cCircuit;
233  e_circuit m_eCirctype;
234  e_role m_eMyRole;
235  uint32_t m_nShareBitLen;
236  uint32_t m_nMaxDepth;
237 
238  vector<deque<uint32_t> > m_vLocalQueueOnLvl; //for locally evaluatable gates, first dimension is the level of the gates, second dimension presents the queue on which the gateids are put
239  vector<deque<uint32_t> > m_vInteractiveQueueOnLvl; //for gates that need interaction, first dimension is the level of the gates, second dimension presents the queue on which the gateids are put
240  vector<deque<uint32_t> > m_vInputGates; //input gates for the parties
241  vector<deque<uint32_t> > m_vOutputGates; //input gates for the parties
242  vector<uint32_t> m_vInputBits; //number of input bits for the parties
243  vector<uint32_t> m_vOutputBits; //number of output bits for the parties
244 
245  uint32_t m_nMULs; //number of AND gates in the circuit
246  uint32_t m_nCONVGates; //number of Boolean to arithmetic conversion gates
247 
248  uint32_t m_nGates;
249  uint32_t m_nRoundsAND;
250  uint32_t m_nRoundsXOR;
251  vector<uint32_t> m_nRoundsIN;
252  vector<uint32_t> m_nRoundsOUT;
253 
254  const deque<uint32_t> EMPTYQUEUE;
255 };
256 
258 class share {
259 public:
261  share(uint32_t sharelen, Circuit* circ);
263  share(vector<uint32_t> gates, Circuit* circ);
269  void init(Circuit* circ, uint32_t maxbitlen = 32);
270 
272  ~share() {
273  }
274  ;
275 
276  vector<uint32_t>& get_gates() {
277  return m_ngateids;
278  }
279  ;
280  uint32_t get_gate(uint32_t shareid);
281  void set_gate(uint32_t shareid, uint32_t gateid);
282  void resize(uint32_t sharelen) {
283  m_ngateids.resize(sharelen);
284  }
285  ;
286  void set_gates(vector<uint32_t> shares) {
287  m_ngateids = shares;
288  }
289  ;
290  uint32_t size() {
291  return m_ngateids.size();
292  }
293  ;
294  uint32_t max_size() {
295  return m_nmaxbitlen;
296  }
297  ;
298  uint32_t set_max_size(uint32_t maxsize) {
299  assert(maxsize >= m_ngateids.size());
300  m_nmaxbitlen = maxsize;
301  }
302  ;
303  e_circuit get_circuit_type() {
304  return m_ccirc->GetCircuitType();
305  }
306  ;
307  e_sharing get_share_type() {
308  return m_ccirc->GetContext();
309  }
310  ;
311 
312  template<class T> T get_clear_value() {
313  assert(sizeof(T) * 8 >= m_ngateids.size());
314  T val = 0;
315  for (uint32_t i = 0; i < m_ngateids.size(); i++) {
316  val += (*m_ccirc->GetOutputGateValue(m_ngateids[i]) << i);
317  }
318 
319  return val;
320  }
321 
322  virtual uint8_t* get_clear_value() = 0;
323  virtual void get_clear_value_vec(uint32_t** vec, uint32_t *bitlen, uint32_t *nvals) = 0;
324 
325 protected:
326  vector<uint32_t> m_ngateids;
327  Circuit* m_ccirc;
328  uint32_t m_nmaxbitlen;
329 };
330 
332 class boolshare: public share {
333 public:
335  boolshare(uint32_t sharelen, Circuit* circ) :
336  share(sharelen, circ) {
337  }
338  ;
340  boolshare(vector<uint32_t> gates, Circuit* circ) :
341  share(gates, circ) {
342  }
343  ;
352  }
353  ; // : share() {};
354 
355  uint8_t* get_clear_value();
356  void get_clear_value_vec(uint32_t** vec, uint32_t *bitlen, uint32_t *nvals);
357 };
358 
360 class arithshare: public share {
361 public:
364  share(1, circ) {
365  }
366  ;
368  arithshare(vector<uint32_t> gates, Circuit* circ) :
369  share(gates, circ) {
370  }
371  ;
372 
375  }
376  ; // : share() {};
377 
378  uint8_t* get_clear_value();
379  void get_clear_value_vec(uint32_t** vec, uint32_t* bitlen, uint32_t* nvals);
380 
381 };
382 
383 static share* create_new_share(uint32_t size, Circuit* circ, e_circuit circtype);
384 static share* create_new_share(vector<uint32_t> vals, Circuit* circ, e_circuit circtype);
385 
386 #endif /* CIRCUIT_H_ */
void Cleanup()
Definition: circuit.cpp:35
~arithshare()
Definition: circuit.h:374
arithshare(vector< uint32_t > gates, Circuit *circ)
Definition: circuit.h:368
deque< uint32_t > GetInteractiveQueueOnLvl(uint32_t lvl)
Definition: circuit.h:93
uint32_t GetMaxDepth()
Definition: circuit.h:70
deque< uint32_t > GetOutputGatesForParty(e_role party)
Definition: circuit.h:149
uint32_t GetShareBitLen()
Definition: circuit.h:62
uint32_t GetNumInputBitsForParty(e_role party)
Definition: circuit.h:122
share(uint32_t sharelen, Circuit *circ)
Definition: circuit.cpp:114
Definition: abycircuit.h:122
Definition: circuit.h:258
e_sharing m_eContext
Definition: circuit.h:232
virtual ~Circuit()
Definition: circuit.h:40
ABYCircuit class.
uint32_t GetNumLocalLayers()
Definition: circuit.h:105
uint32_t GetNumOutputBitsForParty(e_role party)
Definition: circuit.h:131
deque< uint32_t > GetInputGatesForParty(e_role party)
Definition: circuit.h:140
void Reset()
Definition: circuit.cpp:40
Circuit(ABYCircuit *aby, e_sharing context, e_role myrole, uint32_t bitlen, e_circuit circ)
Definition: circuit.h:34
~boolshare()
Definition: circuit.h:351
e_role
Defines the role of the party or the source / target for certain operations (e.g., input/output)
Definition: constants.h:139
Definition: circuit.h:332
void init(Circuit *circ, uint32_t maxbitlen=32)
Definition: circuit.cpp:125
~share()
Definition: circuit.h:272
arithshare(Circuit *circ)
Definition: circuit.h:363
e_circuit
Definition: constants.h:44
Definition: abycircuit.h:144
e_sharing
Enumeration which defines the different sharing which are there in the framework. ...
Definition: constants.h:124
void Init()
Definition: circuit.cpp:21
boolshare(uint32_t sharelen, Circuit *circ)
Definition: circuit.h:335
deque< uint32_t > GetLocalQueueOnLvl(uint32_t lvl)
Definition: circuit.h:79
Definition: circuit.h:30
uint32_t GetNumInteractiveLayers()
Definition: circuit.h:113
Definition: circuit.h:360
GATE * m_pGates
Definition: circuit.h:231
boolshare(vector< uint32_t > gates, Circuit *circ)
Definition: circuit.h:340