helayers::Encoder Class Reference

A class used to encode, encrypt, decode and decrypt ciphertexts and plaintexts. More...

#include <Encoder.h>

Public Member Functions

 Encoder (HeContext &he)
 Constructs a ready to use object. More...
 
 Encoder (const Encoder &src)=delete
 Copy constructor deleted. Construct new objects when needed.
 
Encoderoperator= (const Encoder &src)=delete
 Assignment deleted. Construct new objects when needed.
 
void setDefaultScale (double scale)
 Default scale is a parameter relevant to CKKS-like schemes. More...
 
double getDefaultScale () const
 Returns the scale used by the encoder as set by setDefaultScale. More...
 
void restoreDefaultScale ()
 Restores the default scale to the default scale defined by the HeContext object.
 
void setDecryptAddedNoiseEnabled (bool val)
 Allows to determine whether to enable the decrypt added noise. More...
 
bool getDecryptAddedNoiseEnabled () const
 Returns a boolean indicating whether to enable the decrypt added noise.
 
void setDecryptAddedNoisePrecision (int val)
 Allows to set the level of precision to be preserved in the process of adding decrypt noise, if the decrypt added noise is enabled. More...
 
int getDecryptAddedNoisePrecision () const
 Returns an integer indicating precision to be preserved. More...
 
void encode (PTile &res, int val, int chainIndex=-1) const
 Encodes the given int into all slots of a PTile. More...
 
void encode (PTile &res, double val, int chainIndex=-1) const
 Encodes the given double into all slots of a PTile. More...
 
void encode (PTile &res, const std::vector< int > &vals, int chainIndex=-1) const
 Encodes the given vector of integers into a PTile. More...
 
void encode (PTile &res, const std::vector< long > &vals, int chainIndex=-1) const
 Encodes the given vector of longs into a PTile. More...
 
void encode (PTile &res, const std::vector< double > &vals, int chainIndex=-1) const
 Encodes the given vector of doubles into a PTile. More...
 
void encode (PTile &res, const std::vector< std::complex< double >> &vals, int chainIndex=-1) const
 Encodes the given vector of complex numbers into a PTile. More...
 
std::vector< int > decodeInt (const PTile &src) const
 Decodes the value of the given PTile into a vector of ints. More...
 
std::vector< long > decodeLong (const PTile &src) const
 Decodes the value of the given PTile into a vector of longs. More...
 
std::vector< double > decodeDouble (const PTile &src) const
 Decodes the value of the given PTile into a vector of doubles. More...
 
std::vector< std::complex< double > > decodeComplex (const PTile &src) const
 Decodes the value of the given PTile into a vector of complex<double>. More...
 
void encrypt (CTile &res, const PTile &src) const
 Encrypts "src" into "res". More...
 
void decrypt (PTile &res, const CTile &src) const
 Encrypts "src" into "res". More...
 
void encodeEncrypt (CTile &res, const std::vector< int > &vals, int chainIndex=-1) const
 Encodes and then encrypts the given vector into a CTile. More...
 
void encodeEncrypt (CTile &res, const std::vector< long > &vals, int chainIndex=-1) const
 Encodes and then encrypts the given vector into a CTile. More...
 
void encodeEncrypt (CTile &res, const std::vector< double > &vals, int chainIndex=-1) const
 Encodes and then encrypts the given vector into a CTile. More...
 
void encodeEncrypt (CTile &res, const std::vector< std::complex< double >> &vals, int chainIndex=-1) const
 Encodes and then encrypts the given vector into a CTile. More...
 
std::vector< int > decryptDecodeInt (const CTile &src) const
 Decodes and then decryots the given CTile into a vector of ints. More...
 
std::vector< long > decryptDecodeLong (const CTile &src) const
 Decodes and then decryots the given CTile into a vector of ints. More...
 
std::vector< double > decryptDecodeDouble (const CTile &src) const
 Decodes and then decryots the given CTile into a vector of doubles. More...
 
