Skip to content

Commit

Permalink
integer
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Mar 19, 2024
1 parent 3cdc805 commit 35b2ee0
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
135 changes: 135 additions & 0 deletions tfhe/src/integer/ciphertext/compressed_modulus_switched_ciphertext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use super::{
BaseRadixCiphertext, BaseSignedRadixCiphertext, RadixCiphertext, SignedRadixCiphertext,
};
use crate::integer::ServerKey;
use crate::shortint::ciphertext::CompressedModulusSwitchedCiphertext;

/// An object to store a ciphertext in little memory.
/// Decompressing it requires a PBS
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
/// use tfhe::shortint::PBSParameters;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix::<PBSParameters>(PARAM_MESSAGE_2_CARRY_2_KS_PBS.into(), size);
///
/// let clear = 3u8;
///
/// let ctxt = cks.encrypt(clear);
///
/// let compressed_ct = sks.switch_modulus_and_pack(&ctxt);
///
/// let uncompressed_ct = sks.unpack(&compressed_ct);
///
/// let dec = cks.decrypt(&uncompressed_ct);
///
/// assert_eq!(clear, dec);
/// ```
pub type CompressedModulusSwitchedRadixCiphertext =
BaseRadixCiphertext<CompressedModulusSwitchedCiphertext>;

impl ServerKey {
// Packs a ciphertext to have a smaller serialization size
pub fn switch_modulus_and_compress(
&self,
ct: &RadixCiphertext,
) -> CompressedModulusSwitchedRadixCiphertext {
let blocks = ct
.blocks
.iter()
.map(|a| self.key.switch_modulus_and_compress(a))
.collect();

BaseRadixCiphertext { blocks }
}
// Unpacks a compressed ciphertext
// This operation costs a PBS
pub fn decompress(
&self,
compressed_ct: &CompressedModulusSwitchedRadixCiphertext,
) -> RadixCiphertext {
let blocks = compressed_ct
.blocks
.iter()
.map(|a| self.key.decompress(a))
.collect();

BaseRadixCiphertext { blocks }
}
}

pub type CompressedModulusSwitchedSignedRadixCiphertext =
BaseSignedRadixCiphertext<CompressedModulusSwitchedCiphertext>;

impl ServerKey {
// Packs a ciphertext to have a smaller serialization size
pub fn switch_modulus_and_compress_signed(
&self,
ct: &SignedRadixCiphertext,
) -> CompressedModulusSwitchedSignedRadixCiphertext {
let blocks = ct
.blocks
.iter()
.map(|a| self.key.switch_modulus_and_compress(a))
.collect();

BaseSignedRadixCiphertext { blocks }
}
// Unpacks a compressed ciphertext
// This operation costs a PBS
pub fn uncompress_signed(
&self,
compressed_ct: &CompressedModulusSwitchedSignedRadixCiphertext,
) -> SignedRadixCiphertext {
let blocks = compressed_ct
.blocks
.iter()
.map(|a| self.key.decompress(a))
.collect();

BaseSignedRadixCiphertext { blocks }
}
}

#[cfg(test)]
mod test {
use rand::Rng;

use crate::integer::gen_keys_radix;
use crate::integer::tests::create_parametrized_test;
use crate::shortint::parameters::*;

const NB_TESTS: usize = 10;

create_parametrized_test!(modulus_switch_compression);

fn modulus_switch_compression<P>(param: P)
where
P: Into<PBSParameters>,
{
// We have 4 * 2 = 8 bits of message
let size = 4;
let (cks, sks) = gen_keys_radix(param.into(), size);

let mut rng = rand::thread_rng();

for _ in 0..NB_TESTS {
let clear = rng.gen::<u8>();

let ctxt = cks.encrypt(clear);

let compressed_ct = sks.switch_modulus_and_compress(&ctxt);

let uncompressed_ct = sks.decompress(&compressed_ct);

let dec: u8 = cks.decrypt(&uncompressed_ct);

assert_eq!(clear, dec);
}
}
}
2 changes: 2 additions & 0 deletions tfhe/src/integer/ciphertext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ mod base;
pub mod boolean_value;
mod compact_list;
mod compressed;
mod compressed_modulus_switched_ciphertext;
mod utils;

pub use base::*;
pub use boolean_value::*;
pub use compact_list::*;
pub use compressed::*;
pub use compressed_modulus_switched_ciphertext::*;
pub use utils::*;

0 comments on commit 35b2ee0

Please sign in to comment.