Operators

The operators/ directory implements MPC-enabled data-parallel operators used by CryptDough queries.

Contents:

  • operators.h – Umbrella header aggregating all operator headers.

  • aggregation_selector.h - Internal helper class to select between aggregation types at runtime

  • aggregation.h – Secure aggregation functions (SUM, COUNT, etc.) and oblivious aggregation network

  • circuits.h - Implementation of various boolean circuits.

  • common.h – Common utilities shared by operators.

  • distinct.h – Distinct operator.

  • join.h - Join operator.

  • machine_learning.h – Secure machine-learning operators (e.g. weight loading, matrix-based inference).

  • merge.h – Oblivious Merge.

  • prefix_network.h – Base class for prefix (scan) networks over shared vectors.

  • quicksort.h – Quicksort implementation.

  • radixsort.h – Radixsort implementation.

  • shuffle.h – Oblivious shuffle operators.

  • sorting.h – Sorting helper functions.

  • sorting_network.h – Pairwise sorting-network building blocks.

  • streaming.h – Streaming operators.

Note: For documentation on the join functions, see cdough::relational::EncodedTable::_join().

Boolean Circuits

Implementation of various boolean circuits.

(moved out of BSharedVector.h)

namespace cdough
namespace operators

Typedefs

template<typename S, typename E, typename Eng>
using unique_B = std::unique_ptr<BSharedVector<S, E, Eng>>

Alias for a unique pointer to a BSharedVector.

Template Parameters:
  • S

  • E

Shuffle

Defines

random_buffer_size
namespace cdough
namespace operators

Typedefs

template<typename E, typename Engine>
using AElementwisePermutation = ASharedVector<int, cdough::EVector<int, E::replicationNumber>, Engine>
template<typename E, typename Engine>
using BElementwisePermutation = BSharedVector<int, cdough::EVector<int, E::replicationNumber>, Engine>

Functions

template<typename Share, typename EVector, typename Engine>
void oblivious_apply_sharded_perm(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<ShardedPermutation> &perm)

Protocol agnostic function to apply sharded permutations.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • perm – The permutation to apply.

template<typename Share, typename EVector, typename Engine>
void oblivious_apply_inverse_sharded_perm(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<ShardedPermutation> &perm)

Protocol agnostic function to apply sharded permutations.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • perm – The permutation whose inverse to apply.

template<typename Share, typename EVector, typename Engine, typename EVectorPerm>
void oblivious_apply_elementwise_perm(SharedVector<Share, EVector, Engine> &x, ElementwisePermutation<EVectorPerm, Engine> &perm)

Obliviously apply an elementwise secret-shared permutation.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

  • EVectorPerm – Permutation type.

Parameters:
  • x – The secret-shared vector to permute.

  • perm – The permutation to apply.

std::vector<int> gen_perm(size_t size, std::shared_ptr<random::CommonPRG> generator)

Generate a pseudorandom local permutation using Fisher-Yates shuffle.

Creates a vector {0, 1, …, size-1}, permutes it, and returns it.

Parameters:
  • size – The size of the permutation.

  • generator – The common PRG object used as the pseudorandomness source.

Returns:

The permutation as a vector of destination indices.

template<typename T, typename Engine>
void local_apply_perm(Vector<T> &x, const LocalPermutation &permutation, Engine &engine)

Apply a local permutation to a vector.

Template Parameters:

T – Data type of vector elements.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply.

template<typename T, typename Engine>
void local_apply_perm(Vector<T> &x, Vector<int> &permutation, Engine &engine)

Overload for both vector and permutation represented as cdough Vector.

Template Parameters:

T – Data type of vector elements.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply as cdough Vector.

template<typename T>
void local_apply_perm_single_threaded(Vector<T> &x, const LocalPermutation &permutation)

Single-threaded version of local_apply_perm.

Avoids reentering the engine within a thread.

Template Parameters:

T – Data type of vector elements.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply.

template<typename T>
void local_apply_perm_single_threaded(Vector<T> &x, Vector<int> &permutation)

Single-threaded overload for both vector and permutation represented as cdough Vector.

Template Parameters:

T – Data type of vector elements.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply.

template<typename T, int R, typename P, typename Engine>
void local_apply_perm(EVector<T, R> &x, P &permutation, Engine &engine)

Catch-all overload for arbitrary vector types and permutation as cdough Vector

Template Parameters:
  • T – Data type of vector elements.

  • R – Replication number.

  • P – Permutation type.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply.

template<typename EVector, typename Engine>
void local_apply_perm(ElementwisePermutation<EVector, Engine> &x, Vector<int> &permutation)

Overload when vector is an ElementwisePermutation.

Template Parameters:
  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply.

template<typename S, typename E, typename Eng, typename P>
void local_apply_perm(SharedVector<S, E, Eng> &x, P &permutation)

Overload for permutation a SharedVector. Permutation type P can be a LocalPermutation or Vector<int>.

Template Parameters:
  • S – Share data type.

  • E – Share container type.

  • P – Permutation type.

Parameters:
  • x – The vector to permute (modified in place).

  • permutation – The permutation to apply.

template<typename T, typename Engine, typename S = int>
void local_apply_inverse_perm(Vector<T> &x, const std::vector<S> &permutation, Engine &engine)

Apply the inverse of a local permutation to a vector.

NOTE: this can also be implemented by just applying permutation as a new mapping. However, this is currently less efficient.

Template Parameters:
  • T – Data type of vector elements.

  • S – Permutation data type.

Parameters:
  • x – The vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename T, typename Engine, typename S = int>
void local_apply_inverse_perm(Vector<T> &x, const Vector<S> &permutation, Engine &engine)

Overload when both are cdough Vectors.

Template Parameters:
  • T – Data type of vector elements.

  • S – Permutation data type.

Parameters:
  • x – The vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename T, int R, typename Engine, typename S = int>
void local_apply_inverse_perm(EVector<T, R> &x, const std::vector<S> &permutation, Engine &engine)

Overload when we are permutation an EVector.

Template Parameters:
  • T – Data type of vector elements.

  • R – Replication number.

  • S – Permutation data type.

Parameters:
  • x – The vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename Share, typename EVector, typename Engine, typename S = int>
void local_apply_inverse_perm(SharedVector<Share, EVector, Engine> &x, const std::vector<S> &permutation)

Overload when vector is a SharedVector.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

  • S – Permutation data type.

Parameters:
  • x – The vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename Share, typename EVector, typename Engine>
void local_apply_inverse_perm(SharedVector<Share, EVector, Engine> &x, Vector<int> &permutation)

Overload when vector is a SharedVector and permutation is a cdough Vector

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename EVector, typename Engine>
void local_apply_inverse_perm(ElementwisePermutation<EVector, Engine> &x, Vector<int> &permutation)

Overload when the vector is an ElementwisePermutation and permutation is a cdough Vector

Template Parameters:
  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename Share, typename EVector, typename Engine>
void hm_oblivious_apply_sharded_perm(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<HMShardedPermutation> &permutation)

Obliviously apply a sharded secret-shared permutation (Honest Majority).

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • permutation – The permutation to apply.

