Skip to content

Commit

Permalink
refactor(core): make GGSW encryption consistent
Browse files Browse the repository at this point in the history
- functions take un-encoded values, reflect that by taking Cleartext
instead of Plaintext
  • Loading branch information
IceTDrinker committed Jul 24, 2024
1 parent ebb11b1 commit 91f05b0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 78 deletions.
6 changes: 3 additions & 3 deletions tfhe/src/c_api/core_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub unsafe extern "C" fn core_crypto_lwe_encrypt(
#[no_mangle]
pub unsafe extern "C" fn core_crypto_ggsw_encrypt(
output_ct_ptr: *mut u64,
pt: u64,
cleartext: u64,
glwe_sk_ptr: *const u64,
glwe_sk_dim: usize,
poly_size: usize,
Expand Down Expand Up @@ -225,7 +225,7 @@ pub unsafe extern "C" fn core_crypto_ggsw_encrypt(
&mut deterministic_seeder,
);

let plaintext = Plaintext(pt);
let cleartext = Cleartext(cleartext);
let output_ct = std::slice::from_raw_parts_mut(
output_ct_ptr,
ggsw_ciphertext_size(
Expand All @@ -248,7 +248,7 @@ pub unsafe extern "C" fn core_crypto_ggsw_encrypt(
encrypt_constant_ggsw_ciphertext(
&glwe_sk,
&mut ct,
plaintext,
cleartext,
glwe_noise_distribution,
&mut encryption_generator,
);
Expand Down
92 changes: 47 additions & 45 deletions tfhe/src/core_crypto/algorithms/ggsw_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ pub fn ggsw_encryption_multiplicative_factor<Scalar: UnsignedInteger>(
ciphertext_modulus: CiphertextModulus<Scalar>,
decomp_level: DecompositionLevel,
decomp_base_log: DecompositionBaseLog,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
) -> Scalar {
match ciphertext_modulus.kind() {
CiphertextModulusKind::Other => DecompositionTermNonNative::new(
decomp_level,
decomp_base_log,
encoded.0.wrapping_neg(),
cleartext.0.wrapping_neg(),
ciphertext_modulus,
)
.to_approximate_recomposition_summand(),
CiphertextModulusKind::Native | CiphertextModulusKind::NonNativePowerOfTwo => {
let native_decomp_term =
DecompositionTerm::new(decomp_level, decomp_base_log, encoded.0.wrapping_neg())
DecompositionTerm::new(decomp_level, decomp_base_log, cleartext.0.wrapping_neg())
.to_recomposition_summand();
// We scale the factor down from the native torus to whatever our power of 2 torus is,
// the encryption process will scale it back up
Expand Down Expand Up @@ -77,8 +77,8 @@ pub fn ggsw_encryption_multiplicative_factor<Scalar: UnsignedInteger>(
/// &mut secret_generator,
/// );
///
/// // Create the plaintext
/// let plaintext = Plaintext(3u64);
/// // Create the cleartext
/// let cleartext = Cleartext(3u64);
///
/// // Create a new GgswCiphertext
/// let mut ggsw = GgswCiphertext::new(
Expand All @@ -93,18 +93,18 @@ pub fn ggsw_encryption_multiplicative_factor<Scalar: UnsignedInteger>(
/// encrypt_constant_ggsw_ciphertext(
/// &glwe_secret_key,
/// &mut ggsw,
/// plaintext,
/// cleartext,
/// glwe_noise_distribution,
/// &mut encryption_generator,
/// );
///
/// let decrypted = decrypt_constant_ggsw_ciphertext(&glwe_secret_key, &ggsw);
/// assert_eq!(decrypted, plaintext);
/// assert_eq!(decrypted, cleartext);
/// ```
pub fn encrypt_constant_ggsw_ciphertext<Scalar, NoiseDistribution, KeyCont, OutputCont, Gen>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut GgswCiphertext<OutputCont>,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
noise_distribution: NoiseDistribution,
generator: &mut EncryptionRandomGenerator<Gen>,
) where
Expand Down Expand Up @@ -146,7 +146,7 @@ pub fn encrypt_constant_ggsw_ciphertext<Scalar, NoiseDistribution, KeyCont, Outp
ciphertext_modulus,
decomp_level,
decomp_base_log,
encoded,
cleartext,
);

// We iterate over the rows of the level matrix, the last row needs special treatment
Expand Down Expand Up @@ -210,8 +210,8 @@ pub fn encrypt_constant_ggsw_ciphertext<Scalar, NoiseDistribution, KeyCont, Outp
/// &mut secret_generator,
/// );
///
/// // Create the plaintext
/// let plaintext = Plaintext(3u64);
/// // Create the cleartext
/// let cleartext = Cleartext(3u64);
///
/// // Create a new GgswCiphertext
/// let mut ggsw = GgswCiphertext::new(
Expand All @@ -226,18 +226,18 @@ pub fn encrypt_constant_ggsw_ciphertext<Scalar, NoiseDistribution, KeyCont, Outp
/// par_encrypt_constant_ggsw_ciphertext(
/// &glwe_secret_key,
/// &mut ggsw,
/// plaintext,
/// cleartext,
/// glwe_noise_distribution,
/// &mut encryption_generator,
/// );
///
/// let decrypted = decrypt_constant_ggsw_ciphertext(&glwe_secret_key, &ggsw);
/// assert_eq!(decrypted, plaintext);
/// assert_eq!(decrypted, cleartext);
/// ```
pub fn par_encrypt_constant_ggsw_ciphertext<Scalar, NoiseDistribution, KeyCont, OutputCont, Gen>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut GgswCiphertext<OutputCont>,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
noise_distribution: NoiseDistribution,
generator: &mut EncryptionRandomGenerator<Gen>,
) where
Expand Down Expand Up @@ -278,11 +278,10 @@ pub fn par_encrypt_constant_ggsw_ciphertext<Scalar, NoiseDistribution, KeyCont,
ciphertext_modulus,
decomp_level,
decomp_base_log,
encoded,
cleartext,
);

// We iterate over the rows of the level matrix, the last row needs special
// treatment
// We iterate over the rows of the level matrix, the last row needs special treatment
let gen_iter = generator
.par_try_fork_from_config(
level_matrix.encryption_fork_config(Uniform, noise_distribution),
Expand Down Expand Up @@ -386,7 +385,7 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut SeededGgswCiphertext<OutputCont>,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
noise_distribution: NoiseDistribution,
generator: &mut EncryptionRandomGenerator<Gen>,
) where
Expand All @@ -412,7 +411,7 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
ciphertext_modulus,
decomp_level,
decomp_base_log,
encoded,
cleartext,
);

// We iterate over the rows of the level matrix, the last row needs special treatment
Expand Down Expand Up @@ -440,7 +439,7 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
}
}

/// Encrypt a plaintext in a [`seeded GGSW ciphertext`](`SeededGgswCiphertext`) in the constant
/// Encrypt a cleartext in a [`seeded GGSW ciphertext`](`SeededGgswCiphertext`) in the constant
/// coefficient.
///
/// See the [`formal definition`](`GgswCiphertext#ggsw-encryption`) for the definition of the
Expand Down Expand Up @@ -475,9 +474,8 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
/// &mut secret_generator,
/// );
///
/// // Create the plaintext
/// let encoded_msg = 3u64 << 60;
/// let plaintext = Plaintext(encoded_msg);
/// // Create the cleartext
/// let cleartext = Cleartext(3u64);
///
/// // Create a new GgswCiphertext
/// let mut ggsw = SeededGgswCiphertext::new(
Expand All @@ -493,10 +491,15 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
/// encrypt_constant_seeded_ggsw_ciphertext(
/// &glwe_secret_key,
/// &mut ggsw,
/// plaintext,
/// cleartext,
/// glwe_noise_distribution,
/// seeder,
/// );
///
/// let ggsw = ggsw.decompress_into_ggsw_ciphertext();
///
/// let decrypted = decrypt_constant_ggsw_ciphertext(&glwe_secret_key, &ggsw);
/// assert_eq!(decrypted, cleartext);
/// ```
pub fn encrypt_constant_seeded_ggsw_ciphertext<
Scalar,
Expand All @@ -507,7 +510,7 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext<
>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut SeededGgswCiphertext<OutputCont>,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
noise_distribution: NoiseDistribution,
noise_seeder: &mut NoiseSeeder,
) where
Expand Down Expand Up @@ -542,7 +545,7 @@ pub fn encrypt_constant_seeded_ggsw_ciphertext<
encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator(
glwe_secret_key,
output,
encoded,
cleartext,
noise_distribution,
&mut generator,
);
Expand All @@ -564,7 +567,7 @@ pub fn par_encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut SeededGgswCiphertext<OutputCont>,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
noise_distribution: NoiseDistribution,
generator: &mut EncryptionRandomGenerator<Gen>,
) where
Expand All @@ -589,7 +592,7 @@ pub fn par_encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
ciphertext_modulus,
decomp_level,
decomp_base_log,
encoded,
cleartext,
);

