-
Notifications
You must be signed in to change notification settings - Fork 29
/
quad_approx.rs
145 lines (138 loc) · 4.55 KB
/
quad_approx.rs
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
use crate::{beavers_mul::BeaversMulProtocol, AdditiveShare};
use algebra::{
fields::PrimeField,
fixed_point::{FixedPoint, FixedPointParameters},
fp_64::Fp64Parameters,
FpParameters, Polynomial,
};
use crypto_primitives::{BeaversMul, Triple};
use io_utils::imux::IMuxSync;
use protocols_sys::*;
use rand::{CryptoRng, RngCore};
use std::{
io::{Read, Write},
marker::PhantomData,
};
pub struct QuadApproxProtocol<P: FixedPointParameters> {
_share: PhantomData<P>,
}
impl<P: FixedPointParameters> QuadApproxProtocol<P>
where
<P::Field as PrimeField>::Params: Fp64Parameters,
P::Field: PrimeField<BigInt = <<P::Field as PrimeField>::Params as FpParameters>::BigInt>,
{
pub fn offline_server_protocol<
M: BeaversMul<FixedPoint<P>>,
R: Read + Send,
W: Write + Send,
RNG: RngCore + CryptoRng,
>(
reader: &mut IMuxSync<R>,
writer: &mut IMuxSync<W>,
sfhe: &ServerFHE,
num_approx: usize,
rng: &mut RNG,
) -> Result<Vec<Triple<P::Field>>, bincode::Error> {
if num_approx != 0 {
BeaversMulProtocol::offline_server_protocol::<M, _, _, _>(
reader, writer, sfhe, num_approx, rng,
)
} else {
Ok(Vec::new())
}
}
pub fn offline_client_protocol<
M: BeaversMul<FixedPoint<P>>,
R: Read + Send,
W: Write + Send,
RNG: RngCore + CryptoRng,
>(
reader: &mut IMuxSync<R>,
writer: &mut IMuxSync<W>,
cfhe: &ClientFHE,
num_approx: usize,
rng: &mut RNG,
) -> Result<Vec<Triple<P::Field>>, bincode::Error> {
if num_approx != 0 {
BeaversMulProtocol::offline_client_protocol::<M, _, _, _>(
reader, writer, cfhe, num_approx, rng,
)
} else {
Ok(Vec::new())
}
}
pub fn online_server_protocol<M: BeaversMul<FixedPoint<P>>, R: Read + Send, W: Write + Send>(
party_index: usize,
reader: &mut IMuxSync<R>,
writer: &mut IMuxSync<W>,
polynomial: &Polynomial<FixedPoint<P>>,
x_s: &[AdditiveShare<P>],
triples: &[Triple<P::Field>],
) -> Result<Vec<AdditiveShare<P>>, bincode::Error> {
let mut x_squared = BeaversMulProtocol::online_server_protocol::<M, R, W>(
party_index,
reader,
writer,
x_s,
x_s,
&triples,
)?;
let coeffs = polynomial.coeffs();
assert_eq!(coeffs.len(), 3);
let a_0 = coeffs[0];
let a_1 = coeffs[1];
let a_2 = coeffs[2];
// Reduce down to correct size.
for (x2, x) in x_squared.iter_mut().zip(x_s) {
x2.inner.signed_reduce_in_place();
*x2 *= a_2;
x2.inner.signed_reduce_in_place();
let mut a_1_x = *x * a_1;
a_1_x.inner.signed_reduce_in_place();
*x2 += a_1_x;
// Only add the constant if we are the client.
// (Both parties adding it would mean that the constant is doubled.)
if party_index == crate::neural_network::CLIENT {
x2.add_constant_in_place(a_0);
}
}
Ok(x_squared)
}
pub fn online_client_protocol<M: BeaversMul<FixedPoint<P>>, R: Read + Send, W: Write + Send>(
party_index: usize,
reader: &mut IMuxSync<R>,
writer: &mut IMuxSync<W>,
polynomial: &Polynomial<FixedPoint<P>>,
x_s: &[AdditiveShare<P>],
triples: &[Triple<P::Field>],
) -> Result<Vec<AdditiveShare<P>>, bincode::Error> {
let mut x_squared = BeaversMulProtocol::online_client_protocol::<M, R, W>(
party_index,
reader,
writer,
x_s,
x_s,
&triples,
)?;
let coeffs = polynomial.coeffs();
assert_eq!(coeffs.len(), 3);
let a_0 = coeffs[0];
let a_1 = coeffs[1];
let a_2 = coeffs[2];
// Reduce down to correct size.
for (x2, x) in x_squared.iter_mut().zip(x_s) {
x2.inner.signed_reduce_in_place();
*x2 *= a_2;
x2.inner.signed_reduce_in_place();
let mut a_1_x = *x * a_1;
a_1_x.inner.signed_reduce_in_place();
*x2 += a_1_x;
// Only add the constant if we are the client.
// (Both parties adding it would mean that the constant is doubled.)
if party_index == crate::neural_network::CLIENT {
x2.add_constant_in_place(a_0);
}
}
Ok(x_squared)
}
}