emp-toolkit
halfgate_eva.h
Go to the documentation of this file.
1 #ifndef HALFGATE_EVA_H__
2 #define HALFGATE_EVA_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_eva_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_eva_and(GarbleCircuit* gc, const block&a, const block&b);
21 
22 template<typename T, RTCktOpt rt>
23 block halfgate_eva_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 HalfGateEva:public GarbleCircuit{ public:
31  T * io;
33  bool with_file_io = false;
35  HalfGateEva(T * io) :io(io) {
36  is_public_ptr = &halfgate_eva_is_public<T, rt>;
37  public_label_ptr = &halfgate_eva_public_label<T, rt>;
38  gc_and_ptr = &halfgate_eva_and<T, rt>;
39  gc_xor_ptr = &halfgate_eva_xor<T, rt>;
40  gc_not_ptr = &halfgate_eva_not<T, rt>;
41  }
42  void set_file_io(FileIO * fio) {
43  with_file_io = true;
44  this->fio = fio;
45  }
46  bool is_public_impl(const block & b, int party) {
47  return isZero(&b) or isOne(&b);
48  }
50  return b? one_block() : zero_block();
51  }
52  block and_gate(const block& a, const block& b) {
53  block out, table[2];
54  if (isZero(&a) or isOne(&a) or isZero(&b) or isOne(&b)) {
55  return _mm_and_si128(a, b);
56  } else {
57  io->recv_block(table, 2);
58  if(with_file_io) {
59  fio->send_block(table, 2);
60  return prp.H(a, gid++);
61  }
62  garble_gate_eval_halfgates(GARBLE_GATE_AND, a, b, &out, table, gid++, prp.aes);
63  return out;
64  }
65  }
66  block xor_gate(const block&a, const block& b) {
67  if(isOne(&a))
68  return not_gate(b);
69  else if (isOne(&b))
70  return not_gate(a);
71  else
72  return xorBlocks(a, b);
73  }
74  block not_gate(const block&a) {
75  if (isZero(&a))
76  return one_block();
77  else if (isOne(&a))
78  return zero_block();
79  else
80  return a;
81  }
82  void generic_to_xor(block* new_block, const block * old_block, int length) {
83  block h[4], t;
84  for(int i = 0; i < length; ++i) {
85  io->recv_block(h, 4);
86  t = prp.H(old_block[i], 2*i);
87  if(block_cmp(&t, &h[0], 1)) {
88  new_block[i] = xorBlocks(h[1], prp.H(old_block[i], 2*i+1));
89  } else {
90  new_block[i] = xorBlocks(h[3], prp.H(old_block[i], 2*i+1));
91  }
92  }
93  }
94 };
95 template<typename T>
96 class HalfGateEva<T,RTCktOpt::off>:public GarbleCircuit{ public:
98  T * io;
100  bool with_file_io = false;
102  block constant[2];
103  HalfGateEva(T * io) :io(io) {
104  is_public_ptr = &halfgate_eva_is_public<T, RTCktOpt::off>;
105  public_label_ptr = &halfgate_eva_public_label<T, RTCktOpt::off>;
106  gc_and_ptr = &halfgate_eva_and<T, RTCktOpt::off>;
107  gc_xor_ptr = &halfgate_eva_xor<T, RTCktOpt::off>;
108  gc_not_ptr = &halfgate_eva_not<T, RTCktOpt::off>;
109  PRG prg2(fix_key);prg2.random_block(constant, 2);
110  }
111  void set_file_io(FileIO * fio) {
112  with_file_io = true;
113  this->fio = fio;
114  }
115  bool is_public_impl(const block & b, int party) {
116  return false;
117  }
119  return constant[b];
120 // return b? one_block() : zero_block();
121  }
122  block and_gate(const block& a, const block& b) {
123  block out, table[2];
124  io->recv_block(table, 2);
125  if(with_file_io) {
126  fio->send_block(table, 2);
127  return prp.H(a, gid++);
128  }
129  garble_gate_eval_halfgates(GARBLE_GATE_AND, a, b, &out, table, gid++, prp.aes);
130  return out;
131  }
132  block xor_gate(const block& a, const block& b) {
133  return xorBlocks(a,b);
134  }
135  block not_gate(const block&a){
136  return xor_gate(a, public_label_impl(true));
137  }
138  void generic_to_xor(block* new_block, const block * old_block, int length) {
139  block h[4], t;
140  for(int i = 0; i < length; ++i) {
141  io->recv_block(h, 4);
142  t = prp.H(old_block[i], 2*i);
143  if(block_cmp(&t, &h[0], 1)) {
144  new_block[i] = xorBlocks(h[1], prp.H(old_block[i], 2*i+1));
145  } else {
146  new_block[i] = xorBlocks(h[3], prp.H(old_block[i], 2*i+1));
147  }
148  }
149  }
150 };
151 
152 template<typename T, RTCktOpt rt>
153 bool halfgate_eva_is_public(GarbleCircuit* gc, const block & b, int party) {
154  return ((HalfGateEva<T,rt>*)gc)->is_public_impl(b, party);
155 }
156 template<typename T, RTCktOpt rt>
158  return ((HalfGateEva<T,rt>*)gc)->public_label_impl(b);
159 }
160 template<typename T, RTCktOpt rt>
162  return ((HalfGateEva<T,rt>*)gc)->and_gate(a, b);
163 }
164 template<typename T, RTCktOpt rt>
166  return ((HalfGateEva<T,rt>*)gc)->xor_gate(a, b);
167 }
168 template<typename T, RTCktOpt rt>
170  return ((HalfGateEva<T,rt>*)gc)->not_gate(a);
171 }
172 
173 #endif// HALFGATE_EVA_H__
void generic_to_xor(block *new_block, const block *old_block, int length)
Definition: halfgate_eva.h:138
HalfGateEva(T *io)
Definition: halfgate_eva.h:103
FileIO * fio
Definition: halfgate_eva.h:34
PRP prp
Definition: halfgate_eva.h:97
block and_gate(const block &a, const block &b)
Definition: halfgate_eva.h:122
__m128i block
Definition: block.h:8
bool isZero(const block *b)
Definition: block.h:71
T * io
Definition: halfgate_eva.h:31
block halfgate_eva_not(GarbleCircuit *gc, const block &a)
Definition: halfgate_eva.h:169
uint64_t gid
Definition: garble_circuit.h:8
block xorBlocks(block x, block y)
Definition: block.h:35
block halfgate_eva_xor(GarbleCircuit *gc, const block &a, const block &b)
Definition: halfgate_eva.h:165
Hash hash
Definition: halfgate_eva.h:32
bool isOne(const block *b)
Definition: block.h:75
block xor_gate(const block &a, const block &b)
Definition: halfgate_eva.h:132
block(* gc_and_ptr)(GarbleCircuit *gc, const block &a, const block &b)
Definition: garble_circuit.h:11
#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
PRP prp
Definition: halfgate_eva.h:30
HalfGateEva(T *io)
Definition: halfgate_eva.h:35
block public_label_impl(bool b)
Definition: halfgate_eva.h:118
Definition: garble.h:28
void random_block(block *data, int nblocks=1)
Definition: prg.h:75
const char fix_key[]
Definition: block.h:130
void set_file_io(FileIO *fio)
Definition: halfgate_eva.h:42
Hash hash
Definition: halfgate_eva.h:99
Definition: garble_circuit.h:6
block H(block in, uint64_t id)
Definition: prp.h:48
T * io
Definition: halfgate_eva.h:98
block public_label_impl(bool b)
Definition: halfgate_eva.h:49
Definition: prp.h:11
bool is_public_impl(const block &b, int party)
Definition: halfgate_eva.h:46
block and_gate(const block &a, const block &b)
Definition: halfgate_eva.h:52
block halfgate_eva_public_label(GarbleCircuit *gc, bool b)
Definition: halfgate_eva.h:157
block halfgate_eva_and(GarbleCircuit *gc, const block &a, const block &b)
Definition: halfgate_eva.h:161
AES_KEY * aes
Definition: prp.h:12
block(* gc_not_ptr)(GarbleCircuit *gc, const block &a)
Definition: garble_circuit.h:13
block(* gc_xor_ptr)(GarbleCircuit *gc, const block &a, const block &b)
Definition: garble_circuit.h:12
void set_file_io(FileIO *fio)
Definition: halfgate_eva.h:111
block not_gate(const block &a)
Definition: halfgate_eva.h:135
Definition: prg.h:16
bool block_cmp(const block *x, const block *y, int nblocks)
Definition: block.h:56
Definition: hash.h:12
Definition: halfgate_eva.h:29
bool with_file_io
Definition: halfgate_eva.h:33
#define one_block()
Definition: block.h:67
Definition: garble_circuit.h:7
void send_block(const block *data, int nblock)
Definition: io_channel.h:132
block xor_gate(const block &a, const block &b)
Definition: halfgate_eva.h:66
bool(* is_public_ptr)(GarbleCircuit *gc, const block &b, int party)
Definition: garble_circuit.h:9
block not_gate(const block &a)
Definition: halfgate_eva.h:74
int party
Definition: input-check-malicious.cpp:12
bool is_public_impl(const block &b, int party)
Definition: halfgate_eva.h:115
bool halfgate_eva_is_public(GarbleCircuit *gc, const block &b, int party)
Definition: halfgate_eva.h:153
void generic_to_xor(block *new_block, const block *old_block, int length)
Definition: halfgate_eva.h:82
Definition: file_io_channel.h:21
FileIO * fio
Definition: halfgate_eva.h:101