Skip to content

Commit

Permalink
chore(integer): restore assert after using 3_3 params for CRT doctests
Browse files Browse the repository at this point in the history
- fix max degree for CRT keys which don't need to propagate carries

BREAKING CHANGE:
pub API removed from pub interface
  • Loading branch information
IceTDrinker committed Nov 9, 2023
1 parent cf7a7f1 commit 9ac0c63
Show file tree
Hide file tree
Showing 27 changed files with 452 additions and 417 deletions.
2 changes: 1 addition & 1 deletion tfhe/src/high_level_api/integers/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl IntegerServerKey {
4,
"This API only supports integers with 2 bits per block (MessageModulus(4))",
);
let base_integer_key = crate::integer::ServerKey::new(cks);
let base_integer_key = crate::integer::ServerKey::new_radix_server_key(cks);
let wopbs_key = client_key
.wopbs_block_parameters
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion tfhe/src/high_level_api/integers/types/static_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ where
}
#[cfg(not(feature = "internal-keycache"))]
{
Self::new(client_key)
Self::new_radix_server_key(client_key)
}
}

Expand Down
8 changes: 8 additions & 0 deletions tfhe/src/integer/client_key/crt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ impl CrtClientKey {
self.key.encrypt_crt(message, self.moduli.clone())
}

pub fn encrypt_native_crt(&self, message: u64) -> CrtCiphertext {
self.key.encrypt_native_crt(message, self.moduli.clone())
}

pub fn decrypt(&self, ciphertext: &CrtCiphertext) -> u64 {
self.key.decrypt_crt(ciphertext)
}

pub fn decrypt_native_crt(&self, ciphertext: &CrtCiphertext) -> u64 {
self.key.decrypt_native_crt(ciphertext)
}

/// Returns the parameters used by the client key.
pub fn parameters(&self) -> crate::shortint::PBSParameters {
self.key.parameters()
Expand Down
12 changes: 9 additions & 3 deletions tfhe/src/integer/client_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ impl AsRef<Self> for ClientKey {
}
}

impl AsRef<ShortintClientKey> for ClientKey {
fn as_ref(&self) -> &ShortintClientKey {
&self.key
}
}