template<typename EVector, typename Engine>
void hm_oblivious_apply_sharded_perm(ElementwisePermutation<EVector, Engine> &x, std::shared_ptr<HMShardedPermutation> &permutation)

Obliviously apply a sharded secret-shared permutation (Honest Majority).

This overloaded function just passes the ElementwisePermutation’s underlying SharedVector.

Parameters:
template<typename Share, typename EVector, typename Engine>
void hm_oblivious_apply_inverse_sharded_perm(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<HMShardedPermutation> &permutation)

Obliviously apply the inverse of a sharded secret-shared permutation.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename EVector, typename Engine>
void hm_oblivious_apply_inverse_sharded_perm(ElementwisePermutation<EVector, Engine> &x, std::shared_ptr<HMShardedPermutation> &permutation)

Obliviously apply the inverse of a sharded secret-shared permutation (Honest Majority).

Template Parameters:
  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
template<typename Share, typename EVector, typename Engine>
void permute_and_share(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<DMShardedPermutation<Share>> &perm, int send_party, std::optional<size_t> thread_id = std::nullopt)

Apply a permutation correlation to a secret-shared vector (2PC only).

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute and share.

  • perm – The permutation correlation.

  • send_party – The index of the party acting as the sender.

template<typename Share, typename EVector, typename Engine>
void permute_and_share_inverse(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<DMShardedPermutation<Share>> &perm, int send_party)

Apply the inverse of a permutation correlation to a secret-shared vector (2PC only).

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute and share.

  • perm – The permutation correlation whose inverse to apply.

  • send_party – The index of the party acting as the sender.

template<typename Share, typename EVector, typename Engine>
void dm_oblivious_apply_sharded_perm(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<DMShardedPermutation<Share>> &permutation)

Obliviously apply a sharded secret-shared permutation (Dishonest Majority).

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • permutation – The permutation to apply.

template<typename Share, typename EVector, typename Engine>
void dm_oblivious_apply_inverse_sharded_perm(SharedVector<Share, EVector, Engine> &x, std::shared_ptr<DMShardedPermutation<Share>> &permutation)

Obliviously apply the inverse of a sharded secret-shared permutation (Dishonest Majority).

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename EVector, typename Engine>
void oblivious_apply_sharded_perm(ElementwisePermutation<EVector, Engine> &x, std::shared_ptr<ShardedPermutation> &permutation)

Protocol agnostic function to apply sharded permutations.

This overloaded function just passes the ElementwisePermutation’s underlying SharedVector.

Template Parameters:

EVector – Share container type.

Parameters:
  • Engine – Secure computation engine type.

  • x – The secret-shared vector to permute.

  • permutation – The permutation to apply.

template<typename EVector, typename Engine>
void oblivious_apply_inverse_sharded_perm(ElementwisePermutation<EVector, Engine> &x, std::shared_ptr<ShardedPermutation> &permutation)

Protocol agnostic function to apply inversesharded permutations.

Template Parameters:
  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – The secret-shared vector to permute.

  • permutation – The permutation whose inverse to apply.

template<typename EVector, typename Engine>
ElementwisePermutation<EVector, Engine> compose_permutations(ElementwisePermutation<EVector, Engine> &sigma, ElementwisePermutation<EVector, Engine> &rho)

Compose two elementwise secret-shared permutations.

Template Parameters:
  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • sigma – The first permutation to be applied in the composition.

  • rho – The second permutation to be applied in the composition.

Returns:

An elementwise secret-sharing of the composed permutation.

template<typename Share, typename EVector, typename Engine>
static void shuffle(SharedVector<Share, EVector, Engine> &x)

Obliviously shuffle a secret-shared vector.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:

x – The vector to shuffle.

template<typename Share, typename EVector, typename Engine>
static void shuffle(std::vector<ASharedVector<Share, EVector, Engine>*> _data_a, std::vector<BSharedVector<Share, EVector, Engine>*> _data_b, size_t size)

Obliviously shuffle a table of secret-shared vectors.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • _data_a – List of pointers to all arithmetic columns.

  • _data_b – List of pointers to all binary columns.

  • size – Size of the vectors to shuffle.

Sorting

namespace cdough

Enums

enum SortingProtocol

Available sorting protocols.

Values:

enumerator NETWORK
enumerator QUICKSORT
enumerator RADIXSORT
enumerator BITONICMERGE
enumerator BITONICSORT

Variables

SortingProtocol DEFAULT_SORT_PROTO = CONFIDENTIAL_1PC ? NETWORK : QUICKSORT
namespace operators

Typedefs

template<typename T>
using PadWidth = typename std::conditional<std::is_same<T, int64_t>::value, __int128_t, int64_t>::type
template<typename E, typename Engine>
using PaddedBSharedVector = BSharedVector<PadWidth<typename E::ShareT>, cdough::EVector<PadWidth<typename E::ShareT>, E::replicationNumber>, Engine>

Enums

enum SortOrder

Sorting order enumeration.

Values:

enumerator ASC
enumerator DESC

Functions

template<typename Share, typename EVector, typename Engine> static DOXYGEN_IGNORE BSharedVector< Share, EVector, Engine > compare_rows (const std::vector< BSharedVector< Share, EVector, Engine > * > &x, const std::vector< BSharedVector< Share, EVector, Engine > * > &y)

Compares two MxN arrays row-wise by applying M greater-than comparisons on N keys.

NOTE: The i-th row, let l, from the left array is greater than the i-th row, let r, from the right array if l’s first key is greater than r’s first key, or the first keys are the same and l’s second key is greater than r’s second key, or the first two keys are the same and so forth, for all keys.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x_vec – The left column-first array with M rows and N columns.

  • y_vec – The right column-first array with M rows and N columns.

  • order – A vector that denotes the order of comparison per key.

Returns:

A new shared vector that contains the result bits of the M greater-than comparisons.

template<typename Share, typename EVector, typename Engine>
static BSharedVector<Share, EVector, Engine> compare_rows(std::vector<BSharedVector<Share, EVector, Engine>> &x_vec, std::vector<BSharedVector<Share, EVector, Engine>> &y_vec, const std::vector<SortOrder> &order)

Same as above but accepts the N columns by reference.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x_vec – The left column-first array with M rows and N columns.

  • y_vec – The right column-first array with M rows and N columns.

  • order – A vector that denotes the order of comparison per key.

Returns:

A new shared vector that contains the result bits of the M greater-than comparisons.

template<typename S, typename E, typename Eng>
static void swap(std::vector<ASharedVector<S, E, Eng>> &x_vec, std::vector<ASharedVector<S, E, Eng>> &y_vec, const ASharedVector<S, E, Eng> &bits)
template<typename S, typename E, typename Eng>
static void swap(std::vector<BSharedVector<S, E, Eng>> &x_vec, std::vector<BSharedVector<S, E, Eng>> &y_vec, const BSharedVector<S, E, Eng> &bits)

Swaps rows of two MxN arrays in place using the provided bits.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Eng – Secure computation engine type.

Parameters:
  • x_vec – The left column-first array with M rows and N columns.

  • y_vec – The right column-first array with M rows and N columns.

  • bits – The B-shared vector that contains ‘M’ bits to use for oblivious swapping (if bits[i]=True, the i-th rows will be swapped).