// We iterate over the rows of the level matrix, the last row needs special treatment
Expand Down Expand Up @@ -656,9 +659,8 @@ pub fn par_encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
/// &mut secret_generator,
/// );
///
/// // Create the plaintext
/// let encoded_msg = 3u64 << 60;
/// let plaintext = Plaintext(encoded_msg);
/// // Create the cleartext
/// let cleartext = Cleartext(3u64);
///
/// // Create a new GgswCiphertext
/// let mut ggsw = SeededGgswCiphertext::new(
Expand All @@ -674,7 +676,7 @@ pub fn par_encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator<
/// par_encrypt_constant_seeded_ggsw_ciphertext(
/// &glwe_secret_key,
/// &mut ggsw,
/// plaintext,
/// cleartext,
/// glwe_noise_distribution,
/// seeder,
/// );
Expand All @@ -688,7 +690,7 @@ pub fn par_encrypt_constant_seeded_ggsw_ciphertext<
>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut SeededGgswCiphertext<OutputCont>,
encoded: Plaintext<Scalar>,
cleartext: Cleartext<Scalar>,
noise_distribution: NoiseDistribution,
noise_seeder: &mut NoiseSeeder,
) where
Expand Down Expand Up @@ -723,7 +725,7 @@ pub fn par_encrypt_constant_seeded_ggsw_ciphertext<
par_encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator(
glwe_secret_key,
output,
encoded,
cleartext,
noise_distribution,
&mut generator,
);
Expand Down Expand Up @@ -800,7 +802,7 @@ fn encrypt_constant_seeded_ggsw_level_matrix_row<
);
}

