Homomorphic Encryption Controller

class FHEONHEController

Public Functions

inline FHEONHEController(CryptoContext<DCRTPoly> ctx)
inline CryptoContext<DCRTPoly> getContext() const
void generate_context(int ringDim = 14, int numSlots = 12, int mlevelBootstrap = 10, bool serialize = true)

Simplified version of generate_context using standard values for unspecified parameters.

This function sets up the FHE context with default parameters for all unspecified values, simplifying context generation for typical use cases.

Parameters:
  • ringDim – Ring dimension.

  • numSlots – Number of slots (batch size).

  • mlevelBootstrap – Multiplication level after bootstrapping.

  • serialize – Whether to serialize and save keys.

void generate_context(int ringDim = 15, int numSlots = 14, int mlevelBootstrap = 10, int dcrtBits = 55, int firstMod = 56, int numDigits = 3, vector<uint32_t> levelBudget = {4, 4}, bool serialize = true)

Generate the full FHE context for the project.

This function sets up the FHE context with all specified parameters, allowing fine-grained control over scaling, decomposition, and level budgets. Keys can be optionally serialized and saved.

Parameters:
  • ringDim – Ring dimension.

  • numSlots – Number of slots (batch size).

  • mlevelBootstrap – Multiplication level after bootstrapping.

  • dcrtBits – Scaling factor for DCRT representation.

  • firstMod – Scaling factor for the first coefficients.

  • numDigits – Number of digits used in key decomposition.

  • levelBudget – Vector of budget levels.

  • serialize – Whether to serialize and save keys.

void generate_bootstrapping_keys(int bootstrap_slots, string filename, bool serialize)

Generate the bootstrapping keys for the FHE context.

This function generates bootstrapping keys for the specified number of slots. The generated keys can be optionally serialized and saved to a file.

Parameters:
  • bootstrap_slots – Number of bootstrapping slots.

  • filename – Filename to use when saving the keys.

  • serialize – Whether to serialize and save the bootstrapping keys.

void generate_rotation_keys(vector<int> rotations, string filename = "", bool serialize = true)

Generate and serialize rotation keys for the FHE context.

This function generates rotation keys for the specified rotation positions. The generated keys can be optionally serialized and saved to a file.

Parameters:
  • rotations – Vector of rotation positions to generate keys for.

  • filename – Filename to use when saving the rotation keys.

  • serialize – Whether to serialize and save the rotation keys.

void generate_bootstrapping_and_rotation_keys(vector<int> rotations, int bootstrap_slots, const string &filename, bool serialize)

Generate and serialize both rotation keys and bootstrapping keys for the FHE context.

This function generates rotation keys for the specified rotation positions and bootstrapping keys for the given number of slots. The generated keys can be optionally serialized and saved to a file.

Parameters:
  • rotations – Vector of rotation positions to generate keys for.

  • bootstrap_slots – Number of bootstrapping slots.

  • filename – Filename to use when saving the keys.

  • serialize – Whether to serialize and save the generated keys.

void load_context(bool verbose = false)

Load all serialized keys from the storage folder.

This function reads and loads all keys that were previously serialized and stored in files, typically from the “sskeys” folder.

void load_rotation_keys(const string &filename, bool verbose = false)

Load rotation keys from a specified file.

This function loads rotation keys that were previously generated and serialized from the given filename. Verbose mode can be enabled to display loading details.

Parameters:
  • filename – Filename from which to load the rotation keys.

  • verbose – Whether to display detailed loading information.

void load_bootstrapping_and_rotation_keys(int bootstrap_slots, const string &filename, bool verbose = false)

Load previously generated bootstrapping and rotation keys from storage.

This function loads bootstrapping and rotation keys that were previously generated and serialized, using the specified filename. Verbose mode can be enabled to display loading details.

Parameters:
  • bootstrap_slots – Number of bootstrapping slots.

  • filename – Filename from which to load the keys.

  • verbose – Whether to display detailed loading information.

void clear_rotation_keys()

Clear all rotation keys stored in the FHE context.

This function removes all previously stored rotation keys from the context, allowing new rotation keys to be generated without conflicts.

