Encoder#

class Encoder#

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

assert_equals(*args, **kwargs)#

Overloaded function.

  1. assert_equals(self: pyhelayers.Encoder, c: pyhelayers.CTile, title: str, expected_vals: numpy.ndarray[numpy.int32], eps: float = 1e-06, percent: bool = False) -> float

    Asserts that “c” and “expected_vals” are considered equal.

    "c" and “expected_vals” 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] - expected_vals[i]) < eps, foreach i. 2. percent == true and fabs(v[i] - expected_vals[i]) / fabs(v[i]) < eps, foreach i.

    The maximum found absolute or relative difference (depending on the value of “percent” flag) is returned.

    param c:

    The CTile to compare with expectedVals.

    type c:

    CTile

    param title:

    The title used in case of printing a failure message.

    type title:

    string

    param expected_vals:

    The vector to compare with “c”.

    type expected_vals:

    numpy.array

    param eps:

    Comparison tolerance. Defaults to 1e-6.

    type eps:

    double

    param percent:

    If true, the relative difference between “c” and “expectedVals” is checked. Otherwise, the absolute difference is checked. Defaults to false.

    type percent:

    bool

  2. assert_equals(self: pyhelayers.Encoder, c: pyhelayers.CTile, title: str, expected_vals: numpy.ndarray[numpy.float64], eps: float = 1e-06, percent: bool = False) -> float

    Asserts that “c” and “expected_vals” are considered equal. “c” and “expected_vals” 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] - expected_vals[i]) < eps, foreach i. 2. percent == true and fabs(v[i] - expected_vals[i]) / fabs(v[i]) < eps, foreach i. The maximum found absolute or relative difference (depending on the value of “percent” flag) is returned.

    param c:

    The CTile to compare with expectedVals.

    type c:

    CTile

    param title:

    The title used in case of printing a failure message.

    type title:

    string

    param expected_vals:

    The vector to compare with “c”.

    type expected_vals:

    numpy.array

    param eps:

    Comparison tolerance. Defaults to 1e-6.

    type eps:

    double

    param percent:

    If true, the relative difference between “c” and “expectedVals” is checked. Otherwise, the absolute difference is checked. Defaults to false.

    type percent:

    bool

  3. assert_equals(self: pyhelayers.Encoder, c: pyhelayers.CTile, title: str, expected_vals: numpy.ndarray[numpy.complex128], eps: float = 1e-06, percent: bool = False) -> float

    Asserts that “c” and “expected_vals” are considered equal. “c” and “expected_vals” 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] - expected_vals[i]) < eps, foreach i. 2. percent == true and fabs(v[i] - expected_vals[i]) / fabs(v[i]) < eps, foreach i. The maximum found absolute or relative difference (depending on the value of “percent” flag) is returned.

    param c:

    The CTile to compare with expectedVals.

    type c:

    CTile

    param title:

    The title used in case of printing a failure message.

    type title:

    string

    param expected_vals:

    The vector to compare with “c”.

    type expected_vals:

    numpy.array

    param eps:

    Comparison tolerance. Defaults to 1e-6.

    type eps:

    double

    param percent:

    If true, the relative difference between “c” and “expectedVals” is checked. Otherwise, the absolute difference is checked. Defaults to false.

    type percent:

    bool

decode_complex(self: pyhelayers.Encoder, src: pyhelayers.PTile) numpy.ndarray[numpy.complex128]#

Decodes the given PTile into an array of doubles.

Parameters:

src – The PTile to decode.

Returns:

the decoded array of doubles

Return type:

numpy.array

decode_double(self: pyhelayers.Encoder, src: pyhelayers.PTile) numpy.ndarray[numpy.float64]#

Decodes the given PTile into an array of doubles.

Parameters:

src – The PTile to decode.

Returns:

the decoded array of doubles

Return type:

numpy.array

decode_int(self: pyhelayers.Encoder, src: pyhelayers.PTile) numpy.ndarray[numpy.int32]#

Decodes the given PTile into an array of integers.

Parameters:

src – The PTile to decode.

Returns:

the decoded array of integers

Return type:

numpy.array

decrypt_decode_complex(self: pyhelayers.Encoder, ctxt: pyhelayers.CTile) numpy.ndarray[numpy.complex128]#

Decrypts and then decodes the given CTile into an array of complex numbers.

