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
use crate::{
errors::FancyError,
fancy::{Fancy, FancyInput, FancyReveal, HasModulus},
FancyArithmetic, FancyBinary,
};
use std::cmp::max;
#[derive(Clone, Debug)]
pub struct DepthItem {
modulus: u16,
depth: usize,
}
impl HasModulus for DepthItem {
fn modulus(&self) -> u16 {
self.modulus
}
}
#[derive(Debug)]
pub enum DepthError {
ProjUnsupported,
Underlying(FancyError),
}
impl From<FancyError> for DepthError {
fn from(e: FancyError) -> Self {
DepthError::Underlying(e)
}
}
impl std::fmt::Display for DepthError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::ProjUnsupported => writeln!(f, "Projection unsupported"),
Self::Underlying(e) => writeln!(f, "Fancy error: {}", e),
}
}
}
#[derive(Clone, Debug)]
pub struct DepthInformer {
ninputs: usize,
nconstants: usize,
nadds: usize,
nsubs: usize,
ncmuls: usize,
nmuls: usize,
mul_depth: usize,
}
impl std::fmt::Display for DepthInformer {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(f, "computation info:")?;
writeln!(f, " inputs: {:16}", self.ninputs)?;
writeln!(f, " constants: {:16}", self.nconstants)?;
writeln!(f, " additions: {:16}", self.nadds)?;
writeln!(f, " subtractions: {:16}", self.nsubs)?;
writeln!(f, " cmuls: {:16}", self.ncmuls)?;
writeln!(f, " muls: {:16}", self.nmuls)?;
writeln!(
f,
" total gates: {:16}",
self.nadds + self.nsubs + self.ncmuls + self.nmuls
)?;
writeln!(f, " mul depth: {:16}", self.mul_depth)?;
Ok(())
}
}
impl DepthInformer {
pub fn new() -> DepthInformer {
DepthInformer {
ninputs: 0,
nconstants: 0,
nadds: 0,
nsubs: 0,
ncmuls: 0,
nmuls: 0,
mul_depth: 0,
}
}
}
impl FancyInput for DepthInformer {
type Item = DepthItem;
type Error = DepthError;
fn receive_many(&mut self, moduli: &[u16]) -> Result<Vec<Self::Item>, Self::Error> {
self.ninputs += moduli.len();
Ok(moduli
.iter()
.map(|q| DepthItem {
modulus: *q,
depth: 0,
})
.collect())
}
fn encode_many(
&mut self,
_values: &[u16],
moduli: &[u16],
) -> Result<Vec<Self::Item>, Self::Error> {
self.receive_many(moduli)
}
}
impl FancyBinary for DepthInformer {
fn xor(&mut self, x: &Self::Item, y: &Self::Item) -> Result<Self::Item, Self::Error> {
FancyArithmetic::add(self, x, y)
}
fn and(&mut self, x: &Self::Item, y: &Self::Item) -> Result<Self::Item, Self::Error> {
FancyArithmetic::mul(self, x, y)
}
fn negate(&mut self, x: &Self::Item) -> Result<Self::Item, Self::Error> {
self.nadds += 1;
Ok(DepthItem {
modulus: x.modulus,
depth: x.depth,
})
}
}
impl FancyArithmetic for DepthInformer {
fn add(&mut self, x: &Self::Item, y: &Self::Item) -> Result<Self::Item, Self::Error> {
self.nadds += 1;
Ok(DepthItem {
modulus: x.modulus,
depth: max(x.depth, y.depth),
})
}
fn sub(&mut self, x: &Self::Item, y: &Self::Item) -> Result<Self::Item, Self::Error> {
self.nsubs += 1;
Ok(DepthItem {
modulus: x.modulus,
depth: max(x.depth, y.depth),
})
}
fn cmul(&mut self, x: &Self::Item, _y: u16) -> Result<Self::Item, Self::Error> {
self.ncmuls += 1;
Ok(DepthItem {
modulus: x.modulus,
depth: x.depth + 1,
})
}
fn mul(&mut self, x: &Self::Item, y: &Self::Item) -> Result<Self::Item, Self::Error> {
self.nmuls += 1;
Ok(DepthItem {
modulus: x.modulus,
depth: max(x.depth, y.depth) + 1,
})
}
fn proj(
&mut self,
_x: &Self::Item,
_q: u16,
_tt: Option<Vec<u16>>,
) -> Result<Self::Item, Self::Error> {
Err(DepthError::ProjUnsupported)
}
}
impl Fancy for DepthInformer {
type Item = DepthItem;
type Error = DepthError;
fn constant(&mut self, _val: u16, q: u16) -> Result<Self::Item, Self::Error> {
self.nconstants += 1;
Ok(DepthItem {
modulus: q,
depth: 0,
})
}
fn output(&mut self, x: &Self::Item) -> Result<Option<u16>, Self::Error> {
self.mul_depth = max(self.mul_depth, x.depth);
Ok(None)
}
}
impl FancyReveal for DepthInformer {
fn reveal(&mut self, _x: &Self::Item) -> Result<u16, Self::Error> {
Ok(0)
}
}