tfhe/shortint/mod.rs
1//! # Description
2//!
3//! This library makes it possible to execute modular operations over encrypted short integer.
4//!
5//! It allows to execute an integer circuit on an untrusted server because both circuit inputs and
6//! outputs are kept private.
7//!
8//! Data are encrypted on the client side, before being sent to the server.
9//! On the server side every computation is performed on ciphertexts.
10//!
11//! The server however, has to know the integer circuit to be evaluated.
12//! At the end of the computation, the server returns the encryption of the result to the user.
13//!
14//! # Keys
15//!
16//! This crates exposes two type of keys:
17//! * The [`ClientKey`](crate::shortint::client_key::ClientKey) is used to encrypt and decrypt and
18//! has to be kept secret;
19//! * The [`ServerKey`](crate::shortint::server_key::ServerKey) is used to perform homomorphic
20//! operations on the server side and it is meant to be published (the client sends it to the
21//! server).
22//!
23//!
24//! # Quick Example
25//!
26//! The following piece of code shows how to generate keys and run a small integer circuit
27//! homomorphically.
28//!
29//! ```rust
30//! use tfhe::shortint::gen_keys;
31//! use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
32//!
33//! // We generate a set of client/server keys, using the default parameters:
34//! let (client_key, server_key) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
35//!
36//! let msg1 = 1;
37//! let msg2 = 0;
38//!
39//! // We use the client key to encrypt two messages:
40//! let ct_1 = client_key.encrypt(msg1);
41//! let ct_2 = client_key.encrypt(msg2);
42//!
43//! // We use the server public key to execute an integer circuit:
44//! let ct_3 = server_key.unchecked_add(&ct_1, &ct_2);
45//!
46//! // We use the client key to decrypt the output of the circuit:
47//! let output = client_key.decrypt(&ct_3);
48//! assert_eq!(output, 1);
49//! ```
50pub mod atomic_pattern;
51pub mod backward_compatibility;
52pub mod ciphertext;
53pub mod client_key;
54pub(crate) mod encoding;
55pub mod engine;
56pub mod key_switching_key;
57#[cfg(any(test, doctest, feature = "internal-keycache"))]
58pub mod keycache;
59pub mod list_compression;
60pub mod noise_squashing;
61pub mod oprf;
62pub mod parameters;
63pub mod prelude;
64pub mod public_key;
65pub mod server_key;
66#[cfg(feature = "experimental")]
67pub mod wopbs;
68#[cfg(all(
69 not(feature = "experimental"),
70 any(test, doctest, feature = "internal-keycache")
71))]
72pub(crate) mod wopbs;
73
74pub use ciphertext::{Ciphertext, CompressedCiphertext, PBSOrder};
75pub use client_key::ClientKey;
76pub(crate) use encoding::{PaddingBit, ShortintEncoding};
77pub use key_switching_key::{CompressedKeySwitchingKey, KeySwitchingKey, KeySwitchingKeyView};
78pub use parameters::{
79 AtomicPatternKind, AtomicPatternParameters, CarryModulus, CiphertextModulus,
80 ClassicPBSParameters, EncryptionKeyChoice, MaxNoiseLevel, MessageModulus,
81 MultiBitPBSParameters, PBSParameters, ShortintParameterSet, WopbsParameters,
82};
83pub use public_key::{
84 CompactPrivateKey, CompactPublicKey, CompressedCompactPublicKey, CompressedPublicKey, PublicKey,
85};
86pub use server_key::{CheckError, CompressedServerKey, ServerKey};
87
88/// Generate a couple of client and server keys.
89///
90/// # Example
91///
92/// Generating a pair of [ClientKey] and [ServerKey] using the default parameters.
93///
94/// ```rust
95/// use tfhe::shortint::gen_keys;
96/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
97///
98/// // generate the client key and the server key:
99/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
100/// ```
101pub fn gen_keys<P>(parameters_set: P) -> (ClientKey, ServerKey)
102where
103 P: TryInto<ShortintParameterSet>,
104 <P as TryInto<ShortintParameterSet>>::Error: std::fmt::Debug,
105{
106 let cks = ClientKey::new(parameters_set);
107 let sks = ServerKey::new(&cks);
108
109 (cks, sks)
110}