Artificial Neural Networks Controller

class FHEONANNController

Public Functions

inline FHEONANNController(CryptoContext<DCRTPoly> &ctx)
void setContext(CryptoContext<DCRTPoly> &in_context)

FHE Controller for defining and managing homomorphic ANN functions.

This class provides different methods for convolution, pooling, fully connected relu, etc for neural network development on encrypted data using FHE.

inline void setNumSlots(int numSlots)
vector<int> generate_convolution_rotation_positions(int inputWidth, int inputChannels, int outputChannels, int kernelWidth, int padding, int Stride)

Generate the rotation positions required for convolution layers in homomorphic encryption.

This function computes the rotation positions needed for performing convolutions on encrypted data. It first calculates the output shape of the convolution layer given the input image dimensions, kernel parameters, padding, and stride, and then derives the set of rotation positions based on the output width.

Parameters:
  • inputWidth – Width of the input image (assuming square images).

  • inputChannels – Number of input channels to the convolution layer.

  • outputChannels – Number of output channels in the convolution layer.

  • kernelWidth – Size of the convolution kernel (assumed square).

  • padding – Amount of zero-padding applied around the input image.

  • Stride – Stride length used for the convolution.

Returns:

A vector of integers representing the rotation positions required to perform the convolution.

vector<int> generate_linear_rotation_positions(int maxFCLayeroutputs, int rotationPosition)

Generate rotation positions for fully connected (FC) layers in homomorphic encryption.

This function computes the rotation positions required for fully connected layers in an FHE-based ANN. Rather than generating keys separately for each FC layer, it uses the maximum number of outputs across all FC layers and the maximum number of output channels from feature extraction layers. The rotation keys are then derived by dividing the maximum FC outputs by the maximum channel outputs, leveraging the fact that rotation keys for ranges [0 … maxChannelOutput] are already available from convolution layers.

Parameters:
  • maxFCLayeroutputs – Maximum number of outputs across all fully connected layers.

  • rotationPositions – Maximum number of output channels already covered by convolution layers.

Returns:

A vector of integers representing the rotation positions required for fully connected layers.

vector<int> generate_avgpool_rotation_positions(int inputWidth, int kernelWidth, int Stride, int inputChannels)

Generate the rotation positions required for average pooling layers in homomorphic encryption.

This function computes the set of rotation positions needed for performing average pooling operations on encrypted data. It calculates the output dimensions of the pooling layer using the input width, kernel size, and stride. For each channel, the output size is multiplied by the squared width to derive the required rotations. Each row of the pooled feature map corresponds to a rotation position.

Parameters:
  • inputWidth – Width of the input feature map (assumed square).

  • kernelWidth – Size of the pooling kernel (assumed square).

  • Stride – Stride length used for the pooling operation.

  • inputChannels – Number of input channels to the pooling layer.

Returns:

A vector of integers representing the rotation positions required for average pooling.

vector<int> generate_optimized_convolution_rotation_positions(int inputWidth, int inputChannels, int outputChannels, int Stride = 1, string stridingType = "multi_channels")

Generate rotation positions for optimized convolution layers in homomorphic encryption.

This function computes the set of rotation positions required for performing optimized convolution operations on encrypted data. Unlike the standard convolution rotation generation, this version is designed to work with optimized schemes that reduce the requires kernel size of 3, padding =1 and striding = 0

Parameters:
  • inputWidth – Width of the input image (assumed square).

  • inputChannels – Number of input channels in the convolution layer.

  • outputChannels – Number of output channels in the convolution layer.

  • Stride – Stride length used for the convolution.

  • stridingType – Define the type of striding to be used (basic, single_channel, multi_channels)

Returns:

A vector of integers representing the rotation positions required for the optimized convolution.

vector<int> generate_avgpool_optimized_rotation_positions(int inputWidth, int inputChannels, int kernelWidth, int Stride, bool globalPooling = false, string stridingType = "multi_channels", int rotationIndex = 16)

Generate rotation positions for optimized average pooling layers in homomorphic encryption.

