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 ciphertexts
  • variances - the variance of the noise of the LWE ciphertext
  • dimension - the length the LWE mask
  • encoder - the encoder of the LWE ciphertext

Fields

ciphertext: LweCiphertext<Vec<Torus>>variance: f64dimension: usizeencoder: 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();

Encode a message and then directly encrypt the plaintext into an LWE structure

Arguments
  • sk - an LWE secret key
  • message - a message as u64
  • encoder - 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();

Encrypt a raw plaintext (a Torus element instead of a struct Plaintext) with the provided key and standard deviation

Arguments
  • sk - an LWE secret key
  • plaintext - a Torus element
  • std_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();

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

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 struct
  • new_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();

Compute an homomorphic addition between two LWE ciphertexts

Arguments
  • ct - an LWE struct
  • new_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();

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

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 consumed
  • messages - 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();

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 consumed
  • messages - a list of real messages as f64
Output
  • ConstantMaximumError - if one element of constants if bigger than max_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();

Compute a bootstrap and apply an arbitrary function to the LWE ciphertext

Argument
  • bsk - the bootstrapping key
  • f - the function to aply
  • encoder_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 multiplication
  • bsk - the bootstrapping key used to evaluate the function
  • n_self - the index of the ciphertext to multiply in the self struct
  • n_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

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

Print needed pieces of information about an LWE

Formats the value using the given formatter. Read more

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

Converts the given value to a String. 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.