std::vector< std::complex< double > > decryptDecodeComplex (const CTile &src) const
 Decodes and then decryots the given CTile into a vector of complex<double>. More...
 
void printErrorStats (CTile &actualC, std::vector< double > expectedZ, std::ostream &out=std::cout, bool asCsv=false) const
 Prints statstical information relating to the difference between "actualC" and "expectedZ". More...
 
int validateChainIndex (int chainIndex) const
 checks whether a chain index is valid. More...
 
double assertEquals (const CTile &c, const std::string &title, const std::vector< int > &expectedVals, double eps=1e-6, bool percent=false) const
 Asserts that "c" and "expectedVals" are considered equal. More...
 
double assertEquals (const CTile &c, const std::string &title, const std::vector< long > &expectedVals, double eps=1e-6, bool percent=false) const
 Asserts that "c" and "expectedVals" are considered equal. More...
 
double assertEquals (const CTile &c, const std::string &title, const std::vector< double > &expectedVals, double eps=1e-6, bool percent=false) const
 Asserts that "c" and "expectedVals" are considered equal. More...
 
double assertEquals (const CTile &c, const std::string &title, const std::vector< std::complex< double >> &expectedVals, double eps=1e-6, bool percent=false) const
 Asserts that "c" and "expectedVals" are considered equal. More...
 
const AbstractEncodergetImpl () const
 Reserved for debugging and internal use.
 
template<typename T >
vector< T > verifyEncodeVecSize (const vector< T > &vec) const
 

Detailed Description

A class used to encode, encrypt, decode and decrypt ciphertexts and plaintexts.

Constructor & Destructor Documentation

◆ Encoder()

helayers::Encoder::Encoder ( HeContext he)

Constructs a ready to use object.

Parameters
[in]hethe underlying context.

Member Function Documentation

◆ assertEquals() [1/4]

double helayers::Encoder::assertEquals ( const CTile c,
const std::string &  title,
const std::vector< double > &  expectedVals,
double  eps = 1e-6,
bool  percent = false 
) const

Asserts that "c" and "expectedVals" are considered equal.

"c" and "expectedVals" will be considered equal if when decrypting and decoding "c" into a vector, v, one of the following holds:

  1. percent == false and fabs(v[i] - expectedVals[i]) < eps, foreach i.
  2. percent == true and fabs(v[i] - expectedVals[i]) / fabs(v[i]) < eps, foreach i. The maximum found absolute or relative difference (depending on the value of "percent" flag) is returned.
    Parameters
    [in]cThe CTile to compare with expectedVals
    [in]titleThe title used in case of printing a failure message.
    [in]expectedValsThe vector to compare with "c".
    [in]epsComparison tolerance
    ]in]percent If true, the relative difference between "c" and "expectedVals" is checked. Otherwise, the absolute difference is checked.

◆ assertEquals() [2/4]

double helayers::Encoder::assertEquals ( const CTile c,
const std::string &  title,
const std::vector< int > &  expectedVals,
double  eps = 1e-6,
bool  percent = false 
) const

Asserts that "c" and "expectedVals" are considered equal.

"c" and "expectedVals" will be considered equal if when decrypting and decoding "c" into a vector, v, one of the following holds:

  1. percent == false and fabs(v[i] - expectedVals[i]) < eps, foreach i.
  2. percent == true and fabs(v[i] - expectedVals[i]) / fabs(v[i]) < eps, foreach i. The maximum found absolute or relative difference (depending on the value of "percent" flag) is returned.
    Parameters
    [in]cThe CTile to compare with expectedVals
    [in]titleThe title used in case of printing a failure message.
    [in]expectedValsThe vector to compare with "c".
    [in]epsComparison tolerance
    ]in]percent If true, the relative difference between "c" and "expectedVals" is checked. Otherwise, the absolute difference is checked.

◆ assertEquals() [3/4]

double helayers::Encoder::assertEquals ( const CTile c,
const std::string &  title,
const std::vector< long > &  expectedVals,
double  eps = 1e-6,
bool  percent = false 
) const