template<typename Share, typename EVector, typename Engine>
void swap(BSharedVector<Share, EVector, Engine> &x_vec, BSharedVector<Share, EVector, Engine> &y_vec, BSharedVector<Share, EVector, Engine> &bits)

Same as above but accepts the N columns by reference.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x_vec – The left column-first array with M rows and N columns.

  • y_vec – The right column-first array with M rows and N columns.

  • bits – The B-shared vector that contains ‘M’ bits to use for oblivious swapping (if bits[i]=True, the i-th rows will be swapped).

template<typename KeyT, typename ADataT, typename BDataT = KeyT>
static void bitonic_sort(std::vector<KeyT*> keys, std::vector<ADataT*> _data_a, std::vector<BDataT*> _data_b, const std::vector<SortOrder> &order)

Sorts rows in the given array on all columns. Updates array in place.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

Parameters:
  • key – The sorting columns.

  • order – The sorting direction per column.

template<typename S, typename E, typename Eng>
static void bitonic_sort(BSharedVector<S, E, Eng> &vec, SortOrder order = SortOrder::ASC)

Sorts rows in the given array on all columns. Updates array in place.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • _columns – The columns of the array.

  • order – The sorting direction per column (default ascending).

template<typename Share, typename EVector, typename Engine>
static PaddedBSharedVector<EVector, Engine> pad_input(BSharedVector<Share, EVector, Engine> &v, bool reverse_order)

Extend <=32 bit elements to 64 bit elements.

Shift original value into most significant 32 bits. Set least significant 32 bits equal to the initial index. If reverse_order is set, pad with the values -1 to -n, otherwise pad with 0 to n-1.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • v – The input vector with <=32 bits.

  • reverse_order – Indicates whether the upcoming sort is in reverse order.

Returns:

The 64 bit shared vector.

template<typename S, typename E, typename Eng>
static ElementwisePermutation<E, Eng> remove_padding(BSharedVector<S, E, Eng> &v, PaddedBSharedVector<E, Eng> &padded, bool reverse_order, bool convert_to_arithmetic = true)

Remove the padding from the 64 bit result to obtain the original <=32 bit values.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Eng – Secure computation engine type.

Parameters:
  • v – The original input vector to place the result in.

  • padded – The 64 bit shared vector.

  • reverse_order – Indicates whether the upcoming sort is in reverse order.

  • convert_to_arithmetic – Indicates whether to apply b2a on the permutation or not.

Returns:

The extracted permutation.

template<typename Share, typename EVector, typename Engine>
static std::pair<int, int> get_perm_counts(std::vector<BSharedVector<Share, EVector, Engine>*> _columns, std::vector<ASharedVector<Share, EVector, Engine>*> _data_a, std::vector<BSharedVector<Share, EVector, Engine>*> _data_b, const std::vector<SortOrder> &order, const std::vector<bool> &single_bit, const SortingProtocol protocol)

Get the permutation counts for the given specification.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • _columns – The columns to sort by.

  • _data_a – The AShared columns of the array to be sorted.

  • _data_b – The BShared columns of the array to be sorted.

  • order – The sorting direction per column.

  • single_bit – The single-bit columns.

  • protocol – The sorting protocol to use.

Returns:

The number of permutations and pairs required.

template<typename Share, typename EVector, typename Engine>
static void table_sort(std::vector<BSharedVector<Share, EVector, Engine>*> _columns, std::vector<ASharedVector<Share, EVector, Engine>*> _data_a, std::vector<BSharedVector<Share, EVector, Engine>*> _data_b, const std::vector<SortOrder> &order, const std::vector<bool> &single_bit, const SortingProtocol protocol)

Sorts rows in the given array on all columns. Updates array in place.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • _columns – The columns to sort by.

  • _data_a – The AShared columns of the array to be sorted.

  • _data_b – The BShared columns of the array to be sorted.

  • single_bit – which columns are single-bit columns (thus use 1-bit radixsort)

  • protocol – which sorting protocol to use

  • order – The sorting direction per column.

namespace cdough
namespace operators

Functions

template<typename KeyT, typename ADataT, typename BDataT = KeyT>
void pairwise_sort(std::vector<KeyT*> keys, std::vector<ADataT*> data_a, std::vector<BDataT*> data_b, const std::vector<SortOrder> &order)

Pairwise sorting network for tables.

Template Parameters:
  • KeyT – Key type

  • ADataT – Arithmetic shared vector type

  • BDataT – Boolean shared vector type

Parameters:
  • keys – sorting keys

  • data_a – arithmetic-shared data

  • data_b – boolean-shared data

  • order – whether ascending or descending. must be the same length as keys.

template<typename S, typename E, typename Eng>
void pairwise_sort(BSharedVector<S, E, Eng> &vec, SortOrder order = SortOrder::ASC)

Sort a single vector with the pairwise network.

Template Parameters:
  • S – Shared type

  • E – Container type

  • Eng – engine type

Parameters:
  • vec

  • order

namespace pairwise

Functions

std::vector<PairwiseRound> pw_comparisons(size_t n_)

Generate a list of comparison operations for the pairwise sorting network. Each round consists of a series of (sliced) alternating subset references. Works with non-power-of-two inputs.

Parameters:

n_ – input size

Returns:

std::vector<PairwiseRound>

struct PairwiseRound
#include <sorting_network.h>

Defines a single round of the pairwise sorting network.

Vectorized comparisons start at start_left and start_right, and occur in chunks of size step.

Public Members

size_t start_left
size_t start_right
size_t step
namespace cdough
namespace operators

Functions

template<typename T, typename E, typename Eng>
void partition(BSharedVector<T, E, Eng> &v, Vector<T> &comparisons, int start, int end, Vector<long> &pivots)

Partitions a vector segment based on comparison results.

Rearranges elements between start and end indices based on pivot comparisons. Elements equal to 0 in comparisons are moved to the left partition.

Template Parameters:
  • T – Share data type.

  • E – Share container type.

  • Eng – Engine type.

Parameters:
  • v – Vector to partition (modified in place).

  • comparisons – Comparison results for each element.

  • start – Starting index (inclusive).

  • end – Ending index (inclusive).

  • pivots – Vector tracking pivot positions.

template<typename T, typename EVector, typename Engine>
static void quicksort_body(BSharedVector<T, EVector, Engine> &v)

Core quicksort algorithm implementation.

Performs the main quicksort logic on a padded vector.

Template Parameters:
  • T – Share data type.

  • EVector – Share container type.

Parameters:

v – Vector to sort (modified in place).

template<typename Share, typename EVector, typename Engine>
static ElementwisePermutation<EVector, Engine> quicksort(BSharedVector<Share, EVector, Engine> &v, SortOrder order, bool no_invert)

Main quicksort entry point.

Sorts a vector using the quicksort algorithm with padding for stability. Handles both ascending and descending order sorting.

Template Parameters:
  • Share – Share data type.

  • EVector – Share container type.

Parameters:
  • v – Vector to sort (modified in place).

  • order – Sorting direction (ASC or DESC).

Returns:

Permutation representing the applied sort order.

namespace cdough
namespace operators

Typedefs

