emp-toolkit
hcot.h
Go to the documentation of this file.
1 #ifndef HCOT_H__
2 #define HCOT_H__
3 #include <2pctool/2pctool.h>
4 
5 class HcOT {public:
6  bn_t y, alpha0, alpha1;
7  eb_t g[2][EB_TABLE_MAX];
8  eb_t h[2][EB_TABLE_MAX];
9  bn_t * r = nullptr;
11  bool sender;
13  bn_t q;
14  int K;
15  PRG ** prgs = nullptr;
16  int length;
17  eb_tpl *Y = nullptr;
18  HcOT(bool is_sender, NetIO * _io, int _k, int l, block * seed=nullptr)
19  : io(_io), sender(is_sender), K(_k), length(l) {
21  eb_curve_get_ord(q);
22  if (is_sender) {
23  prgs = new PRG*[K];
24  for(int i = 0; i < K; ++i) {
25  prgs[i] = new PRG(&seed[i]);
26  }
27  io->recv_eb((eb_t*)g, 2*EB_TABLE_MAX);
28  io->recv_eb((eb_t*)h, 2*EB_TABLE_MAX);
29  } else {
30  eb_t tmp[2];
31  for(int i = 0; i < 2; ++i) {
32  prg.random_eb(tmp, 2);
33  eb_mul_pre(g[i], tmp[0]);
34  eb_mul_pre(h[i], tmp[1]);
35  }
36  io->send_eb((eb_t*)g, 2*EB_TABLE_MAX);
37  io->send_eb((eb_t*)h, 2*EB_TABLE_MAX);
38  r = new bn_t[length];
39  prg.random_bn(r, length);
40  }
41  }
42  ~HcOT() {
43  if(prgs!=nullptr) {
44  for(int i = 0; i < K; ++i)
45  delete prgs[i];
46  delete[] prgs;
47  }
48  if(r!=nullptr) delete[] r;
49  if(Y!=nullptr) delete[] Y;
50  }
51 
52  void send(eb_tpl *L ) {
53  eb_tpl *X = new eb_tpl[K*length];
54  bn_tpl *r = new bn_tpl[K*length];
55  bn_tpl *s = new bn_tpl[K*length];
56  eb_t (*glT)[EB_TABLE_MAX] = new eb_t[length][EB_TABLE_MAX];
57  eb_t (*hlT)[EB_TABLE_MAX] = new eb_t[length][EB_TABLE_MAX];
58  eb_t tmp, tmp2;
59  for(int i = 0; i < length; ++i) {
60  io->recv_eb(&tmp, 1);
61  eb_mul_pre(glT[i], tmp);
62  io->recv_eb(&tmp, 1);
63  eb_mul_pre(hlT[i], tmp);
64  }
65 
66  for(int i = 0; i < length; ++i) {
67  for(int j = 0; j < K; ++j) {
68  prgs[j]->random_bn(r[i*K+j], 2);
69  prgs[j]->random_bn(s[i*K+j], 2);
70  for(int ind = 0; ind < 2; ++ind){
71  eb_mul_fix_norm(X[i*K+j][ind], g[ind], r[i*K+j][ind]);
72  eb_mul_fix_norm(tmp, h[ind], s[i*K+j][ind]);
73  eb_add_norm(X[i*K+j][ind], X[i*K+j][ind], tmp);
74  }
75  io->send_eb(X[i*K+j], 2);
76  }
77  }
78 
79  for(int i = 0; i < length; ++i) {
80  for(int j = 0; j < K; ++j) {
81  for(int ind = 0; ind < 2; ++ind) {
82  eb_mul_fix_norm(tmp, glT[i], r[i*K+j][ind]);
83  eb_mul_fix_norm(tmp2, hlT[i], s[i*K+j][ind]);
84  eb_add_norm(tmp, tmp2, tmp);
85  eb_add_norm(tmp, tmp, L[i*K+j][ind]);
86  io->send_eb(&tmp, 1);
87  }
88  }
89  }
90  delete[] X;
91  delete[] r;
92  delete[] s;
93  delete glT;
94  delete hlT;
95  }
96 
97 
98  void recv(eb_t * L, bool * b) {
99  eb_t g_loc, h_loc;
100  Y = new eb_tpl[K*length];
101  for(int i = 0; i < length; ++i) {
102  int ind = b[i] ? 1: 0;
103  eb_mul_fix_norm(g_loc, g[ind], r[i]);
104  io->send_eb(&g_loc, 1);
105  eb_mul_fix_norm(h_loc, h[ind], r[i]);
106  io->send_eb(&h_loc, 1);
107  }
108 
109  eb_tpl *X = new eb_tpl[K*length];
110  for(int i = 0; i < length; ++i) {
111  for(int j = 0; j < K; ++j) {
112  io->recv_eb(X[i*K+j], 2);
113  int ind = b[i] ? 1: 0;
114  eb_mul_norm(X[i*K+j][ind], X[i*K+j][ind], r[i]);
115  }
116  }
117 
118  for(int i = 0; i < length; ++i) {
119  for(int j = 0; j < K; ++j) {
120  io->recv_eb(Y[i*K+j], 2);
121  int ind = b[i] ? 1: 0;
122  eb_sub_norm(L[i*K+j], Y[i*K+j][ind], X[i*K+j][ind]);
123  }
124  }
125  delete[] X;
126  }
127 
128  void open(eb_tpl* Xs, const eb_t* old_X, block seed, bool *b, int index) {
129  PRG seed_prg(&seed);
130  bn_tpl s, t;
131  for(int i = 0; i < length; ++i) {
132  seed_prg.random_bn(s, 2);
133  seed_prg.random_bn(t, 2);
134  int ind = b[i] ? 1 : 0;
135  eb_copy(Xs[i*K+index][ind], old_X[i*K+index]);
136  eb_t tmp;
137  bn_mul_mod(s[1-ind], s[1-ind], r[i], q);
138  eb_mul_fix_norm(tmp, g[ind], s[1-ind]);
139  bn_mul_mod(t[1-ind], t[1-ind], r[i], q);
140  eb_mul_fix_norm(Xs[i*K+index][1-ind], h[ind], t[1-ind]);
141  eb_add_norm(Xs[i*K+index][1-ind], Xs[i*K+index][1-ind], tmp);
142  eb_sub_norm(Xs[i*K+index][1-ind], Y[i*K+index][1-ind], Xs[i*K+index][1-ind]);
143  }
144  }
145 };
146 
147 #endif
int K
Definition: hcot.h:14
eb_t g[2][EB_TABLE_MAX]
Definition: hcot.h:7
PRG prg
Definition: hcot.h:12
NetIO * io
Definition: hcot.h:10
__m128i block
Definition: block.h:8
bn_t alpha1
Definition: hcot.h:6
HcOT(bool is_sender, NetIO *_io, int _k, int l, block *seed=nullptr)
Definition: hcot.h:18
Definition: hcot.h:5
~HcOT()
Definition: hcot.h:42
bn_t * r
Definition: hcot.h:9
eb_tpl * Y
Definition: hcot.h:17
Definition: net_io_channel.h:22
void send(eb_tpl *L)
Definition: hcot.h:52
bn_t q
Definition: hcot.h:13
bn_t y
Definition: hcot.h:6
void recv_eb(eb_t *eb, size_t num)
Definition: io_channel.h:150
eb_t eb_tpl[2]
Definition: utils_ec.h:14
void send_eb(const eb_t *eb, size_t num)
Definition: io_channel.h:140
bn_t bn_tpl[2]
Definition: utils_ec.h:15
int length
Definition: hcot.h:16
bn_t alpha0
Definition: hcot.h:6
void open(eb_tpl *Xs, const eb_t *old_X, block seed, bool *b, int index)
Definition: hcot.h:128
PRG ** prgs
Definition: hcot.h:15
void initialize_relic()
Definition: utils_ec.hpp:8
Definition: prg.h:16
void random_bn(T t, L... l)
Definition: prg.h:87
void random_eb(T t, L... l)
Definition: prg.h:113
eb_t h[2][EB_TABLE_MAX]
Definition: hcot.h:8
void recv(eb_t *L, bool *b)
Definition: hcot.h:98
bool sender
Definition: hcot.h:11