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
use crate::integer::server_key::CheckError;
use crate::integer::{CrtCiphertext, ServerKey};
impl ServerKey {
/// Computes homomorphically an addition between a scalar and a ciphertext.
///
/// This function computes the operation without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
///
/// sks.unchecked_crt_scalar_add_assign(&mut ctxt_1, clear_2);
///
/// // Decrypt
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn unchecked_crt_scalar_add(&self, ct: &CrtCiphertext, scalar: u64) -> CrtCiphertext {
let mut result = ct.clone();
self.unchecked_crt_scalar_add_assign(&mut result, scalar);
result
}
/// Computes homomorphically an addition between a scalar and a ciphertext.
///
/// This function computes the operation without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is assigned to the `ct_left` ciphertext.
pub fn unchecked_crt_scalar_add_assign(&self, ct: &mut CrtCiphertext, scalar: u64) {
//Add the crt representation of the scalar to the ciphertext
for (ct_i, mod_i) in ct.blocks.iter_mut().zip(ct.moduli.iter()) {
let scalar_i = scalar % mod_i;
self.key.unchecked_scalar_add_assign(ct_i, scalar_i as u8);
}
}
/// Verifies if a scalar can be added to a ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// // Encrypt two messages
/// let ctxt_1 = cks.encrypt(clear_1);
///
/// sks.is_crt_scalar_add_possible(&ctxt_1, clear_2).unwrap();
/// ```
pub fn is_crt_scalar_add_possible(
&self,
ct: &CrtCiphertext,
scalar: u64,
) -> Result<(), CheckError> {
for (ct_i, mod_i) in ct.blocks.iter().zip(ct.moduli.iter()) {
let scalar_i = scalar % mod_i;
self.key
.is_scalar_add_possible(ct_i.noise_degree(), scalar_i as u8)?;
}
Ok(())
}
/// Computes homomorphically an addition between a scalar and a ciphertext.
///
/// If the operation can be performed, the result is returned in a new ciphertext.
/// Otherwise a [CheckError] is returned.
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
///
/// sks.checked_crt_scalar_add_assign(&mut ctxt_1, clear_2)
/// .unwrap();
///
/// // Decrypt
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn checked_crt_scalar_add(
&self,
ct: &CrtCiphertext,
scalar: u64,
) -> Result<CrtCiphertext, CheckError> {
self.is_crt_scalar_add_possible(ct, scalar)?;
Ok(self.unchecked_crt_scalar_add(ct, scalar))
}
/// Computes homomorphically an addition between a scalar and a ciphertext.
///
/// If the operation can be performed, the result is stored in the `ct_left` ciphertext.
/// Otherwise a [CheckError] is returned, and `ct_left` is not modified.
pub fn checked_crt_scalar_add_assign(
&self,
ct: &mut CrtCiphertext,
scalar: u64,
) -> Result<(), CheckError> {
self.is_crt_scalar_add_possible(ct, scalar)?;
self.unchecked_crt_scalar_add_assign(ct, scalar);
Ok(())
}
/// Computes homomorphically the addition of ciphertext with a scalar.
///
/// The result is returned in a new ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
///
/// let ctxt = sks.smart_crt_scalar_add(&mut ctxt_1, clear_2);
///
/// // Decrypt
/// let res = cks.decrypt(&ctxt);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn smart_crt_scalar_add(&self, ct: &mut CrtCiphertext, scalar: u64) -> CrtCiphertext {
if self.is_crt_scalar_add_possible(ct, scalar).is_err() {
self.full_extract_message_assign(ct);
}
self.is_crt_scalar_add_possible(ct, scalar).unwrap();
let mut ct = ct.clone();
self.unchecked_crt_scalar_add_assign(&mut ct, scalar);
ct
}
/// Computes homomorphically the addition of ciphertext with a scalar.
///
/// The result is assigned to the `ct_left` ciphertext.
///
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128;
///
/// // Generate the client key and the server key:
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt(clear_1);
///
/// sks.smart_crt_scalar_add_assign(&mut ctxt_1, clear_2);
///
/// // Decrypt
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn smart_crt_scalar_add_assign(&self, ct: &mut CrtCiphertext, scalar: u64) {
if self.is_crt_scalar_add_possible(ct, scalar).is_err() {
self.full_extract_message_assign(ct);
}
self.is_crt_scalar_add_possible(ct, scalar).unwrap();
self.unchecked_crt_scalar_add_assign(ct, scalar);
}
}