PyCtxt

class Pyfhel.PyCtxt(PyCtxt copy_ctxt=None, Pyfhel pyfhel=None, fileName=None, bytestring=None, scheme=None)

Bases: object

Ciphertext class of Pyfhel, contains a value/vector of encrypted ints/doubles.

This class references SEAL and PALISADE ciphertexts, using the one corresponding to the backend selected in Pyfhel. By default, it is SEAL.

__init__(PyCtxt copy_ctxt=None, Pyfhel pyfhel=None, fileName=None, bytestring=None, scheme=None)

Initializes an empty PyCtxt ciphertext.

To fill the ciphertext during initialization you can:
  • Provide a PyCtxt to deep copy.

  • Provide a pyfhel instance to act as its backend.

  • Provide a fileName and an scheme to load the data from a saved file.

copy_ctxt

Other PyCtxt to deep copy.

Type:

PyCtxt, optional

pyfhel

Pyfhel instance needed to operate.

Type:

Pyfhel, optional

fileName

Load PyCtxt from this file. Requires non-empty scheme.

Type:

str, pathlib.Path, optional

bytestring

Read PyCtxt from a bytes bytestring string, obtained by calling the to_bytes method.

Type:

bytes, optional

scheme

scheme type of the new PyCtxt.

Type:

str, type, int, optional

Methods

__add__(self, other)

__add__(other)

__sub__(self, other)

__sub__(other)

__neg__(self)

__neg__()

__mul__(self, other)

Multiplies this ciphertext with either another PyCtxt or a PyPtxt plaintext.

__pow__(self, exponent, modulo)

__pow__(exponent)

__rshift__(self, k)

__rshift__(k)

__invert__(self)

__invert__()

__repr__(self)

__repr__()

__bytes__(self)

__bytes__()

__init__(PyCtxt copy_ctxt=None, ...[, ...])

Initializes an empty PyCtxt ciphertext.

copy(self)

copy() -> PyCtxt

decrypt(self)

decrypt()

encode_operand(self, other)

encode_operand(other)

encrypt(self, value)

encrypt(value)

from_bytes(self, bytes content[, scheme])

from_bytes(bytes content, scheme)

get_multiplicative_inverse(self, other)

get_multiplicative_inverse(other)

load(self, str fileName[, scheme])

load(self, str fileName, scheme)

round_scale(self)

round_scale()

save(self, str fileName, str compr_mode=)

save(str fileName)

set_scale(self, double new_scale)

set_scale(double new_scale)

size(self)

Current size of the ciphertext.

sizeof_ciphertext(self, str compr_mode=)

Returns the number of bytes that will be written when saving the ciphertext.

to_bytes(self, str compr_mode=)

to_bytes()

Attributes

capacity

Maximum size the ciphertext can hold.

mod_level

returns the number of moduli consumed so far.

noiseBudget

Noise budget.

scale

multiplying factor to encode values in ckks.

scale_bits

number of bits in scale to encode values in ckks

scheme

returns the FHE scheme of this ciphertext.

API description

__add__(self, other)

__add__(other)

Sums this ciphertext with either another PyCtx or a PyPtxt plaintext.

Sums with a PyPtxt/PyCtxt, storing the result a new ciphertext.

Parameters:

other (PyCtxt, PyPtxt) – Second summand.

Returns:

Ciphertext resulting of addition.

Return type:

PyCtxt

Raises:

TypeError – if other doesn’t have a valid type.

See also

add()

__bytes__(self)

__bytes__()

Serialize current ciphertext to bytes

__invert__(self)

__invert__()

Relinarizes this ciphertext in-place.

Requires valid relinearization keys.

See also

relinearize()

__mul__(self, other)

Multiplies this ciphertext with either another PyCtxt or a PyPtxt plaintext.

Multiplies with a PyPtxt/PyCtxt, storing the result in a new ciphertext.

Parameters:

other (PyCtxt, PyPtxt) – Multiplier, to be multiplied with this ciphertext.

Returns:

Ciphertext resulting of multiplication

Return type:

PyCtxt

Raises:

TypeError – if other doesn’t have a valid type.

See also

multiply()

__neg__(self)

__neg__()

Negates this ciphertext.

__pow__(self, exponent, modulo)

__pow__(exponent)

Exponentiates this ciphertext to the desired exponent.

Exponentiates to the desired exponent.

Parameters:

exponent (int) – Exponent for the power.

See also

power()

__repr__(self)

__repr__()

Prints information about the current ciphertext

__rshift__(self, k)

__rshift__(k)

Rotates this ciphertext k positions to the right.

Parameters:

k (int) – Number of positions to rotate.

__sub__(self, other)

__sub__(other)