template<typename E, typename Eng>
using ASharedPerm = ASharedVector<int, cdough::EVector<int, E::replicationNumber>, Eng>
template<typename E, typename Eng>
using BSharedPerm = BSharedVector<int, cdough::EVector<int, E::replicationNumber>, Eng>

Functions

template<typename S, typename E, typename Eng>
static ElementwisePermutation<E, Eng> radix_sort_ccs(BSharedVector<S, E, Eng> &v, const int bits, const bool full_width = true)

Direct implementation of the AHI+22 radix sort algorithm.

Template Parameters:
  • S – Share data type.

  • E – Share container type.

Parameters:
  • Eng – Engine type.

  • v – Vector to sort.

  • bits – Number of bits to sort on.

  • full_width – Whether sorting on full bitwidth (affects sign bit handling).

Returns:

Permutation representing the sort order.

template<typename S, typename E, typename Eng>
static void radix_sort_body(BSharedVector<S, E, Eng> &v, const int bits, const bool full_width = true, bool no_invert = false)

The radix sort protocol.

Implements the radix sort protocol for a given number of bits.

Template Parameters:
  • S – Share data type.

  • E – Share container type.

  • Eng – Engine type.

Parameters:
  • v – Vector to sort.

  • bits – Number of bits to sort on.

  • full_width – Whether sorting on full bitwidth (affects sign bit handling).

  • no_invert – Whether to not invert the permutation.

template<typename S, typename E, typename Eng>
static ElementwisePermutation<E, Eng> radix_sort(BSharedVector<S, E, Eng> &v, SortOrder order, const size_t bits, bool no_invert)

The radix sort protocol entry point.

Template Parameters:
  • S – Share data type.

  • E – Share container type.

  • Eng – Engine type.

Parameters:
  • v – Vector to sort.

  • order – Sort order (ascending or descending).

  • bits – Number of bits to sort on.

  • no_invert – Whether to not invert the permutation.

Returns:

Permutation representing the applied sort order.

Merge

namespace cdough
namespace operators

Functions

template<typename Share, typename EVector, typename Engine>
static void compare_swap(BSharedVector<Share, EVector, Engine> &x, BSharedVector<Share, EVector, Engine> &y)

Performs conditional swap on two boolean shared vectors.

Swaps elements between x and y based on comparison result (x >= y). After the swap, x contains the smaller values and y contains the larger values.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • x – First vector (modified in place).

  • y – Second vector (modified in place).

template<typename Share, typename EVector, typename Engine>
static void odd_even_merge(BSharedVector<Share, EVector, Engine> &v)

Performs odd-even merge on a boolean shared vector.

Implements the odd-even merge algorithm. The vector size must be a power of two.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:

v – Vector to be merged (modified in place).

template<typename Share, typename EVector, typename Engine>
static void bitonic_merge(std::vector<BSharedVector<Share, EVector, Engine>*> _columns, std::vector<ASharedVector<Share, EVector, Engine>*> _data_a, std::vector<BSharedVector<Share, EVector, Engine>*> _data_b, const std::vector<SortOrder> &order)

Sorts vectors based on some keys.

Each key needs to have two already sorted halves in the same order.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • _columns – The keys to merge based on.

  • _data_a – The AShared data vectors to merge.

  • _data_b – The BShared data vectors to merge.

  • order – The sorting direction per column.

template<typename Share, typename EVector, typename Engine>
static void bitonic_merge(std::vector<BSharedVector<Share, EVector, Engine>> _columns, std::vector<ASharedVector<Share, EVector, Engine>> _data_a, std::vector<BSharedVector<Share, EVector, Engine>> _data_b, const std::vector<SortOrder> &order)

Sorts vectors based on some keys.

Each key needs to have two already sorted halves in the same order.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

Parameters:
  • _columns – The keys to merge based on.

  • _data_a – The AShared data vectors to merge.

  • _data_b – The BShared data vectors to merge.

  • order – The sorting direction per column (default ascending).

template<typename Share, typename EVector, typename Engine>
static void bitonic_merge(BSharedVector<Share, EVector, Engine> &vec, SortOrder order = SortOrder::ASC)

Sorts a vector that has two already sorted halves in the same order.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

Parameters:
  • vec – The vector to merge based on.

  • order – The sorting direction (default ascending).

Relational

namespace cdough
namespace aggregators

Typedefs

template<typename Share, typename EVector, typename Engine>
using A_ = ASharedVector<Share, EVector, Engine>
template<typename Share, typename EVector, typename Engine>
using B_ = BSharedVector<Share, EVector, Engine>

Functions

template<typename A>
void sum(const A &group, A &a, const A &b)

Arithmetic sum aggregation.

Template Parameters:

A – Arithmetic shared vector type.

Parameters:
  • group – Grouping bits indicating which elements to aggregate.

  • a – Accumulator vector (modified in place).

  • b – Input vector to be aggregated.

template<typename B>
void bit_or(const B &group, B &a, const B &b)

Boolean OR aggregation.

Template Parameters:

B – Boolean shared vector type.

Parameters:
  • group – Grouping bits indicating which elements to aggregate.

  • a – Accumulator vector (modified in place).

  • b – Input vector to be aggregated.

template<typename A>
void count(const A &group, A &a, const A &b)

Count aggregation (delegates to sum).

Template Parameters:

A – Arithmetic shared vector type.

Parameters:
  • group – Grouping bits indicating which elements to count.

  • a – Accumulator vector (modified in place).

  • b – Input vector (typically all ones for counting).

template<typename B>
void _min_max_agg(const B &group, B &a, const B &b, const bool &minimum = false)

Internal helper for min/max aggregation.

Template Parameters:

B – Boolean shared vector type.

Parameters:
  • group – Grouping bits indicating which elements to aggregate.

  • a – Accumulator vector (modified in place).

  • b – Input vector to be aggregated.

  • minimum – If true, computes minimum; if false, computes maximum.

template<typename B>
void max(const B &group, B &a, const B &b)

Maximum aggregation.

Template Parameters:

B – Boolean shared vector type.

Parameters:
  • group – Grouping bits indicating which elements to aggregate.

  • a – Accumulator vector (modified in place).

  • b – Input vector to be aggregated.

template<typename B>
void min(const B &group, B &a, const B &b)

Minimum aggregation.

Template Parameters:

B – Boolean shared vector type.

Parameters:
  • group – Grouping bits indicating which elements to aggregate.

  • a – Accumulator vector (modified in place).

  • b – Input vector to be aggregated.

template<typename T>
void copy(const T &group, T &a, const T &b)

Identity “aggregation”, used for non-aggregated joins. Templated to accept either arithmetic or boolean types. Copies rows from left to the right.

Parameters:
  • group – grouping bits.

  • a – first vector, which will be updated.

  • b – second vector.

template<typename B>
void valid(const B &group, B &a, const B &b)

validity aggregation. For internal use while updating valid column.

Template Parameters:

B – Boolean shared vector type.

Parameters:
  • group – Grouping bits.

  • a – Accumulator vector (modified in place).

  • b – Input vector.

