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
use crate::error::*;
use crate::{Ciphertext, GaloisKeys, Plaintext, RelinearizationKeys};
/**
* An interface for an evaluator.
*/
pub trait Evaluator {
/**
* Negates a ciphertext inplace.
* * `a` - the value to negate
*/
fn negate_inplace(&self, a: &mut Ciphertext) -> Result<()>;
/**
* Negates a ciphertext into a new ciphertext.
* * `a` - the value to negate
*/
fn negate(&self, a: &Ciphertext) -> Result<Ciphertext>;
/**
* Add `a` and `b` and store the result in `a`.
* * `a` - the accumulator
* * `b` - the added value
*/
fn add_inplace(&self, a: &mut Ciphertext, b: &Ciphertext) -> Result<()>;
/**
* Adds `a` and `b`.
* * `a` - first operand
* * `b` - second operand
*/
fn add(&self, a: &Ciphertext, b: &Ciphertext) -> Result<Ciphertext>;
/**
* Performs an addition reduction of multiple ciphertexts packed into a slice.
* * `a` - a slice of ciphertexts to sum.
*/
fn add_many(&self, a: &[Ciphertext]) -> Result<Ciphertext>;
/**
* Performs an multiplication reduction of multiple ciphertexts packed into a slice. This
* method creates a tree of multiplications with relinearization after each operation.
* * `a` - a slice of ciphertexts to sum.
* * `relin_keys` - the relinearization keys.
*/
fn multiply_many(
&self,
a: &[Ciphertext],
relin_keys: &RelinearizationKeys,
) -> Result<Ciphertext>;
/**
* Subtracts `b` from `a` and stores the result in `a`.
* * `a` - the left operand and destination
* * `b` - the right operand
*/
fn sub_inplace(&self, a: &mut Ciphertext, b: &Ciphertext) -> Result<()>;
/**
* Subtracts `b` from `a`.
* * `a` - the left operand
* * `b` - the right operand
*/
fn sub(&self, a: &Ciphertext, b: &Ciphertext) -> Result<Ciphertext>;
/**
* Multiplies `a` and `b` and stores the result in `a`.
* * `a` - the left operand and destination.
* * `b` - the right operand.
*/
fn multiply_inplace(&self, a: &mut Ciphertext, b: &Ciphertext) -> Result<()>;
/**
* Multiplies `a` and `b`.
* * `a` - the left operand.
* * `b` - the right operand.
*/
fn multiply(&self, a: &Ciphertext, b: &Ciphertext) -> Result<Ciphertext>;
/**
* Squares `a` and stores the result in `a`.
* * `a` - the value to square.
*/
fn square_inplace(&self, a: &mut Ciphertext) -> Result<()>;
/**
* Squares `a`.
* * `a` - the value to square.
*/
fn square(&self, a: &Ciphertext) -> Result<Ciphertext>;
/**
* Given a ciphertext encrypted modulo q_1...q_k, this function switches the modulus down to q_1...q_{k-1} and
* stores the result in the destination parameter.
*
* # Remarks
* In the BFV scheme if you've set up a coefficient modulus chain, this reduces the
* number of bits needed to represent the ciphertext. This in turn speeds up operations.
*
* If you haven't set up a modulus chain, don't use this.
*
* TODO: what does this mean for CKKS?
*/
fn mod_switch_to_next(&self, a: &Ciphertext) -> Result<Ciphertext>;
/**
* Given a ciphertext encrypted modulo q_1...q_k, this function switches the modulus down to q_1...q_{k-1} and
* stores the result in the destination parameter. This does function does so in-place.
*
* # Remarks
* In the BFV scheme if you've set up a coefficient modulus chain, this reduces the
* number of bits needed to represent the ciphertext. This in turn speeds up operations.
*
* If you haven't set up a modulus chain, don't use this.
*
* TODO: what does this mean for CKKS?
*/
fn mod_switch_to_next_inplace(&self, a: &Ciphertext) -> Result<()>;
/**
* Modulus switches an NTT transformed plaintext from modulo q_1...q_k down to modulo q_1...q_{k-1}.
*/
fn mod_switch_to_next_plaintext(&self, a: &Plaintext) -> Result<Plaintext>;
/**
* Modulus switches an NTT transformed plaintext from modulo q_1...q_k down to modulo q_1...q_{k-1}.
* This variant does so in-place.
*/
fn mod_switch_to_next_inplace_plaintext(&self, a: &Plaintext) -> Result<()>;
/**
* This functions raises encrypted to a power and stores the result in the destination parameter. Dynamic
* memory allocations in the process are allocated from the memory pool pointed to by the given
* MemoryPoolHandle. The exponentiation is done in a depth-optimal order, and relinearization is performed
* automatically after every multiplication in the process. In relinearization the given relinearization keys
* are used.
*/
fn exponentiate(
&self,
a: &Ciphertext,
exponent: u64,
relin_keys: &RelinearizationKeys,
) -> Result<Ciphertext>;
/**
* This functions raises encrypted to a power and stores the result in the destination parameter. Dynamic
* memory allocations in the process are allocated from the memory pool pointed to by the given
* MemoryPoolHandle. The exponentiation is done in a depth-optimal order, and relinearization is performed
* automatically after every multiplication in the process. In relinearization the given relinearization keys
* are used.
*/
fn exponentiate_inplace(
&self,
a: &Ciphertext,
exponent: u64,
relin_keys: &RelinearizationKeys,
) -> Result<()>;
/**
* Adds a ciphertext and a plaintext.
* * `a` - the ciphertext
* * `b` - the plaintext
*/
fn add_plain(&self, a: &Ciphertext, b: &Plaintext) -> Result<Ciphertext>;
/**
* Adds a ciphertext and a plaintext.
* * `a` - the ciphertext
* * `b` - the plaintext
*/
fn add_plain_inplace(&self, a: &mut Ciphertext, b: &Plaintext) -> Result<()>;
/**
* Subtract a plaintext from a ciphertext.
* * `a` - the ciphertext
* * `b` - the plaintext
*/
fn sub_plain(&self, a: &Ciphertext, b: &Plaintext) -> Result<Ciphertext>;
/**
* Subtract a plaintext from a ciphertext and store the result in the ciphertext.
* * `a` - the ciphertext
* * `b` - the plaintext
*/
fn sub_plain_inplace(&self, a: &mut Ciphertext, b: &Plaintext) -> Result<()>;
/**
* Multiply a ciphertext by a plaintext.
* * `a` - the ciphertext
* * `b` - the plaintext
*/
fn multiply_plain(&self, a: &Ciphertext, b: &Plaintext) -> Result<Ciphertext>;
/**
* Multiply a ciphertext by a plaintext and store in the ciphertext.
* * `a` - the ciphertext
* * `b` - the plaintext
*/
fn multiply_plain_inplace(&self, a: &mut Ciphertext, b: &Plaintext) -> Result<()>;
/**
* This functions relinearizes a ciphertext in-place, reducing it to 2 polynomials. This
* reduces future noise growth under multiplication operations.
*/
fn relinearize_inplace(
&self,
a: &mut Ciphertext,
relin_keys: &RelinearizationKeys,
) -> Result<()>;
/**
* This functions relinearizes a ciphertext, reducing it to 2 polynomials. This
* reduces future noise growth under multiplication operations.
*/
fn relinearize(&self, a: &Ciphertext, relin_keys: &RelinearizationKeys) -> Result<Ciphertext>;
/**
* Rotates plaintext matrix rows cyclically.
*
* When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix rows
* cyclically to the left (steps > 0) or to the right (steps < 0). Since the size of the batched matrix
* is 2-by-(N/2), where N is the degree of the polynomial modulus, the number of steps to rotate must have
* absolute value at most N/2-1.
*
* * `a` - The ciphertext to rotate
* * `steps` - The number of steps to rotate (positive left, negative right)
* * `galois_keys` - The Galois keys
*/
fn rotate_rows(
&self,
a: &Ciphertext,
steps: i32,
galois_keys: &GaloisKeys,
) -> Result<Ciphertext>;
/**
* Rotates plaintext matrix rows cyclically. This variant does so in-place
*
* When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix rows
* cyclically to the left (steps > 0) or to the right (steps < 0). Since the size of the batched matrix
* is 2-by-(N/2), where N is the degree of the polynomial modulus, the number of steps to rotate must have
* absolute value at most N/2-1.
*
* * `a` - The ciphertext to rotate
* * `steps` - The number of steps to rotate (positive left, negative right)
* * `galois_keys` - The Galois keys
*/
fn rotate_rows_inplace(
&self,
a: &Ciphertext,
steps: i32,
galois_keys: &GaloisKeys,
) -> Result<()>;
/**
* Rotates plaintext matrix columns cyclically.
*
* When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix columns
* cyclically. Since the size of the batched matrix is 2-by-(N/2), where N is the degree of the polynomial
* modulus, this means simply swapping the two rows. Dynamic memory allocations in the process are allocated
* from the memory pool pointed to by the given MemoryPoolHandle.
*
* * `encrypted` - The ciphertext to rotate
* * `galoisKeys` - The Galois keys
*/
fn rotate_columns(&self, a: &Ciphertext, galois_keys: &GaloisKeys) -> Result<Ciphertext>;
/**
* Rotates plaintext matrix columns cyclically. This variant does so in-place.
*
* When batching is used with the BFV scheme, this function rotates the encrypted plaintext matrix columns
* cyclically. Since the size of the batched matrix is 2-by-(N/2), where N is the degree of the polynomial
* modulus, this means simply swapping the two rows. Dynamic memory allocations in the process are allocated
* from the memory pool pointed to by the given MemoryPoolHandle.
*
* * `encrypted` - The ciphertext to rotate
* * `galoisKeys` - The Galois keys
*/
fn rotate_columns_inplace(&self, a: &Ciphertext, galois_keys: &GaloisKeys) -> Result<()>;
}