Subtracts this ciphertext with either another PyCtxt or a PyPtxt plaintext.

Subtracts with a PyPtxt/PyCtxt, storing the result in a new ciphertext.

Parameters:

other (PyCtxt, PyPtxt) – Substrahend, to be subtracted from this ciphertext.

Returns:

Ciphertext resulting of subtraction

Return type:

PyCtxt

Raises:

TypeError – if other doesn’t have a valid type.

See also

sub()

copy(self) PyCtxt

copy() -> PyCtxt

Returns a deep copy of the PyCtxt.

decrypt(self)

decrypt()

Decrypts itself using _pyfhel.

Parameters:

None

Returns:

value decrypted.

Return type:

int, float, np.array

See also

decrypt()

encode_operand(self, other)

encode_operand(other)

Encodes the given value into a PyPtxt using _pyfhel.

Parameters:

other (int, float, np.array, list) – Encodes accordingly to the type

Returns:

Encoded value

Return type:

PyPtxt

See also

encode()

encrypt(self, value)

encrypt(value)

Encrypts the given value using _pyfhel.

Parameters:

value (int, float, np.array) – Encrypts accordingly to the type

Returns:

None

See also

encrypt()

from_bytes(self, bytes content, scheme=None) void

from_bytes(bytes content, scheme)

Recover the serialized ciphertext from a binary/bytes string.

Parameters:
  • content (bytes) – Python bytes object containing the PyCtxt.

  • scheme (str, type, int, Scheme_t) –

    One of the following:

    • (‘int’, ‘INTEGER’, int, 1, Scheme_t.bfv) -> integer scheme.

    • (‘float’, ‘FRACTIONAL’, float, 2, Scheme_t.ckks) -> fractional scheme.

Returns:

None

See also

to_Scheme_t()

get_multiplicative_inverse(self, other)

get_multiplicative_inverse(other)

Returns the inverse of the given Value/s.

Parameters:

other (int|float|np.array) – Value/s to invert

Returns:

Inverse of the given Value/s, encoded to operate with self.

Return type:

PyPtxt

load(self, str fileName, scheme=None) size_t

load(self, str fileName, scheme)

Load the ciphertext from a file.

Parameters:
  • fileName – (str) Valid file where the ciphertext is retrieved from.

  • scheme (str, type, int, Scheme_t) –

    One of the following:

    • (‘int’, ‘INTEGER’, int, 1, Scheme_t.bfv) -> integer scheme.

    • (‘float’, ‘FRACTIONAL’, float, 2, Scheme_t.ckks) -> fractional scheme.

    • (3, Scheme_t.bgv) -> integer scheme.

Returns:

number of loaded bytes.

Return type:

size_t

See also

to_Scheme_t()

round_scale(self) void

round_scale()

Rounds the scale of the ciphertext to the nearest power of 2.

save(self, str fileName, str compr_mode='zstd') size_t

save(str fileName)

Save the ciphertext into a file. The file can new one or exist already, in which case it will be overwriten.

Parameters:
  • fileName – (str) File where the ciphertext will be stored.

  • compr_mode – (str) Compression mode. One of “none”, “zlib”, “zstd”.

Returns:

Number of bytes written.

Return type:

size_t

set_scale(self, double new_scale) void

set_scale(double new_scale)

Sets the scale of the ciphertext.

Parameters:

scale (double) – new scale of the ciphertext.

size(self) int

Current size of the ciphertext.

Returns:

size of this ciphertext

Return type:

int

sizeof_ciphertext(self, str compr_mode='none') size_t

Returns the number of bytes that will be written when saving the ciphertext.

Parameters:

compr_mode – (str) Compression mode. One of “none”, “zlib”, “zstd”.

Returns:

Number of bytes that will be written.

Return type:

size_t

to_bytes(self, str compr_mode='none') bytes

to_bytes()

Serialize the ciphertext into a binary/bytes string.

Parameters:

compr_mode – (str) Compression mode. One of “none”, “zlib”, “zstd”.

Returns:

serialized ciphertext

Return type:

bytes

_pyfhel

A Pyfhel instance, used for operations

capacity

Maximum size the ciphertext can hold.

Type:

int

mod_level

returns the number of moduli consumed so far.

Only usable in ckks.

Type:

mod_level

noiseBudget

Noise budget.

A value of 0 means that it cannot be decrypted correctly anymore.

See also

noiseLevel()

Type:

int

scale

multiplying factor to encode values in ckks.

Type:

double

scale_bits

number of bits in scale to encode values in ckks

Type:

int

scheme

returns the FHE scheme of this ciphertext.

Can be set to: none, bfv, bgv (INTEGER) or ckks (FRACTIONAL).

See also

to_Scheme_t()

Type:

scheme