template<typename S, typename E, typename Eng>
void aggregate(std::vector<B_<S, E, Eng>> &keys, const std::vector<std::tuple<B_<S, E, Eng>, B_<S, E, Eng>, void (*)(const B_<S, E, Eng>&, B_<S, E, Eng>&, const B_<S, E, Eng>&)>> &agg_spec_b, const std::vector<std::tuple<A_<S, E, Eng>, A_<S, E, Eng>, void (*)(const A_<S, E, Eng>&, A_<S, E, Eng>&, const A_<S, E, Eng>&)>> &agg_spec_a, const enum Direction dir = Direction::Forward, std::optional<B_<S, E, Eng>> sel_b = {})

Sorting-network based agregation. Assumes all vectors are the same size.

Template Parameters:
  • S – underlying data type of vectors

  • E – Share container type.

Parameters:
  • keys – vector of keys to join and aggregate on

  • agg_spec_b – boolean aggregations

  • agg_spec_a – arithmetic aggregations

  • dir – which direction to run the aggregation

  • sel_b – selection column (for table operations, usually table ID)

template<typename S, typename E, typename Eng>
void aggregate(std::vector<B_<S, E, Eng>> &keys, const std::vector<std::tuple<B_<S, E, Eng>, B_<S, E, Eng>, void (*)(const B_<S, E, Eng>&, B_<S, E, Eng>&, const B_<S, E, Eng>&)>> &agg_spec_b, const std::vector<std::tuple<A_<S, E, Eng>, A_<S, E, Eng>, void (*)(const A_<S, E, Eng>&, A_<S, E, Eng>&, const A_<S, E, Eng>&)>> &agg_spec_a, B_<S, E, Eng> sel_b)

Aggregation, with additional selection (window) argument. Performs reverse aggregation.

Template Parameters:
  • S

  • E

Parameters:
  • keys

  • agg_spec_b

  • agg_spec_a

  • sel_b

template<typename T>
void tree_prefix_sum(T v, const bool &reverse = false)

Compute a prefix sum over a vector using a log-depth aggregation tree. Based on sorting network aggregation but entirely local computation, with the simplification that all entries belong to the same group.

TODO: extend this to any user-supplied associative operation

Template Parameters:

T – vector type (shared or plaintext)

Parameters:
  • v – input vector

  • reverse – whether to compute a suffix sum instead

namespace cdough
namespace operators

Functions

template<typename Share, typename EVector, typename Engine>
static void distinct(std::vector<BSharedVector<Share, EVector, Engine>*> &keys, BSharedVector<Share, EVector, Engine> *res)

Obliviously marks distinct rows based on keys (adjacent rows only).

Only adjacent rows are considered, so vectors should be sorted before calling for global uniqueness.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • keys – List of keys to consider for uniqueness.

  • res – Vector to place the result in.

template<typename Share, typename EVector, typename Engine>
static void distinct(std::vector<BSharedVector<Share, EVector, Engine>> &keys, BSharedVector<Share, EVector, Engine> &res)

Marks distinct rows based on keys (vector reference overload).

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • keys – Vector of keys to consider for uniqueness.

  • res – Vector to place the result in.

namespace cdough
namespace aggregators
template<typename A, typename B>
class AggregationSelector
#include <aggregation_selector.h>

A helper type to store an aggregation of either AShared or BShared type.

Template Parameters:
  • A

  • B

Public Functions

inline AggregationSelector(bf_t bFunc)
inline AggregationSelector(af_t aFunc)
inline af_t getA()

Get the AShared function. If this object does not store an AShared function, abort.

Returns:

af_t

inline bf_t getB()

Get the BShared function. If this object does not store a BShared function, abort.

Returns:

bf_t

inline bool isAggregation()

Check if the stored function is a copy aggregation or aggregation.

Returns:

true the stored function is an aggregation (non-copy)

Returns:

false the stored function is a copy function

Private Types

using af_t = void (*)(const A&, A&, const A&)
using bf_t = void (*)(const B&, B&, const B&)

Private Members

bf_t bFunc
af_t aFunc

Implementation of EncodedTable join.

Defines

TEMPLATE_DEF
TABLE_T
namespace cdough
namespace relational
namespace cdough
namespace aggregators
template<typename T, typename E, typename Engine>
class BrentKung : public cdough::aggregators::PrefixNetwork<T, E, Engine>
#include <prefix_network.h>

The Brent-Kung network. Conceptually similar to a Blelloch prefix network.

Asymptotically optimal depth \(2 \log_2 n\) and work \(2 n\). Not as parallelizable due to a sparser network structure.

Example network on 32 elements (each O is a gate):

O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O.
O...O...O...O...O...O...O...O...
O.......O.......O.......O.......
O...............O...............
O...............................  "reduce" phase
........O.......................
....O.......O.......O...........
..O...O...O...O...O...O...O.....
.O.O.O.O.O.O.O.O.O.O.O.O.O.O.O..  "distribute" phase

Here we use only 57 gates, but 9 rounds.

Public Functions

inline BrentKung(enum Direction dir, size_t _n, Engine &engine)

Private Types

using _S_t = SharedVector<T, E, Engine>

Private Functions

inline int half_depth() const
inline virtual std::pair<_S_t, _S_t> _level(int i, _S_t &input) const

Return a pair of shared vectors representing the two sides of the comparison at level i. Both should be of length level_size(i)

Parameters:
  • i – the level of the prefix network

  • input – the input (full-span) vector

Returns:

std::pair<_S_t, _S_t>

inline virtual size_t level_size(int i) const

How many elements participate in this level.

Parameters:

i

Returns:

size_t

inline virtual int depth() const

The depth of this network.

Returns:

int

template<typename T, typename E, typename Engine>
class HillisSteele : public cdough::aggregators::PrefixNetwork<T, E, Engine>
#include <prefix_network.h>

The Hillis-Steele / Kogge-Stone network. Optimal depth \(\log_2 n\) and highly parallelizable, but not work efficient ( \(n \log n\) operations).

Example network on 32 elements (each O is a gate):

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO.
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO..
OOOOOOOOOOOOOOOOOOOOOOOOOOOO....
OOOOOOOOOOOOOOOOOOOOOOOO........
OOOOOOOOOOOOOOOO................

In this example, we use 80 gates but only 5 rounds.

Template Parameters:
  • T

  • E

Public Functions

inline HillisSteele(enum Direction dir, size_t _n, Engine &engine)

Private Types

using _S_t = SharedVector<T, E, Engine>

Private Functions

inline virtual std::pair<_S_t, _S_t> _level(int i, _S_t &input) const

Return a pair of shared vectors representing the two sides of the comparison at level i. Both should be of length level_size(i)

Parameters:
  • i – the level of the prefix network

  • input – the input (full-span) vector

Returns:

std::pair<_S_t, _S_t>

inline virtual size_t level_size(int i) const

How many elements participate in this level.

Parameters:

i

Returns:

size_t

inline virtual int depth() const

The depth of this network.

Returns:

int

template<typename T, typename E, typename Engine>
class PrefixNetwork
#include <prefix_network.h>

Parent class for Prefix Networks. Automatically handles type conversions for ASharedVector / BSharedVector.

Subclasses must implement three functions:

  • _level(i, input), which produces a view of input for level $i$;

  • level_size(i), which returns the size of the view at level $i$; and

  • depth(), which returns the depth of the current network.

Template Parameters:
  • T

  • E

