Struct concrete::encoder::Encoder [−][src]
pub struct Encoder {
pub o: f64,
pub delta: f64,
pub nb_bit_precision: usize,
pub nb_bit_padding: usize,
pub round: bool,
}
Expand description
Structure describing one particular Encoding
Attributes
o
- the offset of the encodingdelta
- the delta of the encodingnb_bit_precision
- the minimum number of bits to represent a plaintextnb_bit_padding
- the number of bits set to zero in the MSB
Fields
o: f64
delta: f64
nb_bit_precision: usize
nb_bit_padding: usize
round: bool
Implementations
Instantiate a new Encoder with the provided interval as [min,max[ This encoder is meant to be use in an approximate context.
Arguments
min
- the minimum real value of the intervalmax
- the maximum real value of the intervalnb_bit_precision
- number of bits to represent a plaintextnb_bit_padding
- number of bits for left padding with zeros
Output
- a new instantiation of an Encoder
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// instantiation
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
pub fn new_rounding_context(
min: f64,
max: f64,
nb_bit_precision: usize,
nb_bit_padding: usize
) -> Result<Encoder, CryptoAPIError>
pub fn new_rounding_context(
min: f64,
max: f64,
nb_bit_precision: usize,
nb_bit_padding: usize
) -> Result<Encoder, CryptoAPIError>
Instantiate a new Encoder with the provided interval as [min,max[ This encoder is meant to be use in an exact computation context. It will round at encode and at decode.
Arguments
min
- the minimum real value of the intervalmax
- the maximum real value of the intervalnb_bit_precision
- number of bits to represent a plaintextnb_bit_padding
- number of bits for left padding with zeros
Output
- a new instantiation of an Encoder
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// instantiation
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
pub fn update_precision_from_variance(
&mut self,
variance: f64
) -> Result<usize, CryptoAPIError>
pub fn update_precision_from_variance(
&mut self,
variance: f64
) -> Result<usize, CryptoAPIError>
After an homomorphic operation, update an encoder using the variance
Arguments
variance
- variance
Output
- return the number of bits of precision affected by the noise
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// instantiation
let mut encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
let variance: f64 = f64::powi(2., -30);
let nb_bit_overlap: usize = encoder.update_precision_from_variance(variance).unwrap();
pub fn new_centered(
center: f64,
radius: f64,
nb_bit_precision: usize,
nb_bit_padding: usize
) -> Result<Encoder, CryptoAPIError>
pub fn new_centered(
center: f64,
radius: f64,
nb_bit_precision: usize,
nb_bit_padding: usize
) -> Result<Encoder, CryptoAPIError>
Instantiate a new Encoder with the provided interval as [center-radius,center+radius[
Arguments
center
- the center value of the intervalradius
- the distance between the center and the endpoints of the intervalnb_bit_precision
- number of bits to represent a plaintextnb_bit_padding
- number of bits for left padding with zeros
Output
- a new instantiation of an Encoder
Example
use concrete::Encoder;
// parameters
let center: f64 = 0.;
let radius: f64 = 5.4;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// instantiation
let encoder = Encoder::new_centered(center, radius, nb_bit_precision, nb_bit_padding).unwrap();
Encode one single message according to this Encoder parameters
Arguments
message
- a message as a f64
Output
- a new instantiation of an Plaintext containing only one encoded value (the one we just computed with this function)
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
let message = 0.6;
// creation of the encoder
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
// encoding
let m = encoder.encode_single(message).unwrap();
Decode one single plaintext according to this Encoder parameters
Arguments
ec
- an plaintext
Output
- the decoded value as a f64
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
let message = 0.6;
// creation of the encoder
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
// encoding
let m = encoder.encode_single(message).unwrap();
// decoding
let new_message = encoder.decode_single(m.plaintexts[0]).unwrap();
Encode several message according to this (one) Encoder parameters The output Plaintext will have plaintexts all computed with the same Encoder parameters
Arguments
messages
- a list of messages as a f64
Example
use concrete::Encoder;
// parameters
let (min, max): (f64, f64) = (0.2, 0.4);
let (precision, padding): (usize, usize) = (8, 4);
let messages: Vec<f64> = vec![0.3, 0.34];
let encoder = Encoder::new(min, max, precision, padding).unwrap();
let plaintexts = encoder.encode(&messages).unwrap();
Computes the smallest real number that this encoding can handle
Copy the content of the input encoder inside the self encoder
Argument
encoder
- the encoder to be copied
Example
use concrete::Encoder;
// parameters
let (min, max): (f64, f64) = (0.2, 0.4);
let (precision, padding): (usize, usize) = (8, 4);
let encoder_1 = Encoder::new(min, max, precision, padding).unwrap();
let mut encoder_2 = Encoder::zero();
encoder_2.copy(&encoder_1);
pub fn new_square_divided_by_four(
&self,
nb_bit_padding: usize
) -> Result<Encoder, CryptoAPIError>
pub fn new_square_divided_by_four(
&self,
nb_bit_padding: usize
) -> Result<Encoder, CryptoAPIError>
Crete a new encoder as if one computes a square function divided by 4
Argument
nb_bit_padding
- number of bits for left padding with zeros
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// instantiation
let encoder_in = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
let encoder_out = encoder_in
.new_square_divided_by_four(nb_bit_padding)
.unwrap();
Wrap the core_api encode function with the padding
Argument
m
- the message to encode
Example
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// message
let m = 0.3;
// instantiation
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
let plaintext = encoder.encode_core(m).unwrap();
Wrap the core_api encode function with the padding and allows to encode a message that is outside of the interval of the encoder It is used for correction after homomorphic computation
Argument
m
- the message to encode
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// message
let m = 1.2;
// instantiation
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
let plaintext = encoder.encode_outside_interval_operators(m).unwrap();
Wrap the core_api decode function with the padding and the rounding
Argument
pt
- the noisy plaintext
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// message
let m = 0.3;
// instantiation
let encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
let plaintext = encoder.encode_core(m).unwrap();
let new_message = encoder.decode_core(plaintext).unwrap();
Modify the encoding to be use after an homomorphic opposite
use concrete::Encoder;
// parameters
let min: f64 = 0.2;
let max: f64 = 0.8;
let nb_bit_precision = 8;
let nb_bit_padding = 4;
// message
let m = 0.3;
// instantiation
let mut encoder = Encoder::new(min, max, nb_bit_precision, nb_bit_padding).unwrap();
encoder.opposite_inplace();
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 Encoder
Auto Trait Implementations
impl RefUnwindSafe for Encoder
impl UnwindSafe for Encoder
Blanket Implementations
Mutably borrows from an owned value. Read more