Asserts that "c" and "expectedVals" are considered equal.

"c" and "expectedVals" will be considered equal if when decrypting and decoding "c" into a vector, v, one of the following holds:

  1. percent == false and fabs(v[i] - expectedVals[i]) < eps, foreach i.
  2. percent == true and fabs(v[i] - expectedVals[i]) / fabs(v[i]) < eps, foreach i. The maximum found absolute or relative difference (depending on the value of "percent" flag) is returned.
    Parameters
    [in]cThe CTile to compare with expectedVals
    [in]titleThe title used in case of printing a failure message.
    [in]expectedValsThe vector to compare with "c".
    [in]epsComparison tolerance
    ]in]percent If true, the relative difference between "c" and "expectedVals" is checked. Otherwise, the absolute difference is checked.

◆ assertEquals() [4/4]

double helayers::Encoder::assertEquals ( const CTile c,
const std::string &  title,
const std::vector< std::complex< double >> &  expectedVals,
double  eps = 1e-6,
bool  percent = false 
) const

Asserts that "c" and "expectedVals" are considered equal.

"c" and "expectedVals" will be considered equal if when decrypting and decoding "c" into a vector, v, one of the following holds:

  1. percent == false and fabs(v[i] - expectedVals[i]) < eps, foreach i.
  2. percent == true and fabs(v[i] - expectedVals[i]) / fabs(v[i]) < eps, foreach i. The maximum found absolute or relative difference (depending on the value of "percent" flag) is returned.
    Parameters
    [in]cThe CTile to compare with expectedVals
    [in]titleThe title used in case of printing a failure message.
    [in]expectedValsThe vector to compare with "c".
    [in]epsComparison tolerance
    ]in]percent If true, the relative difference between "c" and "expectedVals" is checked. Otherwise, the absolute difference is checked.

◆ decodeComplex()

vector< complex< double > > helayers::Encoder::decodeComplex ( const PTile src) const

Decodes the value of the given PTile into a vector of complex<double>.

Parameters
[in]srcThe PTile to decode.

◆ decodeDouble()

vector< double > helayers::Encoder::decodeDouble ( const PTile src) const

Decodes the value of the given PTile into a vector of doubles.

Parameters
[in]srcThe PTile to decode.

◆ decodeInt()

vector< int > helayers::Encoder::decodeInt ( const PTile src) const

Decodes the value of the given PTile into a vector of ints.

If the underlying FHE scheme is a scheme that supports floating point values, then "src" is first decrypted into a vector of doubles and then rounded to the nearest integers.

Parameters
[in]srcThe PTile to decode.

◆ decodeLong()

vector< long > helayers::Encoder::decodeLong ( const PTile src) const

Decodes the value of the given PTile into a vector of longs.

If the underlying FHE scheme is a scheme that supports floating point values, then "src" is first decrypted into a vector of doubles and then rounded to the nearest integers.

Parameters
[in]srcThe PTile to decode.

◆ decrypt()

void helayers::Encoder::decrypt ( PTile res,
const CTile src 
) const

Encrypts "src" into "res".

Parameters
[out]resThe resulting PTile.
[in]srcThe CTile to encrypt

◆ decryptDecodeComplex()

vector< complex< double > > helayers::Encoder::decryptDecodeComplex ( const CTile src) const

Decodes and then decryots the given CTile into a vector of complex<double>.

Parameters
[in]srcThe CTile to decode and decrypt.

◆ decryptDecodeDouble()

vector< double > helayers::Encoder::decryptDecodeDouble ( const CTile src) const

Decodes and then decryots the given CTile into a vector of doubles.

Parameters
[in]srcThe CTile to decode and decrypt.

◆ decryptDecodeInt()

vector< int > helayers::Encoder::decryptDecodeInt ( const CTile src) const

Decodes and then decryots the given CTile into a vector of ints.

