forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 272
/
YaoHalfGate.h
97 lines (86 loc) · 2.41 KB
/
YaoHalfGate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* YaoHalfGate.h
*
*/
#ifndef YAO_YAOHALFGATE_H_
#define YAO_YAOHALFGATE_H_
#include "BMR/Key.h"
#include "YaoGarbleWire.h"
#include "YaoEvalWire.h"
class YaoHalfGate
{
Key TG, TE;
public:
static const int N_EVAL_HASHES = 2;
static void eval_inputs(Key* output, const Key& left, const Key& right,
long T);
static void E_inputs(Key* output, const YaoGarbleWire& left,
const YaoGarbleWire& right, const Key& left_delta,
const Key& right_delta, long T);
static void randomize(YaoGarbleWire&, PRNG&) {}
static Key garble_public_input(bool value, Key delta)
{
return value ? delta : 0;
}
YaoHalfGate() {}
YaoHalfGate(YaoGarbleWire&, const YaoGarbleWire&,
const YaoGarbleWire&, Function);
void and_garble(YaoGarbleWire& out, const Key* hashes,
const YaoGarbleWire& left, const YaoGarbleWire& right, Key delta);
void garble(const YaoGarbleWire&, const Key*, bool,
bool, Function, Key);
void eval(YaoEvalWire&, const YaoEvalWire&,
const YaoEvalWire&);
void eval(YaoEvalWire& out, const Key* hashes, const YaoEvalWire& left,
const YaoEvalWire& right);
};
inline void YaoHalfGate::E_inputs(Key* output, const YaoGarbleWire& left,
const YaoGarbleWire& right, const Key& left_delta, const Key&, long T)
{
auto l = left.full_key().doubling(1);
auto r = right.full_key().doubling(1);
long j = T << 1;
output[0] = l ^ j;
output[1] = output[0] ^ left_delta;
output[2] = r ^ (j + 1);
output[3] = output[2] ^ left_delta;
}
inline void YaoHalfGate::and_garble(YaoGarbleWire& out, const Key* hashes,
const YaoGarbleWire& left, const YaoGarbleWire& right, Key delta)
{
bool pa = left.mask();
bool pb = right.mask();
TG = hashes[0] ^ hashes[1];
if (pb)
TG ^= delta;
Key WG = hashes[0];
if (pa)
WG ^= TG;
TE = hashes[2] ^ hashes[3] ^ left.full_key();
Key WE = hashes[2];
if (pb)
WE ^= TE ^ left.full_key();
out.set_full_key(WG ^ WE);
assert(out.mask() == out.full_key().get_signal());
}
inline void YaoHalfGate::eval_inputs(Key* output, const Key& left,
const Key& right, long T)
{
long j = T << 1;
output[0] = left.doubling(1) ^ j;
output[1] = right.doubling(1) ^ (j + 1);
}
inline void YaoHalfGate::eval(YaoEvalWire& out, const Key* hashes,
const YaoEvalWire& left, const YaoEvalWire& right)
{
bool sa = left.external();
bool sb = right.external();
Key WG = hashes[0];
if (sa)
WG ^= TG;
Key WE = hashes[1];
if (sb)
WE ^= TE ^ left.key();
out.set(WG ^ WE);
}
#endif /* YAO_YAOHALFGATE_H_ */