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));

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));

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);

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);
}

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);

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);
}

Decrypts a single ciphertext.

See [‘encrypt_lwe’] for an example.

Decrypts a list of ciphertexts.

See [‘encrypt_lwe_list’] for an example.

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,
);

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

The element type.

The container used by the tensor.

Returns a mutable reference to the enclosed tensor.

The element type.

The container used by the tensor.

Returns a reference to the enclosed tensor.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

The element type of the collection container.

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 !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.