Struct concrete_core::crypto::lwe::LweCiphertext [−][src]
pub struct LweCiphertext<Cont> { /* fields omitted */ }
Expand description
A ciphertext encrypted using the LWE scheme.
Implementations
Creates a ciphertext from a container of values.
Example
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_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::*}; 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_core::crypto::{*, lwe::*}; 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_core::crypto::{*, lwe::*}; 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::*}; 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::*}; 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_core::crypto::{*, lwe::*}; 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::LogStandardDev; use concrete_core::crypto::{*, secret::LweSecretKey, lwe::*, encoding::*}; use concrete_core::math::random::{RandomGenerator, EncryptionRandomGenerator}; let mut generator = RandomGenerator::new(None); let mut secret_gen = EncryptionRandomGenerator::new(None); let secret_key = LweSecretKey::generate(LweDimension(256), &mut 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 secret_gen); 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::LogStandardDev; use concrete_core::crypto::{*, secret::LweSecretKey, lwe::*, encoding::*}; use concrete_core::math::random::{RandomGenerator, EncryptionRandomGenerator}; let mut generator = RandomGenerator::new(None); let mut secret_gen = EncryptionRandomGenerator::new(None); let secret_key = LweSecretKey::generate(LweDimension(4), &mut 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 secret_gen); 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::LogStandardDev; use concrete_core::crypto::{*, secret::LweSecretKey, lwe::*, encoding::*}; use concrete_core::math::random::{RandomGenerator, EncryptionRandomGenerator}; let mut generator = RandomGenerator::new(None); let mut secret_gen = EncryptionRandomGenerator::new(None); let secret_key = LweSecretKey::generate(LweDimension(256), &mut 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 secret_gen); 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 secret_gen); 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::LogStandardDev; use concrete_core::crypto::{*, secret::LweSecretKey, lwe::*, encoding::*}; use concrete_core::math::random::{RandomGenerator, EncryptionRandomGenerator}; let mut generator = RandomGenerator::new(None); let mut secret_gen = EncryptionRandomGenerator::new(None); let secret_key = LweSecretKey::generate(LweDimension(256), &mut 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 secret_gen); 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 secret_gen); 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::LogStandardDev; use concrete_core::crypto::{*, secret::LweSecretKey, lwe::*, encoding::*}; use concrete_core::math::random::{RandomGenerator, EncryptionRandomGenerator}; let mut generator = RandomGenerator::new(None); let mut secret_gen = EncryptionRandomGenerator::new(None); let secret_key = LweSecretKey::generate(LweDimension(256), &mut 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 secret_gen); 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::LogStandardDev; use concrete_core::crypto::{*, secret::LweSecretKey, lwe::*, encoding::*}; use concrete_core::math::random::{RandomGenerator, EncryptionRandomGenerator}; let mut generator = RandomGenerator::new(None); let mut secret_gen = EncryptionRandomGenerator::new(None); let secret_key = LweSecretKey::generate(LweDimension(256), &mut 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 secret_gen); 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);
Trait Implementations
type Element = Element
type Element = Element
The element type.
Returns a mutable reference to the enclosed tensor.
type Element = Element
type Element = Element
The element type.
Returns a mutable reference to the enclosed tensor.
type Element = Element
type Element = Element
The element type.
type Container = AlignedVec<Element>
type Container = AlignedVec<Element>
The container used by the tensor.
Returns a mutable reference to the enclosed tensor.
type Element = Element
type Element = Element
The element type.
type Container = &'a mut [Element]
type Container = &'a mut [Element]
The container used by the tensor.
Returns a mutable reference to the enclosed tensor.
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 = Element
type Element = Element
The element type of the collection container.
type Container = AlignedVec<Element>
type Container = AlignedVec<Element>
The type of the collection container.
Consumes self
and returns an owned tensor.
type Element = Element
type Element = Element
The element type of the collection container.
type Container = &'a [Element]
type Container = &'a [Element]
The type of the collection container.
Consumes self
and returns an owned tensor.
type Element = Element
type Element = Element
The element type of the collection container.
type Container = &'a mut [Element]
type Container = &'a mut [Element]
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
type Output = T
type Output = T
Should always be Self