Skip to content

Commit

Permalink
refactor(all): decompress takes shared reference
Browse files Browse the repository at this point in the history
remove from/into decompression
  • Loading branch information
mayeul-zama committed Mar 28, 2024
1 parent a11d690 commit c2c2bda
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion tfhe/src/c_api/high_level_api/integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ macro_rules! create_integer_wrapper_type {
$crate::c_api::utils::catch_panic(|| {
let compressed = $crate::c_api::utils::get_ref_checked(sself).unwrap();

let decompressed_inner = compressed.0.clone().into();
let decompressed_inner = compressed.0.decompress();
*result = Box::into_raw(Box::new($name(decompressed_inner)));
})
}
Expand Down
2 changes: 1 addition & 1 deletion tfhe/src/c_api/shortint/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub unsafe extern "C" fn shortint_decompress_ciphertext(

let compressed_ciphertext = get_ref_checked(compressed_ciphertext).unwrap();

let ciphertext = compressed_ciphertext.0.clone().into();
let ciphertext = compressed_ciphertext.0.decompress();

let heap_allocated_ciphertext = Box::new(ShortintCiphertext(ciphertext));

Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/core_crypto/entities/seeded_lwe_ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ impl<Scalar: UnsignedInteger> SeededLweCiphertext<Scalar> {
/// [`LweCiphertext`].
///
/// See [`SeededLweCiphertext::from_scalar`] for usage.
pub fn decompress_into_lwe_ciphertext(self) -> LweCiphertextOwned<Scalar>
pub fn decompress_into_lwe_ciphertext(&self) -> LweCiphertextOwned<Scalar>
where
Scalar: UnsignedTorus,
{
let mut decompressed_ct =
LweCiphertext::new(Scalar::ZERO, self.lwe_size(), self.ciphertext_modulus());
decompress_seeded_lwe_ciphertext::<_, _, ActivatedRandomGenerator>(
&mut decompressed_ct,
&self,
self,
);
decompressed_ct
}
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/high_level_api/integers/signed/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ where
/// Decompress to a [FheInt]
///
/// See [CompressedFheInt] example.
pub fn decompress(self) -> FheInt<Id> {
let inner = self.ciphertext.into();
pub fn decompress(&self) -> FheInt<Id> {
let inner = self.ciphertext.decompress();
FheInt::new(inner)
}
}
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/high_level_api/integers/unsigned/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ where
/// Decompress to a [FheUint]
///
/// See [CompressedFheUint] example.
pub fn decompress(self) -> FheUint<Id> {
let inner: crate::integer::RadixCiphertext = self.ciphertext.into();
pub fn decompress(&self) -> FheUint<Id> {
let inner: crate::integer::RadixCiphertext = self.ciphertext.decompress();
let mut ciphertext = FheUint::new(inner);
ciphertext.move_to_device_of_server_key_if_set();
ciphertext
Expand Down
40 changes: 19 additions & 21 deletions tfhe/src/integer/ciphertext/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ impl ParameterSetConformant for CompressedRadixCiphertext {
}
}

impl From<CompressedRadixCiphertext> for RadixCiphertext {
fn from(compressed: CompressedRadixCiphertext) -> Self {
Self::from(
compressed
.blocks
.into_iter()
.map(From::from)
impl CompressedRadixCiphertext {
pub fn decompress(&self) -> RadixCiphertext {
RadixCiphertext::from(
self.blocks
.iter()
.map(CompressedCiphertext::decompress)
.collect::<Vec<_>>(),
)
}
Expand All @@ -49,13 +48,12 @@ impl ParameterSetConformant for CompressedSignedRadixCiphertext {
}
}

impl From<CompressedSignedRadixCiphertext> for SignedRadixCiphertext {
fn from(compressed: CompressedSignedRadixCiphertext) -> Self {
Self::from(
compressed
.blocks
.into_iter()
.map(From::from)
impl CompressedSignedRadixCiphertext {
pub fn decompress(&self) -> SignedRadixCiphertext {
SignedRadixCiphertext::from(
self.blocks
.iter()
.map(CompressedCiphertext::decompress)
.collect::<Vec<_>>(),
)
}
Expand All @@ -64,14 +62,14 @@ impl From<CompressedSignedRadixCiphertext> for SignedRadixCiphertext {
/// Structure containing a **compressed** ciphertext in CRT decomposition.
pub type CompressedCrtCiphertext = BaseCrtCiphertext<CompressedCiphertext>;

impl From<CompressedCrtCiphertext> for CrtCiphertext {
fn from(compressed: CompressedCrtCiphertext) -> Self {
let blocks = compressed
impl CompressedCrtCiphertext {
pub fn decompress(&self) -> CrtCiphertext {
let blocks = self
.blocks
.into_iter()
.map(From::from)
.iter()
.map(CompressedCiphertext::decompress)
.collect::<Vec<_>>();
let moduli = compressed.moduli;
Self::from((blocks, moduli))
let moduli = self.moduli.clone();
CrtCiphertext::from((blocks, moduli))
}
}
2 changes: 1 addition & 1 deletion tfhe/src/js_on_wasm_api/shortint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl Shortint {
compressed_ciphertext: &ShortintCompressedCiphertext,
) -> ShortintCiphertext {
set_hook(Box::new(console_error_panic_hook::hook));
ShortintCiphertext(compressed_ciphertext.0.clone().into())
ShortintCiphertext(compressed_ciphertext.0.decompress())
}

#[wasm_bindgen]
Expand Down
18 changes: 6 additions & 12 deletions tfhe/src/shortint/ciphertext/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ParameterSetConformant for CompressedCiphertext {
}

impl CompressedCiphertext {
pub fn decompress(self) -> Ciphertext {
pub fn decompress(&self) -> Ciphertext {
let Self {
ct,
degree,
Expand All @@ -46,11 +46,11 @@ impl CompressedCiphertext {

Ciphertext {
ct: ct.decompress_into_lwe_ciphertext(),
degree,
message_modulus,
carry_modulus,
pbs_order,
noise_level,
degree: *degree,
message_modulus: *message_modulus,
carry_modulus: *carry_modulus,
pbs_order: *pbs_order,
noise_level: *noise_level,
}
}

Expand Down Expand Up @@ -103,9 +103,3 @@ impl CompressedCiphertext {
}
}
}

impl From<CompressedCiphertext> for Ciphertext {
fn from(value: CompressedCiphertext) -> Self {
value.decompress()
}
}

0 comments on commit c2c2bda

Please sign in to comment.