impl ClientKey {
/// Creates a Client Key.
///
Expand Down Expand Up @@ -431,19 +437,19 @@ impl ClientKey {
/// # Example
///
/// ```
/// use tfhe::integer::{gen_keys, BooleanBlock};
/// use tfhe::integer::{gen_keys_radix, BooleanBlock};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, size);
///
/// let a = cks.encrypt_bool(false);
/// let dec = cks.decrypt_bool(&a);
/// assert_eq!(dec, false);
///
/// let a = a.into_radix(size, &sks);
/// let dec: u64 = cks.decrypt_radix(&a);
/// let dec: u64 = cks.decrypt(&a);
/// assert_eq!(dec, 0);
/// ```
pub fn encrypt_bool(&self, msg: bool) -> BooleanBlock {
Expand Down
15 changes: 15 additions & 0 deletions tfhe/src/integer/client_key/radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ impl RadixClientKey {
self.key.encrypt_radix(message, self.num_blocks)
}

pub fn encrypt_without_padding<T: DecomposableInto<u64> + UnsignedNumeric>(
&self,
message: T,
) -> RadixCiphertext {
self.key
.encrypt_radix_without_padding(message, self.num_blocks)
}

pub fn encrypt_signed<T: DecomposableInto<u64> + SignedNumeric>(
&self,
message: T,
Expand All @@ -79,6 +87,13 @@ impl RadixClientKey {
self.key.decrypt_radix(ciphertext)
}

pub fn decrypt_without_padding<T>(&self, ctxt: &RadixCiphertext) -> T
where
T: RecomposableFrom<u64> + UnsignedNumeric,
{
self.key.decrypt_radix_without_padding(ctxt)
}

pub fn decrypt_signed<T>(&self, ciphertext: &SignedRadixCiphertext) -> T
where
T: RecomposableSignedInteger,
Expand Down
7 changes: 5 additions & 2 deletions tfhe/src/integer/key_switching_key/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::shortint::parameters::ShortintKeySwitchingParameters;
use crate::shortint::prelude::{PARAM_MESSAGE_1_CARRY_1_KS_PBS, PARAM_MESSAGE_2_CARRY_2_KS_PBS};

use crate::integer::key_switching_key::KeySwitchingKey;
use crate::integer::IntegerKeyKind;

#[test]
fn gen_multi_keys_test_rdxinteger_to_rdxinteger() {
Expand Down Expand Up @@ -97,10 +98,12 @@ fn gen_multi_keys_test_crtinteger_to_crtinteger_fail() {
#[test]
fn gen_multi_keys_test_integer_to_integer() {
// We generate a set of client/server keys, using the default parameters:
let (client_key_1, server_key_1) = crate::integer::gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
let (client_key_1, server_key_1) =
crate::integer::gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix);

// We generate a set of client/server keys, using the default parameters:
let (client_key_2, server_key_2) = crate::integer::gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
let (client_key_2, server_key_2) =
crate::integer::gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS, IntegerKeyKind::Radix);

// Get casting key
let ksk_params = ShortintKeySwitchingParameters::new(
Expand Down
52 changes: 26 additions & 26 deletions tfhe/src/integer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
//! //4 blocks for the radix decomposition
//! let number_of_blocks = 4;
//! // Modulus = (2^2)*4 = 2^8 (from the parameters chosen and the number of blocks
//! let modulus = 1 << 8;
//! let modulus = 1u64 << 8;
//!
//! // Generation of the client/server keys, using the default parameters:
//! let (mut client_key, mut server_key) =
//! gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, number_of_blocks);
//!
//! let msg1 = 153;
//! let msg2 = 125;
//! let msg1 = 153u64;
//! let msg2 = 125u64;
//!
//! // Encryption of two messages using the client key:
//! let ct_1 = client_key.encrypt(msg1);
Expand All @@ -37,7 +37,7 @@
//! let ct_3 = server_key.unchecked_add(&ct_1, &ct_2);
//!
//! // Decryption of the ciphertext using the client key:
//! let output = client_key.decrypt(&ct_3);
//! let output: u64 = client_key.decrypt(&ct_3);
//! assert_eq!(output, (msg1 + msg2) % modulus);
//! ```
//!
Expand Down Expand Up @@ -76,6 +76,14 @@ pub use client_key::{ClientKey, CrtClientKey, RadixClientKey};
pub use public_key::{CompressedCompactPublicKey, CompressedPublicKey, PublicKey};
pub use server_key::{CheckError, CompressedServerKey, ServerKey};

/// Enum to indicate which kind of computations the [`ServerKey`] will be performing, this changes
/// the parameterization of the key to manage carries in the Radix case.
#[derive(Clone, Copy, Debug)]
pub enum IntegerKeyKind {
Radix,
CRT,
}

/// Unless you know what you are doing you are likely looking for [`gen_keys_radix`] or
/// [`gen_keys_crt`].
///
Expand All @@ -84,15 +92,7 @@ pub use server_key::{CheckError, CompressedServerKey, ServerKey};
/// * the client key is used to encrypt and decrypt and has to be kept secret;
/// * the server key is used to perform homomorphic operations on the server side and it is meant to
/// be published (the client sends it to the server).
///
/// ```rust
/// use tfhe::integer::gen_keys;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
///
/// // generate the client key and the server key:
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
/// ```
pub fn gen_keys<P>(parameters_set: P) -> (ClientKey, ServerKey)
pub(crate) fn gen_keys<P>(parameters_set: P, key_kind: IntegerKeyKind) -> (ClientKey, ServerKey)
where
P: TryInto<crate::shortint::parameters::ShortintParameterSet>,
<P as TryInto<crate::shortint::parameters::ShortintParameterSet>>::Error: std::fmt::Debug,
Expand Down Expand Up @@ -133,7 +133,10 @@ where

let gen_keys_inner = |parameters_set| {
let cks = ClientKey::new(parameters_set);
let sks = ServerKey::new(&cks);
let sks = match key_kind {
IntegerKeyKind::Radix => ServerKey::new_radix_server_key(&cks),
IntegerKeyKind::CRT => ServerKey::new_crt_server_key(&cks),
};

(cks, sks)
};
Expand All @@ -154,9 +157,7 @@ where
}
}

/// Generate a couple of client and server keys with given parameters
///
/// Contrary to [gen_keys], this returns a [RadixClientKey]
/// Generate a couple of client and server keys with given parameters.
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
Expand All @@ -171,14 +172,12 @@ where
P: TryInto<crate::shortint::parameters::ShortintParameterSet>,
<P as TryInto<crate::shortint::parameters::ShortintParameterSet>>::Error: std::fmt::Debug,
{
let (cks, sks) = gen_keys(parameters_set);
let (cks, sks) = gen_keys(parameters_set, IntegerKeyKind::Radix);

(RadixClientKey::from((cks, num_blocks)), sks)
}

/// Generate a couple of client and server keys with given parameters
///
/// Contrary to [gen_keys], this returns a [CrtClientKey]
/// Generate a couple of client and server keys with given parameters.
///
/// ```rust
/// use tfhe::integer::gen_keys_crt;
Expand All @@ -188,11 +187,12 @@ where
/// let basis = vec![2, 3, 5];
/// let (cks, sks) = gen_keys_crt(PARAM_MESSAGE_2_CARRY_2_KS_PBS, basis);
/// ```
pub fn gen_keys_crt(
parameters_set: crate::shortint::parameters::ClassicPBSParameters,
basis: Vec<u64>,
) -> (CrtClientKey, ServerKey) {
let (cks, sks) = gen_keys(parameters_set);
pub fn gen_keys_crt<P>(parameters_set: P, basis: Vec<u64>) -> (CrtClientKey, ServerKey)
where
P: TryInto<crate::shortint::parameters::ShortintParameterSet>,
<P as TryInto<crate::shortint::parameters::ShortintParameterSet>>::Error: std::fmt::Debug,
{
let (cks, sks) = gen_keys(parameters_set, IntegerKeyKind::CRT);

(CrtClientKey::from((cks, basis)), sks)
}
4 changes: 2 additions & 2 deletions tfhe/src/integer/public_key/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rand::Rng;

use crate::integer::{gen_keys, CompressedPublicKey, PublicKey};
use crate::integer::{gen_keys, CompressedPublicKey, IntegerKeyKind, PublicKey};
use crate::shortint::parameters::*;
use crate::shortint::ClassicPBSParameters;

Expand Down Expand Up @@ -78,7 +78,7 @@ fn small_radix_encrypt_decrypt_compact_128_bits_list(params: ClassicPBSParameter
}

fn radix_encrypt_decrypt_compact_128_bits_list(params: ClassicPBSParameters) {
let (cks, _) = gen_keys(params);
let (cks, _) = gen_keys(params, IntegerKeyKind::Radix);
let pk = crate::integer::public_key::CompactPublicKey::new(&cks);

let mut rng = rand::thread_rng();
Expand Down
14 changes: 7 additions & 7 deletions tfhe/src/integer/server_key/crt/add_crt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ impl ServerKey {
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys;
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_3_CARRY_3_KS_PBS);
/// 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, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let mut ctxt_2 = cks.encrypt_crt(clear_2, basis);
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let mut ctxt_2 = cks.encrypt(clear_2);
///
/// sks.smart_crt_add_assign(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt_1);
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn smart_crt_add(
Expand Down
14 changes: 7 additions & 7 deletions tfhe/src/integer/server_key/crt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ impl ServerKey {
/// # Example
///
///```rust
/// use tfhe::integer::gen_keys;
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_3_CARRY_3_KS_PBS);
/// 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, basis);
///
/// let clear_1 = 14;
/// let clear_2 = 14;
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let ctxt_2 = cks.encrypt_crt(clear_2, basis);
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// sks.unchecked_crt_add_assign(&mut ctxt_1, &ctxt_2);
///
/// sks.full_extract_message_assign(&mut ctxt_1);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt_1);
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 + clear_2) % modulus, res);
/// ```
pub fn full_extract_message_assign(&self, ctxt: &mut CrtCiphertext) {
Expand Down
28 changes: 14 additions & 14 deletions tfhe/src/integer/server_key/crt/mul_crt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ impl ServerKey {
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys;
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_3_CARRY_3_KS_PBS);
/// 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, basis);
///
/// let clear_1 = 29;
/// let clear_2 = 23;
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let ctxt_2 = cks.encrypt_crt(clear_2, basis);
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// sks.unchecked_crt_mul_assign(&mut ctxt_1, &ctxt_2);
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt_1);
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 * clear_2) % modulus, res);
/// ```
pub fn unchecked_crt_mul_assign(&self, ct_left: &mut CrtCiphertext, ct_right: &CrtCiphertext) {
Expand Down Expand Up @@ -57,24 +57,24 @@ impl ServerKey {
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys;
/// use tfhe::integer::gen_keys_crt;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_3_CARRY_3_KS_PBS;
///
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_3_CARRY_3_KS_PBS);
/// 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, basis);
///
/// let clear_1 = 29;
/// let clear_2 = 29;
/// let basis = vec![2, 3, 5];
/// let modulus: u64 = basis.iter().product();
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let mut ctxt_2 = cks.encrypt_crt(clear_2, basis);
/// let mut ctxt_1 = cks.encrypt(clear_1);
/// let mut ctxt_2 = cks.encrypt(clear_2);
///
/// // Compute homomorphically a multiplication
/// sks.smart_crt_mul_assign(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt_1);
/// let res = cks.decrypt(&ctxt_1);
/// assert_eq!((clear_1 * clear_2) % modulus, res);
/// ```
pub fn smart_crt_mul_assign(&self, ct_left: &mut CrtCiphertext, ct_right: &mut CrtCiphertext) {
Expand Down
Loading

0 comments on commit 9ac0c63

Please sign in to comment.