Parameters:

in – The CTile to decrypt and decode.

Return type:

numpy.array

decrypt_decode_double(self: pyhelayers.Encoder, ctxt: pyhelayers.CTile) numpy.ndarray[numpy.float64]#

Decrypts and then decodes the given CTile into an array of doubles.

Parameters:

in – The CTile to decrypt and decode.

Return type:

numpy.array

decrypt_decode_int(self: pyhelayers.Encoder, ctxt: pyhelayers.CTile) numpy.ndarray[numpy.int32]#

Decrypts and then decodes the given CTile into an array of integers.

Parameters:

in – The CTile to decrypt and decode.

Return type:

numpy.array

decrypt_decode_tile_double(self: pyhelayers.Encoder, src: pyhelayers.Tile) numpy.ndarray[numpy.float64]#

If “src” is a CTile, decrypts and decodes it into a numpy array of doubles. If “src” is a PTile, decodes it into a numpy array of doubles.

Parameters:

src (Tile) – The Tile to encrypt.

Returns:

the decrypt-decode result.

Return type:

numpy array of doubles

encode(*args, **kwargs)#

Overloaded function.

  1. encode(self: pyhelayers.Encoder, val: int, chain_index: int = -1) -> pyhelayers.PTile

    Encodes the given integer into all slots of a PTile. This is Usually more effecient than regular encoding.

    param val:

    The value to encode.

    type val:

    int

    param chain_index:

    If the HeContext used to create this PTile is a CKKS context, specifies the chain index of the resulting PTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted PTile.

    rtype:

    PTile

  2. encode(self: pyhelayers.Encoder, val: float, chain_index: int = -1) -> pyhelayers.PTile

    Encodes the given double into all slots of a PTile. This is Usually more effecient than regular encoding.

    param val:

    The value to encode.

    type val:

    Float

    param chain_index:

    If the HeContext used to create this PTile is a CKKS context, specifies the chain index of the resulting PTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted PTile.

    rtype:

    PTile

  3. encode(self: pyhelayers.Encoder, val: List[int], chain_index: int = -1) -> pyhelayers.PTile

    Encodes the given list of integers into a PTile. If the length of the given list is less than slot count, the empty slots of the PTile will be set to zero. If the length of the given list is more than slot count, a RuntimeError will be thrown.

    param vals:

    The plain integers list to encode.

    type input:

    list or numpy.array

    param chain_index:

    If the HeContext used to create this PTile is a CKKS context, specifies the chain index of the resulting PTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted PTile.

    rtype:

    numpy.array

    raises RuntimeError:

    If len(vals) is greater than the number of slots.

  4. encode(self: pyhelayers.Encoder, vals: List[float], chain_index: int = -1) -> pyhelayers.PTile

    Encodes the given list of doubles into a PTile. If the length of the given list is less than slot count, the empty slots of the PTile will be set to zero. If the length of the given list is more than slot count, a RuntimeError will be thrown.

    param vals:

    The plain double list to encode.

    type input:

    list or numpy.array

    param chain_index:

    If the HeContext used to create this PTile is a CKKS context, specifies the chain index of the resulting PTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted PTile.

    rtype:

    PTile

    raises RuntimeError:

    If len(vals) is greater than the number of slots.

  5. encode(self: pyhelayers.Encoder, vals: List[complex], chain_index: int = -1) -> pyhelayers.PTile

    Encodes the given list of complex numbers into a PTile. If the length of the given list is less than slot count, the empty slots of the PTile will be set to zero. If the length of the given list is more than slot count, a RuntimeError will be thrown.

    param vals:

    The plain list of complex numbers to encode.

    type input:

    list or numpy.array

    param chain_index:

    If the HeContext used to create this PTile is a CKKS context, specifies the chain index of the resulting PTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted PTile.

    rtype:

    PTile

    raises RuntimeError:

    If len(vals) is greater than the number of slots.

encode_encrypt(*args, **kwargs)#

