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__(other)

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

__sub__(other)

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

__neg__()

Negates this ciphertext.

__mul__

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

__pow__(exponent)

Exponentiates this ciphertext to the desired exponent.

__rshift__(k)

Rotates this ciphertext k positions to the right.

__invert__()

Relinarizes this ciphertext in-place.

__repr__()

Prints information about the current ciphertext

__bytes__()

Serialize current ciphertext to bytes

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

Initializes an empty PyCtxt ciphertext.

copy(-> PyCtxt)

Returns a deep copy of the PyCtxt.

decrypt()

Decrypts itself using _pyfhel.

encode_operand()

Encodes the given value into a PyPtxt using _pyfhel.

encrypt()

Encrypts the given value using _pyfhel.

from_bytes(-> void)

Recover the serialized ciphertext from a binary/bytes string.

get_multiplicative_inverse()

Returns the inverse of the given Value/s.

load(-> size_t)

Load the ciphertext from a file.

round_scale(-> void)

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

save(-> size_t)

Save the ciphertext into a file.

set_scale(-> void)

Sets the scale of the ciphertext.

size(self)

Current size of the ciphertext.

sizeof_ciphertext(self, unicode compr_mode=u)

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

to_bytes(-> bytes)

Serialize the ciphertext into a binary/bytes string.

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 scheme type.

API description

__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__() None

Serialize current ciphertext to bytes

__invert__()

Relinarizes this ciphertext in-place.

Requires valid relinearization keys.

See also

relinearize()

__mul__()

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

Negates this ciphertext.

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

Prints information about the current ciphertext

__rshift__(k)

Rotates this ciphertext k positions to the right.

Parameters:

k (int) – Number of positions to rotate.

__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() None

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) None

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) None

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) None

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) None

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, unicode fileName, scheme=None) size_t
load(self, str fileName, scheme) None

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

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

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

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) None

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, unicode compr_mode=u'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, unicode compr_mode=u'none') bytes
to_bytes() None

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 scheme type.

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

See also

to_Scheme_t()

Type:

scheme