Struct concrete_core::crypto::secret::LweSecretKey [−][src]
pub struct LweSecretKey<Kind, Cont> where
Kind: KeyKind, { /* fields omitted */ }
Expand description
A LWE secret key.
Implementations
Generates a new binary secret key; e.g. allocates a storage and samples random values for the key.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::generators::SecretRandomGenerator;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let mut generator = SecretRandomGenerator::new(None);
let secret_key: LweSecretKey<_, Vec<u32>> =
LweSecretKey::generate_binary(LweDimension(256), &mut generator);
assert_eq!(secret_key.key_size(), LweDimension(256));
Generates a new ternary secret key; e.g. allocates a storage and samples random values for the key.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::generators::SecretRandomGenerator;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let mut generator = SecretRandomGenerator::new(None);
let secret_key: LweSecretKey<_, Vec<u32>> =
LweSecretKey::generate_ternary(LweDimension(256), &mut generator);
assert_eq!(secret_key.key_size(), LweDimension(256));
impl<Scalar> LweSecretKey<GaussianKeyKind, Vec<Scalar>> where
(Scalar, Scalar): RandomGenerable<Gaussian<f64>>,
Scalar: UnsignedTorus,
impl<Scalar> LweSecretKey<GaussianKeyKind, Vec<Scalar>> where
(Scalar, Scalar): RandomGenerable<Gaussian<f64>>,
Scalar: UnsignedTorus,
Generates a new gaussian secret key; e.g. allocates a storage and samples random values for the key.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::generators::SecretRandomGenerator;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let mut generator = SecretRandomGenerator::new(None);
let secret_key: LweSecretKey<_, Vec<u32>> =
LweSecretKey::generate_gaussian(LweDimension(256), &mut generator);
assert_eq!(secret_key.key_size(), LweDimension(256));
Generates a new gaussian secret key; e.g. allocates a storage and samples random values for the key.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::generators::SecretRandomGenerator;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let mut generator = SecretRandomGenerator::new(None);
let secret_key: LweSecretKey<_, Vec<u32>> =
LweSecretKey::generate_uniform(LweDimension(256), &mut generator);
assert_eq!(secret_key.key_size(), LweDimension(256));
Creates a binary lwe secret key from a container.
Notes
This method does not fill the container with random values to create a new key. It merely
wraps a container into the appropriate type. See LweSecretKey::generate_binary
for a
generation method.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let secret_key = LweSecretKey::binary_from_container(vec![true; 256]);
assert_eq!(secret_key.key_size(), LweDimension(256));
Creates a ternary lwe secret key from a container.
Notes
This method does not fill the container with random values to create a new key. It merely
wraps a container into the appropriate type. See LweSecretKey::generate_ternary
for a
generation method.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let secret_key = LweSecretKey::ternary_from_container(vec![true; 256]);
assert_eq!(secret_key.key_size(), LweDimension(256));
Creates a gaussian lwe secret key from a container.
Notes
This method does not fill the container with random values to create a new key. It merely
wraps a container into the appropriate type. See LweSecretKey::generate_gaussian
for a
generation method.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let secret_key = LweSecretKey::gaussian_from_container(vec![true; 256]);
assert_eq!(secret_key.key_size(), LweDimension(256));
Creates a uniform lwe secret key from a container.
Notes
This method does not fill the container with random values to create a new key. It merely
wraps a container into the appropriate type. See LweSecretKey::generate_uniform
for a
generation method.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let secret_key = LweSecretKey::uniform_from_container(vec![true; 256]);
assert_eq!(secret_key.key_size(), LweDimension(256));
Returns the size of the secret key.
Example
use concrete_commons::parameters::LweDimension;
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let secret_key = LweSecretKey::binary_from_container(vec![true; 256]);
assert_eq!(secret_key.key_size(), LweDimension(256));
pub fn encrypt_lwe<OutputCont, Scalar>(
&self,
output: &mut LweCiphertext<OutputCont>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<OutputCont>: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn encrypt_lwe<OutputCont, Scalar>(
&self,
output: &mut LweCiphertext<OutputCont>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<OutputCont>: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Encrypts a single ciphertext.
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::*;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let noise = LogStandardDev::from_log_standard_dev(-15.);
let clear = Cleartext(2. as f32);
let plain: Plaintext<u32> = encoder.encode(clear);
let mut encrypted = LweCiphertext::allocate(0u32, LweSize(257));
let mut encryption_generator = EncryptionRandomGenerator::new(None);
secret_key.encrypt_lwe(&mut encrypted, &plain, noise, &mut encryption_generator);
let mut decrypted = Plaintext(0u32);
secret_key.decrypt_lwe(&mut decrypted, &encrypted);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - clear.0).abs() < 0.1);
pub fn encrypt_lwe_list<OutputCont, InputCont, Scalar>(
&self,
output: &mut LweList<OutputCont>,
encoded: &PlaintextList<InputCont>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweList<OutputCont>: AsMutTensor<Element = Scalar>,
PlaintextList<InputCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn encrypt_lwe_list<OutputCont, InputCont, Scalar>(
&self,
output: &mut LweList<OutputCont>,
encoded: &PlaintextList<InputCont>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweList<OutputCont>: AsMutTensor<Element = Scalar>,
PlaintextList<InputCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Encrypts a list of ciphertexts.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{
CiphertextCount, CleartextCount, LweDimension, LweSize, PlaintextCount,
};
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let noise = LogStandardDev::from_log_standard_dev(-15.);
let clear_values = CleartextList::allocate(2. as f32, CleartextCount(100));
let mut plain_values = PlaintextList::allocate(0u32, PlaintextCount(100));
encoder.encode_list(&mut plain_values, &clear_values);
let mut encrypted_values = LweList::allocate(0u32, LweSize(257), CiphertextCount(100));
let mut encryption_generator = EncryptionRandomGenerator::new(None);
secret_key.encrypt_lwe_list(
&mut encrypted_values,
&plain_values,
noise,
&mut encryption_generator,
);
let mut decrypted_values = PlaintextList::allocate(0u32, PlaintextCount(100));
secret_key.decrypt_lwe_list(&mut decrypted_values, &encrypted_values);
let mut decoded_values = CleartextList::allocate(0. as f32, CleartextCount(100));
encoder.decode_list(&mut decoded_values, &decrypted_values);
for (clear, decoded) in clear_values
.cleartext_iter()
.zip(decoded_values.cleartext_iter())
{
assert!((clear.0 - decoded.0).abs() < 0.1);
}
pub fn trivial_encrypt_lwe<OutputCont, Scalar>(
&self,
output: &mut LweCiphertext<OutputCont>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<OutputCont>: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn trivial_encrypt_lwe<OutputCont, Scalar>(
&self,
output: &mut LweCiphertext<OutputCont>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<OutputCont>: AsMutTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Encrypts a single ciphertext with null masks.
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::*;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let noise = LogStandardDev::from_log_standard_dev(-15.);
let clear = Cleartext(2. as f32);
let plain: Plaintext<u32> = encoder.encode(clear);
let mut encrypted = LweCiphertext::allocate(0u32, LweSize(257));
let mut encryption_generator = EncryptionRandomGenerator::new(None);
secret_key.trivial_encrypt_lwe(&mut encrypted, &plain, noise, &mut encryption_generator);
let mut decrypted = Plaintext(0u32);
secret_key.decrypt_lwe(&mut decrypted, &encrypted);
let decoded = encoder.decode(decrypted);
assert!((decoded.0 - clear.0).abs() < 0.1);
pub fn trivial_encrypt_lwe_list<OutputCont, InputCont, Scalar>(
&self,
output: &mut LweList<OutputCont>,
encoded: &PlaintextList<InputCont>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweList<OutputCont>: AsMutTensor<Element = Scalar>,
PlaintextList<InputCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn trivial_encrypt_lwe_list<OutputCont, InputCont, Scalar>(
&self,
output: &mut LweList<OutputCont>,
encoded: &PlaintextList<InputCont>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
LweList<OutputCont>: AsMutTensor<Element = Scalar>,
PlaintextList<InputCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Encrypts a list of ciphertexts with null masks.
Example
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{
CiphertextCount, CleartextCount, LweDimension, LweSize, PlaintextCount,
};
use concrete_core::crypto::encoding::*;
use concrete_core::crypto::lwe::*;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::*;
use concrete_core::crypto::*;
let mut secret_generator = SecretRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let encoder = RealEncoder {
offset: 0. as f32,
delta: 10.,
};
let noise = LogStandardDev::from_log_standard_dev(-15.);
let clear_values = CleartextList::allocate(2. as f32, CleartextCount(100));
let mut plain_values = PlaintextList::allocate(0u32, PlaintextCount(100));
encoder.encode_list(&mut plain_values, &clear_values);
let mut encrypted_values = LweList::allocate(0u32, LweSize(257), CiphertextCount(100));
let mut encryption_generator = EncryptionRandomGenerator::new(None);
secret_key.trivial_encrypt_lwe_list(
&mut encrypted_values,
&plain_values,
noise,
&mut encryption_generator,
);
for ciphertext in encrypted_values.ciphertext_iter() {
for mask in ciphertext.get_mask().mask_element_iter() {
assert_eq!(*mask, 0);
}
}
let mut decrypted_values = PlaintextList::allocate(0u32, PlaintextCount(100));
secret_key.decrypt_lwe_list(&mut decrypted_values, &encrypted_values);
let mut decoded_values = CleartextList::allocate(0. as f32, CleartextCount(100));
encoder.decode_list(&mut decoded_values, &decrypted_values);
for (clear, decoded) in clear_values
.cleartext_iter()
.zip(decoded_values.cleartext_iter())
{
assert!((clear.0 - decoded.0).abs() < 0.1);
}
pub fn decrypt_lwe<Scalar, CipherCont>(
&self,
output: &mut Plaintext<Scalar>,
cipher: &LweCiphertext<CipherCont>
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<CipherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn decrypt_lwe<Scalar, CipherCont>(
&self,
output: &mut Plaintext<Scalar>,
cipher: &LweCiphertext<CipherCont>
) where
Self: AsRefTensor<Element = Scalar>,
LweCiphertext<CipherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Decrypts a single ciphertext.
See [‘encrypt_lwe’] for an example.
pub fn decrypt_lwe_list<Scalar, EncodedCont, CipherCont>(
&self,
output: &mut PlaintextList<EncodedCont>,
cipher: &LweList<CipherCont>
) where
Self: AsRefTensor<Element = Scalar>,
PlaintextList<EncodedCont>: AsMutTensor<Element = Scalar>,
LweList<CipherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn decrypt_lwe_list<Scalar, EncodedCont, CipherCont>(
&self,
output: &mut PlaintextList<EncodedCont>,
cipher: &LweList<CipherCont>
) where
Self: AsRefTensor<Element = Scalar>,
PlaintextList<EncodedCont>: AsMutTensor<Element = Scalar>,
LweList<CipherCont>: AsRefTensor<Element = Scalar>,
Scalar: UnsignedTorus,
Decrypts a list of ciphertexts.
See [‘encrypt_lwe_list’] for an example.
pub fn encrypt_constant_gsw<OutputCont, Scalar>(
&self,
encrypted: &mut GswCiphertext<OutputCont, Scalar>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
GswCiphertext<OutputCont, Scalar>: AsMutTensor<Element = Scalar>,
OutputCont: AsMutSlice<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn encrypt_constant_gsw<OutputCont, Scalar>(
&self,
encrypted: &mut GswCiphertext<OutputCont, Scalar>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
GswCiphertext<OutputCont, Scalar>: AsMutTensor<Element = Scalar>,
OutputCont: AsMutSlice<Element = Scalar>,
Scalar: UnsignedTorus,
This function encrypts a message as a GSW ciphertext.
Examples
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{
DecompositionBaseLog, DecompositionLevelCount, LweDimension, LweSize,
};
use concrete_core::crypto::encoding::Plaintext;
use concrete_core::crypto::gsw::GswCiphertext;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
let mut generator = SecretRandomGenerator::new(None);
let secret_key = LweSecretKey::generate_binary(LweDimension(256), &mut generator);
let mut ciphertext = GswCiphertext::allocate(
0 as u32,
LweSize(257),
DecompositionLevelCount(3),
DecompositionBaseLog(7),
);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let mut secret_generator = EncryptionRandomGenerator::new(None);
secret_key.encrypt_constant_gsw(
&mut ciphertext,
&Plaintext(10),
noise,
&mut secret_generator,
);
pub fn trivial_encrypt_constant_gsw<OutputCont, Scalar>(
&self,
encrypted: &mut GswCiphertext<OutputCont, Scalar>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
GswCiphertext<OutputCont, Scalar>: AsMutTensor<Element = Scalar>,
OutputCont: AsMutSlice<Element = Scalar>,
Scalar: UnsignedTorus,
pub fn trivial_encrypt_constant_gsw<OutputCont, Scalar>(
&self,
encrypted: &mut GswCiphertext<OutputCont, Scalar>,
encoded: &Plaintext<Scalar>,
noise_parameters: impl DispersionParameter,
generator: &mut EncryptionRandomGenerator
) where
Self: AsRefTensor<Element = Scalar>,
GswCiphertext<OutputCont, Scalar>: AsMutTensor<Element = Scalar>,
OutputCont: AsMutSlice<Element = Scalar>,
Scalar: UnsignedTorus,
This function encrypts a message as a GSW ciphertext whose lwe masks are all zeros.
Examples
use concrete_commons::dispersion::LogStandardDev;
use concrete_commons::parameters::{
DecompositionBaseLog, DecompositionLevelCount, LweDimension, LweSize,
};
use concrete_core::crypto::encoding::Plaintext;
use concrete_core::crypto::gsw::GswCiphertext;
use concrete_core::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use concrete_core::crypto::secret::LweSecretKey;
let mut secret_generator = SecretRandomGenerator::new(None);
let secret_key: LweSecretKey<_, Vec<u32>> =
LweSecretKey::generate_binary(LweDimension(256), &mut secret_generator);
let mut ciphertext = GswCiphertext::allocate(
0 as u32,
LweSize(257),
DecompositionLevelCount(3),
DecompositionBaseLog(7),
);
let noise = LogStandardDev::from_log_standard_dev(-15.);
let mut encryption_generator = EncryptionRandomGenerator::new(None);
secret_key.trivial_encrypt_constant_gsw(
&mut ciphertext,
&Plaintext(10),
noise,
&mut encryption_generator,
);
Trait Implementations
impl<Kind, Element, Cont> AsMutTensor for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: AsMutSlice<Element = Element>,
impl<Kind, Element, Cont> AsMutTensor for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: AsMutSlice<Element = Element>,
impl<Kind, Element, Cont> AsRefTensor for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: AsRefSlice<Element = Element>,
impl<Kind, Element, Cont> AsRefTensor for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: AsRefSlice<Element = Element>,
impl<'de, Kind, Cont> Deserialize<'de> for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: Deserialize<'de>,
impl<'de, Kind, Cont> Deserialize<'de> for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: Deserialize<'de>,
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
impl<Kind, Cont> IntoTensor for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: AsRefSlice,
impl<Kind, Cont> IntoTensor for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
Cont: AsRefSlice,
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.
impl<Kind: PartialEq, Cont: PartialEq> PartialEq<LweSecretKey<Kind, Cont>> for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
impl<Kind: PartialEq, Cont: PartialEq> PartialEq<LweSecretKey<Kind, Cont>> for LweSecretKey<Kind, Cont> where
Kind: KeyKind,
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<Kind, Cont> RefUnwindSafe for LweSecretKey<Kind, Cont> where
Cont: RefUnwindSafe,
Kind: RefUnwindSafe,
impl<Kind, Cont> Send for LweSecretKey<Kind, Cont> where
Cont: Send,
Kind: Send,
impl<Kind, Cont> Sync for LweSecretKey<Kind, Cont> where
Cont: Sync,
impl<Kind, Cont> Unpin for LweSecretKey<Kind, Cont> where
Cont: Unpin,
Kind: Unpin,
impl<Kind, Cont> UnwindSafe for LweSecretKey<Kind, Cont> where
Cont: UnwindSafe,
Kind: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more