Overloaded function.

  1. encode_encrypt(self: pyhelayers.Encoder, vals: List[int], chain_index: int = -1) -> pyhelayers.CTile

    Encodes and then encrypts the given list of integers into a CTile. if the length of the given list is less than slot count, the empty slots of the CTile will be set to zero. If the length of the given list is more than slot count, a RuntimeError will be thrown.

    param vals:

    The plain integers list to encrypt.

    type vals:

    list or numpy.array

    param chain_index:

    If the HeContext used to create this CTile is a CKKS context, specifies the chain index of the resulting CTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted CTile.

    rtype:

    CTile

    raises RuntimeError:

    If len(vals) is greater than the number of slots.

  2. encode_encrypt(self: pyhelayers.Encoder, vals: List[float], chain_index: int = -1) -> pyhelayers.CTile

    Encodes and then encrypts the given list of doubles into a CTile. if the length of the given list is less than slot count, the empty slots of the CTile will be set to zero. If the length of the given list is more than slot count, a RuntimeError will be thrown.

    param vals:

    The plain doubles list to encrypt.

    type vals:

    list or numpy.array

    param chain_index:

    If the HeContext used to create this CTile is a CKKS context, specifies the chain index of the resulting CTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted CTile.

    rtype:

    CTile

    raises RuntimeError:

    If len(vals) is greater than the number of slots.

  3. encode_encrypt(self: pyhelayers.Encoder, vals: List[complex], chain_index: int = -1) -> pyhelayers.CTile

    Encodes and then encrypts the given list of complex numbers into a CTile. if the length of the given list is less than slot count, the empty slots of the CTile will be set to zero. If the length of the given list is more than slot count, a RuntimeError will be thrown.

    param vals:

    The plain list of complex numbers to encrypt.

    type vals:

    list or numpy.array

    param chain_index:

    If the HeContext used to create this CTile is a CKKS context, specifies the chain index of the resulting CTile. Otherwise, this parameter is ignored. Defaults to -1.

    type chainIndex:

    int

    return:

    The encrpyted CTile.

    rtype:

    CTile

    raises RuntimeError:

    If len(vals) is greater than the number of slots.

encrypt_tile(self: pyhelayers.Encoder, src: pyhelayers.Tile) pyhelayers.CTile#

If “src” is a PTile, encrypts it and returns the resulting CTile. If “src” is a CTile, returns a copy of it.

Parameters:

src (Tile) – The Tile to encrypt.

Returns:

The encrypted CTile.

Return type:

CTile

get_decrypt_added_noise_enabled(self: pyhelayers.Encoder) bool#

Returns a boolean indicating whether to enable the decrypt added noise.

get_decrypt_added_noise_precision(self: pyhelayers.Encoder) int#

Returns an integer indicating precision to be preserved.

Raises:

RuntimeError – If the undelying library does not support decrypt added noise.

get_default_device(self: pyhelayers.Encoder) pyhelayers.DeviceType#

Returns the default device of the ciphertexts and plaintexts generated using this HeContext.

Return type:

DeviceType

get_default_scale(self: pyhelayers.Encoder, chain_index: int) float#

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.

Parameters:

chain_index – The chain index, may affect the sacle used by the scheme’s default if no other default scale was set.

Returns:

The default scale

Return type:

double

print_error_stats(self: pyhelayers.Encoder, actual_c: pyhelayers.CTile, expected_z: numpy.ndarray[numpy.float64], as_csv: bool = False) str#

Prints statstical information relating to the difference between “actual_c” and “expected_z”.

Parameters:
  • actual_c – The CTile to decode-decrypt and to compare to expected_z.

  • expected_z (numpy.array) – The vector of doubles to compare actual_c with.

  • as_csv (bool) – Whether to print the stats as CSV. Defaults to false.

restore_default_scale(self: pyhelayers.Encoder) None#

Restores the default scale to the default scale defined by the HeContext object.

set_decrypt_added_noise_enabled(self: pyhelayers.Encoder, val: bool) None#

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:

val (bool) – A boolean indicating whether to enable the decrypt added noise.

set_decrypt_added_noise_precision(self: pyhelayers.Encoder, arg0: int) None#

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:

val – An integer indicating precision to be preserved.

Raises:

RuntimeError – If the undelying library does not support decrypt added noise.

set_default_device(self: pyhelayers.Encoder, device: pyhelayers.DeviceType) None#

Sets the default device of the ciphertexts and plaintexts generated using this HeContext.

Parameters:

device – the new default device

set_default_scale(self: pyhelayers.Encoder, scale: float) None#

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:

scale (double) – The new default scale.