This function computes the set of rotation positions needed to perform optimized average pooling operations on encrypted data. It calculates the output size of the pooling layer given the input width, kernel size, and stride, and derives rotation positions across all channels. When global pooling is enabled, the function generates rotation positions for reducing each channel to a single value.

Parameters:
  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels in the pooling layer.

  • kernelWidth – Size of the pooling kernel (assumed square).

  • Stride – Stride length used for the pooling operation.

  • globalPooling – Boolean flag indicating whether global average pooling is applied (true = pool entire feature map).

  • stridingType – Define the type of striding to be used (basic, single_channel, multi_channels)

  • rotationIndex – It is the rotation index for global pooling management

Returns:

A vector of integers representing the rotation positions required for optimized average pooling.

Ctext he_convolution(Ctext &encryptedInput, vector<vector<Ptext>> &kernelData, Ptext &biasInput, int inputWidth, int inputChannels, int outputChannels, int kernelWidth, int padding = 0, int stride = 1)

Perform a secure convolution operation on encrypted data.

This function implements a convolutional layer in the encrypted domain using homomorphic encryption. Given an encrypted input, convolution kernels, and a bias term, it applies the convolution operation while respecting the specified input dimensions, kernel size, padding, and stride. The computation is performed ciphertext-wise, enabling convolutional neural networks to be evaluated securely without decryption.

See also

