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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use crate::error::{FancyError, GarblerError};
use crate::fancy::{BinaryBundle, CrtBundle, Fancy, HasModulus};
use crate::util::{output_tweak, tweak, tweak2, RngExt};
use crate::wire::Wire;
use rand::{CryptoRng, RngCore};
use scuttlebutt::{AbstractChannel, Block};
use std::collections::HashMap;
pub struct Garbler<C, RNG> {
channel: C,
deltas: HashMap<u16, Wire>,
current_output: usize,
current_gate: usize,
rng: RNG,
}
impl<C: AbstractChannel, RNG: CryptoRng + RngCore> Garbler<C, RNG> {
#[inline]
pub fn new(channel: C, rng: RNG, reused_deltas: &[Wire]) -> Self {
Garbler {
channel,
deltas: reused_deltas
.iter()
.map(|w| (w.modulus(), w.clone()))
.collect(),
current_gate: 0,
current_output: 0,
rng,
}
}
#[inline]
fn current_gate(&mut self) -> usize {
let current = self.current_gate;
self.current_gate += 1;
current
}
#[inline]
pub fn delta(&mut self, q: u16) -> Wire {
if let Some(delta) = self.deltas.get(&q) {
return delta.clone();
}
let w = Wire::rand_delta(&mut self.rng, q);
self.deltas.insert(q, w.clone());
w
}
#[inline]
fn current_output(&mut self) -> usize {
let current = self.current_output;
self.current_output += 1;
current
}
#[inline]
pub fn get_deltas(self) -> HashMap<u16, Wire> {
self.deltas
}
#[inline]
pub fn send_wire(&mut self, wire: &Wire) -> Result<(), GarblerError> {
self.channel.write_block(&wire.as_block())?;
Ok(())
}
#[inline]
pub fn encode_wire(&mut self, val: u16, modulus: u16) -> (Wire, Wire) {
let zero = Wire::rand(&mut self.rng, modulus);
let delta = self.delta(modulus);
let enc = zero.plus(&delta.cmul(val));
(zero, enc)
}
#[inline]
pub fn encode_many_wires(
&mut self,
vals: &[u16],
moduli: &[u16],
) -> Result<(Vec<Wire>, Vec<Wire>), GarblerError> {
if vals.len() != moduli.len() {
return Err(GarblerError::EncodingError);
}
assert!(vals.len() == moduli.len());
let mut gbs = Vec::with_capacity(vals.len());
let mut evs = Vec::with_capacity(vals.len());
for (x, q) in vals.iter().zip(moduli.iter()) {
let (gb, ev) = self.encode_wire(*x, *q);
gbs.push(gb);
evs.push(ev);
}
Ok((gbs, evs))
}
#[inline]
pub fn crt_encode_wire(
&mut self,
val: u128,
modulus: u128,
) -> Result<(CrtBundle<Wire>, CrtBundle<Wire>), GarblerError> {
let ms = crate::util::factor(modulus);
let xs = crate::util::crt(val, &ms);
let (gbs, evs) = self.encode_many_wires(&xs, &ms)?;
Ok((CrtBundle::new(gbs), CrtBundle::new(evs)))
}
#[inline]
pub fn bin_encode_wire(
&mut self,
val: u128,
nbits: usize,
) -> Result<(BinaryBundle<Wire>, BinaryBundle<Wire>), GarblerError> {
let xs = crate::util::u128_to_bits(val, nbits);
let ms = vec![2; nbits];
let (gbs, evs) = self.encode_many_wires(&xs, &ms)?;
Ok((BinaryBundle::new(gbs), BinaryBundle::new(evs)))
}
}
impl<C: AbstractChannel, RNG: CryptoRng + RngCore> Fancy for Garbler<C, RNG> {
type Item = Wire;
type Error = GarblerError;
#[inline]
fn constant(&mut self, x: u16, q: u16) -> Result<Wire, GarblerError> {
let zero = Wire::rand(&mut self.rng, q);
let wire = zero.plus(&self.delta(q).cmul_eq(x));
self.send_wire(&wire)?;
Ok(zero)
}
#[inline]
fn add(&mut self, x: &Wire, y: &Wire) -> Result<Wire, GarblerError> {
if x.modulus() != y.modulus() {
return Err(GarblerError::FancyError(FancyError::UnequalModuli));
}
Ok(x.plus(y))
}
#[inline]
fn sub(&mut self, x: &Wire, y: &Wire) -> Result<Wire, GarblerError> {
if x.modulus() != y.modulus() {
return Err(GarblerError::FancyError(FancyError::UnequalModuli));
}
Ok(x.minus(y))
}
#[inline]
fn cmul(&mut self, x: &Wire, c: u16) -> Result<Wire, GarblerError> {
Ok(x.cmul(c))
}
#[inline]
fn mul(&mut self, A: &Wire, B: &Wire) -> Result<Wire, GarblerError> {
if A.modulus() < B.modulus() {
return self.mul(B, A);
}
let q = A.modulus();
let qb = B.modulus();
let gate_num = self.current_gate();
let D = self.delta(q);
let Db = self.delta(qb);
let r;
let mut gate = vec![Block::default(); q as usize + qb as usize - 2];
if q != qb {
if qb > 8 {
return Err(GarblerError::AsymmetricHalfGateModuliMax8(qb))?;
}
r = self.rng.gen_u16() % q;
let t = tweak2(gate_num as u64, 1);
let mut minitable = vec![u128::default(); qb as usize];
let mut B_ = B.clone();
for b in 0..qb {
if b > 0 {
B_.plus_eq(&Db);
}
let new_color = ((r + b) % q) as u128;
let ct = (u128::from(B_.hash(t)) & 0xFFFF) ^ new_color;
minitable[B_.color() as usize] = ct;
}
let mut packed = 0;
for i in 0..qb as usize {
packed += minitable[i] << (16 * i);
}
gate.push(Block::from(packed));
} else {
r = B.color();
}
let g = tweak2(gate_num as u64, 0);
let alpha = (q - A.color()) % q;
let X = A
.plus(&D.cmul(alpha))
.hashback(g, q)
.plus_mov(&D.cmul(alpha * r % q));
let beta = (qb - B.color()) % qb;
let Y = B
.plus(&Db.cmul(beta))
.hashback(g, q)
.plus_mov(&A.cmul((beta + r) % q));
let mut precomp = Vec::with_capacity(q as usize);
let mut X_ = X.clone();
precomp.push(X_.as_block());
for _ in 1..q {
X_.plus_eq(&D);
precomp.push(X_.as_block());
}
let mut A_ = A.clone();
for a in 0..q {
if a > 0 {
A_.plus_eq(&D);
}
if A_.color() != 0 {
gate[A_.color() as usize - 1] =
A_.hash(g) ^ precomp[((q - (a * r % q)) % q) as usize];
}
}
precomp.clear();
let mut Y_ = Y.clone();
precomp.push(Y_.as_block());
for _ in 1..q {
Y_.plus_eq(&A);
precomp.push(Y_.as_block());
}
let mut B_ = B.clone();
for b in 0..qb {
if b > 0 {
B_.plus_eq(&Db);
}
if B_.color() != 0 {
gate[q as usize - 1 + B_.color() as usize - 1] =
B_.hash(g) ^ precomp[((q - ((b + r) % q)) % q) as usize];
}
}
for block in gate.iter() {
self.channel.write_block(block)?;
}
Ok(X.plus_mov(&Y))
}
#[inline]
fn proj(&mut self, A: &Wire, q_out: u16, tt: Option<Vec<u16>>) -> Result<Wire, GarblerError> {
let tt = tt.ok_or(GarblerError::TruthTableRequired)?;
let q_in = A.modulus();
let mut gate = vec![Block::default(); q_in as usize - 1];
let tao = A.color();
let g = tweak(self.current_gate());
let Din = self.delta(q_in);
let Dout = self.delta(q_out);
let C = A
.plus(&Din.cmul((q_in - tao) % q_in))
.hashback(g, q_out)
.plus_mov(&Dout.cmul((q_out - tt[((q_in - tao) % q_in) as usize]) % q_out));
let C_precomputed = {
let mut C_ = C.clone();
(0..q_out)
.map(|x| {
if x > 0 {
C_.plus_eq(&Dout);
}
C_.as_block()
})
.collect::<Vec<Block>>()
};
let mut A_ = A.clone();
for x in 0..q_in {
if x > 0 {
A_.plus_eq(&Din);
}
let ix = (tao as usize + x as usize) % q_in as usize;
if ix == 0 {
continue;
}
let ct = A_.hash(g) ^ C_precomputed[tt[x as usize] as usize];
gate[ix - 1] = ct;
}
for block in gate.iter() {
self.channel.write_block(block)?;
}
Ok(C)
}
#[inline]
fn output(&mut self, X: &Wire) -> Result<(), GarblerError> {
let q = X.modulus();
let mut cts = Vec::with_capacity(q as usize);
let i = self.current_output();
let D = self.delta(q);
for k in 0..q {
let t = output_tweak(i, k);
cts.push(X.plus(&D.cmul(k)).hash(t));
}
for block in cts.iter() {
self.channel.write_block(block)?;
}
Ok(())
}
}