-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cdc805
commit 35b2ee0
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
tfhe/src/integer/ciphertext/compressed_modulus_switched_ciphertext.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters