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
use crate::ciphertext::RadixCiphertext;
use crate::server_key::CheckError;
use crate::server_key::CheckError::CarryFull;
use crate::ServerKey;
impl ServerKey {
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// This function computes the opposite of a message without checking if it exceeds the
/// capacity of the ciphertext.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
/// ```rust
/// // Encrypt two messages:
/// 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 modulus = 1 << 8;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 159;
///
/// // Encrypt a message
/// let mut ctxt = cks.encrypt(msg);
///
/// // Compute homomorphically a negation
/// sks.unchecked_neg_assign(&mut ctxt);
///
/// // Decrypt
/// let dec = cks.decrypt(&ctxt);
/// assert_eq!(modulus - msg, dec);
/// ```
pub fn unchecked_neg(&self, ctxt: &RadixCiphertext) -> RadixCiphertext {
let mut result = ctxt.clone();
self.unchecked_neg_assign(&mut result);
result
}
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// This function computes the opposite of a message without checking if it exceeds the
/// capacity of the ciphertext.
///
/// The result is assigned to the `ct_left` ciphertext.
pub fn unchecked_neg_assign(&self, ctxt: &mut RadixCiphertext) {
//z is used to make sure the negation doesn't fill the padding bit
let mut z;
let mut z_b;
for i in 0..ctxt.blocks.len() {
let c_i = &mut ctxt.blocks[i];
z = self.key.unchecked_neg_assign_with_z(c_i);
// Subtract z/B to the next ciphertext to compensate for the addition of z
z_b = z / self.key.message_modulus.0 as u64;
if i < ctxt.blocks.len() - 1 {
let c_j = &mut ctxt.blocks[i + 1];
self.key.unchecked_scalar_add_assign(c_j, z_b as u8);
}
}
}
/// Verifies if ct can be negated.
///
/// # 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;
///
/// // Encrypt a message
/// let ctxt = cks.encrypt(msg);
///
/// // Check if we can perform a negation
/// let res = sks.is_neg_possible(&ctxt);
///
/// assert_eq!(true, res);
/// ```
pub fn is_neg_possible(&self, ctxt: &RadixCiphertext) -> bool {
for ct_i in ctxt.blocks.iter() {
if !self.key.is_neg_possible(ct_i) {
return false;
}
}
true
}
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// This function computes the opposite of a message 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 = 1;
///
/// // Encrypt a message
/// let ctxt = cks.encrypt(msg);
///
/// // Compute homomorphically a negation:
/// let ct_res = sks.checked_neg(&ctxt);
///
/// match ct_res {
/// Err(x) => panic!("{:?}", x),
/// Ok(y) => {
/// let clear = cks.decrypt(&y);
/// assert_eq!(255, clear);
/// }
/// }
/// ```
pub fn checked_neg(&self, ctxt: &RadixCiphertext) -> Result<RadixCiphertext, CheckError> {
//If the ciphertext cannot be negated without exceeding the capacity of a ciphertext
if self.is_neg_possible(ctxt) {
let mut result = ctxt.clone();
self.unchecked_neg_assign(&mut result);
Ok(result)
} else {
Err(CarryFull)
}
}
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// This function computes the opposite of a message without checking if it exceeds the
/// capacity of the 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 modulus = 1 << 8;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 1;
///
/// // Encrypt a message
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically a negation:
/// sks.checked_neg_assign(&mut ct);
///
/// let clear_res = cks.decrypt(&ct);
/// assert_eq!(clear_res, (modulus - msg));
/// ```
pub fn checked_neg_assign(&self, ctxt: &mut RadixCiphertext) -> Result<(), CheckError> {
//If the ciphertext cannot be negated without exceeding the capacity of a ciphertext
if self.is_neg_possible(ctxt) {
self.unchecked_neg_assign(ctxt);
Ok(())
} else {
Err(CarryFull)
}
}
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// 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 = 1;
///
/// // Encrypt two messages:
/// let mut ctxt = cks.encrypt(msg);
///
/// // Compute homomorphically a negation
/// let ct_res = sks.smart_neg(&mut ctxt);
///
/// // Decrypt
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(255, dec);
/// ```
pub fn smart_neg(&self, ctxt: &mut RadixCiphertext) -> RadixCiphertext {
if !self.is_neg_possible(ctxt) {
self.full_propagate(ctxt);
}
self.unchecked_neg(ctxt)
}
}