void clear_context(int bootstrapping_key_slots)

Clear the entire FHE context, including multiplication, bootstrapping, and rotation keys.

This function removes all keys stored in the context, allowing a fresh setup or reinitialization of the FHE environment.

Parameters:

bootstrapping_key_slots – Number of bootstrapping slots to clear.

void clear_bootstrapping_and_rotation_keys(int bootstrap_num_slots)

Clear all bootstrapping and rotation keys in the FHE context.

This function removes all stored bootstrapping and rotation keys up to the specified number of bootstrapping slots, allowing new keys to be generated.

Parameters:

bootstrap_num_slots – Number of bootstrapping slots to clear.

Ctext bootstrap_function(Ctext &encryptedInput, int level = 2)

Bootstrap a ciphertext to refresh its noise budget.

This function applies bootstrapping to the input ciphertext, effectively reducing accumulated noise and enabling further homomorphic operations. The bootstrapping level controls the depth and parameters used.

Parameters:
  • encryptedInput – Ciphertext to be bootstrapped.

  • encode_level – Bootstrapping level as defined in OpenFHE (e.g., 1 or 2).

Returns:

Refreshed ciphertext after bootstrapping.

Ctext encrypt_input(vector<double> &inputData)

Encrypt a vector of input data into a packed ciphertext.

This function takes a vector of double-precision values and encrypts them into a single packed ciphertext suitable for homomorphic computations.

Parameters:

inputData – Vector of data to be encrypted.

Returns:

Ciphertext containing the encrypted input data.

Ctext reencrypt_data(Ptext plaintextInput)

Re-encrypt a plaintext vector into a ciphertext.

This function takes a plaintext containing encoded data and encrypts it into a ciphertext suitable for homomorphic computations.

Parameters:

plaintextData – Plaintext data to be re-encrypted.

Returns:

Ciphertext containing the encrypted data.

Ptext encode_input(vector<double> &inputData, int encode_level = 1)

Encode a vector of input data into a packed plaintext.

This function encodes a vector of double-precision values into a plaintext suitable for homomorphic encryption, using the specified encoding level.

Parameters:
  • inputData – Vector of data to be encoded.

  • encode_level – Encoding level to use for the plaintext.

Returns:

Plaintext containing the encoded input data.

Ptext encode_input(vector<double> &inputData, int num_slots, int encode_level = 1)

Encode a vector of input data into a packed plaintext with a specified number of slots.

This function encodes a vector of double-precision values into a plaintext suitable for homomorphic encryption, using the specified number of slots and encoding level.

Parameters:
  • inputData – Vector of data to be encoded.

  • num_slots – Number of slots to use in the plaintext.

  • encode_level – Encoding level to use for the plaintext.

Returns:

Plaintext containing the encoded input data.

Ptext decrypt_data(Ctext encryptedInput, int cols)

Decrypt a ciphertext into a plaintext vector.

This function takes an encrypted ciphertext and decrypts it into a plaintext vector suitable for further processing or inspection.

Parameters:
  • encryptedinputData – Ciphertext to be decrypted.

  • cols – Number of elements in the decrypted vector.

Returns:

Plaintext containing the decrypted data.

vector<vector<Ctext>> encrypt_kernel(vector<vector<vector<double>>> &kernelData, int colsSquare)

Encrypt a 3D kernel matrix into a 2D vector of ciphertexts.

This function takes a 3D vector of double-precision kernel values and encrypts each 2D slice into a vector of ciphertexts suitable for homomorphic convolution operations.

Parameters:
  • kernelData – 3D vector containing kernel values to be encrypted.

  • cols_square – Size of the column square for the encryption.

Returns:

2D vector of ciphertexts representing the encrypted kernel.

vector<Ptext> encode_kernel(vector<vector<vector<double>>> &kernelData, int colsSquare)

Encode and replicate kernel values for homomorphic convolution.

This function selects values from the kernel positions for all kernels, repeats them by the square of the width, concatenates them into a single long vector, and encodes the result. The output is a k^2 vector of repeated kernel values suitable for homomorphic convolution operations.

