Standard ANN Controller

Single-input primitives on the standard layout (one channel per slot block, plaintext weights). Shares its interface with FHEONANNDiagonalController.

class FHEONANNStandardController

Standard ANN controller: the ANN layers (convolution, average pooling, linear, ReLU) as originally defined in FHEON. FHEONANNDiagonalController offers the same interface with the diagonal primitives, so a model written against the shared interface (the primitives below and the encode_* / convolution_kernel_level helpers) switches implementation by changing only the controller type.

Subclassed by FHEONANNDiagonalController

Public Types

using KernelSource = std::function<vector<Ptext>(int)>

the same convolutions with the weights of one output channel produced on demand (kernelAt(outCh): its 9 kernel plaintexts, shortcutAt(outCh): its shortcut plaintext): only the plaintexts of the output channel being computed are kept in memory instead of those of the whole layer

using ShortcutSource = std::function<Ptext(int)>

Public Functions

inline FHEONANNStandardController(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.

Set the global crypto context.

Parameters:

in_context – The crypto context to set.

inline void setNumSlots(int numSlots)
vector<int> generate_convolution_rotation_positions(int inputWidth, int inputChannels, int outputChannels, int kernelWidth, int padding, int stride, const FHEONLayout &layout = FHEONLayout())

Generate rotation index positions needed for standard convolution.

Parameters:
  • inputWidth – Dimensions of input channel.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels.

  • kernelWidth – Dimension of square kernel.

  • padding – Size of zero padding.

  • stride – Convolution stride.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

List of unique rotation steps.

vector<int> generate_linear_rotation_positions(int maxFCLayeroutputs, int rotationPosition, const FHEONLayout &layout = FHEONLayout())

Generate rotation positions for fully connected linear layers.

Parameters:
  • maxFCLayeroutputs – Maximum output size.

  • rotationPositions – Base rotation steps.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

List of unique rotation steps.

vector<int> generate_avgpool_rotation_positions(int inputWidth, int kernelWidth, int stride, int inputChannels, const FHEONLayout &layout = FHEONLayout())

Generate rotation index positions needed for average pooling.

Parameters:
  • inputWidth – Dimension of input.

  • kernelWidth – Dimension of pooling window.

  • stride – Pooling stride.

  • inputChannels – Number of channels.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

List of unique rotation steps.

vector<int> generate_optimized_convolution_rotation_positions(int inputWidth, int inputChannels, int outputChannels, int stride = 1, string stridingType = "multi_channels", const FHEONLayout &layout = FHEONLayout())

Generate rotation positions optimized for convolution.

Parameters:
  • inputWidth – Dimension of input.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels.

  • stride – Convolution stride.

  • stridingType – Type of striding optimization.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

List of unique rotation steps.

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, const FHEONLayout &layout = FHEONLayout())

Generate rotation positions optimized for average pooling.

Parameters:
  • inputWidth – Dimension of input.

  • inputChannels – Number of channels.

  • kernelWidth – Dimension of pooling window.

  • stride – Pooling stride.

  • globalPooling – Whether it’s global average pooling.

  • stridingType – Type of striding optimization.

  • rotationIndex – Base rotation index.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

List of unique rotation steps.

vector<int> generate_sum_rotation_positions(int sumSize)

Rotation positions of EvalSum(ciphertext, sumSize) as used by he_linear, he_linear_optimized and he_globalavgpool: EvalSum adds the rotations by the powers of two below sumSize. They replace the EvalSum keys of the full batch (EvalSumKeyGen), which are the same automorphisms for all powers of two below the number of slots.

rotations of EvalSum (he_linear, he_linear_optimized): replace the full-batch EvalSum keys

Parameters:

sumSize – Largest number of slots summed by EvalSum.

Returns:

List of rotation steps.

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, const FHEONLayout &layout = FHEONLayout())

Homomorphic standard convolution layer.

Parameters:
  • encryptedInput – Encrypted input ciphertext.

  • kernelData – Encoded weights filter matrix.

  • biasInput – Encoded bias vector.

  • inputWidth – Dimension of input.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output channels.

  • kernelWidth – Dimension of square kernel.

  • paddingLen – Padding size.

  • stride – Stride size.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted convolution output.

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

