Struct concrete_core::crypto::lwe::LweCiphertext [−][src]
pub struct LweCiphertext<Cont> { /* fields omitted */ }
Expand description
A ciphertext encrypted using the LWE scheme.
Implementations
Allocates a new ciphertext.
Example
use concrete_commons::parameters::{LweDimension, LweSize};
use concrete_core::crypto::lwe::LweCiphertext;
let ct = LweCiphertext::allocate(0 as u8, LweSize(4));
assert_eq!(ct.lwe_size(), LweSize(4));
assert_eq!(ct.get_mask().mask_size(), LweDimension(3));
Creates a ciphertext from a container of values.
Example
use concrete_commons::parameters::{LweDimension, LweSize};
use concrete_core::crypto::lwe::LweCiphertext;
let vector = vec![0 as u8; 10];
let ct = LweCiphertext::from_container(vector.as_slice());
assert_eq!(ct.lwe_size(), LweSize(10));
assert_eq!(ct.get_mask().mask_size(), LweDimension(9));
Returns the size of the cipher, e.g. the size of the mask + 1 for the body.
Example
use concrete_commons::parameters::LweSize;
use concrete_core::crypto::lwe::LweCiphertext;
let ct = LweCiphertext::allocate(0 as u8, LweSize(4));
assert_eq!(ct.lwe_size(), LweSize(4));
Returns the body of the ciphertext.
Example
use concrete_core::crypto::lwe::{LweBody, LweCiphertext};
let ciphertext = LweCiphertext::from_container(vec![0 as u8; 10]);
let body = ciphertext.get_body();
assert_eq!(body, &LweBody(0 as u8));
pub fn get_mask<Scalar>(&self) -> LweMask<&[Scalar]> where
Self: AsRefTensor<Element = Scalar>,
pub fn get_mask<Scalar>(&self) -> LweMask<&[Scalar]> where
Self: AsRefTensor<Element = Scalar>,
Returns the mask of the ciphertext.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::lwe::LweCiphertext;
let ciphertext = LweCiphertext::from_container(vec![0 as u8; 10]);
let mask = ciphertext.get_mask();
assert_eq!(mask.mask_size(), LweDimension(9));
pub fn get_body_and_mask<Scalar>(
&self
) -> (&LweBody<Scalar>, LweMask<&[Scalar]>) where
Self: AsRefTensor<Element = Scalar>,
pub fn get_body_and_mask<Scalar>(
&self
) -> (&LweBody<Scalar>, LweMask<&[Scalar]>) where
Self: AsRefTensor<Element = Scalar>,
Returns the body and the mask of the ciphertext.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::lwe::{LweBody, LweCiphertext};
let ciphertext = LweCiphertext::from_container(vec![0 as u8; 10]);
let (body, mask) = ciphertext.get_body_and_mask();
assert_eq!(body, &LweBody(0));
assert_eq!(mask.mask_size(), LweDimension(9));
pub fn get_mut_body<Scalar>(&mut self) -> &mut LweBody<Scalar> where
Self: AsMutTensor<Element = Scalar>,
pub fn get_mut_body<Scalar>(&mut self) -> &mut LweBody<Scalar> where
Self: AsMutTensor<Element = Scalar>,
Returns the mutable body of the ciphertext.
Example
use concrete_core::crypto::lwe::{LweBody, LweCiphertext};
let mut ciphertext = LweCiphertext::from_container(vec![0 as u8; 10]);
let mut body = ciphertext.get_mut_body();
*body = LweBody(8);
let body = ciphertext.get_body();
assert_eq!(body, &LweBody(8 as u8));
pub fn get_mut_mask<Scalar>(&mut self) -> LweMask<&mut [Scalar]> where
Self: AsMutTensor<Element = Scalar>,
pub fn get_mut_mask<Scalar>(&mut self) -> LweMask<&mut [Scalar]> where
Self: AsMutTensor<Element = Scalar>,
Returns the mutable mask of the ciphertext.
Example
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::*;
let mut ciphertext = LweCiphertext::from_container(vec![0 as u8; 10]);
let mut mask = ciphertext.get_mut_mask();
for mut elt in mask.mask_element_iter_mut() {
*elt = 8;
}
let mask = ciphertext.get_mask();
for elt in mask.mask_element_iter() {
assert_eq!(*elt, 8);
}
assert_eq!(mask.mask_element_iter().count(), 9);
pub fn get_mut_body_and_mask<Scalar>(
&mut self
) -> (&mut LweBody<Scalar>, LweMask<&mut [Scalar]>) where
Self: AsMutTensor<Element = Scalar>,
pub fn get_mut_body_and_mask<Scalar>(
&mut self
) -> (&mut LweBody<Scalar>, LweMask<&mut [Scalar]>) where
Self: AsMutTensor<Element = Scalar>,
Returns the mutable body and mask of the ciphertext.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::*;
let mut ciphertext = LweCiphertext::from_container(vec![0 as u8; 10]);
let (body, mask) = ciphertext.get_mut_body_and_mask();
assert_eq!(body, &mut LweBody(0));
assert_eq!(mask.mask_size(), LweDimension(9));
pub fn fill_with_scalar_mul<Scalar, InputCont>(
&mut self,
input: &LweCiphertext<InputCont>,
scalar: &Cleartext<Scalar>
) where
Self: AsMutTensor<Element = Scalar>,
LweCiphertext<InputCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedInteger,
pub fn fill_with_scalar_mul<Scalar, InputCont>(
&mut self,
input: &LweCiphertext<InputCont>,
scalar: &Cleartext<Scalar>
) where
Self: AsMutTensor<Element = Scalar>,
LweCiphertext<InputCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedInteger,
Fills the ciphertext with the result of the multiplication of the input
ciphertext by the
scalar
cleartext.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let cleartext = Cleartext(2. as f32);
let plaintext: Plaintext<u32> = encoder.encode(cleartext);
let mut ciphertext = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(
&mut ciphertext,
&plaintext,
noise,
&mut encryption_generator,
);
let mut processed = LweCiphertext::from_container(vec![0 as u32; 257]);
processed.fill_with_scalar_mul(&ciphertext, &Cleartext(4));
let mut decrypted = Plaintext(0 as u32);
secret_key.decrypt_lwe(&mut decrypted, &processed);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - (cleartext.0 * 4.)).abs() < 0.2);
pub fn fill_with_multisum_with_bias<Scalar, InputCont, WeightCont>(
&mut self,
input_list: &LweList<InputCont>,
weights: &CleartextList<WeightCont>,
bias: &Plaintext<Scalar>
) where
Self: AsMutTensor<Element = Scalar>,
LweList<InputCont>: AsRefTensor<Element = Scalar>,
CleartextList<WeightCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedInteger,
pub fn fill_with_multisum_with_bias<Scalar, InputCont, WeightCont>(
&mut self,
input_list: &LweList<InputCont>,
weights: &CleartextList<WeightCont>,
bias: &Plaintext<Scalar>
) where
Self: AsMutTensor<Element = Scalar>,
LweList<InputCont>: AsRefTensor<Element = Scalar>,
CleartextList<WeightCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedInteger,
Fills the ciphertext with the result of the multisum of the input_list
with the
weights
values, and adds a bias.
Said differently, this function fills self
with:
$$
bias + \sum_i input_list[i] * weights[i]
$$
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{LweDimension, LweSize};
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(4), &mut secret_generator);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 100.,
};
let clear_values = CleartextList::from_container(vec![1. as f32, 2., 3.]);
let mut plain_values = PlaintextList::from_container(vec![0 as u32; 3]);
encoder.encode_list(&mut plain_values, &clear_values);
let mut ciphertext_values = LweList::from_container(vec![0. as u32; 5 * 3], LweSize(5));
secret_key.encrypt_lwe_list(
&mut ciphertext_values,
&plain_values,
noise,
&mut encryption_generator,
);
let mut output = LweCiphertext::from_container(vec![0. as u32; 5]);
let weights = CleartextList::from_container(vec![7, 8, 9]);
let bias = encoder.encode(Cleartext(13.));
output.fill_with_multisum_with_bias(&ciphertext_values, &weights, &bias);
let mut decrypted = Plaintext(0 as u32);
secret_key.decrypt_lwe(&mut decrypted, &output);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - 63.).abs() < 0.1);
pub fn update_with_add<OtherCont, Scalar>(
&mut self,
other: &LweCiphertext<OtherCont>
) where
Self: AsMutTensor<Element = Scalar>,
LweCiphertext<OtherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn update_with_add<OtherCont, Scalar>(
&mut self,
other: &LweCiphertext<OtherCont>
) where
Self: AsMutTensor<Element = Scalar>,
LweCiphertext<OtherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Adds the other
ciphertext to the current one.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let clear_1 = Cleartext(2. as f32);
let plain_1: Plaintext<u32> = encoder.encode(clear_1);
let mut cipher_1 = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(&mut cipher_1, &plain_1, noise, &mut encryption_generator);
let clear_2 = Cleartext(3. as f32);
let plain_2: Plaintext<u32> = encoder.encode(clear_2);
let mut cipher_2 = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(&mut cipher_2, &plain_2, noise, &mut encryption_generator);
cipher_1.update_with_add(&cipher_2);
let mut decrypted = Plaintext(0 as u32);
secret_key.decrypt_lwe(&mut decrypted, &cipher_1);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - 5.).abs() < 0.1);
pub fn update_with_sub<OtherCont, Scalar>(
&mut self,
other: &LweCiphertext<OtherCont>
) where
Self: AsMutTensor<Element = Scalar>,
LweCiphertext<OtherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn update_with_sub<OtherCont, Scalar>(
&mut self,
other: &LweCiphertext<OtherCont>
) where
Self: AsMutTensor<Element = Scalar>,
LweCiphertext<OtherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Subtracts the other
ciphertext from the current one.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let clear_1 = Cleartext(3. as f32);
let plain_1: Plaintext<u32> = encoder.encode(clear_1);
let mut cipher_1 = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(&mut cipher_1, &plain_1, noise, &mut encryption_generator);
let clear_2 = Cleartext(2. as f32);
let plain_2: Plaintext<u32> = encoder.encode(clear_2);
let mut cipher_2 = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(&mut cipher_2, &plain_2, noise, &mut encryption_generator);
cipher_1.update_with_sub(&cipher_2);
let mut decrypted = Plaintext(0 as u32);
secret_key.decrypt_lwe(&mut decrypted, &cipher_1);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - 1.).abs() < 0.1);
pub fn update_with_neg<Scalar>(&mut self) where
Self: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn update_with_neg<Scalar>(&mut self) where
Self: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Negates the ciphertext.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let encoder = RealEncoder {
offset: -5. as f32,
delta: 10.,
};
let clear = Cleartext(2. as f32);
let plain: Plaintext<u32> = encoder.encode(clear);
let mut cipher = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(&mut cipher, &plain, noise, &mut encryption_generator);
cipher.update_with_neg();
let mut decrypted = Plaintext(0 as u32);
secret_key.decrypt_lwe(&mut decrypted, &cipher);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - (-2.)).abs() < 0.1);
pub fn update_with_scalar_mul<Scalar>(&mut self, scalar: Cleartext<Scalar>) where
Self: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn update_with_scalar_mul<Scalar>(&mut self, scalar: Cleartext<Scalar>) where
Self: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Multiplies the current ciphertext with a scalar value inplace.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let clear = Cleartext(2. as f32);
let plain: Plaintext<u32> = encoder.encode(clear);
let mut cipher = LweCiphertext::from_container(vec![0. as u32; 257]);
secret_key.encrypt_lwe(&mut cipher, &plain, noise, &mut encryption_generator);
cipher.update_with_scalar_mul(Cleartext(3));
let mut decrypted = Plaintext(0 as u32);
secret_key.decrypt_lwe(&mut decrypted, &cipher);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - 6.).abs() < 0.2);
pub fn fill_with_glwe_sample_extraction<InputCont, Element>(
&mut self,
glwe: &GlweCiphertext<InputCont>,
n_th: MonomialDegree
) where
Self: AsMutTensor<Element = Element>,
GlweCiphertext<InputCont>: AsRefTensor<Element = Element>,
Element: UnsignedTorus,
pub fn fill_with_glwe_sample_extraction<InputCont, Element>(
&mut self,
glwe: &GlweCiphertext<InputCont>,
n_th: MonomialDegree
) where
Self: AsMutTensor<Element = Element>,
GlweCiphertext<InputCont>: AsRefTensor<Element = Element>,
Element: UnsignedTorus,
Fills an LWE ciphertext with the sample extraction of one of the coefficients of a GLWE ciphertext.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{GlweDimension, LweDimension, PolynomialSize};
use concrete_core::crypto::encoding::{Plaintext, PlaintextList};
use concrete_core::crypto::glwe::GlweCiphertext;
use concrete_core::crypto::lwe::LweCiphertext;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::GlweSecretKey;
use concrete_core::math::polynomial::MonomialDegree;
use concrete_core::math::tensor::AsRefTensor;
let mut secret_generator = SecretRandomGenerator::new(None);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
let poly_size = PolynomialSize(4);
let glwe_dim = GlweDimension(2);
let glwe_secret_key =
GlweSecretKey::generate_binary(glwe_dim, poly_size, &mut secret_generator);
let mut plaintext_list =
PlaintextList::from_container(vec![100000 as u32, 200000, 300000, 400000]);
let mut glwe_ct = GlweCiphertext::allocate(0u32, poly_size, glwe_dim.to_glwe_size());
let mut lwe_ct =
LweCiphertext::allocate(0u32, LweDimension(poly_size.0 * glwe_dim.0).to_lwe_size());
glwe_secret_key.encrypt_glwe(
&mut glwe_ct,
&plaintext_list,
LogStandardDev(-25.),
&mut encryption_generator,
);
let lwe_secret_key = glwe_secret_key.into_lwe_secret_key();
// Check for the first
for i in 0..4 {
// We sample extract
lwe_ct.fill_with_glwe_sample_extraction(&glwe_ct, MonomialDegree(i));
// We decrypt
let mut output = Plaintext(0u32);
lwe_secret_key.decrypt_lwe(&mut output, &lwe_ct);
// We check that the decryption is correct
let plain = plaintext_list.as_tensor().get_element(i);
let d0 = output.0.wrapping_sub(*plain);
let d1 = plain.wrapping_sub(output.0);
let dist = std::cmp::min(d0, d1);
assert!(dist < 400);
}
Trait Implementations
impl<Element, Cont> AsMutTensor for LweCiphertext<Cont> where
Cont: AsMutSlice<Element = Element>,
impl<Element, Cont> AsMutTensor for LweCiphertext<Cont> where
Cont: AsMutSlice<Element = Element>,
impl<Element, Cont> AsRefTensor for LweCiphertext<Cont> where
Cont: AsRefSlice<Element = Element>,
impl<Element, Cont> AsRefTensor for LweCiphertext<Cont> where
Cont: AsRefSlice<Element = Element>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
type Element = <Cont as AsRefSlice>::Element
type Element = <Cont as AsRefSlice>::Element
The element type of the collection container.
type Container = Cont
type Container = Cont
The type of the collection container.
Consumes self
and returns an owned tensor.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
Auto Trait Implementations
impl<Cont> RefUnwindSafe for LweCiphertext<Cont> where
Cont: RefUnwindSafe,
impl<Cont> Send for LweCiphertext<Cont> where
Cont: Send,
impl<Cont> Sync for LweCiphertext<Cont> where
Cont: Sync,
impl<Cont> Unpin for LweCiphertext<Cont> where
Cont: Unpin,
impl<Cont> UnwindSafe for LweCiphertext<Cont> where
Cont: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more