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
use crate::ciphertext::RadixCiphertext;
use crate::server_key::CheckError;
use crate::server_key::CheckError::CarryFull;
use crate::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 concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 4;
/// let scalar = 40;
///
/// let ct = cks.encrypt(msg);
///
/// // Compute homomorphically an addition:
/// let ct_res = sks.unchecked_scalar_add(&ct, scalar);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(msg + scalar, dec);
/// ```
pub fn unchecked_scalar_add(&self, ct: &RadixCiphertext, scalar: u64) -> RadixCiphertext {
let mut result = ct.clone();
self.unchecked_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_scalar_add_assign(&self, ct: &mut RadixCiphertext, scalar: u64) {
// Bits of message put to 1
let mask = (self.key.message_modulus.0 - 1) as u64;
let mut power = 1_u64;
// Put each decomposition into a new ciphertext
for ct_i in ct.blocks.iter_mut() {
let mut decomp = scalar & (mask * power);
decomp /= power;
self.key.unchecked_scalar_add_assign(ct_i, decomp as u8);
//modulus to the power i
power *= self.key.message_modulus.0 as u64;
}
}
/// Verifies if a scalar can be added to a ciphertext.
///
/// # Example
///
///```rust
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 2;
/// let scalar = 40;
///
/// // Encrypt two messages:
/// let ct1 = cks.encrypt(msg);
/// let ct2 = cks.encrypt(msg);
///
/// // Check if we can perform an addition
/// let res = sks.is_scalar_add_possible(&ct1, scalar);
///
/// assert_eq!(true, res);
/// ```
pub fn is_scalar_add_possible(&self, ct: &RadixCiphertext, scalar: u64) -> bool {
//Bits of message put to 1
let mask = (self.key.message_modulus.0 - 1) as u64;
let mut power = 1_u64;
for ct_i in ct.blocks.iter() {
let mut decomp = scalar & (mask * power);
decomp /= power;
if !self.key.is_scalar_add_possible(ct_i, decomp as u8) {
return false;
}
//modulus to the power i
power *= self.key.message_modulus.0 as u64;
}
true
}
/// 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 [CheckError::CarryFull] is returned.
///
/// # Example
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 4;
/// let scalar = 40;
///
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically an addition:
/// let ct_res = sks.checked_scalar_add(&mut ct, scalar)?;
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(msg + scalar, dec);
/// # Ok(())
/// # }
/// ```
pub fn checked_scalar_add(
&self,
ct: &RadixCiphertext,
scalar: u64,
) -> Result<RadixCiphertext, CheckError> {
if self.is_scalar_add_possible(ct, scalar) {
Ok(self.unchecked_scalar_add(ct, scalar))
} else {
Err(CarryFull)
}
}
/// 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 [CheckError::CarryFull] is returned, and `ct_left` is not modified.
pub fn checked_scalar_add_assign(
&self,
ct: &mut RadixCiphertext,
scalar: u64,
) -> Result<(), CheckError> {
if self.is_scalar_add_possible(ct, scalar) {
self.unchecked_scalar_add_assign(ct, scalar);
Ok(())
} else {
Err(CarryFull)
}
}
/// Computes homomorphically the addition of ciphertext with a scalar.
///
/// The result is returned in a new ciphertext.
///
/// # Example
///
/// ```rust
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 4;
/// let scalar = 40;
///
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically an addition:
/// let ct_res = sks.smart_scalar_add(&mut ct, scalar);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(msg + scalar, dec);
/// ```
pub fn smart_scalar_add(&self, ct: &mut RadixCiphertext, scalar: u64) -> RadixCiphertext {
if !self.is_scalar_add_possible(ct, scalar) {
self.full_propagate(ct);
}
let mut ct = ct.clone();
self.unchecked_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 concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 129;
/// let scalar = 40;
///
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically an addition:
/// sks.smart_scalar_add_assign(&mut ct, scalar);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct);
/// assert_eq!(msg + scalar, dec);
/// ```
pub fn smart_scalar_add_assign(&self, ct: &mut RadixCiphertext, scalar: u64) {
if !self.is_scalar_add_possible(ct, scalar) {
self.full_propagate(ct);
}
self.unchecked_scalar_add_assign(ct, scalar);
}
}