Homomorphic convolution with advanced padding and stride configurations.

Parameters:
  • encryptedInput – Encrypted input ciphertext.

  • kernelData – Pre-encoded filter weight vectors.

  • biasInput – Pre-encoded bias input vector.

  • inputWidth – Dimensions of the input channels.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output filter channels.

  • kernelWidth – Dimension of the square convolution kernel.

  • padding – Size of zero padding to apply.

  • stride – Convolution stride.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted output of the convolution layer.

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

Optimized homomorphic convolution using parallel channels.

Parameters:
  • encryptedInput – Encrypted input ciphertext.

  • kernelData – Pre-encoded filter weight vectors.

  • biasInput – Pre-encoded bias input vector.

  • inputWidth – Dimensions of the input channels.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output filter channels.

  • stride – Convolution stride.

  • index – Optimization index indicator.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted convolution output.

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

Optimized convolution supporting multiple packed channels.

Parameters:
  • encryptedInput – Encrypted input ciphertext.

  • kernelData – Pre-encoded filter weight vectors.

  • biasInput – Pre-encoded bias input vector.

  • inputWidth – Dimensions of the input channels.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output filter channels.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted convolution output.

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

Shortcut connection convolution layer for ResNet skip connections.

Parameters:
  • encryptedInput – Encrypted input ciphertext.

  • kernelData – Pre-encoded filter weight vectors.

  • biasInput – Pre-encoded bias input vector.

  • inputWidth – Dimensions of the input channels.

  • inputChannels – Number of input channels.

  • outputChannels – Number of output filter channels.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted convolution output.

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, const FHEONLayout &layout = FHEONLayout())

Combined optimized convolution and shortcut branch computation.

Parameters:
  • encryptedInput – Input ciphertext.

  • kernelData – Convolution weights.

  • shortcutKernelData – Shortcut weights.

  • biasInput – Convolution bias.

  • shortcutBiasVector – Shortcut bias.

  • inputWidth – Input dimension.

  • inputChannels – Input channels count.

  • outputChannels – Output channels count.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Vector containing [convolution_output, shortcut_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, const FHEONLayout &layout = FHEONLayout())

Combined optimized convolution and shortcut branch for multiple channels.

Parameters:
  • encryptedInput – Input ciphertext.

  • kernelData – Convolution weights.

  • shortcutKernelData – Shortcut weights.

  • biasInput – Convolution bias.

  • shortcutBiasInput – Shortcut bias.

  • inputWidth – Input dimension.

  • inputChannels – Input channels count.

  • outputChannels – Output channels count.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Vector containing [convolution_output, shortcut_output].

Ctext he_convolution_optimized(Ctext &encryptedInput, const KernelSource &kernelAt, Ptext &biasInput, int inputWidth, int inputChannels, int outputChannels, int stride = 1)

he_convolution_optimized with the 9 kernel plaintexts of an output channel produced on demand by kernelAt.

vector<Ctext> he_convolution_and_shortcut_optimized(const Ctext &encryptedInput, const KernelSource &kernelAt, const ShortcutSource &shortcutAt, Ptext &biasVector, Ptext &shortcutBiasVector, int inputWidth, int inputChannels, int outputChannels)

he_convolution_and_shortcut_optimized with the plaintexts of an output channel produced on demand.

vector<Ctext> he_convolution_and_shortcut_optimized_with_multiple_channels(const Ctext &encryptedInput, const KernelSource &kernelAt, const ShortcutSource &shortcutAt, Ptext &biasInput, Ptext &shortcutBiasInput, int inputWidth, int inputChannels, int outputChannels)

he_convolution_and_shortcut_optimized_with_multiple_channels with the plaintexts of an output channel produced on demand.

Ctext he_avgpool(Ctext encryptedInput, int imgCols, int outputChannels, int kernelWidth = 2, int stride = 2, const FHEONLayout &layout = FHEONLayout())

Homomorphic standard average pooling layer.

