Struct concrete::lwe::LWE [−][src]
pub struct LWE {
pub ciphertext: LweCiphertext<Vec<Torus>>,
pub variance: f64,
pub dimension: usize,
pub encoder: Encoder,
}
Expand description
Structure containing a single LWE ciphertext.
Attributes
ciphertext
- the LWE ciphertextsvariances
- the variance of the noise of the LWE ciphertextdimension
- the length the LWE maskencoder
- the encoder of the LWE ciphertext
Fields
ciphertext: LweCiphertext<Vec<Torus>>
variance: f64
dimension: usize
encoder: Encoder
Implementations
Instantiate a new LWE filled with zeros from a dimension
Arguments
dimension
- the length the LWE mask
Output
- a new instantiation of an LWE
- ZeroCiphertextsInStructureError if we try to create a structure with no ciphertext in it
Example
use concrete::*;
// creates an LWE ciphertext with a dimension of 630
let empty_ciphertexts = LWE::zero(630).unwrap();
pub fn encode_encrypt(
sk: &LWESecretKey,
message: f64,
encoder: &Encoder
) -> Result<LWE, CryptoAPIError>
pub fn encode_encrypt(
sk: &LWESecretKey,
message: f64,
encoder: &Encoder
) -> Result<LWE, CryptoAPIError>
Encode a message and then directly encrypt the plaintext into an LWE structure
Arguments
sk
- an LWE secret keymessage
- a message as u64encoder
- an Encoder
Output
an LWE structure
use concrete::*;
// encoder
let encoder = Encoder::new(-2., 6., 4, 4).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// a message
let message: f64 = -1.;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message, &encoder).unwrap();
pub fn encrypt_raw(
&mut self,
sk: &LWESecretKey,
plaintext: Torus
) -> Result<(), CryptoAPIError>
pub fn encrypt_raw(
&mut self,
sk: &LWESecretKey,
plaintext: Torus
) -> Result<(), CryptoAPIError>
Encrypt a raw plaintext (a Torus element instead of a struct Plaintext) with the provided key and standard deviation
Arguments
sk
- an LWE secret keyplaintext
- a Torus elementstd_dev
- the standard deviation used for the error normal distribution
Example
use concrete::*;
// create an Encoder instance where messages are in the interval [-5, 5[
let encoder = Encoder::new(-5., 5., 8, 0).unwrap();
// create one plaintext
let pt: u64 = 0;
// create one LWESecretKey
let sk = LWESecretKey::new(&LWE128_630);
// create a new LWE that encrypts pt
let mut ct = LWE::zero(sk.dimension).unwrap();
ct.encrypt_raw(&sk, pt).unwrap();
Decrypt the ciphertext, meaning compute the phase and directly decode the output
Arguments
sk
- an LWE secret key
Output
result
- a f64- DimensionError - if the ciphertext and the key have incompatible dimensions
use concrete::*;
// create an Encoder instance where messages are in the interval [-5, 5[
let encoder = Encoder::new(-5., 5., 8, 0).unwrap();
// create a list of messages in our interval
let message: f64 = -3.2;
// create an LWESecretKey
let sk = LWESecretKey::new(&LWE128_630);
// create a new LWE that encrypts pt
let mut ct = LWE::encode_encrypt(&sk,message, &encoder).unwrap();
// decryption
let res = ct.decrypt_decode(&sk).unwrap();
Decrypt the ciphertext, meaning compute the phase and directly decode the output as if the encoder was in a rounding context
Arguments
sk
- an LWE secret key
Output
result
- a f64- DimensionError - if the ciphertext and the key have incompatible dimensions
use concrete::*;
// create an Encoder instance where messages are in the interval [-5, 5[
let encoder = Encoder::new(-5., 5., 8, 0).unwrap();
// create a list of messages in our interval
let message: f64 = -3.2;
// create an LWESecretKey
let sk = LWESecretKey::new(&LWE128_630);
// create a new LWE that encrypts pt
let mut ct = LWE::encode_encrypt(&sk,message, &encoder).unwrap();
// decryption
let res = ct.decrypt_decode(&sk).unwrap();
Add a small message to a LWE ciphertext and does not change the encoding but changes the bodies of the ciphertext
Argument
message
- a f64
Output
- a new LWE
Example
use concrete::*;
// encoder
let encoder = Encoder::new(100., 110., 8, 0).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two messages
let message_1: f64 = 106.276;
let message_2: f64 =-4.9;
// encode and encrypt
let ciphertext = LWE::encode_encrypt(&secret_key,message_1, &encoder).unwrap();
// addition between ciphertext and message_2
let ct_add = ciphertext.add_constant_static_encoder(message_2).unwrap();
pub fn add_constant_static_encoder_inplace(
&mut self,
message: f64
) -> Result<(), CryptoAPIError>
pub fn add_constant_static_encoder_inplace(
&mut self,
message: f64
) -> Result<(), CryptoAPIError>
Add small messages to a LWE ciphertext and does not change the encoding but changes the bodies of the ciphertexts
Argument
messages
- a list of messages as f64
Example
use concrete::*;
// encoder
let encoder = Encoder::new(100., 110., 8, 0).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two messages
let message_1: f64 = 106.276;
let message_2: f64 =-4.9;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key,message_1, &encoder).unwrap();
// addition between ciphertext and message_2
ciphertext.add_constant_static_encoder_inplace(message_2).unwrap();
Add a message to a LWE ciphertext and translate the interval of a distance equal to the message but does not change either the bodies or the masks of the ciphertext
Argument
message
- a f64
Output
- a new LWE
- InvalidEncoderError if invalid encoder
Example
use concrete::*;
// encoder
let encoder = Encoder::new(100., 110., 8, 0).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = -4.9;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
// addition between ciphertext and message_2
let ct = ciphertext
.add_constant_dynamic_encoder(message_2)
.unwrap();
pub fn add_constant_dynamic_encoder_inplace(
&mut self,
message: f64
) -> Result<(), CryptoAPIError>
pub fn add_constant_dynamic_encoder_inplace(
&mut self,
message: f64
) -> Result<(), CryptoAPIError>
Add a message to a LWE ciphertext and translate the interval of a distance equal to the message but does not change either the bodies or the masks of the ciphertext
Argument
message
- a f64
Output
- InvalidEncoderError if invalid encoder
Example
use concrete::*;
// encoder
let encoder = Encoder::new(100., 110., 8, 0).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64= 106.276;
let message_2: f64 = -4.9;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
// addition between ciphertext and message_2
ciphertext
.add_constant_dynamic_encoder_inplace(message_2)
.unwrap();
Compute an homomorphic addition between two LWE ciphertexts
Arguments
ct
- an LWE structnew_min
- the min of the interval for the resulting Encoder
Output
- a new LWE
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(100., 110., 8, 0).unwrap();
let encoder_2 = Encoder::new(0., 10., 8, 0).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 =4.9;
// new_min
let new_min: f64 = 103.;
// encode and encrypt
let ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
// addition between ciphertext_1 and ciphertext_2
let ct_add = ciphertext_1
.add_with_new_min(&ciphertext_2, new_min)
.unwrap();
pub fn add_with_new_min_inplace(
&mut self,
ct: &LWE,
new_min: f64
) -> Result<(), CryptoAPIError>
pub fn add_with_new_min_inplace(
&mut self,
ct: &LWE,
new_min: f64
) -> Result<(), CryptoAPIError>
Compute an homomorphic addition between two LWE ciphertexts
Arguments
ct
- an LWE structnew_min
- the min of the interval for the resulting Encoder
Output
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(100., 110., 8, 0).unwrap();
let encoder_2 = Encoder::new(0., 10., 8, 0).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let mut ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
// new_min
let new_min: f64 = 103.;
// addition between ciphertext_1 and ciphertext_2
ciphertext_1
.add_with_new_min_inplace(&ciphertext_2, new_min)
.unwrap();
Compute an homomorphic addition between two LWE ciphertexts. The center of the output Encoder is the sum of the two centers of the input Encoders.
Arguments
ct
- an LWE struct
Output
- a new LWE
use concrete::*;
let min_1: f64 = 85.;
let min_2: f64 = -2.;
let delta: f64 = 34.5;
let max_1: f64 = min_1 + delta;
let max_2: f64 = min_2 + delta;
let (precision, padding) = (5, 2);
let margin: f64 = 10.;
// encoder
let encoder_1 = Encoder::new(min_1 - margin, max_1 + margin, precision, padding).unwrap();
let encoder_2 = Encoder::new(min_2 - margin, max_2 + margin, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
// addition between ciphertext_1 and ciphertext_2
let new_ciphertext = ciphertext_1.add_centered(&ciphertext_2).unwrap();
Compute an homomorphic addition between two LWE ciphertexts. The center of the output Encoder is the sum of the two centers of the input Encoders
Arguments
ct
- an LWE struct
Output
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
Example
use concrete::*;
let min_1: f64 = 85.;
let min_2: f64 = -2.;
let delta: f64 = 34.5;
let max_1: f64 = min_1 + delta;
let max_2: f64 = min_2 + delta;
let (precision, padding) = (5, 2);
let margin: f64 = 10.;
// encoder
let encoder_1 = Encoder::new(min_1 - margin, max_1 + margin, precision, padding).unwrap();
let encoder_2 = Encoder::new(min_2 - margin, max_2 + margin, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let mut ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
// addition between ciphertext_1 and ciphertext_2
ciphertext_1.add_centered_inplace(&ciphertext_2).unwrap();
Compute an addition between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message stays the same: min(nb1,nb2)
Argument
ct
- an LWE struct
Output
- a new LWE
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(100., 110., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 10., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
let ct_add = ciphertext_1.add_with_padding(&ciphertext_2);
Compute an addition between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message stays the same: min(nb1,nb2)
Argument
ct
- an LWE struct
Output
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(100., 110., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 10., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let mut ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
ciphertext_1.add_with_padding_inplace(&ciphertext_2);
Compute an addition between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message increases: max(nb1,nb2) + 1
Argument
ct
- an LWE struct
Output
- a new LWE
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(0., 255., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 255., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.;
let message_2: f64 = 4.;
// encode and encrypt
let ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
let ct_add = ciphertext_1.add_with_padding_exact(&ciphertext_2);
Compute an addition between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message increases: max(nb1,nb2) + 1
Argument
ct
- an LWE struct
Output
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(0., 255., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 255., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.;
let message_2: f64 = 4.;
// encode and encrypt
let mut ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
ciphertext_1.add_with_padding_exact_inplace(&ciphertext_2);
Compute an subtraction between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message stays the same: min(nb1,nb2)
Argument
ct
- an LWE struct
Output
- a new LWE
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(100., 110., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 10., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
let ct_sub = ciphertext_1.add_with_padding(&ciphertext_2);
Compute an subtraction between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message stays the same: min(nb1,nb2)
Argument
ct
- an LWE struct
Output
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(100., 110., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 10., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.276;
let message_2: f64 = 4.9;
// encode and encrypt
let mut ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
ciphertext_1.sub_with_padding_inplace(&ciphertext_2);
Compute an subtraction between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message increases: max(nb1,nb2) + 1
Argument
ct
- an LWE struct
Output
- a new LWE
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(0., 255., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 255., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.;
let message_2: f64 = 4.;
// encode and encrypt
let ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
let ct_sub = ciphertext_1.sub_with_padding_exact(&ciphertext_2);
Compute an subtraction between two LWE ciphertexts by eating one bit of padding. Note that the number of bits of message increases: max(nb1,nb2) + 1
Argument
ct
- an LWE struct
Output
- DimensionError - if the ciphertexts have incompatible dimensions
- DeltaError - if the ciphertexts have incompatible deltas
- PaddingError - if the ciphertexts have incompatible paddings
- NotEnoughPaddingError - if nb bit of padding is zero
Example
use concrete::*;
// encoder
let encoder_1 = Encoder::new(0., 255., 8, 1).unwrap();
let encoder_2 = Encoder::new(0., 255., 8, 1).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 106.;
let message_2: f64 = 4.;
// encode and encrypt
let mut ciphertext_1 = LWE::encode_encrypt(&secret_key, message_1, &encoder_1).unwrap();
let ciphertext_2 = LWE::encode_encrypt(&secret_key, message_2, &encoder_2).unwrap();
ciphertext_1.sub_with_padding_exact_inplace(&ciphertext_2);
Multiply LWE ciphertext with small integer message and does not change the encoding but changes the body and mask of the ciphertext
Argument
messages
- a list of integer messages
Output
- a new LWE
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let b = min.abs().min(max.abs()) / 20.;
let precision = 6;
let padding = 2;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 6.923;
let message_2: i32= 2;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
let new_ciphertext = ciphertext.mul_constant_static_encoder(message_2).unwrap();
pub fn mul_constant_static_encoder_inplace(
&mut self,
message: i32
) -> Result<(), CryptoAPIError>
pub fn mul_constant_static_encoder_inplace(
&mut self,
message: i32
) -> Result<(), CryptoAPIError>
Multiply LWE ciphertext with small integer message and does not change the encoding but changes the body and mask of the ciphertext
Argument
messages
- a list of integer messages
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let b = min.abs().min(max.abs()) / 20.;
let precision = 6;
let padding = 2;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = 6.923;
let message_2: i32= 2;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
ciphertext
.mul_constant_static_encoder_inplace(message_2)
.unwrap();
pub fn mul_constant_with_padding(
&self,
constant: f64,
max_constant: f64,
nb_bit_padding: usize
) -> Result<LWE, CryptoAPIError>
pub fn mul_constant_with_padding(
&self,
constant: f64,
max_constant: f64,
nb_bit_padding: usize
) -> Result<LWE, CryptoAPIError>
Multiply each LWE ciphertext with a real constant and do change the encoding and the ciphertexts by consuming some bits of padding it needs to have the same number of constant than ciphertexts it also needs that the input encoding all contained zero in their intervals the output precision is the minimum between the input and the number of bits of padding consumed
Argument
scale
- a positive scaling factor which has to be greater that any of the messages.abs()nb_bit_padding
- the number of bits of padding to be consumedmessages
- a list of real messages as f64
Output
- a new LWE
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 6;
let padding = precision + 3;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = -106.276;
let message_2: f64 = 2.432;
let b: f64 = 6.;
// encode and encrypt
let ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
let new_ciphertext = ciphertext
.mul_constant_with_padding(message_2, b, precision)
.unwrap();
pub fn mul_constant_with_padding_inplace(
&mut self,
constant: f64,
max_constant: f64,
nb_bit_padding: usize
) -> Result<(), CryptoAPIError>
pub fn mul_constant_with_padding_inplace(
&mut self,
constant: f64,
max_constant: f64,
nb_bit_padding: usize
) -> Result<(), CryptoAPIError>
Multiply each LWE ciphertext with a real constant and do change the encoding and the ciphertexts by consuming some bits of padding it needs to have the same number of constant than ciphertexts it also needs that the input encoding all contained zero in their intervals the output precision is the minimum between the input and the number of bits of padding consumed
Argument
scale
- a positive scaling factor which has to be greater that any of the messages.abs()nb_bit_padding
- the number of bits of padding to be consumedmessages
- a list of real messages as f64
Output
- ConstantMaximumError - if one element of
constants
if bigger thanmax_constant
- ZeroInIntervalError - if zero is not in the interval described by the encoders
- NotEnoughPaddingError - if there is not enough padding
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 6;
let padding = precision + 3;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = -106.276;
let message_2: f64 = 2.432;
let b: f64 = 6.;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
ciphertext
.mul_constant_with_padding_inplace(message_2, b, precision)
.unwrap();
Compute the opposite of the n-th LWE ciphertext in the structure
Output
- a new LWE ciphertext
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 6;
let padding = 5;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 =-106.276;
// encode and encrypt
let ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
let new_ciphertext = ciphertext.opposite().unwrap();
Compute the opposite of the n-th LWE ciphertext in the structure
Output
- IndexError - if the requested ciphertext does not exist
- InvalidEncoderError - if the encoder of the requested ciphertext is not valid (i.e. with nb_bit_precision = 0 or delta = 0)
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 6;
let padding = 5;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message_1: f64 = -106.276;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message_1, &encoder).unwrap();
ciphertext.opposite_inplace().unwrap();
Compute a key switching operation on every ciphertext from the LWE struct self
Argument
ksk
- the key switching key
Output
- a LWE struct
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 6;
let padding = 1;
let level: usize = 3;
let base_log: usize = 3;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// two lists of messages
let message: f64 = -106.276;
// generate two secret keys
let secret_key_before = LWESecretKey::new(&LWE128_1024);
let secret_key_after = LWESecretKey::new(&LWE128_1024);
// generate the key switching key
let ksk = LWEKSK::new(&secret_key_before, &secret_key_after, base_log, level);
// a list of messages that we encrypt
let ciphertext_before =
LWE::encode_encrypt(&secret_key_before, message, &encoder).unwrap();
// key switch
let ciphertext_after = ciphertext_before.keyswitch(&ksk).unwrap();
Compute a bootstrap on the LWE
Argument
bsk
- the bootstrapping key
Output
- a LWE struct
- IndexError - if the requested ciphertext does not exist
- DimensionError - if the bootstrapping key and the input ciphertext have incompatible dimensions
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 4;
let padding = 1;
let level: usize = 3;
let base_log: usize = 3;
// encoder
let encoder = Encoder::new(min, max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// a message
let message: f64 = -106.276;
// generate two secret keys
let rlwe_secret_key = RLWESecretKey::new(&RLWE128_1024_1);
let secret_key_before = LWESecretKey::new(&LWE128_630);
let secret_key_after = rlwe_secret_key.to_lwe_secret_key();
// bootstrapping key
let bootstrapping_key =
LWEBSK::new(&secret_key_before, &rlwe_secret_key, base_log, level);
// encode and encrypt
let ciphertext_before =
LWE::encode_encrypt(&secret_key_before,message, &encoder).unwrap();
let ciphertext_out = ciphertext_before
.bootstrap(&bootstrapping_key)
.unwrap();
pub fn bootstrap_with_function<F: Fn(f64) -> f64>(
&self,
bsk: &LWEBSK,
f: F,
encoder_output: &Encoder
) -> Result<LWE, CryptoAPIError>
pub fn bootstrap_with_function<F: Fn(f64) -> f64>(
&self,
bsk: &LWEBSK,
f: F,
encoder_output: &Encoder
) -> Result<LWE, CryptoAPIError>
Compute a bootstrap and apply an arbitrary function to the LWE ciphertext
Argument
bsk
- the bootstrapping keyf
- the function to aplyencoder_output
- a list of output encoders
Output
- a LWE struct
- IndexError - if the requested ciphertext does not exist
- DimensionError - if the bootstrapping key and the input ciphertext have incompatible dimensions
Example
use concrete::*;
// params
let (min, max): (f64, f64) = (-150., 204.);
let precision = 4;
let padding = 1;
let level: usize = 3;
let base_log: usize = 3;
// encoder
let encoder_input = Encoder::new(min, max, precision, padding).unwrap();
let encoder_output = Encoder::new(0., max, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// a message
let message: f64 = -106.276;
// generate secret keys
let rlwe_secret_key = RLWESecretKey::new(&RLWE128_1024_1);
let secret_key_before = LWESecretKey::new(&LWE128_630);
let secret_key_after = rlwe_secret_key.to_lwe_secret_key();
// bootstrapping key
let bootstrapping_key =
LWEBSK::new(&secret_key_before, &rlwe_secret_key, base_log, level);
// encode and encrypt
let ciphertext_before =
LWE::encode_encrypt(&secret_key_before, message, &encoder_input).unwrap();
let ciphertext_out = ciphertext_before
.bootstrap_with_function(&bootstrapping_key, |x| f64::max(0., x), &encoder_output)
.unwrap();
Multiply two LWE ciphertexts thanks to two bootstrapping procedures need to have 2 bits of padding at least
Argument
ct
- an LWE struct containing the second LWE for the multiplicationbsk
- the bootstrapping key used to evaluate the functionn_self
- the index of the ciphertext to multiply in the self structn_ct
- the index of the ciphertext to multiply in the ct struct
Output
- a LWE struct
- NotEnoughPaddingError - if one of the input does not have at least 2 bits of padding
Example
use concrete::*;
// params
let (min_1, max_1): (f64, f64) = (-150., 204.);
let min_2: f64 = 30.;
let max_2: f64 = min_2 + max_1 - min_1;
let precision = 4;
let padding = 2;
let level: usize = 3;
let base_log: usize = 3;
// encoder
let encoder_1 = Encoder::new(min_1, max_1, precision, padding).unwrap();
let encoder_2 = Encoder::new(min_2, max_2, precision, padding).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_650);
// two lists of messages
let message_1: f64 = -127.;
let message_2: f64 = 72.7;
// generate secret keys
let rlwe_secret_key = RLWESecretKey::new(&RLWE128_1024_1);
let secret_key_before = LWESecretKey::new(&LWE128_630);
let secret_key_after = rlwe_secret_key.to_lwe_secret_key();
// bootstrapping key
let bootstrapping_key =
LWEBSK::new(&secret_key_before, &rlwe_secret_key, base_log, level);
// a list of messages that we encrypt
let ciphertext_1 =
LWE::encode_encrypt(&secret_key_before, message_1, &encoder_1).unwrap();
let ciphertext_2 =
LWE::encode_encrypt(&secret_key_before, message_2, &encoder_2).unwrap();
let ciphertext_out = ciphertext_1
.mul_from_bootstrap(&ciphertext_2, &bootstrapping_key)
.unwrap();
Return the size of one LWE ciphertext with the parameters of self
Output
- a usize with the size of a single LWE ciphertext
Removes nb bits of padding
Arguments
nb
- number of bits of padding to remove
use concrete::*;
// encoder
let encoder = Encoder::new(-2., 6., 4, 4).unwrap();
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// a message
let message: f64 = -1.;
// encode and encrypt
let mut ciphertext = LWE::encode_encrypt(&secret_key, message, &encoder).unwrap();
// removing 2 bits of padding
ciphertext.remove_padding_inplace(2).unwrap();
Trait Implementations
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
Print needed pieces of information about an LWE
Auto Trait Implementations
impl RefUnwindSafe for LWE
impl UnwindSafe for LWE
Blanket Implementations
Mutably borrows from an owned value. Read more