If the underlying FHE scheme is a scheme that supports floating point values, then "src" is first decrypted into a vector of doubles and then rounded to the nearest integers.

Parameters
[in]srcThe CTile to decode and decrypt.

◆ decryptDecodeLong()

vector< long > helayers::Encoder::decryptDecodeLong ( const CTile src) const

Decodes and then decryots the given CTile into a vector of ints.

If the underlying FHE scheme is a scheme that supports floating point values, then "src" is first decrypted into a vector of doubles and then rounded to the nearest integers.

Parameters
[in]srcThe CTile to decode and decrypt.

◆ encode() [1/6]

void helayers::Encoder::encode ( PTile res,
const std::vector< double > &  vals,
int  chainIndex = -1 
) const

Encodes the given vector of doubles into a PTile.

If the size of the given vector is less than slot count, the empty slots of the PTile will be set to zero. If the size of the given vector is more than slot count, a untime_error will be thrown.

Parameters
[out]resThe encoded PTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting PTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encode() [2/6]

void helayers::Encoder::encode ( PTile res,
const std::vector< int > &  vals,
int  chainIndex = -1 
) const

Encodes the given vector of integers into a PTile.

If the size of the given vector is less than slot count, the empty slots of the PTile will be set to zero. If the size of the given vector is more than slot count, a untime_error will be thrown.

Parameters
[out]resThe encoded PTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting PTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encode() [3/6]

void helayers::Encoder::encode ( PTile res,
const std::vector< long > &  vals,
int  chainIndex = -1 
) const

Encodes the given vector of longs into a PTile.

If the size of the given vector is less than slot count, the empty slots of the PTile will be set to zero. If the size of the given vector is more than slot count, a untime_error will be thrown.

Parameters
[out]resThe encoded PTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting PTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encode() [4/6]

void helayers::Encoder::encode ( PTile res,
const std::vector< std::complex< double >> &  vals,
int  chainIndex = -1 
) const

Encodes the given vector of complex numbers into a PTile.

If the size of the given vector is less than slot count, the empty slots of the PTile will be set to zero. If the size of the given vector is more than slot count, a untime_error will be thrown.

Parameters
[out]resThe encoded PTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting PTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encode() [5/6]

void helayers::Encoder::encode ( PTile res,
double  val,
int  chainIndex = -1 
) const

Encodes the given double into all slots of a PTile.

This is usually more efficient than regular encoding.

Parameters
[out]resThe encoded PTile
[in]valThe value to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting PTile. otherwise, this parameter is ignored.

◆ encode() [6/6]

void helayers::Encoder::encode ( PTile res,
int  val,
int  chainIndex = -1 
) const

Encodes the given int into all slots of a PTile.

This is usually more efficient than regular encoding.

Parameters
[out]resThe encoded PTile
[in]valThe value to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting PTile. otherwise, this parameter is ignored.

◆ encodeEncrypt() [1/4]

void helayers::Encoder::encodeEncrypt ( CTile res,
const std::vector< double > &  vals,
int  chainIndex = -1 
) const

Encodes and then encrypts the given vector into a CTile.

if the size of the given vector is less than slot count, the empty slots of the CTile will be set to zero. If the size of the given vector is more than slot count, a runtime_error will be thrown.

Parameters
[out]resThe resulting CTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting CTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encodeEncrypt() [2/4]

void helayers::Encoder::encodeEncrypt ( CTile res,
const std::vector< int > &  vals,
int  chainIndex = -1 
) const

Encodes and then encrypts the given vector into a CTile.

if the size of the given vector is less than slot count, the empty slots of the CTile will be set to zero. If the size of the given vector is more than slot count, a untime_error will be thrown.

Parameters
[out]resThe resulting CTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting CTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encodeEncrypt() [3/4]

void helayers::Encoder::encodeEncrypt ( CTile res,
const std::vector< long > &  vals,
int  chainIndex = -1 
) const

Encodes and then encrypts the given vector into a CTile.