/// Decrypt a [`GGSW ciphertext`](`GgswCiphertext`) only yielding the plaintext from the constant
/// Decrypt a [`GGSW ciphertext`](`GgswCiphertext`) only yielding the cleartext from the constant
/// term of the polynomial.
///
/// # Example
Expand Down Expand Up @@ -834,8 +836,8 @@ fn encrypt_constant_seeded_ggsw_level_matrix_row<
/// &mut secret_generator,
/// );
///
/// // Create the plaintext
/// let plaintext = Plaintext(3u64);
/// // Create the cleartext
/// let cleartext = Cleartext(3u64);
///
/// // Create a new GgswCiphertext
/// let mut ggsw = GgswCiphertext::new(
Expand All @@ -850,18 +852,18 @@ fn encrypt_constant_seeded_ggsw_level_matrix_row<
/// par_encrypt_constant_ggsw_ciphertext(
/// &glwe_secret_key,
/// &mut ggsw,
/// plaintext,
/// cleartext,
/// glwe_noise_distribution,
/// &mut encryption_generator,
/// );
///
/// let decrypted = decrypt_constant_ggsw_ciphertext(&glwe_secret_key, &ggsw);
/// assert_eq!(decrypted, plaintext);
/// assert_eq!(decrypted, cleartext);
/// ```
pub fn decrypt_constant_ggsw_ciphertext<Scalar, KeyCont, InputCont>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
ggsw_ciphertext: &GgswCiphertext<InputCont>,
) -> Plaintext<Scalar>
) -> Cleartext<Scalar>
where
Scalar: UnsignedTorus,
KeyCont: Container<Element = Scalar>,
Expand Down Expand Up @@ -897,7 +899,7 @@ where

let decomp_base_log = ggsw_ciphertext.decomposition_base_log();

let plaintext_ref = decrypted_plaintext_list.get(0);
let cleartext_ref = decrypted_plaintext_list.get(0);

let ciphertext_modulus = ggsw_ciphertext.ciphertext_modulus();

Expand All @@ -911,23 +913,23 @@ where
)
.to_approximate_recomposition_summand();

let decoded = divide_round(*plaintext_ref.0, delta)
let decoded = divide_round(*cleartext_ref.0, delta)
.wrapping_rem(Scalar::ONE << (decomp_level.0 * decomp_base_log.0));

Plaintext(decoded)
Cleartext(decoded)
}
CiphertextModulusKind::Native | CiphertextModulusKind::NonNativePowerOfTwo => {
let decomposer = SignedDecomposer::new(decomp_base_log, decomp_level);

// Glwe decryption maps to a smaller torus potentially, map back to the native torus
let rounded = decomposer.closest_representable(
(*plaintext_ref.0)
(*cleartext_ref.0)
.wrapping_mul(ciphertext_modulus.get_power_of_two_scaling_to_native_torus()),
);
let decoded = rounded
.wrapping_div(Scalar::ONE << (Scalar::BITS - (decomp_base_log.0 * decomp_level.0)));

Plaintext(decoded)
Cleartext(decoded)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn generate_lwe_bootstrap_key<
encrypt_constant_ggsw_ciphertext(
output_glwe_secret_key,
&mut ggsw,
Plaintext(input_key_element),
Cleartext(input_key_element),
noise_distribution,
&mut generator,
);
Expand Down Expand Up @@ -304,7 +304,7 @@ pub fn par_generate_lwe_bootstrap_key<
par_encrypt_constant_ggsw_ciphertext(
output_glwe_secret_key,
&mut ggsw,
Plaintext(input_key_element),
Cleartext(input_key_element),
noise_distribution,
&mut generator,
);
Expand Down Expand Up @@ -426,7 +426,7 @@ pub fn generate_seeded_lwe_bootstrap_key<
encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator(
output_glwe_secret_key,
&mut ggsw,
Plaintext(input_key_element),
Cleartext(input_key_element),
noise_distribution,
&mut generator,
);
Expand Down Expand Up @@ -549,7 +549,7 @@ pub fn par_generate_seeded_lwe_bootstrap_key<
par_encrypt_constant_seeded_ggsw_ciphertext_with_existing_generator(
output_glwe_secret_key,
&mut ggsw,
Plaintext(input_key_element),
Cleartext(input_key_element),
noise_distribution,
&mut generator,
);
Expand Down
Loading

0 comments on commit 91f05b0

Please sign in to comment.