Pyfhel: Python for Homomorphic Encryption Libraries™

Release v (Installation)

https://travis-ci.org/ibarrond/Pyfhel.svg?branch=master https://badge.fury.io/py/Pyfhel.svg https://img.shields.io/badge/Maintained%3F-yes-brightgreen.svg https://img.shields.io/github/issues/ibarrond/Pyfhel.svg https://img.shields.io/pypi/pyversions/Pyfhel.svg

Pyfhel is the a Homomorphic Encryption library for Python.

Note

This library is built on top of C++ libraries. The current version supports only [SEAL](https://www.microsoft.com/en-us/research/project/simple-encrypted-arithmetic-library/)

If you’re interested in contributing, please have a look at the Github repository.

class Pyfhel.PyCtxt

Bases: object

Ciphertext of Pyfhel. Contains a value/vector of encrypted ints/doubles.

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

Attributes:

copy_ctxt (PyCtxt, optional): Other PyCtxt to deep copy.

from_bytes(bytes content)

Recover the serialized ciphertext from a binary/bytes string.

Args:

content: (bytes) Python bytes object containing the PyCtxt. encoding: (:obj: str) String describing the encoding: ‘int’ for

IntegerEncoding (default), ‘float’/’fractional’/’double’ for FractionalEncoding, ‘array’/’batch’/’matrix’ for BatchEncoding

from_file(str fileName)

Alias of load.

load(self, str fileName)

Load the ciphertext from a file.

Args:

fileName: (str) File where the ciphertext is retrieved from. encoding: (:obj: str) String describing the encoding: ‘int’ for

IntegerEncoding (default), ‘float’/’fractional’/’double’ for FractionalEncoding, ‘array’/’batch’/’matrix’ for BatchEncoding

save(str fileName)

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

Args:

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

size()

int: Actual size of the ciphertext.

size_capacity()

int: Maximum size the ciphertext can hold.

to_bytes()

Serialize the ciphertext into a binary/bytes string.

Return:
  • bytes: serialized ciphertext

to_file(Path fileName)

Alias of save using Pathlib.

class Pyfhel.PyPtxt

Bases: object

Plaintext of Pyfhel. Contains a value/vector of unencrypted ints/doubles.

This class references SEAL, PALISADE and HElib ciphertexts, using the one corresponding to the backend selected in Pyfhel (SEAL by default).

Attributes:

other_ptxt (PyPtxt, optional): Other PyPtxt to deep copy

is_zero()

bool: Flag to quickly check if it is empty

load()

Load the plaintext from a file.

Args:

fileName: (str) File where the plaintext is retrieved from.

save()

Save the ciphertext into a file.

Args:

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

to_string()

string: Polynomial representation of the plaintext

class Pyfhel.Pyfhel

Bases: object

PYFHEL, PYthon For Homomorphic Encryption Libraries.

Encrypted addition, multiplication, substraction, exponentiation of integers/doubles. Implementation of homomorphic encryption using SEAL/PALISADE/HELIB as backend. Pyfhel works with PyPtxt as plaintext class and PyCtxt as cyphertext class.

Example: >>> he = Pyfhel() >>> he.ContextGen(p=65537) >>> he.KeyGen(p=65537) >>> p1 = he.encode(4) >>> p2 = he.encode(2) >>> c1 = he.encrypt(p1) >>> c2 = he.encrypt(p2) >>> c1 = c1 + c2 >>> p_res = he.decrypt(c1) 6

add(PyCtxt ctxt, PyCtxt ctxt_other, bool in_new_ctxt=False)

Sum two PyCtxt ciphertexts.

Sums two ciphertexts. Encoding must be the same. Requires same context and encryption with same public key. The result is applied to the first ciphertext.

Args:
  • ctxt (PyCtxt): ciphertext whose values are added with ctxt_other.

  • ctxt_other (PyCtxt): ciphertext left untouched.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

add_plain(PyCtxt ctxt, PyPtxt ptxt, bool in_new_ctxt=False)

Sum a PyCtxt ciphertext and a PyPtxt plaintext.

Sums a ciphertext and a plaintext. Encoding must be the same. Requiressame context and encryption with same public key. The result is applied to the first ciphertext.

Args:
  • ctxt (PyCtxt): ciphertext whose values are added with ptxt.

  • ptxt (PyPtxt): plaintext left untouched.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

batchEnabled(self)

Flag of batch enabled.

Return:
  • bool: Result, True if enabled, False if disabled.

contextGen(long p, long m=2048, bool flagBatching=False, long base=2, long sec=128, int intDigits=64, int fracDigits = 32)

Generates Homomorphic Encryption context based on parameters.

Creates a HE context based in parameters, as well as integer, fractional and batch encoders. The HE context is required for any other function (encryption/decryption,encoding/decoding, operations)

Batch encoding is available if p is prime and p-1 is multiple of 2*m

Some tips:

m-> Higher allows more encrypted operations. In batch mode it

is the number of integers per ciphertext.

base-> Affects size of plaintexts and ciphertexts, and FRACTIONAL

encoding. See encryptFrac.

intDigits & fracDigits-> applicable with FRACTIONAL encoding,

out of ‘m’

Args:
  • p (long): Plaintext modulus. All operations are modulo p.

  • m (long=2048): Polynomial coefficient modulus. (Poly: 1*x^m+1)

  • flagBatching (bool=false): Set to true to enable batching.

  • base (long=2): Polynomial base.

  • sec (long=128): Security level equivalent in AES. 128 or 192.

  • intDigits (int=64): truncated positions for integer part.

  • fracDigits (int=32): truncated positions for fractional part.

Return:

None

decode(PyPtxt ptxt)

Decodes any valid PyPtxt into a value or vector.

Decodes a PyPtxt plaintext based on the current context. Outputs an integer (int), a truncated decimal (float), or in Batch mode a 1D vector of integers. Automatically selects the decoding function based on type.

Args:
  • ptxt (PyPtxt|int|double|np_1d_int_array): plaintext to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • int|double|vector[int]: the decoded value or vector;

Raise:
  • TypeError: if the plaintext doesn’t have a valid type.

decodeArray(PyPtxt ptxt)

Decodes a PyPtxt plaintext into a 1D numpy numpy of integers.

Decodes a PyPtxt plaintext into a 1D numpy array of integers based on the current context. PyPtxt encoding must be BATCH.

Args:
  • ptxt (PyPtxt): plaintext to decode.

Return:
  • vector[int64_t]: the vectort containing the decoded values

Raise:
  • RuntimeError: if the plaintext encoding isn’t ENCODING_T.BATCH.

decodeBatch(PyPtxt ptxt)

Decodes a PyPtxt plaintext into a 1D vector of integers.

Decodes a PyPtxt plaintext into a 1D vector of integers based on the current context. PyPtxt encoding must be BATCH.

Args:
  • ptxt (PyPtxt): plaintext to decode.

Return:
  • vector[int64_t]: the vectort containing the decoded values

Raise:
  • RuntimeError: if the plaintext encoding isn’t ENCODING_T.BATCH.

decodeFrac(PyPtxt ptxt)

Decodes a PyPtxt plaintext into a single float value.

Decodes a PyPtxt plaintext into a single float value based on the current context. PyPtxt encoding must be FRACTIONAL.

Args:
  • ptxt (PyPtxt): plaintext to decode.

Return:
  • double: the decoded float value

Raise:
  • RuntimeError: if the ciphertext encoding isn’t ENCODING_T.FRACTIONAL.

decodeInt(PyPtxt ptxt)

Decodes a PyPtxt plaintext into a single int value.

Decodes a PyPtxt plaintext into a single int value based on the current context. PyPtxt encoding must be INTEGER.

Args:
  • ptxt (PyPtxt=None): plaintext to decode.

Return:
  • int64_t: the decoded integer value

Raise:
  • RuntimeError: if the ciphertext encoding isn’t ENCODING_T.INTEGER.

decrypt(PyCtxt ctxt, bool decode_value=False, PyPtxt ptxt=None)

Decrypts any valid PyCtxt into either a PyPtxt ciphertext or a value.

Decrypts a PyCtxt ciphertext using the current secret key, based on the current context. Outputs an integer (int), a truncated decimal (float), a PyPtxt encoded plaintext, or in Batch mode a 1D numpy vector of integers. Can also return a PyPtxt by setting decode_value to True.

Selects the encryption function based on type.

If provided a plaintext, decrypts the ciphertext inside it.

Args:
  • ctxt (PyCtxt|int|double|np_1d_int_array): plaintext to encrypt.

  • decode_value (bool=False): return value or return ptxt.

  • ptxt (PyPtxt=None): Optional destination ciphertext.

Return:
  • PyPtxt|int|double|vector[int]: the decrypted result

Raise:
  • TypeError: if the plaintext doesn’t have a valid type.

decryptArray(PyCtxt ctxt)

Decrypts a PyCtxt ciphertext into a 1D numpy vector of integers.

Decrypts a PyCtxt ciphertext using the current secret key, based on the current context. If provided an output vector, decrypts the ciphertext inside it.

Args:
  • ctxt (PyCtxt): ciphertext to decrypt.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

Raise:
  • RuntimeError: if the ciphertext encoding isn’t ENCODING_T.BATCH.

decryptBatch(PyCtxt ctxt)

Decrypts a PyCtxt ciphertext into a 1D numpy vector of integers.

Decrypts a PyCtxt ciphertext using the current secret key, based on the current context. If provided an output vector, decrypts the ciphertext inside it.

Args:
  • ctxt (PyCtxt): ciphertext to decrypt.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

Raise:
  • RuntimeError: if the ciphertext encoding isn’t ENCODING_T.BATCH.

decryptFrac(PyCtxt ctxt)

Decrypts a PyCtxt ciphertext into a single float value.

Decrypts a PyCtxt ciphertext using the current secret key, based on the current context. PyCtxt encoding must be FRACTIONAL.

Args:
  • ctxt (PyCtxt): ciphertext to decrypt.

Return:
  • float: the decrypted float value

Raise:
  • RuntimeError: if the ctxt encoding isn’t ENCODING_T.FRACTIONAL.

decryptInt(PyCtxt ctxt)

Decrypts a PyCtxt ciphertext into a single int value.

Decrypts a PyCtxt ciphertext using the current secret key, based on the current context. PyCtxt encoding must be INTEGER.

Args:
  • ctxt (PyCtxt=None): ciphertext to decrypt.

Return:
  • int: the decrypted integer value

Raise:
  • RuntimeError: if the ctxt encoding isn’t ENCODING_T.INTEGER.

decryptPtxt(PyCtxt ctxt, PyPtxt ptxt=None)

Decrypts a PyCtxt ciphertext into a PyPtxt plaintext.

Decrypts a PyCtxt ciphertext using the current secret key, based on the current context. No regard to encoding (decode PyPtxt to obtain value).

Args:
  • ctxt (PyCtxt): ciphertext to decrypt.

  • ptxt (PyPtxt=None): Optional destination plaintext.

Return:
  • PyPtxt: the decrypted plaintext

encode(val_vec not None, PyPtxt ptxt=None)

Encodes any valid value/vector into a PyPtxt plaintext.

Encodes any valid value/vector based on the current context. Value/Vector must be an integer (int), a decimal that will get truncated (float), or in Batch mode a 1D vector of integers.

If provided a plaintext, encodes the vector inside it.

Args:
  • val_vec (int|float|vector[int64_t]): value/vector to encode.

  • ptxt (PyPtxt=None): Optional destination plaintext.

Return:
  • PyPtxt: the plaintext containing the encoded vector.

Raise:
  • TypeError: if the val_vec doesn’t have a valid type.

encodeArray(int64_t[::1] &arr, PyPtxt ptxt=None)

Encodes a 1D numpy array of integers into a PyPtxt plaintext.

Encodes a 1D numpy vector of integers based on the current context. Plaintext must be a 1D vector of integers. Requires batch mode. In Numpy the vector needs to be in ‘contiguous’ or ‘c’ mode. If provided a plaintext, encodes the vector inside it. Maximum size of the vector defined by parameter ‘m’ from context.

Args:
  • vec (vector[int64_t]): vector to encode.

  • ptxt (PyPtxt=None): Optional destination plaintext.

Return:
  • PyPtxt: the plaintext containing the encoded vector.

encodeBatch(vector[int64_t]& vec, PyPtxt ptxt=None)

Encodes a 1D list of integers into a PyPtxt plaintext.

Encodes a 1D vector of integers based on the current context. Plaintext must be a 1D vector of integers. Requires batch mode. In Numpy the vector needs to be in ‘contiguous’ or ‘c’ mode. If provided a plaintext, encodes the vector inside it. Maximum size of the vector defined by parameter ‘m’ from context.

Args:
  • vec (vector[int64_t]): vector to encode.

  • ptxt (PyPtxt=None): Optional destination plaintext.

Return:
  • PyPtxt: the plaintext containing the encoded vector.

encodeFrac(double &value, PyPtxt ptxt=None)

Encodes a single float value into a PyPtxt plaintext.

Encodes a single float value based on the current context. If provided a plaintext, encodes the value inside it.

Args:
  • value (float): value to encrypt.

  • ptxt (PyPtxt=None): Optional destination plaintext.

Return:
  • PyPtxt: the plaintext containing the encoded value

encodeInt(int64_t &value, PyPtxt ptxt=None)

Encodes a single int value into a PyPtxt plaintext.

Encodes a single intvalue based on the current context. If provided a plaintext, encodes the value inside it.

Args:
  • value (int): value to encrypt.

  • ptxt (PyPtxt=None): Optional destination plaintext.

Return:
  • PyPtxt: the plaintext containing the encoded value

encrypt(ptxt not None, PyCtxt ctxt=None)

Encrypts any valid plaintext into a PyCtxt ciphertext.

Encrypts a plaintext using the current secret key, based on the current context. Plaintext must be an integer (int), a decimal that will get truncated (double), a PyPtxt encoded plaintext, or in Batch mode a 1D numpy vector of integers. Selects the encryption function based on type.

If provided a ciphertext, encrypts the plaintext inside it.

Args:
  • ptxt (PyPtxt|int|double|np_1d_int_array): plaintext to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

Raise:
  • TypeError: if the plaintext doesn’t have a valid type.

encryptArray(int64_t[::1] arr, PyCtxt ctxt=None)

Encrypts a 1D numpy array of integers into a PyCtxt ciphertext.

Encrypts a 1D numpy array of integers using the current secret key, based on the current context. Plaintext must be a 1D numpy vector of integers. Requires batch mode. The vector needs to be in ‘contiguous’ or ‘c’ mode. If provided a ciphertext, encrypts the plaintext inside it.

Args:
  • ptxt (np.ndarray[int, ndim=1, mode=”c”]): plaintext to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

encryptBatch(vector[int64_t] vec, PyCtxt ctxt=None)

Encrypts a 1D vector of integers into a PyCtxt ciphertext.

Encrypts a 1D vector of integers using the current secret key, based on the current context. Plaintext must be a 1D numpy vector of integers. Requires batch mode. The vector needs to be in ‘contiguous’ or ‘c’ mode. If provided a ciphertext, encrypts the plaintext inside it.

Args:
  • ptxt (np.ndarray[int, ndim=1, mode=”c”]): plaintext to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

encryptFrac(double value, PyCtxt ctxt=None)

Encrypts a single float value into a PyCtxt ciphertext.

Encrypts a single value using the current secret key, based on the current context. Value must a decimal (float, double) that will get truncated both in the integer part (base^intDigits) and in the decimal part (base^fracDigits). If provided a ciphertext, encrypts the plaintext inside it.

Args:
  • value (float): value to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

encryptInt(int64_t value, PyCtxt ctxt=None)

Encrypts a single int value into a PyCtxt ciphertext.

Encrypts a single value using the current public key, based on the current context. Value must either be an integer (int64_t) If provided a ciphertext, encrypts the value inside it.

Args:
  • value (int): value to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

encryptPtxt(PyPtxt ptxt, PyCtxt ctxt=None)

Encrypts an encoded PyPtxt plaintext into a PyCtxt ciphertext.

Encrypts an encoded PyPtxt plaintext using the current secret key, based on the current context. Plaintext must be a PyPtxt. If provided a ciphertext, encrypts the plaintext inside it.

Args:
  • ptxt (PyPtxt): plaintext to encrypt.

  • ctxt (PyCtxt=None): Optional destination ciphertext.

Return:
  • PyCtxt: the ciphertext containing the encrypted plaintext

Raise:

TypeError: if the plaintext doesn’t have a valid type.

getbase()

Polynomial base.

Polynomial base of polynomials that conform cyphertexts and plaintexts.Affects size of plaintexts and ciphertexts, and FRACTIONAL encoding. See encryptFrac.

Return:
  • int: Polynomial base.

getflagBatch()

Flag for BATCH encoding mode.

If True, allows operations over vectors encrypted in single PyCtxt ciphertexts. Defined in context creation based on the chosen values of p and m, and activated in context creation with a flag.

Return:
  • bool: flag for enabled BATCH encoding and operating.

getfracDigits()

Decimal digits in FRACTIONAL encoding.

When encrypting/encoding double (FRACTIONAL encoding), truncated positions dedicated to deimal part, out of ‘m’ positions.

Return:
  • int: number of fractional digits.

getintDigits()

Integer digits in FRACTIONAL encoding.

When encrypting/encoding double (FRACTIONAL encoding), truncated positions dedicated to integer part, out of ‘m’ positions.

Return:
  • int: number of integer digits.

getm()

Plaintext coefficient of the current context.

The more, the bigger the ciphertexts are, thus allowing for more operations with correct decryption. Also, number of values in a ciphertext in BATCH encoding mode.

Return:
  • int: Plaintext coefficient.

getnSlots()

Maximum umber of slots fitting in a ciphertext in BATCH mode.

Return:
  • int: Maximum umber of slots.

getp()

Plaintext modulus of the current context.

All operations are modulo p.

Return:
  • int: Plaintext modulus.

getsec()

Security level equivalent in AES.

Return:
  • int: Security level equivalent in AES. Either 128 or 192.

keyGen()

Generates a pair of secret/Public Keys.

Based on the current context, initializes a public/secret key pair.

Args:

None

Return:

None

multiply(PyCtxt ctxt, PyCtxt ctxt_other, bool in_new_ctxt=False)

Multiply first PyCtxt ciphertext by the second PyCtxt ciphertext.

Multiplies two ciphertexts. Encoding must be the same. Requires same context and encryption with same public key. The result is applied to the first ciphertext.

Args:
  • ctxt (PyCtxt): ciphertext multiplied with ctxt_other.

  • ctxt_other (PyCtxt): ciphertext left untouched.

  • in_new_ctxt (bool=False): result in a newly created ciphertext.

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

multiply_plain(PyCtxt ctxt, PyPtxt ptxt, bool in_new_ctxt=False)

Multiply a PyCtxt ciphertext and a PyPtxt plaintext.

Multiplies a ciphertext and a plaintext. Encoding must be the same. Requires same context and encryption with same public key. The result is applied to the first ciphertext.

Args:
  • ctxt (PyCtxt): ciphertext whose values are multiplied with ptxt.

  • ptxt (PyPtxt): plaintext left untouched.

Return:
  • PyCtxt resulting ciphertext, either the input transformed or a new one

negate(PyCtxt ctxt, bool in_new_ctxt=False)

Negate PyCtxt ciphertext value/s.

Args:
  • ctxt (PyCtxt): ciphertext whose values are negated.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

noiseLevel(PyCtxt ctxt)

Computes the invariant noise budget (bits) of a PyCtxt ciphertext.

The invariant noise budget measures the amount of room there is for thenoise to grow while ensuring correct decryptions. Decrypts a PyCtxt ciphertext using the current secret key, based on the current context.

Args:
  • ctxt (PyCtxt): ciphertext to be measured.

Return:
  • int: the noise budget level

polyEval(PyCtxt ctxt, vector[int64_t] coeffPoly, bool in_new_ctxt=False)

Evaluates polynomial in PyCtxt ciphertext value/s.

Evaluates a polynomial given by integer coefficients. Requires previously initialized relinearization keys with relinearizeKeyGen(), since it applies relinearization after each multiplication.

Polynomial coefficients are in the form:

coeffPoly[0]*ctxt^2 + coeffPoly[1]*ctxt + coeffPoly[2]

Args:
  • ctxt (PyCtxt): ciphertext whose value/s are exponetiated.

  • coeffPoly (vector[int64_t]): Polynomial coefficients

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

polyEval_double(PyCtxt ctxt, vector[double] coeffPoly, bool in_new_ctxt=False)

Evaluates polynomial in PyCtxt ciphertext value/s.

Evaluates a polynomial given by float coefficients. Requires previously initialized relinearization keys with relinearizeKeyGen(), since it applies relinearization after each multiplication.

Polynomial coefficients are in the form:

coeffPoly[0]*ctxt^2 + coeffPoly[1]*ctxt + coeffPoly[2]

Args:
  • ctxt (PyCtxt): ciphertext whose value/s are exponetiated.

  • coeffPoly (vector[float]): Polynomial coefficients.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

power(PyCtxt ctxt, uint64_t expon, bool in_new_ctxt=False)

Exponentiates PyCtxt ciphertext value/s to expon power.

Performs an exponentiation over a cyphertext. Requires previously initialized relinearization keys with relinearizeKeyGen(), since it applies relinearization after each multiplication.

Args:
  • ctxt (PyCtxt): ciphertext whose value/s are exponetiated.

  • expon (uint64_t): exponent.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

relinBitCount(self)

Relinearization bit count for current evaluation keys.

Return:
  • long: [1-60], based on relinKeyGen parameter.

relinKeyGen(int bitCount, int size)

Generates a relinearization Key.

Generates a relinearization Key, used to reduce size of the ciphertexts when multiplying or exponentiating them. This is needed due to the fact that ciphertexts grow in size after encrypted mults/exponentiations.

Based on the current context, initializes one relinearization key.

Args:
  • bitCount (int): Bigger means faster but noisier (will require

    relinearization). Needs to be within [1, 60]

Return:

None

relinearize(PyCtxt ctxt)

Relinearizes a ciphertext.

Relinearizes a ciphertext. This functions relinearizes ctxt, reducing its size down to 2. If the size of encrypted is K+1, the given evaluation keys need to have size at least K-1.

To relinearize a ciphertext of size M >= 2 back to size 2, we actually need M-2 evaluation keys. Attempting to relinearize a too large ciphertext with too few evaluation keys will result in an exception being thrown.

Args:
  • bitCount (int): The bigger the faster but noisier (will require

    relinearization). Needs to be within [1, 60]

Return:

None

restoreContext(str fileName)

Restores current context from a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

restorepublicKey(str fileName)

Restores current public key from a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

restorerelinKey(str fileName)

Restores current relinearization keys from a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

restorerotateKey(str fileName)

Restores current rotation Keys from a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

restoresecretKey(str fileName)

Restores current secret key from a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

rotate(PyCtxt ctxt, int k, bool in_new_ctxt=False)

Rotates cyclically PyCtxt ciphertext values k positions.

Performs a cyclic rotation over a cyphertext encoded in BATCH mode. Requires previously initialized rotation keys with rotateKeyGen().

Args:
  • ctxt (PyCtxt): ciphertext whose values are rotated.

  • k (int): number of positions to rotate.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

rotateKeyGen(int bitCount)

Generates a rotation Key.

Generates a rotation Key, used in BATCH mode to rotate cyclically the values inside the encrypted vector.

Based on the current context, initializes one rotation key.

Args:
  • bitCount (int): Bigger means faster but noisier (will require

    relinearization). Needs to be within [1, 60]

Return:

None

saveContext(str fileName)

Saves current context in a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

savepublicKey(str fileName)

Saves current public key in a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

saverelinKey(str fileName)

Saves current relinearization keys in a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

saverotateKey(str fileName)

Saves current rotation Keys from a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

savesecretKey(str fileName)

Saves current secret key in a file

Args:
  • fileName (str): Name of the file.

Return:
  • bool: Result, True if OK, False otherwise.

square(PyCtxt ctxt, bool in_new_ctxt=False)

Square PyCtxt ciphertext value/s.

Args:
  • ctxt (PyCtxt): ciphertext whose values are squared.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

sub(PyCtxt ctxt, PyCtxt ctxt_other, bool in_new_ctxt=False)

Substracts one PyCtxt ciphertext from another.

Substracts one ciphertext from another. Encoding must be the same. Requires same context and encryption with same public key. The result is stored/applied to the first ciphertext.

Args:
  • ctxt (PyCtxt): ciphertext substracted by ctxt_other.

  • ctxt_other (PyCtxt): ciphertext being substracted from ctxt.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

sub_plain(PyCtxt ctxt, PyPtxt ptxt, bool in_new_ctxt=False)

Substracts a PyCtxt ciphertext and a plaintext.

Performs ctxt = ctxt - ptxt. Encoding must be the same. Requires same context and encryption with same public key. The result is stored/applied to the ciphertext.

Args:
  • ctxt (PyCtxt): ciphertext substracted by ptxt.

  • ptxt (PyPtxt): plaintext substracted from ctxt.

  • in_new_ctxt (bool=False): result in a newly created ciphertext

Return:
  • PyCtxt resulting ciphertext, the input transformed or a new one

Indices and tables