Subclassed by cdough::aggregators::BrentKung< T, E, Engine >, cdough::aggregators::HillisSteele< T, E, Engine >

Public Functions

inline PrefixNetwork(enum Direction dir, size_t _n, Engine &engine)

Construct a new Prefix Network.

Parameters:
  • dir – whether this is a forward (down) or reverse (up) aggregation

  • _n – the size of the network

  • engine

inline it begin() const
inline it end() const

Protected Functions

virtual std::pair<_S_t, _S_t> _level(int i, _S_t &input) const = 0

Return a pair of shared vectors representing the two sides of the comparison at level i. Both should be of length level_size(i)

Parameters:
  • i – the level of the prefix network

  • input – the input (full-span) vector

Returns:

std::pair<_S_t, _S_t>

template<typename ShVec_t>
inline std::pair<ShVec_t, ShVec_t> level(int i, ShVec_t &input) const

Produce a view of input which corresponds to the prefix network at level i. Calls down into subclasses’ _level() function and handles casting back to the appropriate type.

Template Parameters:

ShVec_t – either ASharedVector or BSharedVector

Parameters:
  • i

  • input

Returns:

std::pair<ShVec_t, ShVec_t>

virtual size_t level_size(int i) const = 0

How many elements participate in this level.

Parameters:

i

Returns:

size_t

virtual int depth() const = 0

The depth of this network.

Returns:

int

Protected Attributes

size_t n
bool reversed

Private Types

using _S_t = SharedVector<T, E, Engine>

Private Members

Engine &engine
struct it
#include <prefix_network.h>

Prefix network iterator struct.

(This allows us to implement C++ range loops over prefix network)

Public Functions

inline bool operator!=(const it &other) const
inline void operator++()
inline Level operator*() const

Public Members

int idx
const PrefixNetwork *parent
struct Level
#include <prefix_network.h>

Public Members

size_t size
LevelFn f
struct LevelFn
#include <prefix_network.h>

Helper struct to handle iterating through a prefix network.

Public Functions

template<typename ShVec_t>
inline auto operator()(ShVec_t &v) const

Public Members

const PrefixNetwork *parent
int i
struct JoinOptions

Options struct for join. Construct an instance and pass it in, or use inline struct initializer syntax.

Available fields:

  • left_outer: if this is a left outer join.

  • right_outer: if this is a right outer join.

  • anti: if this is an anti join.

  • trim_invalid: whether to trim invalid rows from the output (bounded by the size of the right table)

The join options generalize the other kinds of joins: inner joins are neither left outer nor right outer; full outer joins are both left outer and right outer. The default is an inner join; i.e., both are false.

struct AggregationOptions

Options struct for aggregation.

Available fields:

  • reverse: whether this is a forward or reverse aggregation. (default false)

  • do_sort: whether to sort first. (default true)

  • mark_valid: whether to mark valid rows post-aggregation. (default true)

  • table_id: an optional string pointing to the name of the table ID column.

Machine Learning

namespace cdough
namespace operators
namespace ml

Typedefs

using HeightWidth = cdough::matrix::hybrid::HeightWidth

Functions

template<typename T, template<typename> class M>
M<T> forwardRecursive(const M<T> &input, const std::vector<std::shared_ptr<LayerML<T, M>>> &layers, size_t index = 0)

Forward pass through the model layers recursively.

Parameters:
  • input – Input matrix

  • layers – Vector of layer pointers

  • index – Current layer index

Returns:

M<T> Output matrix

template<typename T, template<typename> class M, typename Engine>
M<T> forwardIterative(const M<T> &input, const std::vector<std::shared_ptr<LayerML<T, M>>> &layers, Engine &engine, size_t index = 0)

Forward pass through the model layers iteratively.

Parameters:
  • input – Input matrix

  • layers – Vector of layer pointers

  • index – Starting layer index

Returns:

M<T> Output matrix

template<typename T, template<typename> class M>
class AvgPoolingLayer : public cdough::operators::ml::LayerML<T, M>
#include <machine_learning.h>

Average Pooling Layer.

Template Parameters:
  • T – Data type

  • M – Matrix type

Public Functions

inline AvgPoolingLayer(const HeightWidth &inputSize, size_t inChannels, const HeightWidth &filterSize, const HeightWidth &stride, const HeightWidth &padding)

Construct a new Avg Pooling Layer object.

Parameters:
  • inputSize – Size of the input (height, width)

  • inChannels – Number of input channels

  • filterSize – Size of the filter (height, width)

  • stride – Stride of the pooling (height, width)

  • padding – Padding of the pooling (height, width)

inline virtual M<T> forward(const M<T> &input) override

Forward pass of the Avg Pooling layer.

Parameters:

input – Input matrix

Returns:

M<T> Output matrix

inline virtual std::string name() const override

Get the name of the layer.

Returns:

std::string Name of the layer “Avg”

Private Members

HeightWidth inputSize_
size_t inChannels_
HeightWidth filterSize_
HeightWidth stride_
HeightWidth padding_
template<typename T, template<typename> class M>
class Conv2DLayer : public cdough::operators::ml::LayerML<T, M>
#include <machine_learning.h>

Convolutional Layer.

Template Parameters:
  • T – Data type

  • M – Matrix type

Public Functions

inline Conv2DLayer(const M<T> &filter, const HeightWidth &inputSize, size_t inChannels, size_t outChannels, const HeightWidth &filterSize, const HeightWidth &stride, const HeightWidth &padding)

Construct a new Conv2D Layer object.

Parameters:
  • filter – Convolution filter matrix

  • inputSize – Size of the input (height, width)

  • inChannels – Number of input channels

  • outChannels – Number of output channels

  • filterSize – Size of the filter (height, width)

  • stride – Stride of the convolution (height, width)

  • padding – Padding of the convolution (height, width)

inline virtual M<T> forward(const M<T> &input) override

Forward pass of the Conv2D layer.

Parameters:

input – Input matrix

Returns:

M<T> Output matrix

inline virtual std::string name() const override

Get the name of the layer.

Returns:

std::string Name of the layer “Conv2D”

inline virtual void loadWeights(const std::map<std::string, M<T>> &weightMap) override

Load pretrained filter weights.

Parameters:

weightMap – Must contain “filter” key

Private Members

HeightWidth inputSize_
size_t inChannels_
size_t outChannels_
HeightWidth filterSize_
HeightWidth stride_
HeightWidth padding_
M<T> filter_
template<typename T, template<typename> class M>
class FullyConnectedLayer : public cdough::operators::ml::LayerML<T, M>
#include <machine_learning.h>

Fully Connected Layer.

Template Parameters:
  • T – Data type

  • M – Matrix type

Public Functions

inline FullyConnectedLayer(const M<T> &weights, const M<T> &bias, size_t inputDim, size_t outputDim)

Construct a new Fully Connected Layer object.

Parameters:
  • weights – Weights matrix

  • bias – Bias matrix

  • inputDim – Input dimension

  • outputDim – Output dimension

inline virtual M<T> forward(const M<T> &input) override

Forward pass of the Fully Connected layer.

Parameters:

input – Input matrix

Returns:

M<T> Output matrix

inline virtual std::string name() const override

Get the name of the layer.

Returns:

std::string Name of the layer “Fully”