generate_conv_rotation_positions()

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernels represented as a 2D vector of plaintexts (one kernel per output channel).

  • biasInput – Bias term for each output channel (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels.

  • kernelWidth – Width of the convolution kernel (assumed square).

  • paddingLen – Amount of zero-padding applied around the input.

  • Stride – Stride length for the convolution.

Returns:

Ctext Ciphertext representing the encrypted result of the convolution operation.

Ctext he_convolution_advanced(Ctext &encryptedInput, vector<vector<Ptext>> &kernelData, Ptext &biasInput, int inputWidth, int inputChannels, int outputChannels, int kernelWidth, int padding, int stride)

Perform a secure convolution operation with explicit padding on encrypted data.

This function extends the standard secure convolution by explicitly handling zero-padding within the encrypted domain. The input ciphertext is expanded by adding zeros around the borders according to the specified padding size, after which the convolution is carried out as in the traditional setting.

See also

he_convolution()

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernels represented as a 2D vector of plaintexts.

  • biasInput – Bias term for each output channel (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • kernelWidth – Size of the convolution kernel (assumed square).

  • padding – Amount of zero-padding to apply.

  • stride – Stride length for the convolution.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels.

Returns:

Ctext Ciphertext representing the encrypted result of the convolution with padding.

Ctext he_convolution_optimized(Ctext &encryptedInput, vector<vector<Ptext>> &kernelData, Ptext &biasInput, int inputWidth, int inputChannels, int outputChannels, int Stride = 1, int index = 0)

Perform an optimized secure convolution for the special case of stride = 1, kernel size = 3, and padding = 1.

This function implements a convolutional layer in the encrypted domain using homomorphic encryption, optimized specifically for the case where stride = 1, kernel size = 3, and padding = 3. By exploiting this fixed configuration, the function reduces the number of ciphertext rotations, multiplications, and additions compared to the generic secure convolution implementation, resulting in improved efficiency.

It applies single channel striding given the striding value is greater than 1.

See also

he_convolution()

Note

This optimized implementation should only be used when stride = 1, kernel size = 3, and padding = 3. For other cases, use he_convolution or he_convolution_advanced().

Warning

Supplying parameters outside the supported configuration (stride ≠ 1, kernel size ≠ 3, padding ≠ 3) will lead to incorrect results.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernels represented as a 2D vector of plaintexts.

  • biasInput – Bias term for each output channel (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels.

  • Stride – Stride length (must be 1 for this optimized version).

  • index – Index of the current kernel or output channel being processed.

Returns:

Ctext Ciphertext representing the encrypted result of the optimized convolution.

Ctext he_convolution_optimized_with_multiple_channels(Ctext &encryptedInput, vector<vector<Ptext>> &kernelData, Ptext &biasInput, int inputWidth, int inputChannels, int outputChannels)

Perform secure convolution layer evaluation for the special case of stride = 1, kernel size = 3, and padding = 1.

If striding is greater than 1, it applies striding across multiple channels simultaneously using multi_channels approach rather than channel-by-channel. This approach improves efficiency for FHE-based deep networks with multiple input channels.

Warning

This function assumes that striding and channel-based optimization are supported by the encryption scheme. Using unsupported parameters may lead to incorrect results.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernels for the main branch, represented as a 2D vector of plaintexts.

  • biasInput – Bias term for the main convolution branch (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels (shared across both branches).

Returns:

Ctext A ciphertexts containing the encrypted results of the convolution

Ctext he_shortcut_convolution(Ctext &encryptedInput, vector<Ptext> &kernelData, Ptext &biasInput, int inputWith, int inputChannels, int outputChannels)

Perform a secure shortcut convolution (as used in ResNets) on encrypted data.

This function implements the shortcut (or projection) convolution used in Residual Networks (ResNets). The shortcut convolution adjusts the dimensions of the input feature map so that it can be added to the output of a residual block. In the encrypted setting, this is carried out homomorphically on ciphertexts using pre-encrypted weights.

Conceptually, the operation is identically equal to the convolution rather than it uses a kernel size = 1, striding = 2, padding = 1. It is ddefined in ResNet architectures, but performed securely in the FHE domain. We used the single channel striding approach in this implementation.

See also

he_convolution()

Note

This function is a special case of the standard convolution operation, optimized for use in residual connections of ResNets.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernel weights represented as a vector of plaintexts (projection filter).

  • biasInput – Bias term for the shortcut convolution (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels (dimension after projection).

Returns:

Ctext Ciphertext representing the encrypted result of the shortcut convolution.

vector<Ctext> he_convolution_and_shortcut_optimized(const Ctext &encryptedInput, const vector<vector<Ptext>> &kernelData, const vector<Ptext> &shortcutKernelData, Ptext &biasVector, Ptext &shortcutBiasVector, int inputWidth, int inputChannels, int outputChannels)

Perform a combined secure convolution with stride-2 and shortcut projection for ResNet blocks.

This function implements a custom operation tailored for ResNet architectures in the encrypted domain. It simultaneously evaluates:

  1. The main convolution branch with a stride of 2 (downsampling).

  2. The shortcut (projection) branch to match dimensions.

By handling both the strided convolution and the shortcut convolution in one function, it enables efficient construction of ResNet blocks without relying on the generic optimized convolution with integrated striding. It uses single channel striding approach.

Note

This function is specific to ResNet-style blocks with stride-2 downsampling. For other convolution cases, use he_convolution() or he_convolution_optimized().

Warning

This function assumes stride = 2 in the main convolution branch. Supplying different stride values may lead to incorrect results.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernels for the main branch, represented as a 2D vector of plaintexts.

  • shortcutKernelData – Convolution kernel weights for the shortcut projection branch (plaintexts).

  • biasInput – Bias term for the main convolution branch (plaintext).

  • shortcutBiasVector – Bias term for the shortcut branch (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels (shared across both branches).

Returns:

vector<Ctext> A vector of ciphertexts containing both the encrypted results of the main branch and the shortcut branch, which can then be combined (via ciphertext addition) to form the ResNet residual output.

vector<Ctext> he_convolution_and_shortcut_optimized_with_multiple_channels(const Ctext &encryptedInput, const vector<vector<Ptext>> &kernelData, const vector<Ptext> &shortcutKernelData, Ptext &biasInput, Ptext &shortcutBiasInput, int inputWidth, int inputChannels, int outputChannels)

Perform a channel-optimized secure convolution and shortcut evaluation for ResNet blocks.

This function is a custom ResNet-specific operation in the encrypted domain. It evaluates the shortcut convolution across multiple channels simultaneously rather than channel-by-channel, while also computing the main convolution branch with integrated striding. This approach improves efficiency for FHE-based ResNet implementations by reducing the number of rotations and multiplications. It uses multi channel striding approach.

Note

This function is optimized for performing shortcut convolutions across multiple channels simultaneously. It is recommended for FHE-based ResNet implementations to reduce computation overhead.

Warning

This function assumes that striding and channel-based optimization are supported by the encryption scheme. Using unsupported parameters may lead to incorrect results.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • kernelData – Convolution kernels for the main branch, represented as a 2D vector of plaintexts.

  • shortcutKernelData – Convolution kernels for the shortcut branch (plaintexts).

  • biasInput – Bias term for the main convolution branch (plaintext).

  • shortcutBiasInput – Bias term for the shortcut branch (plaintext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels (shared across both branches).

Returns:

vector<Ctext> A vector of ciphertexts containing the encrypted results of both the main branch and shortcut branch, ready to be combined for the ResNet residual output.

Ctext he_avgpool(Ctext encryptedInput, int imgCols, int outputChannels, int kernelWidth = 2, int Stride = 2)

Perform a secure average pooling operation on encrypted data.

This function implements average pooling in the encrypted domain using homomorphic encryption. Given an encrypted input feature map, it applies pooling with the specified kernel size and stride, aggregating values across local regions while keeping the data encrypted. it uses the single channel by channel striding approach. It can also handle poolings of all kernel sizes and striding values.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • kernelWidth – Width of the pooling kernel (assumed square).

  • Stride – Stride length used for the pooling operation.

Returns:

Ctext Ciphertext representing the encrypted result of the average pooling operation.

Ctext he_avgpool_advanced(Ctext encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int stride, int padding)

Perform a secure average pooling operation with padding and custom stride on encrypted data.

This function implements average pooling in the encrypted domain using homomorphic encryption, allowing explicit control over stride and padding. When padding is greater than zero, zeros are added around the input feature map before pooling. This ensures correct output dimensions and simulates standard pooling behavior in plaintext settings.

See also

he_avgpool()

Note

This function is designed for FHE-based ANN implementations where padding cannot be applied directly to plaintexts. Rotations and additions on ciphertexts simulate the padding and pooling behavior.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • inputWidth – Width of the input feature map (assumed square).

  • outputChannels – Number of output channels.

  • kernelWidth – Width of the pooling kernel (assumed square).

  • stride – Stride length for the pooling operation.

  • padding – Amount of zero-padding applied around the input.

Returns:

Ctext Ciphertext representing the encrypted result of the advanced average pooling operation.

Ctext he_avgpool_optimzed(Ctext &encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int Stride)

Perform an optimized secure average pooling operation on encrypted data.

This function implements an optimized version of average pooling in the encrypted domain using homomorphic encryption. It computes the average over local regions efficiently, reducing the number of ciphertext rotations and additions compared to the standard he_avgpool implementation.

See also

he_avgpool()

See also

he_avgpool__advanced()

Note

This function is optimized for FHE-based ANN implementations where reducing the number of rotations and multiplications is critical for efficiency.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • kernelWidth – Width of the pooling kernel (assumed square).

  • Stride – Stride length for the pooling operation.

Returns:

Ctext Ciphertext representing the encrypted result of the optimized average pooling operation.

Ctext he_avgpool_optimzed_with_multiple_channels(Ctext &encryptedInput, int inputWidth, int inputChannels, int kernelWidth, int Stride)

Perform an optimized secure average pooling operation on encrypted data.

This function implements an optimized version of average pooling in the encrypted domain using homomorphic encryption. It computes the average over local regions efficiently, reducing the number of ciphertext rotations and additions compared to the standard he_avgpool implementation. It allows striding over all output channels at once. Most efficient pooling

See also

he_avgpool()

See also

he_avgpool__advanced()

Note

This function is optimized for FHE-based ANN implementations where reducing the number of rotations and multiplications is critical for efficiency.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • inputWidth – Width of the input feature map (assumed square).

  • inputChannels – Number of input channels.

  • kernelWidth – Width of the pooling kernel (assumed square).

  • Stride – Stride length for the pooling operation.

Returns:

Ctext Ciphertext representing the encrypted result of the optimized average pooling operation.

Ctext he_globalavgpool(Ctext &encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int rotatePositions)

Perform a secure global average pooling operation on encrypted data.

This function reduces each channel of the input feature map to a single value by averaging all elements in the channel. It is particularly useful in ResNet architectures where global pooling is applied before the fully connected layer.

See also

he_avgpool()

See also

he_avgpool__advanced()

Note

This function is optimized for FHE-based ResNet implementations. Reducing each channel to a single value minimizes the number of ciphertexts before the fully connected layer, improving efficiency.

Parameters:
  • encryptedInput – Encrypted input feature map (ciphertext).

  • inputWidth – Width of the input feature map (assumed square).

  • outputChannels – Number of output channels.

  • kernelWidth – Kernel size used for pooling (typically equal to inputWidth for global pooling, but included for flexibility).

  • rotatePositions – Precomputed rotation positions used to perform the homomorphic averaging across all elements in the channel.

Returns:

Ctext Ciphertext representing the encrypted result of the global average pooling operation.

Ctext he_linear(Ctext &encryptedInput, vector<Ptext> &weightMatrix, Ptext &biasInput, int inputSize, int outputSize, int rotatePositions)

Perform a secure fully connected (linear) layer operation on encrypted data.

This function implements a fully connected (dense) layer in the encrypted domain using homomorphic encryption. Given an encrypted input vector (e.g., the output of convolution or pooling layers), a plaintext weight matrix, and a bias vector, it computes the linear transformation:

\[ y = \sum_i w_i \cdot x_i + b \]

entirely on ciphertexts.

Parameters:
  • encryptedInput – Encrypted input vector (ciphertext) of size inputSize.

  • weightMatrix – Weight matrix for the fully connected layer, represented as a vector of plaintexts.

  • biasInput – Bias term for each output neuron (plaintext).

  • inputSize – Number of input features.

  • outputSize – Number of output neurons.

  • rotatePositions – Precomputed rotation positions required to perform the homomorphic summation across input features.

Returns:

Ctext Ciphertext representing the encrypted result of the fully connected layer.

Ctext he_linear_optimized(Ctext &encryptedInput, vector<Ptext> &weightMatrix, Ptext &biasInput, int inputSize, int outputSize)

Perform an optimized secure fully connected (linear) layer operation on encrypted data.

This function computes the fully connected layer in the encrypted domain using homomorphic encryption, optimizing the summation and multiplication of weights and inputs to reduce the number of rotations and homomorphic operations. It merges all computed values to produce the final output ciphertext.

See also

he_linear()

Parameters:
  • encryptedInput – Encrypted input vector (ciphertext) of size inputSize.

  • weightMatrix – Weight matrix for the fully connected layer, represented as a vector of plaintexts.

  • biasInput – Bias term for each output neuron (plaintext).

  • inputSize – Number of input features.

  • outputSize – Number of output neurons.

Returns:

Ctext Ciphertext representing the encrypted result

Ctext he_relu(Ctext &encryptedInput, double scale, int vectorSize, int polyDegree = 59)

Apply a secure ReLU activation on encrypted data using a Chebyshev polynomial approximation.

This function approximates the ReLU function on ciphertexts using the EvalChebyFunction method. Input values are first scaled to the range [-1, 1] to improve the accuracy of the polynomial approximation. The polyDegree parameter determines the degree of the Chebyshev polynomial used for the approximation.

See also

EvalChebyFunction()

Parameters:
  • encryptedInput – Encrypted input vector (ciphertext).

  • scaleValue – Scaling factor to normalize input values to [-1, 1].

  • vectorSize – Number of elements in the input vector.

  • polyDegree – Degree of the Chebyshev polynomial used for the ReLU approximation.

Returns:

Ctext Ciphertext representing the encrypted result of the ReLU activation.

Ctext he_sum_two_ciphertexts(Ctext &firstInput, Ctext &secondInput)

Public Members

string public_data = "sskeys"
int num_slots = 1 << 14