if the size of the given vector is less than slot count, the empty slots of the CTile will be set to zero. If the size of the given vector is more than slot count, a untime_error will be thrown.

Parameters
[out]resThe resulting CTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting CTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorif vals.size() > slot count

◆ encodeEncrypt() [4/4]

void helayers::Encoder::encodeEncrypt ( CTile res,
const std::vector< std::complex< double >> &  vals,
int  chainIndex = -1 
) const

Encodes and then encrypts the given vector into a CTile.

if the size of the given vector is less than slot count, the empty slots of the CTile will be set to zero. If the size of the given vector is more than slot count, a runtime_error will be thrown.

Parameters
[out]resThe resulting CTile
[in]valsThe vector to encode
[in]chainIndexIf HeContext is a CKKS context, specifies the chainIndex of the resulting CTile. otherwise, this parameter is ignored.
Exceptions
runtime_errorIf vals.size() > slot count
runtime_errorIf the underlying FHE scheme is a scheme that does not support complex values (such as BGV, for example).

◆ encrypt()

void helayers::Encoder::encrypt ( CTile res,
const PTile src 
) const

Encrypts "src" into "res".

Parameters
[out]resThe resulting CTile.
[in]srcThe PTile to encrypt

◆ getDecryptAddedNoisePrecision()

int helayers::Encoder::getDecryptAddedNoisePrecision ( ) const

Returns an integer indicating precision to be preserved.

Exceptions
runtime_errorIf the undelying library does not support decrypt added noise

◆ getDefaultScale()

double helayers::Encoder::getDefaultScale ( ) const

Returns the scale used by the encoder as set by setDefaultScale.

If no default was previously set, return the default scale define by the HeContext object.

◆ printErrorStats()

void helayers::Encoder::printErrorStats ( CTile actualC,
std::vector< double >  expectedZ,
std::ostream &  out = std::cout,
bool  asCsv = false 
) const

Prints statstical information relating to the difference between "actualC" and "expectedZ".

Parameters
[in]actualCThe CTile to decode-decrypt and to compare to expectedZ.
[in]expectedZThe vector of doubles to compare actualC with.
[in]outThe ostream to print to.

◆ setDecryptAddedNoiseEnabled()

void helayers::Encoder::setDecryptAddedNoiseEnabled ( bool  val)

Allows to determine whether to enable the decrypt added noise.

The default behaviour for libraries that supports this feature is to have it enabled. Decrypt added noise is relevant to CKKS scheme for the purpose of ensuring security. reference: "On the Security of Homomorphic Encryption on Approximate Numbers", Baiyu Li and Daniele Micciancio, https://eprint.iacr.org/2020/1533.pdf

Parameters
[in]valA boolean indicating whether to enable the decrypt added noise.

◆ setDecryptAddedNoisePrecision()

void helayers::Encoder::setDecryptAddedNoisePrecision ( int  val)

Allows to set the level of precision to be preserved in the process of adding decrypt noise, if the decrypt added noise is enabled.

If decrypt added noise is not enabled, this value is ignored.

Parameters
[in]valAn integer indicating precision to be preserved.
Exceptions
runtime_errorIf the undelying library does not support decrypt added noise

◆ setDefaultScale()

void helayers::Encoder::setDefaultScale ( double  scale)

Default scale is a parameter relevant to CKKS-like schemes.

It is the scale used to encode values into plaintext/ciphertext. If left unset, the encoder will use the scheme's default. After setting, the encoder will use the given value. setDefaultScale sets the scale that the encoder will use henceforth.

Parameters
[in]scaleThe new default scale.

◆ validateChainIndex()

int helayers::Encoder::validateChainIndex ( int  chainIndex) const

checks whether a chain index is valid.

Parameters
[in]chainIndexTo check its validity.

The documentation for this class was generated from the following files:
  • /opt/IBM/FHE-distro/ML-HElib/src/helayers/hebase/Encoder.h
  • /opt/IBM/FHE-distro/ML-HElib/src/helayers/hebase/Encoder.cpp