Parameters:
  • kernelData – Vector containing kernel values to be encoded and replicated.

  • cols_square – Size of the column square for encoding.

Returns:

Vector of plaintexts representing the encoded and replicated kernel values.

vector<Ptext> encode_kernel(vector<double> &kernelData, int colsSquare)

Encode kernel data for fully connected layers.

This function takes a vector of double-precision kernel values and encodes them into plaintexts suitable for homomorphic operations in fully connected layers.

Parameters:
  • kernelData – Vector containing kernel values to be encoded.

  • cols_square – Size of the column square for the encoding.

Returns:

Vector of plaintexts representing the encoded kernel data.

vector<Ptext> encode_kernel_optimized(vector<vector<vector<double>>> &kernelData, int colsSquare, int encode_levels = 1)

Encode kernel data optimized for 3x3 kernels with padding of 1.

This function takes 3D kernel data and encodes it into plaintexts, optimized for kernels of size 3x3 and padding of 1, using the specified encoding level.

Parameters:
  • kernelData – 3D vector containing kernel values to be encoded.

  • cols_square – Size of the column square for encoding.

  • encode_level – Encoding level to use for the plaintexts.

Returns:

Vector of plaintexts representing the encoded and optimized kernel data.

Ptext encode_shortcut_kernel(vector<double> &inputData, int colsSquare)

Encode a vector for use in a shortcut layer.

This function encodes a vector of double-precision values into a plaintext suitable for the shortcut layer in homomorphic neural network operations, using the specified column square size.

Parameters:
  • inputData – Vector of data to be encoded.

  • cols_square – Size of the column square for the encoding.

Returns:

Plaintext containing the encoded shortcut kernel data.

Ptext encode_bais_input(vector<double> &inputData, int colsSquare, int encode_levels = 1)

Encode a vector of bias data into a plaintext.

This function encodes a vector of double-precision bias values into a plaintext suitable for homomorphic neural network operations, using the specified column square size and encoding level.

Parameters:
  • inputData – Vector of bias data to be encoded.

  • cols_square – Size of the column square for the encoding.

  • encode_level – Encoding level to use for the plaintext.

Returns:

Plaintext containing the encoded bias data.

Ctext change_num_slots(Ctext &encryptedInput, uint32_t numSlots)

Adjust the number of slots in a ciphertext after downsampling.

This function modifies the number of slots in the given ciphertext to improve performance by reducing the size of the polynomial being processed.

Parameters:
  • encryptedInput – Ciphertext whose number of slots will be adjusted.

  • num_slots – Desired number of slots in the ciphertext.

Returns:

Ciphertext with the updated number of slots.

int read_inferenced_label(Ctext encryptedInput, int noElements, ofstream &outFile)

Read the predicted label from encrypted inference data.

This function decrypts and reads the predicted label from the given encrypted inference data. The result can be written to an output file.

Parameters:
  • inferencedData – Ciphertext containing the inference results.

  • num_slots – Number of elements in the ciphertext.

  • outFile – Output file stream to write the predicted label.

Returns:

The predicted label as an integer.

int read_minmax(Ctext encryptedInput, int noElements)

Determine the minimum and maximum values from encrypted data.

This helper function decrypts the inference data and computes the minimum and maximum values across all elements.

Parameters:
  • inferencedData – Ciphertext containing the inference data.

  • num_slots – Number of elements in the ciphertext.

Returns:

An integer representing the computed min or max value, depending on implementation.

int read_scaling_value(Ctext encryptedInput, int noElements)

Retrieve the maximum value from encrypted convolution data for ReLU scaling.

This temporary function decrypts the inference data and returns the maximum value, which can be used for scaling in the ReLU operation.

Parameters:
  • inferencedData – Ciphertext containing the inference data.

  • num_slots – Number of elements in the ciphertext.

Returns:

Maximum value as an integer.

Public Members

int circuit_depth
int num_slots
int pLWE
int mult_depth = 10
string keys_folder = "./../HEkeys/"
string rotation_prefix = "rotation_keys_"
string mult_prefix = "./mult_keys_"
string sum_prefix = "./sum_keys_"