inline virtual void loadWeights(const std::map<std::string, M<T>> &weightMap) override

Load pretrained weights and bias.

Parameters:

weightMap – Must contain “weights” and “bias” keys

Private Members

size_t inputDim_
size_t outputDim_
M<T> weights_
M<T> bias_
template<typename T, template<typename> class M>
class LayerML
#include <machine_learning.h>

Base class for Machine Learning layers.

Template Parameters:
  • T – Data type

  • M – Matrix type

Subclassed by cdough::operators::ml::AvgPoolingLayer< T, M >, cdough::operators::ml::Conv2DLayer< T, M >, cdough::operators::ml::FullyConnectedLayer< T, M >, cdough::operators::ml::ReLULayer< T, M >

Public Functions

LayerML() = default
virtual ~LayerML() = default
virtual M<T> forward(const M<T> &input) = 0

Forward pass of the layer.

Parameters:

input – Input matrix

Returns:

M<T> Output matrix

virtual std::string name() const = 0

Get the name of the layer.

Returns:

std::string Name of the layer

inline virtual void loadWeights(const std::map<std::string, M<T>> &weightMap)

Load pretrained weights into this layer.

Layers with learnable parameters (Conv2D, FullyConnected) override this to accept weight matrices keyed by name (“filter”, “weights”, “bias”). Layers without parameters (ReLU, AvgPool) use the default no-op.

Parameters:

weightMap – Map of parameter name to matrix

template<typename T, template<typename> class M, typename Engine>
class ModelML
#include <machine_learning.h>

Machine Learning Model.

The model supports adding layers and performing a forward pass through the layers.

Layer weights have channels in interleaved format. Inputs have the same interleaved channel format. Inputs can be concatenated for multiple instances.

For example, to do inference over a batch of 3 instances,: [ Instance1_ROWS Instance2_ROWS Instance3_ROWS ]

Note for each instance: [ ROW1: ch1_val, ch2_val, ch3_val, … ch1_val, ch2_val, ch3_val ROW2: ch1_val, ch2_val, ch3_val, … ch1_val, ch2_val, ch3_val … ]

Template Parameters:
  • T – Data type

  • M – Matrix type

  • Engine – Engine type

Public Functions

inline ModelML(Engine &engine, size_t precision)

Construct a new Model ML object.

Parameters:
  • engine – Engine reference

  • precision – Precision for fixed-point representation

~ModelML() = default
inline void addLayer(std::shared_ptr<LayerML<T, M>> layer)

Adds an already constructed layer to the model.

Parameters:

layer – Layer pointer

inline M<T> forward(const M<T> &input)

Forward pass through the model to run inference on multiple inputs.

Parameters:

input – Input matrix

Returns:

M<T> Output matrix

inline ModelML &conv2DLayer(const HeightWidth &inputSize, size_t inChannels, size_t outChannels, const HeightWidth &filterSize, const HeightWidth &stride, const HeightWidth &padding)

Adds a Conv2D layer to the model with uninitialized weights.

Parameters:
  • inputSize – Size of the input (height, width) for each channel

  • inChannels – Number of input channels

  • outChannels – Number of output channels

  • filterSize – Size of the filter (height, width)

  • stride – Stride of the convolution (height, width)

  • padding – Padding of the convolution (height, width)

Returns:

ModelML& Reference to the model

inline ModelML &fullyConnectedLayer(size_t inputDim, size_t outputDim)

Adds a Fully Connected layer to the model with random weights.

Parameters:
  • inputDim – Input dimension

  • outputDim – Output dimension

Returns:

ModelML& Reference to the model

inline ModelML &avgPoolingLayer(const HeightWidth &inputSize, size_t inChannels, const HeightWidth &filterSize, const HeightWidth &stride, const HeightWidth &padding)

Adds an Average Pooling layer to the model.

Parameters:
  • inputSize – Size of the input (height, width)

  • inChannels – Number of input channels

  • filterSize – Size of the filter (height, width)

  • stride – Stride of the pooling (height, width)

  • padding – Padding of the pooling (height, width)

Returns:

ModelML& Reference to the model

inline ModelML &reLULayer(size_t inputDim)

Adds a ReLU activation layer to the model.

Parameters:

inputDim – Input dimension

Returns:

ModelML& Reference to the model

inline ModelML &conv2DLayerWithWeights(const M<T> &filter, const HeightWidth &inputSize, const size_t &inChannels, const size_t &outChannels, const HeightWidth &filterSize, const HeightWidth &stride, const HeightWidth &padding)

Adds a Conv2D layer with a pretrained filter matrix.

Use load_bin_file() to load the raw data, construct a PlainMatrix, and secret share it in the calling code (bench file), then pass the resulting SecureMatrix here.

Parameters:
  • filter – Pretrained filter matrix (already secret shared)

  • inputSize – Size of the input (height, width) for each channel

  • inChannels – Number of input channels

  • outChannels – Number of output channels

  • filterSize – Size of the filter (height, width)

  • stride – Stride of the convolution (height, width)

  • padding – Padding of the convolution (height, width)

Returns:

ModelML& Reference to the model

inline ModelML &fullyConnectedLayerWithWeights(const M<T> &weights, const M<T> &bias, const size_t &inputDim, const size_t &outputDim)

Adds a Fully Connected layer with pretrained weight and bias matrices.

Use load_bin_file() to load the raw data, construct PlainMatrix objects, and secret share them in the calling code (bench file), then pass the resulting SecureMatrices here.

Parameters:
  • weights – Pretrained weight matrix (already secret shared)

  • bias – Pretrained bias matrix (already secret shared)

  • inputDim – Input dimension

  • outputDim – Output dimension

Returns:

ModelML& Reference to the model

Private Members

std::vector<std::shared_ptr<LayerML<T, M>>> layers
const size_t precision
Engine &engine
template<typename T, template<typename> class M>
class ReLULayer : public cdough::operators::ml::LayerML<T, M>
#include <machine_learning.h>

ReLU Activation Layer.

Template Parameters:
  • T – Data type

  • M – Matrix type

Public Functions

inline ReLULayer(size_t inputDim)

Construct a new ReLU Layer object.

Parameters:

inputDim – Input dimension

inline virtual M<T> forward(const M<T> &input) override

Forward pass of the ReLU layer.

Parameters:

input – Input matrix

Returns:

M<T> Output matrix

inline virtual std::string name() const override

Get the name of the layer.

Returns:

std::string Name of the layer “ReLU”

Private Members

size_t inputDim

Streaming

namespace cdough
namespace operators

Functions

template<typename Share, typename EVector, typename Engine>
static void tumbling_window(ASharedVector<Share, EVector, Engine> &key, const Share &window_size, ASharedVector<Share, EVector, Engine> &res)

Computes tumbling window identifiers for stream processing.

A tumbling window divides the stream into non-overlapping windows of fixed size. This function computes the window identifier for each element by dividing the key (typically a timestamp or sequence number) by the window size.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • key – Input vector containing keys/timestamps for window assignment.

  • window_size – The size of each tumbling window.

  • res – Output vector that will contain the computed window identifiers.

template<typename Share, typename EVector, typename Engine>
void mark_gap_session(ASharedVector<Share, EVector, Engine> &timestamp, BSharedVector<Share, EVector, Engine> &session_start, const Share &gap)

