ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
abycircuit.h
Go to the documentation of this file.
1 
18 #ifndef __ABYCIRCUIT_H_
19 #define __ABYCIRCUIT_H_
20 
21 #include <math.h>
22 #include "../util/typedefs.h"
23 #include <iostream>
24 #include <limits.h>
25 #include <deque>
26 #include "../util/constants.h"
27 
28 //#define DEBUG_CIRCUIT_CONSTRUCTION
29 
30 //A macro that defines whether a gate requires interaction
31 #define IsInteractive(gatetype, gatecontext) (!((gatecontext == C_ARITH && gatetype == G_ADD) || ((gatecontext == C_BOOL || gatecontext == C_YAO) && gatetype == G_XOR)) || (gatetype == G_MUL))
32 #define ComputeDepth(predecessor) ( (predecessor).depth + (predecessor).nrounds )
33 
34 #define IsSIMDGate(gatetype) (!!((gatetype)&0x80))
35 
36 struct GATE;
37 
38 //TODO redefine outkey as UINT64_T
39 struct yao_fields {
40  //The output wire key
41  BYTE* outKey;
42  //The permutation bit for point-and-permute
43  BYTE* pi;
44 };
45 
46 struct input_fields {
47  e_role src;
48  UGATE_T* inval;
49 };
50 
51 struct output_fields {
52  e_role dst;
53 };
54 
56  uint32_t pos;
57 };
58 
59 struct subset_gate {
60  uint32_t* posids;
61 };
63  uint32_t pos;
64 };
65 
67  uint32_t* posids;
68 };
69 
70 struct callback_gate {
71  void (*callback)(GATE* gate, void* infos);
72  void* infos;
73 };
74 
75 //TODO redefine yval as UINT64_T
76 //TODO store in a specific output field, stored in val right now (which also BOOL values are)
78 
79  //fields of the combiner gate
80  uint32_t* cinput;
81  //fields of the standard gate (pos)
82  splitter_fields sinput;
83  //fields of a yao's garbled circuit gate
84  yao_fields yinput;
85  BYTE* yval;
86  //Arithmetic sharing values, a pointer to a uint16, uint32 or uint64 array with val_size elements
87  UGATE_T* aval;
88  //fields of the evaluated gate
89  UGATE_T* val;
90  //fields for the permutation gate. perm is a vector that first has the id i of the input gate and then the pos p of the input gate for n input gates (i_1,p_1,i_2,p_2,...,i_n,p_n)
91  permutation_gate perm;
92  //fields for the combinepos gate. combinepos first holds the position and then the ids of the input gates it combines
93  //TODO: combine the combine and combinepos gate into one gate
94  combine_at_pos_gate combinepos;
95  //value that is supposed to be shared
96  input_fields ishare;
97  //gate whose value is reconstructed
98  output_fields oshare;
99  //values for the subset gate which combines multiple different positions of one gate into another
100  subset_gate sub_pos;
101  //constant value of a gate
102  UGATE_T constval;
103  //specific field for the conversion type
104  uint32_t pos;
105  //callback routine that handles the evaluation. Functionality is defined by the developer
106  callback_gate cbgate;
107 };
108 typedef union gate_specific gs_t;
109 
110 struct input_gates {
111  union {
112  uint32_t parent;
113  struct {
114  uint32_t left;
115  uint32_t right;
116  } twin;
117  uint32_t* parents;
118  } inputs;
119  uint32_t ningates;
120 };
121 
122 struct GATE {
123  bool instantiated;
124  e_sharing context; // the representation of the value stored in the gate (Public / arithmetic sharing / Boolean sharing / Yao sharing)
125  e_gatetype type; // gate type
126  uint32_t nrounds; // specifies the number of interaction rounds that are required when evaluating this gate
127  uint32_t nused; // number of uses of the gate
128  uint32_t depth; // number of AND gates to the root
129  uint32_t nvals; // the number of values that are stored in this gate
130  gs_t gs; // here the differences for the gates come in
131  uint32_t sharebitlen; // bitlength of the shares in the context
132  input_gates ingates; // the number of input gates together with the values of the input gates
133 };
134 
135 string GetOpName(e_gatetype op);
136 
138  uint32_t bitlen;
139  uint32_t numgates;
140 };
141 
142 uint32_t FindBitLenPositionInVec(uint32_t bitlen, non_lin_vec_ctx* list, uint32_t listentries);
143 
144 class ABYCircuit {
145 public:
146  ABYCircuit(uint32_t maxgates);
147  virtual ~ABYCircuit() {
148  Cleanup();
149  }
150 
151  void Cleanup();
152  void Reset();
153  GATE* Gates() {
154  return m_pGates;
155  }
156  uint32_t PutPrimitiveGate(e_gatetype type, uint32_t inleft, uint32_t inright, uint32_t rounds);
157  uint32_t PutNonLinearVectorGate(e_gatetype type, uint32_t choiceinput, uint32_t vectorinput, uint32_t rounds);
158  uint32_t PutCombinerGate(vector<uint32_t>& input);
159  vector<uint32_t> PutSplitterGate(uint32_t input); //, vector<uint32_t> gatelengths = NULL);
160  uint32_t PutCombineAtPosGate(vector<uint32_t>& input, uint32_t pos);
161  uint32_t PutSubsetGate(uint32_t input, uint32_t* posids, uint32_t nvals);
162  uint32_t PutRepeaterGate(uint32_t input, uint32_t nvals);
163  vector<uint32_t> PutRepeaterGate(vector<uint32_t> input, uint32_t nvals);
164  uint32_t PutPermutationGate();
165 
166  uint32_t PutOUTGate(uint32_t in, e_role dst, uint32_t rounds);
167  vector<uint32_t> PutOUTGate(vector<uint32_t> in, e_role dst, uint32_t rounds);
168 
169  uint32_t PutINGate(e_sharing context, uint32_t nvals, uint32_t sharebitlen, e_role src, uint32_t rounds);
170  uint32_t PutConstantGate(e_sharing context, UGATE_T val, uint32_t nvals, uint32_t sharebitlen);
171  uint32_t PutINVGate(uint32_t in);
172  uint32_t PutCONVGate(vector<uint32_t>& in, uint32_t nrounds, e_sharing dst, uint32_t sharebitlen);
173  uint32_t PutCallbackGate(vector<uint32_t> in, uint32_t rounds, void (*callback)(GATE*, void*), void* infos, uint32_t nvals);
174  uint32_t GetGateHead() {
175  return m_nNextFreeGate;
176  }
177  ;
178  uint32_t GetMaxVectorSize() {
179  return m_nMaxVectorSize;
180  }
181 
182  void FinishCircuitGeneration();
183 
184 private:
185 
186  inline void InitGate(GATE* gate, e_gatetype type);
187  inline void InitGate(GATE* gate, e_gatetype type, uint32_t ina);
188  inline void InitGate(GATE* gate, e_gatetype type, uint32_t ina, uint32_t inb);
189  inline void InitGate(GATE* gate, e_gatetype type, vector<uint32_t>& inputs);
190 
191  inline uint32_t GetNumRounds(e_gatetype type, e_sharing context);
192  inline void MarkGateAsUsed(uint32_t gateid, uint32_t uses = 1);
193 
194  GATE* m_pGates;
195  uint32_t m_nNextFreeGate; // points to the current first unused gate
196  uint32_t m_nSizeOfVal;
197  uint32_t m_nMaxVectorSize; // The maximum vector size in bits, required for correctly instantiating the 0 and 1 gates
198  uint32_t m_nMaxGates; // Maximal number of gates that is allowed
199 
200 };
201 
202 #endif /* __ABYCIRCUIT_H_ */
Definition: abycircuit.h:66
e_gatetype
Enumeration which defines the type of the gate in the circuit.
Definition: constants.h:67
Definition: abycircuit.h:59
Definition: abycircuit.h:55
Definition: abycircuit.h:110
Definition: abycircuit.h:122
Definition: abycircuit.h:70
Definition: abycircuit.h:39
Definition: abycircuit.h:62
e_role
Defines the role of the party or the source / target for certain operations (e.g., input/output)
Definition: constants.h:139
Definition: abycircuit.h:46
Definition: abycircuit.h:51
Definition: abycircuit.h:144
e_sharing
Enumeration which defines the different sharing which are there in the framework. ...
Definition: constants.h:124
Definition: abycircuit.h:77
Definition: abycircuit.h:137