Parameters:
  • encryptedInput – Encrypted input ciphertext.

  • inputWidth – Dimension of input.

  • inputChannels – Number of channels.

  • kernelWidth – Dimension of pooling window.

  • stride – Pooling stride.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Average-pooled encrypted output.

Ctext he_avgpool_advanced(Ctext encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int stride, int padding, const FHEONLayout &layout = FHEONLayout())

Advanced homomorphic average pooling supporting padding.

Parameters:
  • encryptedInput – Encrypted input.

  • inputWidth – Input dimension.

  • outputChannels – Output channels count.

  • kernelWidth – Pooling kernel size.

  • stride – Pooling stride.

  • padding – Pooling padding.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Average-pooled encrypted output.

Ctext he_avgpool_optimzed(Ctext &encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int stride, const FHEONLayout &layout = FHEONLayout())

Optimized average pooling using slot shift aggregation.

Parameters:
  • encryptedInput – Encrypted input.

  • inputWidth – Input dimension.

  • inputChannels – Input channels.

  • kernelWidth – pooling kernel size.

  • stride – pooling stride.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Average-pooled encrypted output.

Ctext he_avgpool_optimzed_with_multiple_channels(Ctext &encryptedInput, int inputWidth, int inputChannels, int kernelWidth, int stride, const FHEONLayout &layout = FHEONLayout())

Optimized average pooling supporting multiple channels.

Parameters:
  • encryptedInput – Encrypted input.

  • inputWidth – Input dimension.

  • inputChannels – Input channels.

  • kernelWidth – pooling kernel size.

  • stride – pooling stride.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Average-pooled encrypted output.

Ctext he_globalavgpool(Ctext &encryptedInput, int inputWidth, int outputChannels, int kernelWidth, int rotatePositions, const FHEONLayout &layout = FHEONLayout())

Homomorphic global average pooling (reduces space dimension to 1).

Parameters:
  • encryptedInput – Input ciphertext.

  • inputWidth – Input dimension.

  • outputChannels – Output channels.

  • kernelWidth – Pooling window dimension.

  • rotatePositions – Rotation positions.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted global pooled output.

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

Homomorphic fully connected linear layer.

Parameters:
  • encryptedInput – Input ciphertext vector.

  • weightMatrix – Weight plaintexts vector.

  • biasInput – Bias plaintext.

  • inputSize – Size of input dimension.

  • outputSize – Size of output dimension.

  • rotatePositions – Rotation positions.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted output of linear layer.

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

Optimized homomorphic fully connected linear layer.

Parameters:
  • encryptedInput – Input ciphertext vector.

  • weightMatrix – Weight plaintexts vector.

  • biasInput – Bias plaintext.

  • inputSize – Size of input.

  • outputSize – Size of output.

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

Encrypted output of linear layer.

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

Homomorphic ReLU activation layer (CKKS polynomial approximation).

Parameters:
  • encryptedInput – Ciphertext to activate.

  • scaleValue – Scaling factor.

  • vectorSize – Number of elements in the vector.

  • polyDegree – Degree of approximating polynomial.

  • normalizedOutput – If true, returns ReLU(x / scaleValue) within [0, 1] (the next layer multiplies its weights by the scale); otherwise ReLU(x) itself, evaluated as scaleValue * ReLU(x / scaleValue).

Returns:

Activated ciphertext.

Ctext he_sum_two_ciphertexts(Ctext &firstInput, Ctext &secondInput)

Homomorphically add two ciphertexts together.

Parameters:
  • firstInput – The first encrypted input.

  • secondInput – The second encrypted input.

Returns:

Encrypted sum of the inputs.

int convolution_kernel_level(const Ctext &encryptedInput, int inputWidth, int stride = 1, const FHEONLayout &layout = FHEONLayout())

Level at which the kernels of he_convolution_optimized / he_convolution_and_shortcut_optimized are encoded.

weight encoding for the primitives above (layout.weightScale multiplies the weights, layout.inputPositions places the linear inputs)