Marks the start of new gap-based sessions in a timestamp sequence.

This function identifies session boundaries based on gaps in timestamps. A new session starts when the gap between consecutive timestamps exceeds the specified threshold. The first element is always marked as a session start.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • timestamp – Input vector of timestamps in ascending order.

  • session_start – Output boolean vector marking session start positions.

  • gap – The maximum allowed gap between timestamps within a session.

template<typename Share, typename EVector, typename Engine>
void gap_session_window(std::vector<BSharedVector<Share, EVector, Engine>> &keys, ASharedVector<Share, EVector, Engine> &timestamp_a, BSharedVector<Share, EVector, Engine> &timestamp_b, BSharedVector<Share, EVector, Engine> &window_id, const Share &gap)

Creates gap-based session windows for stream aggregation.

This function implements session windowing based on timestamp gaps. Sessions are identified using the mark_gap_session function, then window identifiers are computed using reverse aggregation to assign the same window ID to all elements within a session.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • keys – Vector of key vectors used for aggregation operations.

  • timestamp_a – Arithmetic shared vector of timestamps.

  • timestamp_b – Boolean shared vector of timestamps (for multiplexing).

  • window_id – Output vector that will contain the computed window identifiers.

  • gap – The maximum allowed gap between timestamps within a session.

template<typename Share, typename EVector, typename Engine>
void mark_threshold_session(BSharedVector<Share, EVector, Engine> &function_res, BSharedVector<Share, EVector, Engine> &session_start, BSharedVector<Share, EVector, Engine> &potential_window, const Share &threshold)

Marks the start of new threshold-based sessions.

This function identifies session boundaries based on a threshold applied to a function result (e.g., sensor readings, activity levels). A new session starts when the function result exceeds the threshold and the previous element was below the threshold.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • function_res – Input vector containing function results to compare against threshold.

  • session_start – Output boolean vector marking session start positions.

  • potential_window – Output boolean vector indicating elements above threshold.

  • threshold – The threshold value for session detection.

template<typename Share, typename EVector, typename Engine>
void threshold_session_window(std::vector<BSharedVector<Share, EVector, Engine>> &keys, BSharedVector<Share, EVector, Engine> &function_res, BSharedVector<Share, EVector, Engine> &timestamp_b, BSharedVector<Share, EVector, Engine> &window_id, const Share &gap)

Creates threshold-based session windows for stream aggregation.

This function implements session windowing based on threshold detection. Sessions are identified when function results exceed a threshold, and window identifiers are computed using aggregation with the potential_window as a selection mask to only include elements that meet the threshold criteria.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – Share container type.

  • Engine – Secure computation engine type.

Parameters:
  • keys – Vector of key vectors used for aggregation operations.

  • function_res – Input vector containing function results for threshold comparison.

  • timestamp_b – Boolean shared vector of timestamps (for multiplexing).

  • window_id – Output vector that will contain the computed window identifiers.

  • gap – The threshold value for session detection (note: parameter name is misleading).

Common

namespace cdough
namespace aggregators

Enums

enum class Direction

Denotes the direction of an aggregation.

Values:

enumerator Forward
enumerator Reverse

Variables

enum cdough::aggregators::Direction Direction
namespace operators

Functions

template<typename Share, typename EVector, typename Engine>
static ASharedVector<Share, EVector, Engine> multiplex(const ASharedVector<Share, EVector, Engine> &sel, const ASharedVector<Share, EVector, Engine> &a, const ASharedVector<Share, EVector, Engine> &b)

Conditional selection between two arithmetic shared vectors.

Performs oblivious selection: returns a if sel is 0, b if sel is 1. Uses linear combination for arithmetic shares.

Template Parameters:

Share – The underlying data type of the shared vectors.

Parameters:
  • sel – Arithmetic selector vector.

  • a – First input vector (returned when sel is 0).

  • b – Second input vector (returned when sel is 1).

Returns:

The multiplexed result vector.

template<typename Share, typename EVector, typename Engine>
static BSharedVector<Share, EVector, Engine> multiplex(const BSharedVector<Share, EVector, Engine> &sel, const BSharedVector<Share, EVector, Engine> &a, const BSharedVector<Share, EVector, Engine> &b)

Conditional selection between two boolean shared vectors.

Performs oblivious selection: returns a if sel is false, b if sel is true.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – The container type of the shared vectors.

  • Engine – The secure computation engine type.

Parameters:
  • sel – Boolean selector vector (extended to full bit width).

  • a – First input vector (returned when sel is false).

  • b – Second input vector (returned when sel is true).

Returns:

The multiplexed result vector.

template<typename Share, typename EVector, typename Engine>
static void auto_conversion(BSharedVector<Share, EVector, Engine> &x, BSharedVector<Share, EVector, Engine> &res)

Identity conversion for boolean shared vectors.

Template Parameters:

Share – The underlying data type of the shared vectors.

Parameters:
  • x – Input boolean shared vector.

  • res – Output boolean shared vector (copy of input).

template<typename Share, typename EVector, typename Engine>
static void auto_conversion(ASharedVector<Share, EVector, Engine> &x, ASharedVector<Share, EVector, Engine> &res)

Identity conversion for arithmetic shared vectors.

Template Parameters:

Share – The underlying data type of the shared vectors.

Parameters:
  • x – Input arithmetic shared vector.

  • res – Output arithmetic shared vector (copy of input).

template<typename Share, typename EVector, typename Engine>
static void auto_conversion(BSharedVector<Share, EVector, Engine> &x, ASharedVector<Share, EVector, Engine> &res)

Converts boolean shared vector to arithmetic shared vector.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – The container type of the shared vectors.

  • Engine – The secure computation engine type.

Parameters:
  • x – Input boolean shared vector.

  • res – Output arithmetic shared vector.

template<typename Share, typename EVector, typename Engine>
static void auto_conversion(ASharedVector<Share, EVector, Engine> &x, BSharedVector<Share, EVector, Engine> &res)

Converts arithmetic shared vector to boolean shared vector.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – The container type of the shared vectors.

  • Engine – The secure computation engine type.

Parameters:
  • x – Input arithmetic shared vector.

  • res – Output boolean shared vector.

template<typename Share, typename EVector, typename Engine>
static void secret_share_vec(const Vector<Share> &data, BSharedVector<Share, EVector, Engine> &out, PartyID data_party = 0)

Creates boolean shared vector from plaintext data.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – The container type of the shared vectors.

  • Engine – The secure computation engine type.

Parameters:
  • data – Plaintext vector to be secret-shared.

  • out – Output boolean shared vector.

  • data_party – The party that provides the input data.

template<typename Share, typename EVector, typename Engine>
static void secret_share_vec(const Vector<Share> &data, ASharedVector<Share, EVector, Engine> &out, PartyID data_party = 0)

Creates arithmetic shared vector from plaintext data.

Template Parameters:
  • Share – The underlying data type of the shared vectors.

  • EVector – The container type of the shared vectors.

  • Engine – The secure computation engine type.

Parameters:
  • data – Plaintext vector to be secret-shared.

  • out – Output arithmetic shared vector.

  • data_party – The party that provides the input data.