emp-toolkit
halfgate_gen.h
Go to the documentation of this file.
1 #ifndef HALFGATE_GEN_H__
2 #define HALFGATE_GEN_H__
3 #include "io_channel.h"
4 #include "net_io_channel.h"
5 #include "file_io_channel.h"
6 #include "block.h"
7 #include "utils.h"
8 #include "prp.h"
9 #include "hash.h"
11 #include <iostream>
12 
13 template<typename T, RTCktOpt rt>
14 bool halfgate_gen_is_public(GarbleCircuit* gc, const block & b, int party);
15 
16 template<typename T, RTCktOpt rt>
18 
19 template<typename T, RTCktOpt rt>
20 block halfgate_gen_and(GarbleCircuit* gc, const block&a, const block&b);
21 
22 template<typename T, RTCktOpt rt>
23 block halfgate_gen_xor(GarbleCircuit*gc, const block&a, const block&b);
24 
25 template<typename T, RTCktOpt rt>
27 
28 template<typename T, RTCktOpt rt = on>
29 class HalfGateGen:public GarbleCircuit{ public:
33  T * io;
35  bool with_file_io = false;
36  HalfGateGen(T * io) :io(io) {
37  PRG tmp;
38  tmp.random_block(&seed, 1);
39  block a;
40  tmp.random_block(&a, 1);
41  set_delta(a);
42  is_public_ptr = &halfgate_gen_is_public<T, rt>;
43  public_label_ptr = &halfgate_gen_public_label<T, rt>;
44  gc_and_ptr = &halfgate_gen_and<T, rt>;
45  gc_xor_ptr = &halfgate_gen_xor<T, rt>;
46  gc_not_ptr = &halfgate_gen_not<T, rt>;
47  }
48  bool is_public_impl(const block & b, int party) {
49  return isZero(&b) or isOne(&b);
50  }
51  void set_delta(const block &_delta) {
52  this->delta = make_delta(_delta);
53  }
55  return b? one_block() : zero_block();
56  }
57  block gen_and(const block& a, const block& b) {
58  block out[2], table[2];
59  if (isZero(&a) or isZero(&b)) {
60  return zero_block();
61  } else if (isOne(&a)) {
62  return b;
63  } else if (isOne(&b)){
64  return a;
65  } else {
66  garble_gate_garble_halfgates(GARBLE_GATE_AND, a, xorBlocks(a,delta), b, xorBlocks(b,delta),
67  &out[0], &out[1], delta, table, gid++, prp.aes);
68  io->send_block(table, 2);
69  return out[0];
70  }
71  }
72  block gen_xor(const block&a, const block& b) {
73  if(isOne(&a))
74  return gen_not(b);
75  else if (isOne(&b))
76  return gen_not(a);
77  else
78  return xorBlocks(a, b);
79  }
80  block gen_not(const block&a) {
81  if (isZero(&a))
82  return one_block();
83  else if (isOne(&a))
84  return zero_block();
85  else
86  return xorBlocks(a,delta);
87  }
88  void generic_to_xor(const block* new_b0,const block * b0, const block * b1, int length) {
89  block h[4];
90  for(int i = 0; i < length; ++i) {
91  h[0] = prp.H(b0[i], 2*i);
92  h[1] = prp.H(b0[i], 2*i+1);
93  h[2] = prp.H(b1[i], 2*i);
94  h[3] = prp.H(b1[i], 2*i+1);
95 
96  h[1] = xorBlocks(new_b0[i], h[1]);
97  h[3] = xorBlocks(new_b0[i], h[3]);
98  h[3] = xorBlocks(delta, h[3]);
99  io->send_block(h, 4);
100  }
101  }
102 };
103 template<typename T>
104 class HalfGateGen<T,RTCktOpt::off>:public GarbleCircuit{ public:
108  T * io;
110  bool with_file_io = false;
111  block constant[2];
112  HalfGateGen(T * io) :io(io) {
113  PRG tmp;
114  tmp.random_block(&seed, 1);
115  block a;
116  tmp.random_block(&a, 1);
117  is_public_ptr = &halfgate_gen_is_public<T, RTCktOpt::off>;
118  public_label_ptr = &halfgate_gen_public_label<T, RTCktOpt::off>;
119  gc_and_ptr = &halfgate_gen_and<T, RTCktOpt::off>;
120  gc_xor_ptr = &halfgate_gen_xor<T, RTCktOpt::off>;
121  gc_not_ptr = &halfgate_gen_not<T, RTCktOpt::off>;
122  set_delta(a);
123  }
124  bool is_public_impl(const block & b, int party) {
125  return false;
126  }
127  bool isDelta(const block & b) {
128  __m128i neq = _mm_xor_si128(b, delta);
129  return _mm_testz_si128(neq, neq);
130  }
131  void set_delta(const block &_delta) {
132  this->delta = make_delta(_delta);
133  PRG prg2(fix_key);prg2.random_block(constant, 2);
134  constant[1] = xorBlocks(constant[1],delta);
135  }
137  return constant[b];
138  }
139  block gen_and(const block& a, const block& b) {
140  block out[2], table[2];
141  garble_gate_garble_halfgates(GARBLE_GATE_AND, a, xorBlocks(a,delta), b, xorBlocks(b,delta),
142  &out[0], &out[1], delta, table, gid++, prp.aes);
143  io->send_block(table, 2);
144  return out[0];
145  }
146  block gen_xor(const block&a, const block& b) {
147  return xorBlocks(a, b);
148  }
149  block gen_not(const block&a) {
150  return gen_xor(a, public_label_impl(true));
151  }
152  void generic_to_xor(const block* new_b0,const block * b0, const block * b1, int length) {
153  block h[4];
154  for(int i = 0; i < length; ++i) {
155  h[0] = prp.H(b0[i], 2*i);
156  h[1] = prp.H(b0[i], 2*i+1);
157  h[2] = prp.H(b1[i], 2*i);
158  h[3] = prp.H(b1[i], 2*i+1);
159 
160  h[1] = xorBlocks(new_b0[i], h[1]);
161  h[3] = xorBlocks(new_b0[i], h[3]);
162  h[3] = xorBlocks(delta, h[3]);
163  io->send_block(h, 4);
164  }
165  }
166 };
167 
168 template<typename T, RTCktOpt rt>
169 bool halfgate_gen_is_public(GarbleCircuit* gc, const block & b, int party) {
170  return ((HalfGateGen<T, rt>*)gc)->is_public_impl(b, party);
171 }
172 template<typename T, RTCktOpt rt>
174  return ((HalfGateGen<T,rt>*)gc)->public_label_impl(b);
175 }
176 template<typename T, RTCktOpt rt>
178  return ((HalfGateGen<T,rt>*)gc)->gen_and(a, b);
179 }
180 template<typename T, RTCktOpt rt>
182  return ((HalfGateGen<T, rt>*)gc)->gen_xor(a, b);
183 }
184 template<typename T, RTCktOpt rt>
186  return ((HalfGateGen<T, rt>*)gc)->gen_not(a);
187 }
188 
189 #endif// HALFGATE_GEN_H__
static block make_delta(const block &a)
Definition: garble_circuit.h:30
block delta
Definition: halfgate_gen.h:105
bool halfgate_gen_is_public(GarbleCircuit *gc, const block &b, int party)
Definition: halfgate_gen.h:169
HalfGateGen(T *io)
Definition: halfgate_gen.h:36
block halfgate_gen_and(GarbleCircuit *gc, const block &a, const block &b)
Definition: halfgate_gen.h:177
__m128i block
Definition: block.h:8
bool isZero(const block *b)
Definition: block.h:71
T * io
Definition: halfgate_gen.h:108
bool with_file_io
Definition: halfgate_gen.h:35
Hash hash
Definition: halfgate_gen.h:34
uint64_t gid
Definition: garble_circuit.h:8
block xorBlocks(block x, block y)
Definition: block.h:35
block public_label_impl(bool b)
Definition: halfgate_gen.h:54
bool isOne(const block *b)
Definition: block.h:75
block(* gc_and_ptr)(GarbleCircuit *gc, const block &a, const block &b)
Definition: garble_circuit.h:11
HalfGateGen(T *io)
Definition: halfgate_gen.h:112
void set_delta(const block &_delta)
Definition: halfgate_gen.h:131
bool is_public_impl(const block &b, int party)
Definition: halfgate_gen.h:48
#define zero_block()
Definition: block.h:66
block(* public_label_ptr)(GarbleCircuit *gc, bool b)
Definition: garble_circuit.h:10
RTCktOpt
Definition: garble_circuit.h:6
block gen_xor(const block &a, const block &b)
Definition: halfgate_gen.h:72
block halfgate_gen_xor(GarbleCircuit *gc, const block &a, const block &b)
Definition: halfgate_gen.h:181
T * io
Definition: halfgate_gen.h:33
bool isDelta(const block &b)
Definition: halfgate_gen.h:127
void generic_to_xor(const block *new_b0, const block *b0, const block *b1, int length)
Definition: halfgate_gen.h:88
Definition: garble.h:28
void random_block(block *data, int nblocks=1)
Definition: prg.h:75
const char fix_key[]
Definition: block.h:130
Definition: garble_circuit.h:6
block H(block in, uint64_t id)
Definition: prp.h:48
Definition: prp.h:11
AES_KEY * aes
Definition: prp.h:12
block(* gc_not_ptr)(GarbleCircuit *gc, const block &a)
Definition: garble_circuit.h:13
Hash hash
Definition: halfgate_gen.h:109
block gen_not(const block &a)
Definition: halfgate_gen.h:149
block(* gc_xor_ptr)(GarbleCircuit *gc, const block &a, const block &b)
Definition: garble_circuit.h:12
block delta
Definition: halfgate_gen.h:30
block gen_not(const block &a)
Definition: halfgate_gen.h:80
block public_label_impl(bool b)
Definition: halfgate_gen.h:136
Definition: prg.h:16
bool is_public_impl(const block &b, int party)
Definition: halfgate_gen.h:124
Definition: hash.h:12
block halfgate_gen_not(GarbleCircuit *gc, const block &a)
Definition: halfgate_gen.h:185
PRP prp
Definition: halfgate_gen.h:31
void generic_to_xor(const block *new_b0, const block *b0, const block *b1, int length)
Definition: halfgate_gen.h:152
#define one_block()
Definition: block.h:67
block seed
Definition: halfgate_gen.h:107
Definition: garble_circuit.h:7
block gen_and(const block &a, const block &b)
Definition: halfgate_gen.h:139
bool(* is_public_ptr)(GarbleCircuit *gc, const block &b, int party)
Definition: garble_circuit.h:9
block halfgate_gen_public_label(GarbleCircuit *gc, bool b)
Definition: halfgate_gen.h:173
Definition: halfgate_gen.h:29
int party
Definition: input-check-malicious.cpp:12
block gen_and(const block &a, const block &b)
Definition: halfgate_gen.h:57
block gen_xor(const block &a, const block &b)
Definition: halfgate_gen.h:146
block seed
Definition: halfgate_gen.h:32
void set_delta(const block &_delta)
Definition: halfgate_gen.h:51
PRP prp
Definition: halfgate_gen.h:106