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.
- fileName¶
Load PyCtxt from this file. Requires non-empty scheme.
- Type:
str, pathlib.Path, 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:
- Returns:
Ciphertext resulting of addition.
- Return type:
- Raises:
TypeError – if other doesn’t have a valid type.
See also
- __invert__()¶
Relinarizes this ciphertext in-place.
Requires valid relinearization keys.
See also
- __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:
- Raises:
TypeError – if other doesn’t have a valid type.
See also
- __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
- __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:
- Raises:
TypeError – if other doesn’t have a valid type.
See also
- 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:
See also
- from_bytes(self, bytes content, scheme=None) void ¶
- from_bytes(bytes content, scheme) None
Recover the serialized ciphertext from a binary/bytes string.
- Parameters:
- 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.
- load(self, unicode fileName, scheme=None) size_t ¶
- load(self, str fileName, scheme) None
Load the ciphertext from a file.
- Parameters:
- 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.
- 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:
- _pyfhel¶
A Pyfhel instance, used for operations
- 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:
- scale¶
multiplying factor to encode values in ckks.
- Type:
double
- scheme¶
returns the scheme type.
Can be set to: none, bfv, bgv (INTEGER) or ckks (FRACTIONAL).
See also
to_Scheme_t()
- Type:
scheme