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 runtimeaggregation.h– Secure aggregation functions (SUM, COUNT, etc.) and oblivious aggregation networkcircuits.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 –
-
template<typename S, typename E, typename Eng>
-
namespace operators
Shuffle
Defines
-
random_buffer_size
-
namespace cdough
-
namespace operators
Typedefs
Functions
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.
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.
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.
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.
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
permutationas 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.
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.
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.
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.
Obliviously apply a sharded secret-shared permutation (Honest Majority).
This overloaded function just passes the ElementwisePermutation’s underlying SharedVector.
- Parameters:
x – The ElementwisePermutation to permute.
permutation – The permutation to apply.
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.
Obliviously apply the inverse of a sharded secret-shared permutation (Honest Majority).
- Template Parameters:
EVector – Share container type.
Engine – Secure computation engine type.
- Parameters:
x – The ElementwisePermutation to permute.
permutation – The permutation whose inverse to apply.
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.
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.
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.
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.
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.
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.
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.
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.
-
namespace operators
Sorting
-
namespace cdough
Enums
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
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
MxNarrays row-wise by applyingMgreater-than comparisons onNkeys.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
Mrows andNcolumns.y_vec – The right column-first array with
Mrows andNcolumns.order – A vector that denotes the order of comparison per key.
- Returns:
A new shared vector that contains the result bits of the
Mgreater-than comparisons.
Same as above but accepts the
Ncolumns 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
Mrows andNcolumns.y_vec – The right column-first array with
Mrows andNcolumns.order – A vector that denotes the order of comparison per key.
- Returns:
A new shared vector that contains the result bits of the
Mgreater-than comparisons.
Swaps rows of two
MxNarrays in place using the providedbits.- Template Parameters:
Share – Share data type.
EVector – Share container type.
Eng – Secure computation engine type.
- Parameters:
x_vec – The left column-first array with
Mrows andNcolumns.y_vec – The right column-first array with
Mrows andNcolumns.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).
Same as above but accepts the
Ncolumns 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
Mrows andNcolumns.y_vec – The right column-first array with
Mrows andNcolumns.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.
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).
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.
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.
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.
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.
-
template<typename T>
-
SortingProtocol DEFAULT_SORT_PROTO = CONFIDENTIAL_1PC ? NETWORK : QUICKSORT
-
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.
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_leftandstart_right, and occur in chunks of sizestep.
-
std::vector<PairwiseRound> pw_comparisons(size_t n_)
-
template<typename KeyT, typename ADataT, typename BDataT = KeyT>
-
namespace operators
-
namespace cdough
-
namespace operators
Functions
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.
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).
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 operators
-
namespace cdough
-
namespace operators
Typedefs
Functions
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.
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.
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.
-
namespace operators
Merge
-
namespace cdough
-
namespace operators
Functions
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).
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).
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.
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).
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).
-
namespace operators
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
-
template<typename Share, typename EVector, typename Engine>
-
namespace aggregators
-
namespace cdough
-
namespace operators
Functions
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.
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 operators
-
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 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
-
template<typename A, typename B>
-
namespace aggregators
Implementation of EncodedTable join.
-
namespace cdough
-
namespace relational
-
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
Ois 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.
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
-
using _S_t = SharedVector<T, E, Engine>
-
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
Ois a gate):OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO. OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO.. OOOOOOOOOOOOOOOOOOOOOOOOOOOO.... OOOOOOOOOOOOOOOOOOOOOOOO........ OOOOOOOOOOOOOOOO................
In this example, we use 80 gates but only 5 rounds.
- Template Parameters:
T –
E –
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 ofinputfor level $i$;level_size(i), which returns the size of the view at level $i$; anddepth(), 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
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
inputwhich corresponds to the prefix network at leveli. 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
Private Types
-
using _S_t = SharedVector<T, E, 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 void operator++()
-
inline void operator++()
-
struct Level
- #include <prefix_network.h>
-
struct LevelFn
- #include <prefix_network.h>
Helper struct to handle iterating through a prefix network.
-
template<typename T, typename E, typename Engine>
-
namespace aggregators
-
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. (defaultfalse)do_sort: whether to sort first. (defaulttrue)mark_valid: whether to mark valid rows post-aggregation. (defaulttrue)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
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
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”
Private Members
-
HeightWidth inputSize_
-
size_t inChannels_
-
size_t outChannels_
-
HeightWidth filterSize_
-
HeightWidth stride_
-
HeightWidth padding_
-
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”
-
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
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
-
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
-
using HeightWidth = cdough::matrix::hybrid::HeightWidth
-
namespace ml
-
namespace operators
Streaming
-
namespace cdough
-
namespace operators
Functions
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.
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.
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.
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.
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).
-
namespace operators
Common
-
namespace cdough
-
namespace aggregators
Enums
Variables
-
enum cdough::aggregators::Direction Direction
-
enum cdough::aggregators::Direction Direction
-
namespace operators
Functions
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.
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.
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).
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).
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.
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.
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.
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.
-
namespace aggregators