Parameters:
  • encryptedInput – Input ciphertext of the convolution.

  • inputWidth – Spatial width of one input channel (unused here).

  • stride – Stride of the convolution (unused here).

  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

Returns:

The level of the input ciphertext, as the standard models encode their kernels.

double avgpool_weight_scale(int kernelWidth)

Factor the next layer’s weights need after a pooling: 1, the standard pooling returns averages.

vector<vector<Ptext>> encode_convolution_kernel(FHEONHEController &heController, vector<vector<vector<vector<double>>>> &kernelData, int inputWidth, int numSlots, int encodeLevel, int stride = 1, int padding = 1, const FHEONLayout &layout = FHEONLayout())

Encode a 3x3 kernel for he_convolution_optimized (stride 1) or he_convolution_and_shortcut_optimized (stride 2).

Parameters:
  • heController – Controller holding the encoders.

  • kernelData – Weights [outputChannels][inputChannels][3][3].

  • inputWidth – Spatial width of one input channel.

  • numSlots – Slot count of the input ciphertext (unused here).

  • encodeLevel – Encoding level.

  • stride – Stride of the convolution (unused here: both use the input width).

  • layout – Optional layout arguments (FHEONLayout): only weightScale is used, multiplied into the weights (e.g. a factor a preceding pooling expects in the next layer); the other fields are for FHEONANNDiagonalController.

  • padding – Zero padding of the convolution: > 0 (“same”, he_convolution_optimized) uses the masked encoding (encode_kernel_optimized), 0 (“valid”, he_convolution) the plain one.

Returns:

One set of 9 plaintexts per output channel.

Ptext encode_convolution_bias(FHEONHEController &heController, vector<double> &biasData, int outputWidth, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())

Encode a convolution bias, one value per output channel block of outputWidth^2 slots.

Parameters:
  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

  • heController – Controller holding the encoders.

  • biasData – One bias per output channel.

  • outputWidth – Output width: every bias fills outputWidth^2 slots.

  • numSlots – Slot count of the input ciphertext (unused by the standard encoding, kept for the shared interface).

  • encodeLevel – Encoding level (the level of the ciphertexts the plaintexts multiply).

vector<Ptext> encode_shortcut_kernel(FHEONHEController &heController, vector<vector<double>> &kernelData, int inputWidth, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())

Encode the 1x1 shortcut kernel of he_convolution_and_shortcut_optimized (one plaintext per output channel).

Parameters:
  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

  • heController – Controller holding the encoders.

  • kernelData – 1x1 weights [output channel][input channel].

  • inputWidth – Input width: every weight fills inputWidth^2 slots.

  • numSlots – Slot count of the input ciphertext (unused by the standard encoding, kept for the shared interface).

  • encodeLevel – Encoding level (the level of the ciphertexts the plaintexts multiply).

vector<Ptext> encode_linear_weights(FHEONHEController &heController, vector<vector<double>> &weightData, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())

Encode the weights of he_linear_optimized (one plaintext per output).

Parameters:
  • layout – Optional layout arguments (FHEONLayout): only weightScale is used, multiplied into the weights (e.g. a factor a preceding pooling expects in the next layer); the other fields are for FHEONANNDiagonalController.

  • heController – Controller holding the encoders.

  • weightData – Weights [output][input].

  • numSlots – Slot count of the input ciphertext (unused by the standard encoding, kept for the shared interface).

  • encodeLevel – Encoding level (the level of the ciphertexts the plaintexts multiply).

Ptext encode_linear_bias(FHEONHEController &heController, vector<double> &biasData, int numSlots, int encodeLevel, const FHEONLayout &layout = FHEONLayout())

Encode the bias of he_linear_optimized (output k in slot k).

Parameters:
  • layout – Optional diagonal-layout arguments (FHEONLayout); unused by the standard controller, which only keeps the interface shared with FHEONANNDiagonalController.

  • heController – Controller holding the encoders.

  • biasData – One bias per output.

  • numSlots – Slot count of the input ciphertext (unused by the standard encoding, kept for the shared interface).

  • encodeLevel – Encoding level (the level of the ciphertexts the plaintexts multiply).

Public Members

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