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
//! Module with the definition of the PublicKey.
use crate::core_crypto::entities::*;
use crate::shortint::backward_compatibility::public_key::PublicKeyVersions;
use crate::shortint::ciphertext::Ciphertext;
use crate::shortint::engine::ShortintEngine;
use crate::shortint::parameters::{AtomicPatternKind, MessageModulus, ShortintParameterSet};
use crate::shortint::{ClientKey, CompressedPublicKey};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use tfhe_versionable::Versionize;
/// A structure containing a public key.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Versionize)]
#[versionize(PublicKeyVersions)]
pub struct PublicKey {
pub(crate) lwe_public_key: LwePublicKeyOwned<u64>,
pub parameters: ShortintParameterSet,
pub atomic_pattern: AtomicPatternKind,
}
impl PublicKey {
/// Generate a public key.
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::client_key::ClientKey;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
/// use tfhe::shortint::public_key::PublicKey;
///
/// // Generate the client key:
/// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let pk = PublicKey::new(&cks);
/// ```
pub fn new(client_key: &ClientKey) -> Self {
ShortintEngine::with_thread_local_mut(|engine| engine.new_public_key(client_key))
}
/// Deconstruct a [`PublicKey`] into its constituents.
pub fn into_raw_parts(
self,
) -> (
LwePublicKeyOwned<u64>,
ShortintParameterSet,
AtomicPatternKind,
) {
let Self {
lwe_public_key,
parameters,
atomic_pattern,
} = self;
(lwe_public_key, parameters, atomic_pattern)
}
/// Construct a [`PublicKey`] from its constituents.
///
/// # Panics
///
/// Panics if the constituents are not compatible with each others.
pub fn from_raw_parts(
lwe_public_key: LwePublicKeyOwned<u64>,
parameters: ShortintParameterSet,
atomic_pattern: AtomicPatternKind,
) -> Self {
let expected_atomic_pattern =
AtomicPatternKind::Standard(parameters.encryption_key_choice().into());
assert_eq!(
atomic_pattern, expected_atomic_pattern,
"Mismatch between expected atomic pattern ({expected_atomic_pattern:?}) and \
provided atomic pattern ({atomic_pattern:?})"
);
let ciphertext_lwe_dimension = parameters.encryption_lwe_dimension();
assert_eq!(
(*lwe_public_key).lwe_size().to_lwe_dimension(),
ciphertext_lwe_dimension,
"Mismatch between the LwePublicKeyOwned LweDimension ({:?}) and \
the provided parameters LweDimension ({:?})",
(*lwe_public_key).lwe_size().to_lwe_dimension(),
ciphertext_lwe_dimension,
);
assert_eq!(
(*lwe_public_key).ciphertext_modulus(),
parameters.ciphertext_modulus(),
"Mismatch between the LwePublicKeyOwned CiphertextModulus ({:?}) and \
the provided parameters CiphertextModulus ({:?})",
(*lwe_public_key).ciphertext_modulus(),
parameters.ciphertext_modulus(),
);
Self {
lwe_public_key,
parameters,
atomic_pattern,
}
}
/// Encrypt a small integer message using the client key.
///
/// The input message is reduced to the encrypted message space modulus
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
/// use tfhe::shortint::{ClientKey, PublicKey};
///
/// // Generate the client key:
/// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let pk = PublicKey::new(&cks);
///
/// // Encryption of one message that is within the encrypted message modulus:
/// let msg = 3;
/// let ct = pk.encrypt(msg);
///
/// let dec = cks.decrypt(&ct);
/// assert_eq!(msg, dec);
///
/// // Encryption of one message that is outside the encrypted message modulus:
/// let msg = 5;
/// let ct = pk.encrypt(msg);
///
/// let dec = cks.decrypt(&ct);
/// let modulus = cks.parameters().message_modulus().0;
/// assert_eq!(msg % modulus, dec);
/// ```
pub fn encrypt(&self, message: u64) -> Ciphertext {
ShortintEngine::with_thread_local_mut(|engine| {
engine.encrypt_with_public_key(self, message)
})
}
/// Encrypt a small integer message using the client key with a specific message modulus
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::parameters::{MessageModulus, PARAM_MESSAGE_2_CARRY_2_KS_PBS};
/// use tfhe::shortint::{ClientKey, PublicKey};
///
/// // Generate the client key:
/// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let pk = PublicKey::new(&cks);
///
/// let msg = 3;
///
/// // Encryption of one message:
/// let ct = pk.encrypt_with_message_modulus(msg, MessageModulus(6));
///
/// // Decryption:
/// let dec = cks.decrypt(&ct);
/// assert_eq!(msg, dec);
/// ```
pub fn encrypt_with_message_modulus(
&self,
message: u64,
message_modulus: MessageModulus,
) -> Ciphertext {
ShortintEngine::with_thread_local_mut(|engine| {
engine.encrypt_with_message_modulus_and_public_key(self, message, message_modulus)
})
}
/// Encrypt an integer without reducing the input message modulus the message space
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
/// use tfhe::shortint::{ClientKey, PublicKey};
///
/// // Generate the client key:
/// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let pk = PublicKey::new(&cks);
///
/// let msg = 7;
/// let ct = pk.unchecked_encrypt(msg);
/// // | ct |
/// // | carry | message |
/// // |-------|---------|
/// // | 0 1 | 1 1 |
///
/// let dec = cks.decrypt_message_and_carry(&ct);
/// assert_eq!(msg, dec);
/// ```
pub fn unchecked_encrypt(&self, message: u64) -> Ciphertext {
ShortintEngine::with_thread_local_mut(|engine| {
engine.unchecked_encrypt_with_public_key(self, message)
})
}
/// Encrypt a small integer message using the client key without padding bit.
///
/// The input message is reduced to the encrypted message space modulus
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
/// use tfhe::shortint::{ClientKey, PublicKey};
///
/// // Generate the client key:
/// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
/// // DISCLAIMER: Note that this parameter is not guaranteed to be secure
/// let pk = PublicKey::new(&cks);
///
/// // Encryption of one message that is within the encrypted message modulus:
/// let msg = 6;
/// let ct = pk.encrypt_without_padding(msg);
///
/// let dec = cks.decrypt_message_and_carry_without_padding(&ct);
/// assert_eq!(msg, dec);
/// ```
pub fn encrypt_without_padding(&self, message: u64) -> Ciphertext {
ShortintEngine::with_thread_local_mut(|engine| {
engine.encrypt_without_padding_with_public_key(self, message)
})
}
/// Encrypt a small integer message using the client key without padding bit with some modulus.
///
/// The input message is reduced to the encrypted message space modulus
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::parameters::{MessageModulus, PARAM_MESSAGE_2_CARRY_2_KS_PBS};
/// use tfhe::shortint::{ClientKey, PublicKey};
///
/// // Generate the client key:
/// let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let pk = PublicKey::new(&cks);
///
/// let msg = 2;
/// let modulus = MessageModulus(3);
///
/// // Encryption of one message:
/// let ct = pk.encrypt_native_crt(msg, modulus);
///
/// // Decryption:
/// let dec = cks.decrypt_message_native_crt(&ct, modulus);
/// assert_eq!(msg, dec % modulus.0);
/// ```
pub fn encrypt_native_crt(&self, message: u64, message_modulus: MessageModulus) -> Ciphertext {
ShortintEngine::with_thread_local_mut(|engine| {
engine.encrypt_native_crt_with_public_key(self, message, message_modulus)
})
}
}
impl CompressedPublicKey {
pub fn decompress(&self) -> PublicKey {
let parameters = self.parameters;
let decompressed_public_key = self
.lwe_public_key
.as_view()
.par_decompress_into_lwe_public_key();
PublicKey {
lwe_public_key: decompressed_public_key,
parameters,
atomic_pattern: self.atomic_pattern,
}
}
}